Add quickfix "Add 'init' keyword in whole project"
This commit is contained in:
@@ -14,6 +14,8 @@ add.modifier=Add ''{0}'' modifier
|
||||
add.modifier.family=Add Modifier
|
||||
add.init.keyword=Add 'init' keyword
|
||||
add.init.keyword.family=Add 'init' keyword
|
||||
add.init.keyword.in.whole.project=Add 'init' keyword in whole project
|
||||
add.init.keyword.in.whole.project.family=Add 'init' keyword in whole project
|
||||
make.element.in.classifiers.open=Make ''{0}'' in {1} open
|
||||
make.class.annotation.class=Make ''{0}'' an annotation class
|
||||
make.class.annotation.class.family=Make Class an Annotation Class
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.idea.*
|
||||
import com.intellij.openapi.project.*
|
||||
import com.intellij.openapi.editor.*
|
||||
import com.intellij.psi.*
|
||||
import org.jetbrains.kotlin.idea.project.PluginJetFilesProvider
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||
|
||||
@@ -57,3 +58,31 @@ public class AddInitKeywordFix(element: JetClassInitializer) : JetIntentionActio
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class AddInitKeywordFixInWholeProjectFix(elem: JetClassInitializer) : JetIntentionAction<JetClassInitializer>(elem) {
|
||||
override fun getText(): String = JetBundle.message("add.init.keyword.in.whole.project")
|
||||
|
||||
override fun getFamilyName(): String = JetBundle.message("add.init.keyword.in.whole.project.family")
|
||||
|
||||
override fun invoke(project: Project, editor: Editor, file: JetFile) {
|
||||
val files = PluginJetFilesProvider.allFilesInProject(file.getProject())
|
||||
|
||||
files.forEach { it.accept(AddInitKeywordVisitor(file)) }
|
||||
}
|
||||
|
||||
private class AddInitKeywordVisitor(private val file: JetFile) : JetTreeVisitorVoid() {
|
||||
override fun visitAnonymousInitializer(initializer: JetClassInitializer) {
|
||||
initializer.acceptChildren(this)
|
||||
if (!initializer.hasInitKeyword()) {
|
||||
AddInitKeywordFix.addInitKeyword(file, initializer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class object Factory : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic) =
|
||||
diagnostic.getPsiElement().getNonStrictParentOfType<JetClassInitializer>()?.let {
|
||||
AddInitKeywordFixInWholeProjectFix(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,5 +301,6 @@ public class QuickFixRegistrar {
|
||||
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToCompanionObjectFix.Factory);
|
||||
QuickFixes.factories.put(DEPRECATED_CLASS_OBJECT_SYNTAX, ClassObjectToCompanionObjectInWholeProjectFix.Factory);
|
||||
QuickFixes.factories.put(INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED, AddInitKeywordFix.Factory);
|
||||
QuickFixes.factories.put(INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED, AddInitKeywordFixInWholeProjectFix.Factory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
annotation class Ann3
|
||||
annotation class Ann4
|
||||
|
||||
class D {
|
||||
Ann3 init {
|
||||
|
||||
}
|
||||
Ann4 init {
|
||||
class Q {
|
||||
init {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class E {
|
||||
companion object {
|
||||
init {
|
||||
|
||||
}
|
||||
init {}
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() = 1
|
||||
class F {
|
||||
val a1 = foo()
|
||||
|
||||
init {
|
||||
|
||||
}
|
||||
|
||||
val a2 = foo()
|
||||
|
||||
init {
|
||||
|
||||
}
|
||||
|
||||
val a3 = foo(); // el
|
||||
/* abc */init {
|
||||
|
||||
}
|
||||
|
||||
val a4 = foo() // el
|
||||
|
||||
;/* abc */
|
||||
|
||||
init {
|
||||
|
||||
}
|
||||
|
||||
val a5 = foo()
|
||||
/* abc */
|
||||
|
||||
init {
|
||||
|
||||
}
|
||||
|
||||
val a6 = foo();
|
||||
init {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// "Add 'init' keyword in whole project" "true"
|
||||
|
||||
annotation class Ann1
|
||||
annotation class Ann2
|
||||
|
||||
class A {
|
||||
Ann1 Ann2 init {
|
||||
class Q {
|
||||
init {
|
||||
|
||||
}
|
||||
Ann2 init {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Ann1 init {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
Ann1 Ann2 init {
|
||||
class Q {
|
||||
init {
|
||||
|
||||
}
|
||||
Ann2 init {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
init {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// "Add 'init' keyword in whole project" "true"
|
||||
|
||||
annotation class Ann1
|
||||
annotation class Ann2
|
||||
|
||||
class A {
|
||||
Ann1 Ann2 <caret>{
|
||||
class Q {
|
||||
{
|
||||
|
||||
}
|
||||
Ann2 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Ann1 {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
Ann1 Ann2 {
|
||||
class Q {
|
||||
init {
|
||||
|
||||
}
|
||||
Ann2 {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
annotation class Ann3
|
||||
annotation class Ann4
|
||||
|
||||
class D {
|
||||
Ann3 init {
|
||||
|
||||
}
|
||||
Ann4 {
|
||||
class Q {
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class E {
|
||||
companion object {
|
||||
init {
|
||||
|
||||
}
|
||||
{}
|
||||
}
|
||||
}
|
||||
|
||||
fun foo() = 1
|
||||
class F {
|
||||
val a1 = foo();
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
val a2 = foo()
|
||||
|
||||
;{
|
||||
|
||||
}
|
||||
|
||||
val a3 = foo(); // el
|
||||
/* abc */{
|
||||
|
||||
}
|
||||
|
||||
val a4 = foo() // el
|
||||
|
||||
;/* abc */{
|
||||
|
||||
}
|
||||
|
||||
val a5 = foo()
|
||||
/* abc */;{
|
||||
|
||||
}
|
||||
|
||||
val a6 = foo();
|
||||
init {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -860,6 +860,12 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
|
||||
})
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Migration extends AbstractQuickFixMultiFileTest {
|
||||
@TestMetadata("addInitKeywordMultiple.before.Main.kt")
|
||||
public void testAddInitKeywordMultiple() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/addInitKeywordMultiple.before.Main.kt");
|
||||
doTestWithExtraFile(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInMigration() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^(\\w+)\\.before\\.Main\\.kt$"), true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user