Add quick fixes for mod/rem migration
- Remove 'operator' modifier - Rename operator 'mod/modAssign' to 'rem/remAssign'
This commit is contained in:
@@ -132,6 +132,9 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
NON_PRIVATE_CONSTRUCTOR_IN_ENUM.registerFactory(removeModifierFactory)
|
||||
NON_PRIVATE_CONSTRUCTOR_IN_SEALED.registerFactory(removeModifierFactory)
|
||||
|
||||
DEPRECATED_BINARY_MOD.registerFactory(removeModifierFactory)
|
||||
DEPRECATED_BINARY_MOD.registerFactory(RenameModToRemFix.Factory)
|
||||
|
||||
UNRESOLVED_REFERENCE.registerFactory(ImportMemberFix)
|
||||
UNRESOLVED_REFERENCE.registerFactory(ImportFix)
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2010-2016 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.refactoring.rename.RenameProcessor
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions.REM_TO_MOD_OPERATION_NAMES
|
||||
|
||||
class RenameModToRemFix(element: KtNamedFunction, val newName: Name) : KotlinQuickFixAction<KtNamedFunction>(element) {
|
||||
override fun getText(): String = "Rename to '$newName'"
|
||||
|
||||
override fun getFamilyName(): String = text
|
||||
|
||||
override fun startInWriteAction(): Boolean = false
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
RenameProcessor(project, element ?: return, newName.asString(), false, false).run()
|
||||
}
|
||||
|
||||
companion object Factory : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
if (diagnostic.factory != Errors.DEPRECATED_BINARY_MOD) return null
|
||||
|
||||
val operatorMod = diagnostic.psiElement.getNonStrictParentOfType<KtNamedFunction>() ?: return null
|
||||
val newName = REM_TO_MOD_OPERATION_NAMES.inverse()[operatorMod.nameAsName] ?: return null
|
||||
return RenameModToRemFix(operatorMod, newName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Remove 'operator' modifier
|
||||
|
||||
object A {
|
||||
operator<caret> fun mod(x: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Remove 'operator' modifier
|
||||
|
||||
object A {
|
||||
<caret>fun mod(x: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Remove 'operator' modifier
|
||||
|
||||
object A {
|
||||
operator<caret> fun modAssign(x: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Remove 'operator' modifier
|
||||
|
||||
object A {
|
||||
<caret>fun modAssign(x: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Rename to 'rem'" "true"
|
||||
|
||||
object A
|
||||
operator<caret> fun A.mod(x: Int) {}
|
||||
|
||||
fun test() {
|
||||
A.mod(3)
|
||||
A % 2
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Rename to 'rem'" "true"
|
||||
|
||||
object A
|
||||
operator<caret> fun A.rem(x: Int) {}
|
||||
|
||||
fun test() {
|
||||
A.rem(3)
|
||||
A % 2
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Rename to 'rem'" "true"
|
||||
|
||||
object Rem {
|
||||
operator<caret> fun mod(x: Int) {}
|
||||
operator fun modAssign(x: Int) {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Rem % 1
|
||||
Rem.mod(1)
|
||||
Rem.modAssign(1)
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Rename to 'rem'" "true"
|
||||
|
||||
object Rem {
|
||||
operator fun rem(x: Int) {}
|
||||
operator fun modAssign(x: Int) {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Rem % 1
|
||||
Rem.rem(1)
|
||||
Rem.modAssign(1)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Rename to 'remAssign'" "true"
|
||||
|
||||
object Rem {
|
||||
operator fun mod(x: Int) {}
|
||||
operator<caret> fun modAssign(x: Int) {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Rem % 1
|
||||
Rem.mod(1)
|
||||
Rem.modAssign(1)
|
||||
val c = Rem
|
||||
c %= 1
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Rename to 'remAssign'" "true"
|
||||
|
||||
object Rem {
|
||||
operator fun mod(x: Int) {}
|
||||
operator fun remAssign(x: Int) {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
Rem % 1
|
||||
Rem.mod(1)
|
||||
Rem.remAssign(1)
|
||||
val c = Rem
|
||||
c %= 1
|
||||
}
|
||||
@@ -6335,6 +6335,18 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeModifierFromOperatorMod.kt")
|
||||
public void testRemoveModifierFromOperatorMod() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/removeModifierFromOperatorMod.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeModifierFromOperatorModAssign.kt")
|
||||
public void testRemoveModifierFromOperatorModAssign() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/removeModifierFromOperatorModAssign.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("removeProtectedModifier.kt")
|
||||
public void testRemoveProtectedModifier() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/modifiers/removeProtectedModifier.kt");
|
||||
@@ -7645,6 +7657,33 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/renameToRem")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RenameToRem extends AbstractQuickFixTest {
|
||||
public void testAllFilesPresentInRenameToRem() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/renameToRem"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("modAsExtension.kt")
|
||||
public void testModAsExtension() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToRem/modAsExtension.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("modAsMember.kt")
|
||||
public void testModAsMember() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToRem/modAsMember.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("modAssignAsMember.kt")
|
||||
public void testModAssignAsMember() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToRem/modAssignAsMember.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/renameToUnderscore")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user