Add quickfix for DECLARATION_CANT_BE_INLINED #KT-9983 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
c44d3a8d68
commit
23734bae3e
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
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.idea.intentions.declarations.ConvertMemberToExtensionIntention
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtNamedFunction
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||
|
||||
object DeclarationCantBeInlinedFactory : KotlinIntentionActionsFactory() {
|
||||
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
|
||||
val function = diagnostic.psiElement as? KtNamedFunction ?: return emptyList()
|
||||
val containingClass = function.containingClass() ?: return emptyList()
|
||||
val fixes = mutableListOf<IntentionAction>()
|
||||
if (containingClass.isInterface()) {
|
||||
fixes.add(ConvertMemberToExtensionFix(function))
|
||||
} else if (function.hasModifier(KtTokens.OPEN_KEYWORD)) {
|
||||
fixes.add(RemoveModifierFix(function, KtTokens.OPEN_KEYWORD, false))
|
||||
}
|
||||
return fixes
|
||||
}
|
||||
}
|
||||
|
||||
private class ConvertMemberToExtensionFix(element: KtNamedFunction) : KotlinQuickFixAction<KtNamedFunction>(element) {
|
||||
override fun getText() = "Convert member to extension"
|
||||
|
||||
override fun getFamilyName() = text
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
element?.also { ConvertMemberToExtensionIntention.convert(it) }
|
||||
}
|
||||
}
|
||||
@@ -576,5 +576,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
||||
RESOLUTION_TO_CLASSIFIER.registerFactory(ConvertToAnonymousObjectFix)
|
||||
|
||||
NOTHING_TO_INLINE.registerFactory(RemoveModifierFix.createRemoveModifierFactory(isRedundant = false))
|
||||
|
||||
DECLARATION_CANT_BE_INLINED.registerFactory(DeclarationCantBeInlinedFactory)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Make 'foo' not open" "true"
|
||||
open class A {
|
||||
inline open fun foo()<caret> {}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Make 'foo' not open" "true"
|
||||
open class A {
|
||||
inline fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// "Convert member to extension" "true"
|
||||
// WITH_RUNTIME
|
||||
interface B {
|
||||
<caret>inline fun foo()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Convert member to extension" "true"
|
||||
// WITH_RUNTIME
|
||||
interface B {
|
||||
}
|
||||
|
||||
inline fun B.foo() {
|
||||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
|
||||
}
|
||||
+13
@@ -2236,6 +2236,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/declarationCantBeInlined")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class DeclarationCantBeInlined extends AbstractQuickFixMultiFileTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInDeclarationCantBeInlined() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/declarationCantBeInlined"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/decreaseVisibility")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
@@ -5056,6 +5056,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/declarationCantBeInlined")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class DeclarationCantBeInlined extends AbstractQuickFixTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInDeclarationCantBeInlined() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/declarationCantBeInlined"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("inClass.kt")
|
||||
public void testInClass() throws Exception {
|
||||
runTest("idea/testData/quickfix/declarationCantBeInlined/inClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("inInterface.kt")
|
||||
public void testInInterface() throws Exception {
|
||||
runTest("idea/testData/quickfix/declarationCantBeInlined/inInterface.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/quickfix/decreaseVisibility")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user