Add quick-fix for default parameter value removal #KT-25146 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-07-24 09:03:21 +03:00
committed by Mikhail Glukhikh
parent a059d50c8f
commit 1b1e503716
10 changed files with 132 additions and 0 deletions
@@ -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) }
}
}
@@ -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>) {}
}
@@ -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) {}
}
@@ -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) {}
}
@@ -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) {}
}
@@ -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)