Rewritten AddOperatorModifierInspection to be based on an intention (shouldn't we drop the inspection at all?)

This commit is contained in:
Valentin Kipyatkov
2015-10-16 23:11:05 +03:00
parent df9ad17ea2
commit d1de2f65c9
8 changed files with 60 additions and 69 deletions
@@ -0,0 +1 @@
<spot>operator</spot> fun X.get(index: Int): String
@@ -0,0 +1 @@
fun X.get(index: Int): String
@@ -0,0 +1,5 @@
<html>
<body>
This intention to add the 'operator' modifier to a function that matches one of the operator conventions but is not annotated as operator.
</body>
</html>
+6 -1
View File
@@ -1077,6 +1077,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.inspections.AddOperatorModifierIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.DeprecatedCallableAddReplaceWithInspection"
displayName="Add 'replaceWith' argument to 'deprecated' annotation"
groupName="Kotlin"
@@ -1233,7 +1238,7 @@
level="WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.OperatorModifierInspection"
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.AddOperatorModifierInspection"
displayName="Missing 'operator' modifier"
groupName="Kotlin"
language="kotlin"
@@ -0,0 +1,46 @@
/*
* 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.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetNamedFunction
import org.jetbrains.kotlin.util.OperatorChecks
//TODO: do we really need it?
public class AddOperatorModifierInspection : IntentionBasedInspection<JetNamedFunction>(
listOf(IntentionBasedInspection.IntentionData(AddOperatorModifierIntention())),
"Function defines an operator but isn't annotated as such",
JetNamedFunction::class.java)
class AddOperatorModifierIntention : JetSelfTargetingRangeIntention<JetNamedFunction>(JetNamedFunction::class.java, "Add 'operator' modifier") {
override fun applicabilityRange(element: JetNamedFunction): TextRange? {
val nameIdentifier = element.nameIdentifier ?: return null
val functionDescriptor = element.resolveToDescriptor() as? FunctionDescriptor ?: return null
if (functionDescriptor.isOperator || !OperatorChecks.canBeOperator(functionDescriptor)) return null
return nameIdentifier.textRange
}
override fun applyTo(element: JetNamedFunction, editor: Editor) {
element.addModifier(JetTokens.OPERATOR_KEYWORD)
}
}
@@ -1,67 +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.inspections
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters2
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.quickfix.AddModifierFix
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
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.util.OperatorChecks
public class OperatorModifierInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : JetVisitorVoid() {
override fun visitNamedFunction(function: JetNamedFunction) {
val nameIdentifier = function.nameIdentifier ?: return
val functionDescriptor = function.resolveToDescriptor() as? FunctionDescriptor ?: return
if (!functionDescriptor.isOperator && OperatorChecks.canBeOperator(functionDescriptor)) {
holder.registerProblem(nameIdentifier, "Function defines an operator but isn't annotated as such",
AddOperatorModifierFix())
}
}
}
}
private class AddOperatorModifierFix() : LocalQuickFix {
override fun getName(): String = "Add 'operator' modifier"
override fun getFamilyName(): String = getName()
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val function = descriptor.psiElement.getNonStrictParentOfType<JetNamedFunction>()
function?.addModifier(JetTokens.OPERATOR_KEYWORD)
}
}
}
@@ -1 +1 @@
org.jetbrains.kotlin.idea.inspections.OperatorModifierInspection
org.jetbrains.kotlin.idea.inspections.AddOperatorModifierInspection