Add quick fix for TOPLEVEL_TYPEALIASES_ONLY

This commit is contained in:
Toshiaki Kameyama
2020-01-07 03:12:29 +01:00
committed by Vladimir Dolzhenko
parent cd880c779b
commit 8aa120576b
13 changed files with 169 additions and 1 deletions
@@ -2195,4 +2195,5 @@ move.refactoring.error.text.cannot.perform.refactoring.since.the.following.files
kotlin.script.definitions.model.name.is.enabled=Is Enabled
kotlin.script.definitions.model.name.pattern.extension=Pattern/Extension
kotlin.script.definitions.model.name.name=Name
codestyle.name.kotlin=Kotlin
codestyle.name.kotlin=Kotlin
fix.move.typealias.to.top.level=Move typealias to top level
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtTypeAlias
import org.jetbrains.kotlin.psi.psiUtil.parents
class MoveTypeAliasToTopLevelFix(element: KtTypeAlias) : KotlinQuickFixAction<KtTypeAlias>(element) {
override fun getText() = KotlinBundle.message("fix.move.typealias.to.top.level")
override fun getFamilyName() = text
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val typeAlias = element ?: return
val parents = typeAlias.parents.toList().reversed()
val containingFile = parents.firstOrNull() as? KtFile ?: return
val target = parents.getOrNull(1) ?: return
containingFile.addAfter(typeAlias, target)
containingFile.addAfter(KtPsiFactory(typeAlias).createNewLine(2), target)
typeAlias.delete()
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic) = (diagnostic.psiElement as? KtTypeAlias)?.let { MoveTypeAliasToTopLevelFix(it) }
}
}
@@ -644,5 +644,7 @@ class QuickFixRegistrar : QuickFixContributor {
TOO_MANY_ARGUMENTS.registerFactory(RemoveArgumentFix)
FUN_INTERFACE_WRONG_COUNT_OF_ABSTRACT_MEMBERS.registerFactory(RemoveModifierFix.createRemoveFunFromInterfaceFactory())
TOPLEVEL_TYPEALIASES_ONLY.registerFactory(MoveTypeAliasToTopLevelFix)
}
}
@@ -0,0 +1,9 @@
// "Move typealias to top level" "true"
class C {
<caret>typealias Foo = String
fun bar(foo: Foo) {
}
}
fun baz() {}
@@ -0,0 +1,10 @@
// "Move typealias to top level" "true"
class C {
fun bar(foo: Foo) {
}
}
typealias Foo = String
fun baz() {}
@@ -0,0 +1,11 @@
// "Move typealias to top level" "true"
class C {
class CC {
<caret>typealias Foo = String
fun bar(foo: Foo) {
}
}
}
fun baz() {}
@@ -0,0 +1,12 @@
// "Move typealias to top level" "true"
class C {
class CC {
fun bar(foo: Foo) {
}
}
}
typealias Foo = String
fun baz() {}
@@ -0,0 +1,9 @@
// "Move typealias to top level" "true"
fun bar() {
<caret>typealias Foo = String
fun baz(foo: Foo) {
}
}
fun qux() {}
@@ -0,0 +1,10 @@
// "Move typealias to top level" "true"
fun bar() {
fun baz(foo: Foo) {
}
}
typealias Foo = String
fun qux() {}
@@ -0,0 +1,11 @@
// "Move typealias to top level" "true"
fun bar() {
class C {
<caret>typealias Foo = String
fun baz(foo: Foo) {
}
}
}
fun qux() {}
@@ -0,0 +1,12 @@
// "Move typealias to top level" "true"
fun bar() {
class C {
fun baz(foo: Foo) {
}
}
}
typealias Foo = String
fun qux() {}
@@ -3443,6 +3443,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
}
}
@TestMetadata("idea/testData/quickfix/moveTypeAliasToTopLevel")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MoveTypeAliasToTopLevel extends AbstractQuickFixMultiFileTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestWithExtraFile, this, testDataFilePath);
}
public void testAllFilesPresentInMoveTypeAliasToTopLevel() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/moveTypeAliasToTopLevel"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), null, true);
}
}
@TestMetadata("idea/testData/quickfix/nullables")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -9508,6 +9508,39 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/moveTypeAliasToTopLevel")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MoveTypeAliasToTopLevel extends AbstractQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInMoveTypeAliasToTopLevel() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/moveTypeAliasToTopLevel"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("inClass.kt")
public void testInClass() throws Exception {
runTest("idea/testData/quickfix/moveTypeAliasToTopLevel/inClass.kt");
}
@TestMetadata("inClass2.kt")
public void testInClass2() throws Exception {
runTest("idea/testData/quickfix/moveTypeAliasToTopLevel/inClass2.kt");
}
@TestMetadata("inFunction.kt")
public void testInFunction() throws Exception {
runTest("idea/testData/quickfix/moveTypeAliasToTopLevel/inFunction.kt");
}
@TestMetadata("inFunction2.kt")
public void testInFunction2() throws Exception {
runTest("idea/testData/quickfix/moveTypeAliasToTopLevel/inFunction2.kt");
}
}
@TestMetadata("idea/testData/quickfix/nullables")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)