Skip to content

Commit 4f8c065

Browse files
committed
Change API in AbstractApplicabilityBasedInspection
Replace all inspectionTarget with inspectionHighlightRangeInElement & change type of element in applyTo from PsiElement to TElement
1 parent 1ad7a50 commit 4f8c065

30 files changed

+164
-212
lines changed

idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractApplicabilityBasedInspection.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.intellij.codeInspection.*
2020
import com.intellij.openapi.editor.Editor
2121
import com.intellij.openapi.project.Project
2222
import com.intellij.openapi.util.TextRange
23-
import com.intellij.psi.PsiElement
2423
import org.jetbrains.kotlin.psi.KtElement
2524
import org.jetbrains.kotlin.psi.KtVisitorVoid
2625

@@ -44,7 +43,7 @@ abstract class AbstractApplicabilityBasedInspection<TElement : KtElement>(
4443
if (!isApplicable(element)) return
4544

4645
holder.registerProblemWithoutOfflineInformation(
47-
inspectionTarget(element),
46+
element,
4847
inspectionText(element),
4948
isOnTheFly,
5049
inspectionHighlightType(element),
@@ -55,8 +54,6 @@ abstract class AbstractApplicabilityBasedInspection<TElement : KtElement>(
5554

5655
open fun inspectionHighlightRangeInElement(element: TElement): TextRange? = null
5756

58-
open fun inspectionTarget(element: TElement): PsiElement = element
59-
6057
open fun inspectionHighlightType(element: TElement): ProblemHighlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING
6158

6259
abstract fun inspectionText(element: TElement): String
@@ -67,15 +64,16 @@ abstract class AbstractApplicabilityBasedInspection<TElement : KtElement>(
6764

6865
abstract fun isApplicable(element: TElement): Boolean
6966

70-
abstract fun applyTo(element: PsiElement, project: Project = element.project, editor: Editor? = null)
67+
abstract fun applyTo(element: TElement, project: Project = element.project, editor: Editor? = null)
7168

7269
open val startFixInWriteAction = true
7370

7471
private inner class LocalFix(val text: String) : LocalQuickFix {
7572
override fun startInWriteAction() = startFixInWriteAction
7673

7774
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
78-
val element = descriptor.psiElement
75+
@Suppress("UNCHECKED_CAST")
76+
val element = descriptor.psiElement as TElement
7977
applyTo(element, project, element.findExistingEditor())
8078
}
8179

idea/src/org/jetbrains/kotlin/idea/inspections/AddOperatorModifierInspection.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ package org.jetbrains.kotlin.idea.inspections
77

88
import com.intellij.openapi.editor.Editor
99
import com.intellij.openapi.project.Project
10-
import com.intellij.psi.PsiElement
1110
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
1211
import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals
12+
import org.jetbrains.kotlin.idea.util.nameIdentifierTextRangeInThis
1313
import org.jetbrains.kotlin.lexer.KtTokens
1414
import org.jetbrains.kotlin.psi.KtNamedFunction
15-
import org.jetbrains.kotlin.psi.psiUtil.startOffset
1615
import org.jetbrains.kotlin.util.OperatorChecks
1716

1817
class AddOperatorModifierInspection : AbstractApplicabilityBasedInspection<KtNamedFunction>(KtNamedFunction::class.java) {
19-
override fun inspectionHighlightRangeInElement(element: KtNamedFunction) =
20-
element.nameIdentifier?.textRange?.shiftLeft(element.startOffset)
18+
override fun inspectionHighlightRangeInElement(element: KtNamedFunction) = element.nameIdentifierTextRangeInThis()
2119

2220
override fun inspectionText(element: KtNamedFunction) = "Function should have 'operator' modifier"
2321

@@ -29,8 +27,8 @@ class AddOperatorModifierInspection : AbstractApplicabilityBasedInspection<KtNam
2927
return !functionDescriptor.isOperator && OperatorChecks.check(functionDescriptor).isSuccess
3028
}
3129

32-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
33-
for (declaration in (element as KtNamedFunction).withExpectedActuals()) {
30+
override fun applyTo(element: KtNamedFunction, project: Project, editor: Editor?) {
31+
for (declaration in element.withExpectedActuals()) {
3432
declaration.addModifier(KtTokens.OPERATOR_KEYWORD)
3533
}
3634
}

idea/src/org/jetbrains/kotlin/idea/inspections/DeprecatedCallableAddReplaceWithInspection.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.idea.inspections
77

88
import com.intellij.openapi.editor.Editor
99
import com.intellij.openapi.project.Project
10-
import com.intellij.psi.PsiElement
1110
import com.intellij.psi.codeStyle.CodeStyleManager
1211
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
1312
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
@@ -22,8 +21,8 @@ import org.jetbrains.kotlin.idea.core.moveCaret
2221
import org.jetbrains.kotlin.idea.core.unblockDocument
2322
import org.jetbrains.kotlin.idea.imports.importableFqName
2423
import org.jetbrains.kotlin.idea.util.ImportInsertHelper
24+
import org.jetbrains.kotlin.idea.util.textRangeIn
2525
import org.jetbrains.kotlin.psi.*
26-
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
2726
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
2827
import org.jetbrains.kotlin.resolve.BindingContext
2928
import org.jetbrains.kotlin.resolve.DescriptorUtils
@@ -38,14 +37,13 @@ import java.util.*
3837
class DeprecatedCallableAddReplaceWithInspection : AbstractApplicabilityBasedInspection<KtCallableDeclaration>(
3938
KtCallableDeclaration::class.java
4039
) {
41-
override fun inspectionText(element: KtCallableDeclaration) =
42-
"@Deprecated annotation without 'replaceWith' argument"
40+
override fun inspectionText(element: KtCallableDeclaration) = "@Deprecated annotation without 'replaceWith' argument"
4341

44-
override fun inspectionTarget(element: KtCallableDeclaration): KtAnnotationEntry =
45-
element.annotationEntries.first { it.shortName == DEPRECATED_NAME }
42+
override fun inspectionHighlightRangeInElement(element: KtCallableDeclaration) = element.annotationEntries.first {
43+
it.shortName == DEPRECATED_NAME
44+
}.textRangeIn(element)
4645

47-
override val defaultFixText =
48-
"Add 'replaceWith' argument to specify replacement pattern"
46+
override val defaultFixText = "Add 'replaceWith' argument to specify replacement pattern"
4947

5048
private class ReplaceWith(val expression: String, vararg val imports: String)
5149

@@ -54,14 +52,13 @@ class DeprecatedCallableAddReplaceWithInspection : AbstractApplicabilityBasedIns
5452
return element.suggestReplaceWith() != null
5553
}
5654

57-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
58-
val declaration = element.getParentOfType<KtCallableDeclaration>(strict = true) ?: return
59-
val replaceWith = declaration.suggestReplaceWith()!!
55+
override fun applyTo(element: KtCallableDeclaration, project: Project, editor: Editor?) {
56+
val replaceWith = element.suggestReplaceWith()!!
6057

6158
assert('\n' !in replaceWith.expression && '\r' !in replaceWith.expression) { "Formatted expression text should not contain \\n or \\r" }
6259

63-
val annotationEntry = declaration.deprecatedAnnotationWithNoReplaceWith()!!
64-
val psiFactory = KtPsiFactory(declaration)
60+
val annotationEntry = element.deprecatedAnnotationWithNoReplaceWith()!!
61+
val psiFactory = KtPsiFactory(element)
6562

6663
var escapedText = replaceWith.expression.replace("\\", "\\\\").replace("\"", "\\\"")
6764

@@ -212,8 +209,8 @@ class DeprecatedCallableAddReplaceWithInspection : AbstractApplicabilityBasedIns
212209
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
213210
val bindingContext = expression.analyze()
214211
val target = bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, expression]
215-
?: bindingContext[BindingContext.REFERENCE_TARGET, expression]
216-
?: return
212+
?: bindingContext[BindingContext.REFERENCE_TARGET, expression]
213+
?: return
217214
if (target.isExtension || expression.getReceiverExpression() == null) {
218215
val fqName = target.importableFqName ?: return
219216
if (!importHelper.isImportedWithDefault(ImportPath(fqName, false), file)

idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ import org.jetbrains.kotlin.idea.core.replaced
2222
import org.jetbrains.kotlin.idea.core.setType
2323
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.elvisPattern
2424
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull
25+
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.fromIfKeywordToRightParenthesisTextRangeInThis
2526
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed
26-
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.textRange
2727
import org.jetbrains.kotlin.idea.util.CommentSaver
2828
import org.jetbrains.kotlin.lexer.KtTokens
2929
import org.jetbrains.kotlin.psi.*
3030
import org.jetbrains.kotlin.psi.psiUtil.PsiChildRange
3131
import org.jetbrains.kotlin.psi.psiUtil.siblings
32-
import org.jetbrains.kotlin.psi.psiUtil.startOffset
3332
import org.jetbrains.kotlin.resolve.BindingContext
3433
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
3534
import org.jetbrains.kotlin.types.isError
@@ -43,16 +42,15 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
4342

4443
override val defaultFixText: String = "Replace 'if' with elvis operator"
4544

46-
override fun inspectionHighlightRangeInElement(element: KtIfExpression): TextRange? = element.textRange().shiftLeft(element.startOffset)
45+
override fun inspectionHighlightRangeInElement(element: KtIfExpression) = element.fromIfKeywordToRightParenthesisTextRangeInThis()
4746

4847
override fun inspectionHighlightType(element: KtIfExpression): ProblemHighlightType =
4948
if (element.shouldBeTransformed()) ProblemHighlightType.GENERIC_ERROR_OR_WARNING else ProblemHighlightType.INFORMATION
5049

5150
override fun isApplicable(element: KtIfExpression): Boolean = Companion.isApplicable(element)
5251

53-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
54-
val newElvis = Companion.applyTo(element as KtIfExpression)
55-
editor?.caretModel?.moveToOffset(newElvis.right!!.textOffset)
52+
override fun applyTo(element: KtIfExpression, project: Project, editor: Editor?) {
53+
Companion.applyTo(element).right?.textOffset?.let { editor?.caretModel?.moveToOffset(it) }
5654
}
5755

5856
companion object {
@@ -62,7 +60,7 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
6260
val type = data.ifNullExpression.analyze().getType(data.ifNullExpression) ?: return null
6361
if (!type.isNothing()) return null
6462

65-
return element.textRange()
63+
return element.fromIfKeywordToRightParenthesisTextRangeInThis()
6664
}
6765

6866
fun isApplicable(element: KtIfExpression): Boolean = applicabilityRange(element) != null

idea/src/org/jetbrains/kotlin/idea/inspections/JavaMapForEachInspection.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55

66
package org.jetbrains.kotlin.idea.inspections
77

8-
import com.intellij.codeInspection.ProblemHighlightType
98
import com.intellij.openapi.editor.Editor
109
import com.intellij.openapi.project.Project
11-
import com.intellij.psi.PsiElement
10+
import com.intellij.openapi.util.TextRange
1211
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
1312
import org.jetbrains.kotlin.idea.caches.resolve.analyze
1413
import org.jetbrains.kotlin.idea.core.getLastLambdaExpression
1514
import org.jetbrains.kotlin.idea.inspections.collections.isMap
1615
import org.jetbrains.kotlin.idea.intentions.callExpression
16+
import org.jetbrains.kotlin.idea.util.calleeTextRangeInThis
1717
import org.jetbrains.kotlin.psi.KtCallExpression
1818
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
1919
import org.jetbrains.kotlin.psi.KtLambdaExpression
2020
import org.jetbrains.kotlin.psi.KtPsiFactory
21-
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
2221
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
2322
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
2423
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -41,16 +40,14 @@ class JavaMapForEachInspection : AbstractApplicabilityBasedInspection<KtDotQuali
4140
return resolvedCall.isResolvedWithSamConversions()
4241
}
4342

44-
override fun inspectionTarget(element: KtDotQualifiedExpression) = element.callExpression?.calleeExpression ?: element
43+
override fun inspectionHighlightRangeInElement(element: KtDotQualifiedExpression): TextRange? = element.calleeTextRangeInThis()
4544

4645
override fun inspectionText(element: KtDotQualifiedExpression) = "Java Map.forEach method call should be replaced with Kotlin's forEach"
4746

48-
override fun inspectionHighlightType(element: KtDotQualifiedExpression) = ProblemHighlightType.GENERIC_ERROR_OR_WARNING
49-
5047
override val defaultFixText = "Replace with Kotlin's forEach"
5148

52-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
53-
val call = element.getStrictParentOfType<KtCallExpression>() ?: return
49+
override fun applyTo(element: KtDotQualifiedExpression, project: Project, editor: Editor?) {
50+
val call = element.callExpression ?: return
5451
val lambda = call.lambda() ?: return
5552
val valueParameters = lambda.valueParameters
5653
lambda.functionLiteral.valueParameterList?.replace(

idea/src/org/jetbrains/kotlin/idea/inspections/MoveLambdaOutsideParenthesesInspection.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ package org.jetbrains.kotlin.idea.inspections
88
import com.intellij.codeInspection.ProblemHighlightType
99
import com.intellij.openapi.editor.Editor
1010
import com.intellij.openapi.project.Project
11-
import com.intellij.psi.PsiElement
1211
import org.jetbrains.kotlin.idea.core.canMoveLambdaOutsideParentheses
1312
import org.jetbrains.kotlin.idea.core.getLastLambdaExpression
1413
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses
14+
import org.jetbrains.kotlin.idea.util.textRangeIn
1515
import org.jetbrains.kotlin.psi.KtCallExpression
16-
import org.jetbrains.kotlin.psi.KtElement
1716
import org.jetbrains.kotlin.psi.KtValueArgument
18-
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
1917
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
2018
import org.jetbrains.kotlin.psi.unpackFunctionLiteral
2119

@@ -36,19 +34,17 @@ class MoveLambdaOutsideParenthesesInspection : AbstractApplicabilityBasedInspect
3634

3735
override fun isApplicable(element: KtCallExpression) = element.canMoveLambdaOutsideParentheses()
3836

39-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
40-
val expression = element.getParentOfType<KtCallExpression>(strict = false) ?: return
41-
42-
if (expression.canMoveLambdaOutsideParentheses()) {
43-
expression.moveFunctionLiteralOutsideParentheses()
37+
override fun applyTo(element: KtCallExpression, project: Project, editor: Editor?) {
38+
if (element.canMoveLambdaOutsideParentheses()) {
39+
element.moveFunctionLiteralOutsideParentheses()
4440
}
4541
}
4642

4743
override fun inspectionText(element: KtCallExpression) = "Lambda argument ${element.verb} be moved out of parentheses"
4844

49-
override fun inspectionTarget(element: KtCallExpression): KtElement {
50-
return element.getLastLambdaExpression()?.getStrictParentOfType<KtValueArgument>()?.asElement() ?: element
51-
}
45+
override fun inspectionHighlightRangeInElement(element: KtCallExpression) = element.getLastLambdaExpression()
46+
?.getStrictParentOfType<KtValueArgument>()?.asElement()
47+
?.textRangeIn(element)
5248

5349
override val defaultFixText = "Move lambda argument out of parentheses"
5450
}

idea/src/org/jetbrains/kotlin/idea/inspections/NullableBooleanElvisInspection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class NullableBooleanElvisInspection : AbstractKotlinInspection(), CleanupLocalI
7575
val prefixExpression = equalityCheckExpression.getParentOfType<KtPrefixExpression>(strict = true) ?: return
7676
val simplifier = SimplifyNegatedBinaryExpressionInspection()
7777
if (simplifier.isApplicable(prefixExpression)) {
78-
simplifier.applyTo(prefixExpression.operationReference)
78+
simplifier.applyTo(prefixExpression)
7979
}
8080
}
8181
}

idea/src/org/jetbrains/kotlin/idea/inspections/RemoveCurlyBracesFromTemplateInspection.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.idea.inspections
77

88
import com.intellij.openapi.editor.Editor
99
import com.intellij.openapi.project.Project
10-
import com.intellij.psi.PsiElement
1110
import org.jetbrains.kotlin.idea.core.canDropBraces
1211
import org.jetbrains.kotlin.idea.core.dropBraces
1312
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
@@ -20,7 +19,7 @@ class RemoveCurlyBracesFromTemplateInspection :
2019

2120
override fun isApplicable(element: KtBlockStringTemplateEntry): Boolean = element.canDropBraces()
2221

23-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
24-
(element as KtBlockStringTemplateEntry).dropBraces()
22+
override fun applyTo(element: KtBlockStringTemplateEntry, project: Project, editor: Editor?) {
23+
element.dropBraces()
2524
}
2625
}

idea/src/org/jetbrains/kotlin/idea/inspections/ReplaceArrayEqualityOpWithArraysEqualsInspection.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.inspections
1818

1919
import com.intellij.openapi.editor.Editor
2020
import com.intellij.openapi.project.Project
21-
import com.intellij.psi.PsiElement
2221
import org.jetbrains.kotlin.idea.caches.resolve.analyze
2322
import org.jetbrains.kotlin.idea.intentions.resolvedToArrayType
2423
import org.jetbrains.kotlin.lexer.KtTokens
@@ -28,24 +27,23 @@ import org.jetbrains.kotlin.psi.createExpressionByPattern
2827
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
2928

3029
class ReplaceArrayEqualityOpWithArraysEqualsInspection : AbstractApplicabilityBasedInspection<KtBinaryExpression>(
31-
KtBinaryExpression::class.java
30+
KtBinaryExpression::class.java
3231
) {
33-
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
34-
val expression = element as? KtBinaryExpression ?: return
35-
val right = expression.right ?: return
36-
val left = expression.left ?: return
32+
override fun applyTo(element: KtBinaryExpression, project: Project, editor: Editor?) {
33+
val right = element.right ?: return
34+
val left = element.left ?: return
3735
val factory = KtPsiFactory(project)
3836
val template = buildString {
39-
if (expression.operationToken == KtTokens.EXCLEQ) append("!")
37+
if (element.operationToken == KtTokens.EXCLEQ) append("!")
4038
append("$0.contentEquals($1)")
4139
}
42-
expression.replace(factory.createExpressionByPattern(template, left, right))
40+
element.replace(factory.createExpressionByPattern(template, left, right))
4341
}
4442

4543
override fun isApplicable(element: KtBinaryExpression): Boolean {
46-
val operationToken = element.operationToken
47-
when (operationToken) {
48-
KtTokens.EQEQ, KtTokens.EXCLEQ -> {}
44+
when (element.operationToken) {
45+
KtTokens.EQEQ, KtTokens.EXCLEQ -> {
46+
}
4947
else -> return false
5048
}
5149
val right = element.right

0 commit comments

Comments
 (0)