Make "Add operator modifier" an inspection instead of intention
#KT-31533 Fixed
This commit is contained in:
committed by
Dmitry Gridin
parent
5123e1f078
commit
4c62788c3e
@@ -1265,11 +1265,6 @@
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.AddOperatorModifierIntention</className>
|
||||
<category>Kotlin</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaIntention</className>
|
||||
<category>Kotlin</category>
|
||||
@@ -3381,6 +3376,15 @@
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.AddOperatorModifierInspection"
|
||||
displayName="Function should have 'operator' modifier"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports a function that matches one of the operator conventions but is not annotated as <b>operator</b>.
|
||||
</body>
|
||||
</html>
|
||||
@@ -1 +0,0 @@
|
||||
<spot>operator</spot> fun X.get(index: Int): String
|
||||
@@ -1 +0,0 @@
|
||||
fun X.get(index: Int): String
|
||||
@@ -1,5 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
This intention adds an <b>operator</b> modifier to a function that matches one of the operator conventions but is not annotated as <b>operator</b>.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.inspections
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.util.OperatorChecks
|
||||
|
||||
class AddOperatorModifierInspection : AbstractApplicabilityBasedInspection<KtNamedFunction>(
|
||||
KtNamedFunction::class.java
|
||||
) {
|
||||
override fun inspectionTarget(element: KtNamedFunction) = element.nameIdentifier ?: element
|
||||
|
||||
override fun inspectionText(element: KtNamedFunction) = "Function should have 'operator' modifier"
|
||||
|
||||
override val defaultFixText = "Add 'operator' modifier"
|
||||
|
||||
override fun isApplicable(element: KtNamedFunction): Boolean {
|
||||
if (element.nameIdentifier == null || element.hasModifier(KtTokens.OPERATOR_KEYWORD)) return false
|
||||
val functionDescriptor = element.resolveToDescriptorIfAny() ?: return false
|
||||
return !functionDescriptor.isOperator && OperatorChecks.check(functionDescriptor).isSuccess
|
||||
}
|
||||
|
||||
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
|
||||
val function = element.getStrictParentOfType<KtNamedFunction>() ?: return
|
||||
function.withExpectedActuals().forEach { it.addModifier(KtTokens.OPERATOR_KEYWORD) }
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||
import org.jetbrains.kotlin.idea.refactoring.withExpectedActuals
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.util.OperatorChecks
|
||||
|
||||
class AddOperatorModifierIntention : SelfTargetingRangeIntention<KtNamedFunction>(KtNamedFunction::class.java, "Add 'operator' modifier") {
|
||||
override fun applicabilityRange(element: KtNamedFunction): TextRange? {
|
||||
val nameIdentifier = element.nameIdentifier ?: return null
|
||||
val functionDescriptor = element.resolveToDescriptorIfAny() ?: return null
|
||||
if (functionDescriptor.isOperator || !OperatorChecks.check(functionDescriptor).isSuccess) return null
|
||||
return nameIdentifier.textRange
|
||||
}
|
||||
|
||||
override fun applyTo(element: KtNamedFunction, editor: Editor?) {
|
||||
element.withExpectedActuals().forEach { it.addModifier(KtTokens.OPERATOR_KEYWORD) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ object J2KPostProcessingRegistrarImpl : J2KPostProcessingRegistrar {
|
||||
registerIntentionBasedProcessing(IfThenToElvisIntention())
|
||||
registerInspectionBasedProcessing(SimplifyNegatedBinaryExpressionInspection())
|
||||
registerInspectionBasedProcessing(ReplaceGetOrSetInspection())
|
||||
registerIntentionBasedProcessing(AddOperatorModifierIntention())
|
||||
registerInspectionBasedProcessing(AddOperatorModifierInspection())
|
||||
registerIntentionBasedProcessing(ObjectLiteralToLambdaIntention())
|
||||
registerIntentionBasedProcessing(AnonymousFunctionToLambdaIntention())
|
||||
registerIntentionBasedProcessing(RemoveUnnecessaryParenthesesIntention())
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.AddOperatorModifierInspection
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
// PROBLEM: none
|
||||
class A {
|
||||
fun <caret>contains(other: A): Int = -1
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
// PROBLEM: none
|
||||
open class A {
|
||||
open operator fun plus(a: A) = A()
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// IS_APPLICABLE: false
|
||||
// PROBLEM: none
|
||||
class A {
|
||||
}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
org.jetbrains.kotlin.idea.intentions.AddOperatorModifierIntention
|
||||
+48
@@ -29,6 +29,54 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/addOperatorModifier")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class AddOperatorModifier extends AbstractLocalInspectionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInAddOperatorModifier() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/addOperatorModifier"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("containsBool.kt")
|
||||
public void testContainsBool() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/containsBool.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("containsInt.kt")
|
||||
public void testContainsInt() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/containsInt.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extension.kt")
|
||||
public void testExtension() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/extension.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("forOverride.kt")
|
||||
public void testForOverride() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/forOverride.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overridden.kt")
|
||||
public void testOverridden() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/overridden.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/simple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("toplevel.kt")
|
||||
public void testToplevel() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/addOperatorModifier/toplevel.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/arrayInDataClass")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -1476,54 +1476,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/addOperatorModifier")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class AddOperatorModifier extends AbstractIntentionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInAddOperatorModifier() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addOperatorModifier"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("containsBool.kt")
|
||||
public void testContainsBool() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/containsBool.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("containsInt.kt")
|
||||
public void testContainsInt() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/containsInt.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("extension.kt")
|
||||
public void testExtension() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/extension.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("forOverride.kt")
|
||||
public void testForOverride() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/forOverride.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("overridden.kt")
|
||||
public void testOverridden() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/overridden.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("simple.kt")
|
||||
public void testSimple() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/simple.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("toplevel.kt")
|
||||
public void testToplevel() throws Exception {
|
||||
runTest("idea/testData/intentions/addOperatorModifier/toplevel.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/intentions/addPropertyAccessors")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -125,7 +125,7 @@ private val processings: List<GeneralPostProcessing> = listOf(
|
||||
intentionBasedProcessing(IfThenToElvisIntention()),
|
||||
inspectionBasedProcessing(SimplifyNegatedBinaryExpressionInspection()),
|
||||
inspectionBasedProcessing(ReplaceGetOrSetInspection()),
|
||||
intentionBasedProcessing(AddOperatorModifierIntention()),
|
||||
inspectionBasedProcessing(AddOperatorModifierInspection()),
|
||||
intentionBasedProcessing(ObjectLiteralToLambdaIntention()),
|
||||
intentionBasedProcessing(AnonymousFunctionToLambdaIntention()),
|
||||
intentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()),
|
||||
|
||||
Reference in New Issue
Block a user