Deprecated conventions (get -> getValue, plus -> unaryPlus) quickfix

This commit is contained in:
Yan Zhulanow
2015-10-12 18:08:07 +03:00
parent f9e89596fd
commit 528482f625
11 changed files with 138 additions and 2 deletions
@@ -117,7 +117,7 @@ public class OperatorConventions {
.build();
public static final ImmutableSet<Name> CONVENTION_NAMES = ImmutableSet.<Name>builder()
.add(GET, SET, INVOKE, CONTAINS, ITERATOR, NEXT, HAS_NEXT, EQUALS, COMPARE_TO)
.add(GET, SET, INVOKE, CONTAINS, ITERATOR, NEXT, HAS_NEXT, EQUALS, COMPARE_TO, GET_VALUE, SET_VALUE)
.addAll(UNARY_OPERATION_NAMES.values())
.addAll(BINARY_OPERATION_NAMES.values())
.addAll(ASSIGNMENT_OPERATIONS.values())
@@ -92,6 +92,8 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
Errors.NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION,
Errors.BACKING_FIELD_OLD_SYNTAX,
Errors.OPERATOR_MODIFIER_REQUIRED,
Errors.DEPRECATED_UNARY_PLUS_MINUS,
Errors.DELEGATE_RESOLVED_TO_DEPRECATED_CONVENTION,
Errors.INFIX_MODIFIER_REQUIRED,
Errors.CALLABLE_REFERENCE_TO_MEMBER_OR_EXTENSION_WITH_EMPTY_LHS,
Errors.DEPRECATED_TYPE_PARAMETER_SYNTAX,
@@ -0,0 +1,71 @@
/*
* 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.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.refactoring.rename.RenameProcessor
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters2
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters3
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.psi.*
class DeprecatedFunctionConventionFix(
element: JetNamedFunction,
private val newName: String
) : JetIntentionAction<JetNamedFunction>(element), CleanupFix {
override fun getText(): String = "Rename to '$newName'"
override fun getFamilyName(): String = text
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
FilteredRenameProcessor(project, element, newName, false, false).run()
}
companion object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val (functionDescriptor, newName) = when (diagnostic) {
is DiagnosticWithParameters3<*, *, *, *> -> Pair(diagnostic.a, diagnostic.c)
is DiagnosticWithParameters2<*, *, *> -> Pair(diagnostic.a, diagnostic.b)
else -> Pair(null, null)
}
if (functionDescriptor !is FunctionDescriptor || newName !is String) return null
val element = DescriptorToSourceUtilsIde.getAnyDeclaration(diagnostic.psiFile.project, functionDescriptor)
as? JetNamedFunction ?: return null
return DeprecatedFunctionConventionFix(element, newName)
}
}
private class FilteredRenameProcessor(
project: Project,
element: PsiElement,
newName: String,
isSearchInComments: Boolean,
isSearchTextOccurrences: Boolean
) : RenameProcessor(project, element, newName, isSearchInComments, isSearchTextOccurrences) {
override fun findUsages(): Array<out UsageInfo> {
return super.findUsages().filter {
it.element !is JetOperationReferenceExpression
}.toTypedArray()
}
}
}
@@ -341,5 +341,8 @@ public class QuickFixRegistrar : QuickFixContributor {
UNRESOLVED_REFERENCE.registerFactory(KotlinAddOrderEntryActionFactory)
MISPLACED_TYPE_PARAMETER_CONSTRAINTS.registerFactory(MoveTypeParameterConstraintFix)
DELEGATE_RESOLVED_TO_DEPRECATED_CONVENTION.registerFactory(DeprecatedFunctionConventionFix)
DEPRECATED_UNARY_PLUS_MINUS.registerFactory(DeprecatedFunctionConventionFix)
}
}
+7
View File
@@ -45,7 +45,14 @@ class Foo {
set(value) { $x = value }
}
class CustomDelegate {
operator fun get(thisRef: Any?, prop: PropertyMetadata): String = ""
operator fun set(thisRef: Any?, prop: PropertyMetadata, value: String) {}
}
class B {
var a: String by CustomDelegate()
fun plus(a: A): A = A()
}
+8 -1
View File
@@ -36,7 +36,7 @@ const val i = 1
annotation class Fancy(val param: Int)
@Fancy(<caret>i) class D
@Fancy(i) class D
class Foo {
var x: Int = 0
@@ -44,7 +44,14 @@ class Foo {
set(value) { field = value }
}
class CustomDelegate {
operator fun getValue(thisRef: Any?, prop: PropertyMetadata): String = ""
operator fun setValue(thisRef: Any?, prop: PropertyMetadata, value: String) {}
}
class B {
var a: String by CustomDelegate()
operator fun plus(a: A): A = A()
}
@@ -0,0 +1,8 @@
// "Rename to 'getValue'" "true"
class CustomDelegate
operator fun CustomDelegate.get(thisRef: Any?, prop: PropertyMetadata): String = ""
class Example {
val a: String by <caret>CustomDelegate()
}
@@ -0,0 +1,8 @@
// "Rename to 'getValue'" "true"
class CustomDelegate
operator fun CustomDelegate.getValue(thisRef: Any?, prop: PropertyMetadata): String = ""
class Example {
val a: String by CustomDelegate()
}
@@ -0,0 +1,9 @@
// "Rename to 'setValue'" "true"
class CustomDelegate {
operator fun get(thisRef: Any?, prop: PropertyMetadata): String = ""
operator fun set(thisRef: Any?, prop: PropertyMetadata, value: String) {}
}
class Example {
var a: String by <caret>CustomDelegate()
}
@@ -0,0 +1,9 @@
// "Rename to 'setValue'" "true"
class CustomDelegate {
operator fun get(thisRef: Any?, prop: PropertyMetadata): String = ""
operator fun setValue(thisRef: Any?, prop: PropertyMetadata, value: String) {}
}
class Example {
var a: String by CustomDelegate()
}
@@ -4373,6 +4373,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("deprecatedDelegatesGetSetExtension.kt")
public void testDeprecatedDelegatesGetSetExtension() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/deprecatedDelegatesGetSetExtension.kt");
doTest(fileName);
}
@TestMetadata("deprecatedDelegatesGetSetMember.kt")
public void testDeprecatedDelegatesGetSetMember() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/deprecatedDelegatesGetSetMember.kt");
doTest(fileName);
}
@TestMetadata("finalTrait.kt")
public void testFinalTrait() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/finalTrait.kt");