Add quick-fix for default parameter value removal #KT-25146 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
a059d50c8f
commit
1b1e503716
@@ -567,5 +567,8 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
CONST_VAL_NOT_TOP_LEVEL_OR_OBJECT.registerFactory(MoveMemberToCompanionObjectIntention)
|
||||
|
||||
NO_COMPANION_OBJECT.registerFactory(AddIsToWhenConditionFix)
|
||||
|
||||
DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE.registerFactory(RemoveDefaultParameterValueFix)
|
||||
ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS.registerFactory(RemoveDefaultParameterValueFix)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.util.CommentSaver
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtParameter
|
||||
|
||||
class RemoveDefaultParameterValueFix(parameter: KtParameter) : KotlinQuickFixAction<KtParameter>(parameter) {
|
||||
|
||||
override fun getText() = "Remove default parameter value"
|
||||
|
||||
override fun getFamilyName() = text
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val parameter = element ?: return
|
||||
val typeReference = parameter.typeReference ?: return
|
||||
val defaultValue = parameter.defaultValue ?: return
|
||||
val commentSaver = CommentSaver(parameter)
|
||||
parameter.deleteChildRange(typeReference.nextSibling, defaultValue)
|
||||
commentSaver.restore(parameter)
|
||||
}
|
||||
|
||||
companion object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtParameter>? =
|
||||
(diagnostic.psiElement as? KtParameter)?.let { RemoveDefaultParameterValueFix(it) }
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Remove default parameter value" "true"
|
||||
// DISABLE-ERRORS
|
||||
interface Foo {
|
||||
fun test(x: Int, y: Int)
|
||||
}
|
||||
|
||||
expect class Bar : Foo {
|
||||
override fun test(x: Int, y: Int)
|
||||
}
|
||||
|
||||
actual class Bar : Foo {
|
||||
actual override fun test(x: Int, y: Int = 1<caret>) {}
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
// "Remove default parameter value" "true"
|
||||
// DISABLE-ERRORS
|
||||
interface Foo {
|
||||
fun test(x: Int, y: Int)
|
||||
}
|
||||
|
||||
expect class Bar : Foo {
|
||||
override fun test(x: Int, y: Int)
|
||||
}
|
||||
|
||||
actual class Bar : Foo {
|
||||
actual override fun test(x: Int, y: Int) {}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Remove default parameter value" "true"
|
||||
open class A {
|
||||
open fun foo(x: Int, y: Int) {}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo(x : Int = 1<caret>, y: Int) {}
|
||||
}
|
||||
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// "Remove default parameter value" "true"
|
||||
open class A {
|
||||
open fun foo(x: Int, y: Int) {}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo(x : Int, y: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Remove default parameter value" "true"
|
||||
open class A {
|
||||
open fun foo(x: Int, y: Int) {}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo(x : Int /* comment1 */ = /* comment2 */ 1<caret>, y: Int) {}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Remove default parameter value" "true"
|
||||
open class A {
|
||||
open fun foo(x: Int, y: Int) {}
|
||||
}
|
||||
|
||||
class B : A() {
|
||||
override fun foo(x : Int /* comment1 */ /* comment2 */, y: Int) {}
|
||||
}
|
||||
+13
@@ -3298,6 +3298,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeDefaultParameterValue")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveDefaultParameterValue extends AbstractQuickFixMultiFileTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInRemoveDefaultParameterValue() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeDefaultParameterValue"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeFinalUpperBound")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -8963,6 +8963,34 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeDefaultParameterValue")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class RemoveDefaultParameterValue extends AbstractQuickFixTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
@TestMetadata("actualFunctionWithDefaultArguments.kt")
|
||||
public void testActualFunctionWithDefaultArguments() throws Exception {
|
||||
runTest("idea/testData/quickfix/removeDefaultParameterValue/actualFunctionWithDefaultArguments.kt");
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInRemoveDefaultParameterValue() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/removeDefaultParameterValue"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("defaultValueNotAllowedInOverride.kt")
|
||||
public void testDefaultValueNotAllowedInOverride() throws Exception {
|
||||
runTest("idea/testData/quickfix/removeDefaultParameterValue/defaultValueNotAllowedInOverride.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("hasComment.kt")
|
||||
public void testHasComment() throws Exception {
|
||||
runTest("idea/testData/quickfix/removeDefaultParameterValue/hasComment.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/removeFinalUpperBound")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user