Make DeprecatedCallableAddReplaceWithInspection applicability based

Removes intention version of the same thing
Includes some optimization of 'Deprecated' annotation detection
This commit is contained in:
Mikhail Glukhikh
2018-03-27 16:53:20 +03:00
parent ab973b2ff0
commit 2125c42328
54 changed files with 245 additions and 262 deletions
+1 -6
View File
@@ -1203,11 +1203,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddNameToArgumentIntention</className>
<category>Kotlin</category>
@@ -1605,7 +1600,7 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithInspection"
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.DeprecatedCallableAddReplaceWithInspection"
displayName="@Deprecated annotation without 'replaceWith' argument"
groupPath="Kotlin"
groupName="Other problems"
@@ -1,23 +1,13 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
package org.jetbrains.kotlin.idea.inspections
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.codeStyle.CodeStyleManager
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
@@ -31,9 +21,9 @@ import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.moveCaret
import org.jetbrains.kotlin.idea.core.unblockDocument
import org.jetbrains.kotlin.idea.imports.importableFqName
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
@@ -45,30 +35,35 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isUnit
import java.util.*
class DeprecatedCallableAddReplaceWithInspection : IntentionBasedInspection<KtCallableDeclaration>(DeprecatedCallableAddReplaceWithIntention::class)
class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(
KtCallableDeclaration::class.java, "Add 'replaceWith' argument to specify replacement pattern", "Add 'replaceWith' argument to 'Deprecated' annotation"
class DeprecatedCallableAddReplaceWithInspection : AbstractApplicabilityBasedInspection<KtCallableDeclaration>(
KtCallableDeclaration::class.java
) {
override fun inspectionText(element: KtCallableDeclaration) =
"@Deprecated annotation without 'replaceWith' argument"
override fun inspectionTarget(element: KtCallableDeclaration): KtAnnotationEntry =
element.annotationEntries.first { it.shortName == DEPRECATED_NAME }
override val defaultFixText =
"Add 'replaceWith' argument to specify replacement pattern"
private class ReplaceWith(val expression: String, vararg val imports: String)
override fun applicabilityRange(element: KtCallableDeclaration): TextRange? {
val annotationEntry = element.deprecatedAnnotationWithNoReplaceWith() ?: return null
if (element.suggestReplaceWith() == null) return null
return annotationEntry.textRange
override fun isApplicable(element: KtCallableDeclaration): Boolean {
element.deprecatedAnnotationWithNoReplaceWith() ?: return false
return element.suggestReplaceWith() != null
}
override fun applyTo(element: KtCallableDeclaration, editor: Editor?) {
val replaceWith = element.suggestReplaceWith()!!
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
val declaration = element.getParentOfType<KtCallableDeclaration>(strict = true) ?: return
val replaceWith = declaration.suggestReplaceWith()!!
assert('\n' !in replaceWith.expression && '\r' !in replaceWith.expression) { "Formatted expression text should not contain \\n or \\r" }
val annotationEntry = element.deprecatedAnnotationWithNoReplaceWith()!!
val psiFactory = KtPsiFactory(element)
val annotationEntry = declaration.deprecatedAnnotationWithNoReplaceWith()!!
val psiFactory = KtPsiFactory(declaration)
var escapedText = replaceWith.expression
.replace("\\", "\\\\")
.replace("\"", "\\\"")
var escapedText = replaceWith.expression.replace("\\", "\\\\").replace("\"", "\\\"")
// escape '$' if it's followed by a letter or '{'
if (escapedText.contains('$')) {
@@ -108,15 +103,12 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
}
private fun KtCallableDeclaration.deprecatedAnnotationWithNoReplaceWith(): KtAnnotationEntry? {
val bindingContext = this.analyze()
// val deprecatedConstructor = KotlinBuiltIns.getInstance().getDeprecatedAnnotation().getUnsubstitutedPrimaryConstructor()
for (entry in annotationEntries) {
entry.analyze()
if (entry.shortName != DEPRECATED_NAME) continue
val bindingContext = entry.analyze()
val resolvedCall = entry.calleeExpression.getResolvedCall(bindingContext) ?: continue
if (!resolvedCall.isReallySuccess()) continue
// if (resolvedCall.getResultingDescriptor() != deprecatedConstructor) continue
//TODO
val descriptor = resolvedCall.resultingDescriptor.containingDeclaration
val descriptorFqName = DescriptorUtils.getFqName(descriptor).toSafe()
if (descriptorFqName != KotlinBuiltIns.FQ_NAMES.deprecated) continue
@@ -125,7 +117,7 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
val replaceWithArguments = args["replaceWith"] /*TODO: kotlin.deprecated::replaceWith.name*/
val level = args["level"]
if (replaceWithArguments?.arguments?.isNotEmpty() ?: false) return null
if (replaceWithArguments?.arguments?.isNotEmpty() == true) return null
if (level != null && level.arguments.isNotEmpty()) {
val levelDescriptor = level.arguments[0].getArgumentExpression().getResolvedCall(bindingContext)?.candidateDescriptor
@@ -150,7 +142,7 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
} ?: return null
var isGood = true
replacementExpression.accept(object: KtVisitorVoid(){
replacementExpression.accept(object : KtVisitorVoid() {
override fun visitReturnExpression(expression: KtReturnExpression) {
isGood = false
}
@@ -185,13 +177,17 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
val text = replacementExpression.text
var expression = try {
KtPsiFactory(this).createExpression(text.replace('\n', ' '))
}
catch(e: Throwable) { // does not parse in one line
} catch (e: Throwable) { // does not parse in one line
return null
}
expression = CodeStyleManager.getInstance(project).reformat(expression, true) as KtExpression
return ReplaceWith(expression.text, *extractImports(replacementExpression).toTypedArray())
return ReplaceWith(
expression.text,
*extractImports(
replacementExpression
).toTypedArray()
)
}
private fun KtDeclarationWithBody.replacementExpressionFromBody(): KtExpression? {
@@ -212,7 +208,7 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
val importHelper = ImportInsertHelper.getInstance(expression.project)
val result = ArrayList<String>()
expression.accept(object : KtVisitorVoid(){
expression.accept(object : KtVisitorVoid() {
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
val bindingContext = expression.analyze()
val target = bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, expression]
@@ -221,7 +217,8 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
if (target.isExtension || expression.getReceiverExpression() == null) {
val fqName = target.importableFqName ?: return
if (!importHelper.isImportedWithDefault(ImportPath(fqName, false), file)
&& (target.containingDeclaration as? PackageFragmentDescriptor)?.fqName != currentPackageFqName) {
&& (target.containingDeclaration as? PackageFragmentDescriptor)?.fqName != currentPackageFqName
) {
result.add(fqName.asString())
}
}
@@ -233,4 +230,8 @@ class DeprecatedCallableAddReplaceWithIntention : SelfTargetingRangeIntention<Kt
})
return result
}
companion object {
val DEPRECATED_NAME = KotlinBuiltIns.FQ_NAMES.deprecated.shortName()
}
}