quickfix to add 'operator' keyword to operator-like functions

This commit is contained in:
Dmitry Jemerov
2015-09-24 15:25:32 +02:00
parent 3a519ccc0a
commit bda0bd1de1
9 changed files with 119 additions and 5 deletions
@@ -33,6 +33,8 @@ public class OperatorConventions {
public static final Name CONTAINS = Name.identifier("contains");
public static final Name INVOKE = Name.identifier("invoke");
public static final Name ITERATOR = Name.identifier("iterator");
public static final Name GET = Name.identifier("get");
public static final Name SET = Name.identifier("set");
public static final Name NEXT = Name.identifier("next");
public static final Name HAS_NEXT = Name.identifier("hasNext");
@@ -41,10 +41,7 @@ public val ALL_SEARCHABLE_OPERATIONS: ImmutableSet<JetToken> = ImmutableSet
public val ALL_SEARCHABLE_OPERATION_PATTERNS: Set<String> =
ALL_SEARCHABLE_OPERATIONS.map { (it as JetSingleValueToken).getValue() }.toSet()
public val INDEXING_OPERATION_NAMES: ImmutableSet<Name> =
ImmutableSet.of(Name.identifier("get"), Name.identifier("set"))
public val ITERATOR_OPERATION_NAME: Name = Name.identifier("iterator")
public val INDEXING_OPERATION_NAMES: ImmutableSet<Name> = ImmutableSet.of(GET, SET)
public val IN_OPERATIONS_TO_SEARCH: ImmutableSet<JetToken> = ImmutableSet.of(JetTokens.IN_KEYWORD)
@@ -56,7 +53,7 @@ public fun Name.getOperationSymbolsToSearch(): Set<JetToken> {
EQUALS -> return EQUALS_OPERATIONS
IDENTITY_EQUALS -> return IDENTITY_EQUALS_OPERATIONS
CONTAINS -> return IN_OPERATIONS_TO_SEARCH
ITERATOR_OPERATION_NAME -> return ImmutableSet.of<JetToken>(JetTokens.IN_KEYWORD)
ITERATOR -> return ImmutableSet.of<JetToken>(JetTokens.IN_KEYWORD)
in INDEXING_OPERATION_NAMES -> return ImmutableSet.of<JetToken>(JetTokens.LBRACKET, JetTokens.BY_KEYWORD)
DelegatedPropertyResolver.PROPERTY_DELEGATED_FUNCTION_NAME -> return ImmutableSet.of<JetToken>(JetTokens.BY_KEYWORD)
}
@@ -0,0 +1,6 @@
<html>
<body>
This inspection reports functions that were overloading operators according to pre-M14 naming conventions
but are not annotated with the 'operator' modifier. The quickfix for the inspection adds the modifier.
</body>
</html>
+8
View File
@@ -1199,6 +1199,14 @@
level="WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.OperatorModifierInspection"
displayName="Missing 'operator' modifier"
groupName="Kotlin"
language="jet"
enabledByDefault="true"
level="WARNING"
cleanupTool="true"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ConflictingExtensionPropertyInspection"
displayName="Extension property conflicts with synthetic one"
groupName="Kotlin"
@@ -0,0 +1,77 @@
/*
* 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.inspections
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetModifierListOwner
import org.jetbrains.kotlin.psi.JetNamedFunction
import org.jetbrains.kotlin.psi.JetVisitorVoid
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.types.expressions.OperatorConventions
public class OperatorModifierInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : JetVisitorVoid() {
override fun visitNamedFunction(function: JetNamedFunction) {
val nameIdentifier = function.nameIdentifier
if (nameIdentifier != null && function.isOperator() && !function.hasModifier(JetTokens.OPERATOR_KEYWORD)) {
holder.registerProblem(nameIdentifier, "Function defines an operator but isn't annotated as such",
AddModifierLocalQuickFix(JetTokens.OPERATOR_KEYWORD))
}
}
}
}
private fun JetNamedFunction.isOperator(): Boolean {
val arity = valueParameters.size()
if (arity == 0 &&
(nameAsName in OperatorConventions.UNARY_OPERATION_NAMES.values() ||
nameAsName == OperatorConventions.ITERATOR)) {
return true
}
if (arity == 1 && (nameAsName in OperatorConventions.BINARY_OPERATION_NAMES.values() ||
nameAsName in OperatorConventions.ASSIGNMENT_OPERATIONS.values () ||
nameAsName == OperatorConventions.CONTAINS ||
nameAsName == OperatorConventions.COMPARE_TO)) {
return true
}
if (nameAsName == OperatorConventions.INVOKE) {
return true
}
if (arity >= 1 && nameAsName == OperatorConventions.GET) {
return true
}
if (arity >= 2 && nameAsName == OperatorConventions.SET) {
return true
}
return false
}
}
class AddModifierLocalQuickFix(val modifier: JetModifierKeywordToken) : LocalQuickFix {
override fun getName(): String = "Add '${modifier.value}' modifier"
override fun getFamilyName(): String = getName()
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val modifierListOwner = descriptor.psiElement.getNonStrictParentOfType<JetModifierListOwner>()
modifierListOwner?.addModifier(modifier)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.OperatorModifierInspection
@@ -0,0 +1,4 @@
// "Add 'operator' modifier" "true"
class A {
fun <caret>plus(other: A): A = A()
}
@@ -0,0 +1,4 @@
// "Add 'operator' modifier" "true"
class A {
operator fun plus(other: A): A = A()
}
@@ -4187,6 +4187,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/migration/operatorModifier")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class OperatorModifier extends AbstractQuickFixTest {
public void testAllFilesPresentInOperatorModifier() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/operatorModifier"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/operatorModifier/simple.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/removeNameFromFunctionExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)