Implement quick-fix replacing parameter name with _

This quick-fix is registered for UNUSED_* diagnostic in places
where it can be applied (destructured declarations/lambda parameters)

 #KT-14431 Fixed
This commit is contained in:
Denis Zharkov
2016-10-18 18:47:12 +03:00
parent 51f4244980
commit 5e1b219784
13 changed files with 180 additions and 0 deletions
@@ -196,6 +196,9 @@ class QuickFixRegistrar : QuickFixContributor {
VIRTUAL_MEMBER_HIDDEN.registerFactory(AddOverrideToEqualsHashCodeToStringActionFactory)
UNUSED_VARIABLE.registerFactory(RemovePsiElementSimpleFix.RemoveVariableFactory)
UNUSED_VARIABLE.registerFactory(RenameToUnderscoreFix.Factory)
UNUSED_DESTRUCTURED_PARAMETER_ENTRY.registerFactory(RenameToUnderscoreFix.Factory)
SENSELESS_COMPARISON.registerFactory(SimplifyComparisonFix)
@@ -273,6 +276,7 @@ class QuickFixRegistrar : QuickFixContributor {
TOO_MANY_ARGUMENTS.registerFactory(ChangeFunctionSignatureFix)
NO_VALUE_FOR_PARAMETER.registerFactory(ChangeFunctionSignatureFix)
UNUSED_PARAMETER.registerFactory(RemoveUnusedFunctionParameterFix)
UNUSED_PARAMETER.registerFactory(RenameToUnderscoreFix.Factory)
EXPECTED_PARAMETERS_NUMBER_MISMATCH.registerFactory(ChangeFunctionLiteralSignatureFix)
EXPECTED_PARAMETER_TYPE_MISMATCH.registerFactory(ChangeTypeFix)
@@ -0,0 +1,48 @@
/*
* 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 org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
class RenameToUnderscoreFix(element: KtCallableDeclaration) : KotlinQuickFixAction<KtCallableDeclaration>(element) {
override fun getText() = "Rename to _"
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element.nameIdentifier?.replace(KtPsiFactory(project).createIdentifier("_"))
}
companion object Factory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val declaration: KtCallableDeclaration? = when (diagnostic.factory) {
Errors.UNUSED_PARAMETER -> diagnostic.psiElement as? KtParameter
Errors.UNUSED_VARIABLE, Errors.UNUSED_DESTRUCTURED_PARAMETER_ENTRY ->
diagnostic.psiElement as? KtDestructuringDeclarationEntry
else -> null
}
if (declaration?.nameIdentifier == null) return null
return RenameToUnderscoreFix(declaration)
}
}
}
@@ -3,6 +3,7 @@
// ACTION: Make 'Nested' internal
// ACTION: Make 'Nested' public
// ACTION: Remove parameter 'arg'
// ACTION: Rename to _
// ACTION: Convert to expression body
// ERROR: 'internal' function exposes its 'private' parameter type argument Nested
// ERROR: Cannot access 'Nested': it is private in 'Outer'
@@ -0,0 +1,8 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun bar() {
val (x<caret>, y) = A("", 1)
y.hashCode()
}
@@ -0,0 +1,8 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun bar() {
val (_, y) = A("", 1)
y.hashCode()
}
@@ -0,0 +1,9 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun bar(z: List<A>) {
for ((x<caret>, y) in z) {
y.hashCode()
}
}
@@ -0,0 +1,9 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun bar(z: List<A>) {
for ((_, y) in z) {
y.hashCode()
}
}
@@ -0,0 +1,13 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun foo(block: (A) -> Unit) {
block(A("", 1))
}
fun bar() {
foo { (x<caret>, y: Int) ->
y.hashCode()
}
}
@@ -0,0 +1,13 @@
// "Rename to _" "true"
data class A(val x: String, val y: Int)
fun foo(block: (A) -> Unit) {
block(A("", 1))
}
fun bar() {
foo { (_<caret>, y: Int) ->
y.hashCode()
}
}
@@ -0,0 +1,10 @@
// "Rename to _" "true"
fun foo(block: (String, Int) -> Unit) {
block("", 1)
}
fun bar() {
foo { x<caret>: String, y: Int ->
y.hashCode()
}
}
@@ -0,0 +1,10 @@
// "Rename to _" "true"
fun foo(block: (String, Int) -> Unit) {
block("", 1)
}
fun bar() {
foo { _: String, y: Int ->
y.hashCode()
}
}
@@ -0,0 +1,8 @@
// "Rename to _" "false"
// ACTION: Remove variable 'x'
// ACTION: Specify type explicitly
// ACTION: Split property declaration
fun bar() {
val x<caret> = 1
}
@@ -7419,6 +7419,45 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/renameToUnderscore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RenameToUnderscore extends AbstractQuickFixTest {
public void testAllFilesPresentInRenameToUnderscore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/renameToUnderscore"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("commonDestructuring.kt")
public void testCommonDestructuring() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/commonDestructuring.kt");
doTest(fileName);
}
@TestMetadata("forDestructuring.kt")
public void testForDestructuring() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/forDestructuring.kt");
doTest(fileName);
}
@TestMetadata("lambdaDestructuring.kt")
public void testLambdaDestructuring() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/lambdaDestructuring.kt");
doTest(fileName);
}
@TestMetadata("lambdaParameter.kt")
public void testLambdaParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/lambdaParameter.kt");
doTest(fileName);
}
@TestMetadata("noActionForCommonVal.kt")
public void testNoActionForCommonVal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/renameToUnderscore/noActionForCommonVal.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/renameUnresolvedReference")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)