Add quickfix removing names of function expressions

This commit is contained in:
Denis Zharkov
2015-05-16 22:09:04 +03:00
parent b650753643
commit 5b1f3080d3
10 changed files with 228 additions and 0 deletions
@@ -337,5 +337,8 @@ public class QuickFixRegistrar {
QuickFixes.factories.put(MISSING_CONSTRUCTOR_KEYWORD, MissingConstructorKeywordFix.Companion);
QuickFixes.factories.put(MISSING_CONSTRUCTOR_KEYWORD, MissingConstructorKeywordFix.Companion.createWholeProjectFixFactory());
QuickFixes.factories.put(FUNCTION_EXPRESSION_WITH_NAME, RemoveNameFromFunctionExpressionFix.Companion);
QuickFixes.factories.put(FUNCTION_EXPRESSION_WITH_NAME, RemoveNameFromFunctionExpressionFix.Companion.createWholeProjectFixFactory());
}
}
@@ -0,0 +1,83 @@
/*
* 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.PsiWhiteSpace
import org.jetbrains.kotlin.JetNodeTypes
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionFactory
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.resolve.BindingContext
public class RemoveNameFromFunctionExpressionFix(element: JetNamedFunction) : JetIntentionAction<JetNamedFunction>(element) {
override fun getText(): String = "Remove identifier from function expression"
override fun getFamilyName(): String = getText()
override fun invoke(project: Project, editor: Editor?, file: JetFile) = removeNameFromFunction(element)
companion object : JetSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) =
diagnostic.createIntentionForFirstParentOfType(::RemoveNameFromFunctionExpressionFix)
public fun createWholeProjectFixFactory(): JetSingleIntentionActionFactory = createIntentionFactory {
JetWholeProjectForEachElementOfTypeFix.createByPredicate<JetNamedFunction>(
predicate = { isFunctionExpression(it) },
taskProcessor = { removeNameFromFunction(it) },
modalTitle = "Removing identifier from function expressions",
name = "Remove identifier from function expressions in the whole project",
familyName = "Remove identifier from function expressions in the whole project"
)
}
private fun isFunctionExpression(function: JetNamedFunction): Boolean {
var parent = function.getParent()
while (parent is JetAnnotatedExpression || parent is JetLabeledExpression) {
parent = parent.getParent()
}
return function.isLocal() && parent !is JetBlockExpression
}
private fun removeNameFromFunction(function: JetNamedFunction) {
var wereAutoLabelUsages = false
val name = function.getName() ?: return
function.forEachDescendantsOfType<JetReturnExpression> {
if (!wereAutoLabelUsages && it.getLabelName() == name) {
wereAutoLabelUsages = it.analyze().get(BindingContext.LABEL_TARGET, it.getTargetLabel()) == function
}
}
function.getNameIdentifier()?.delete()
if (wereAutoLabelUsages) {
val psiFactory = JetPsiFactory(function)
val newFunction = psiFactory.createExpressionByPattern("$name@ $0", function)
function.replace(newFunction)
}
}
}
}
@@ -0,0 +1,7 @@
// "Remove identifier from function expression" "true"
fun foo() {
(fun bar<caret>() {
return@bar
})
}
@@ -0,0 +1,7 @@
// "Remove identifier from function expression" "true"
fun foo() {
(bar@ fun() {
return@bar
})
}
@@ -0,0 +1,46 @@
// "Remove identifier from function expressions in the whole project" "true"
inline fun run(block: () -> Unit) = block()
annotation class ann
fun foo() {
l2@ @ann l2@ fun local() {
run(l1@ fun() { return@l1 })
run(label@ expr@ fun() {
return@label
return@expr
})
}
}
class A {
fun bar() {
run(toRun@ fun() {
if (1 == 1) return@toRun
return@bar
})
run(
/* abc */ fun /* cde */ () {
return@bar
}
)
run(
/* abc */
foo@ fun /* cde */ () {
return@foo
}
)
}
init {
(foo@ fun A.() {
(fun() {
val x = 1
})
return@foo
})
}
}
@@ -0,0 +1,46 @@
// "Remove identifier from function expressions in the whole project" "true"
inline fun run(block: () -> Unit) = block()
annotation class ann
fun foo() {
l2@ @ann l2@ fun local() {
run(l1@ fun() { return@l1 })
run(label@ fun expr<caret>() {
return@label
return@expr
})
}
}
class A {
fun bar() {
run(fun toRun() {
if (1 == 1) return@toRun
return@bar
})
run(
/* abc */ fun /* cde */ toRun() {
return@bar
}
)
run(
/* abc */
fun /* cde */ foo() {
return@foo
}
)
}
init {
(fun A.foo() {
(fun bar() {
val x = 1
})
return@foo
})
}
}
@@ -0,0 +1,3 @@
fun main() {
(fun foo() {})
}
@@ -918,6 +918,21 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/migration/removeNameFromFunctionExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveNameFromFunctionExpression extends AbstractQuickFixMultiFileTest {
public void testAllFilesPresentInRemoveNameFromFunctionExpression() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/removeNameFromFunctionExpression"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
}
@TestMetadata("manyFilesMuitliple.before.Main.kt")
public void testManyFilesMuitliple() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/removeNameFromFunctionExpression/manyFilesMuitliple.before.Main.kt");
doTestWithExtraFile(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/replaceJavaClassAsAnnotationParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -3295,6 +3295,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/migration/removeNameFromFunctionExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveNameFromFunctionExpression extends AbstractQuickFixTest {
public void testAllFilesPresentInRemoveNameFromFunctionExpression() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/removeNameFromFunctionExpression"), Pattern.compile("^(\\w+)\\.kt$"), true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/removeNameFromFunctionExpression/basic.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/migration/replaceJavaClassAsAnnotationParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)