Add quickfix for 'Illegal inline parameter modifier'

Adds inline to function if crossinline is used on parameter

#KT-14046 Fixed
This commit is contained in:
Vyacheslav Gerasimov
2017-04-18 21:12:55 +03:00
parent 49a211b3cd
commit 199bff7e70
6 changed files with 91 additions and 1 deletions
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2017 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.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
class AddInlineToFunctionFix(function: KtFunction):
KotlinQuickFixAction<KtFunction>(function) {
override fun getFamilyName(): String = "Add 'inline' to function"
override fun getText(): String = "Add 'inline' to function '${element?.name}'"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.addModifier(KtTokens.INLINE_KEYWORD)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val function = diagnostic.psiElement.getParentOfType<KtFunction>(true) ?: return null
if (function.isLocal) {
return null
}
return AddInlineToFunctionFix(function)
}
}
}
@@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.idea.core.overrideImplement.ImplementAsConstructorParameter
import org.jetbrains.kotlin.idea.core.overrideImplement.ImplementMembersHandler
@@ -482,5 +481,7 @@ class QuickFixRegistrar : QuickFixContributor {
UNRESOLVED_REFERENCE.registerFactory(CreateLabelFix)
YIELD_IS_RESERVED.registerFactory(UnsupportedYieldFix)
INVALID_TYPE_OF_ANNOTATION_MEMBER.registerFactory(TypeOfAnnotationMemberFix)
ILLEGAL_INLINE_PARAMETER_MODIFIER.registerFactory(AddInlineToFunctionFix)
}
}
+5
View File
@@ -0,0 +1,5 @@
// "Add 'inline' to function 'foo'" "true"
fun foo(<caret>crossinline body: () -> Unit) {
}
+5
View File
@@ -0,0 +1,5 @@
// "Add 'inline' to function 'foo'" "true"
inline fun foo(<caret>crossinline body: () -> Unit) {
}
+9
View File
@@ -0,0 +1,9 @@
// "Add 'inline' to function 'foo'" "false"
// ACTION: Convert to expression body
// ERROR: Modifier 'crossinline' is allowed only for function parameters of an inline function
fun bar() {
fun foo(<caret>crossinline body: () -> Unit) {
}
}
@@ -384,6 +384,27 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/addInline")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddInline extends AbstractQuickFixTest {
public void testAllFilesPresentInAddInline() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addInline"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInline/basic.kt");
doTest(fileName);
}
@TestMetadata("local.kt")
public void testLocal() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addInline/local.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/addInlineToReifiedFunctionFix")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)