Add QuickFix to add 'crossinline' to parameter (#852)

Fixes #KT-10844
This commit is contained in:
Kirill Rakhman
2016-04-13 13:25:35 +02:00
committed by Dmitry Jemerov
parent 56fd2d28b8
commit 9495257de3
5 changed files with 94 additions and 0 deletions
@@ -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 org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class AddCrossInlineFix(element: KtNameReferenceExpression) : KotlinQuickFixAction<KtNameReferenceExpression>(element) {
override fun getText() = "Add 'crossinline' to parameter '${element.getReferencedName()}'"
override fun getFamilyName() = "Add 'crossinline' to parameter"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val parameter = element.findParameterWithName(element.getReferencedName()) ?: return
if (!parameter.hasModifier(KtTokens.CROSSINLINE_KEYWORD)) {
parameter.addModifier(KtTokens.CROSSINLINE_KEYWORD)
}
}
private fun KtElement.findParameterWithName(name: String): KtParameter? {
val function = getStrictParentOfType<KtFunction>() ?: return null
return function.valueParameters.firstOrNull { it.name == name } ?: function.findParameterWithName(name)
}
companion object Factory : KotlinIntentionActionsFactory() {
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
val casted = Errors.NON_LOCAL_RETURN_NOT_ALLOWED.cast(diagnostic)
val reference = casted.a as? KtNameReferenceExpression ?: return emptyList()
return listOf(AddCrossInlineFix(reference))
}
}
}
@@ -370,5 +370,7 @@ class QuickFixRegistrar : QuickFixContributor {
UNSUPPORTED.registerFactory(UnsupportedAsyncFix)
DATA_CLASS_NOT_PROPERTY_PARAMETER.registerFactory(AddValVarToConstructorParameterAction.QuickFixFactory)
NON_LOCAL_RETURN_NOT_ALLOWED.registerFactory(AddCrossInlineFix)
}
}
+13
View File
@@ -0,0 +1,13 @@
// "Add 'crossinline' to parameter 'block'" "true"
interface I {
fun foo()
}
inline fun bar(block: () -> Unit) {
object : I {
override fun foo() {
<caret>block()
}
}
}
+13
View File
@@ -0,0 +1,13 @@
// "Add 'crossinline' to parameter 'block'" "true"
interface I {
fun foo()
}
inline fun bar(crossinline block: () -> Unit) {
object : I {
override fun foo() {
<caret>block()
}
}
}
@@ -200,6 +200,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addCrossinline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddCrossinline extends AbstractQuickFixTest {
public void testAllFilesPresentInAddCrossinline() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addCrossinline"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addCrossinline/basic.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/addGenericUpperBound")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)