Add quick-fix for empty brackets after primary constructor

So #KT-18534 Fixed
This commit is contained in:
Toshiaki Kameyama
2017-06-22 06:37:39 +03:00
committed by Mikhail Glukhikh
parent 69457ef3f1
commit ed04b4debd
12 changed files with 99 additions and 1 deletions
@@ -0,0 +1,50 @@
/*
* 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 com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.quickfix.quickfixUtil.createIntentionForFirstParentOfType
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class MissingConstructorBracketsFix(element: KtPrimaryConstructor) : KotlinQuickFixAction<KtPrimaryConstructor>(element), CleanupFix {
override fun getFamilyName(): String = text
override fun getText(): String = "Add empty brackets after primary constructor"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val constructor = element ?: return
val constructorKeyword = constructor.getConstructorKeyword() ?: return
if (constructor.valueParameterList != null) return
editor?.run {
val endOffset = constructorKeyword.endOffset
document.insertString(endOffset, "()")
caretModel.moveToOffset(endOffset + 1)
PsiDocumentManager.getInstance(project).commitDocument(document)
}
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? =
diagnostic.createIntentionForFirstParentOfType(::MissingConstructorBracketsFix)
}
}
@@ -386,6 +386,8 @@ class QuickFixRegistrar : QuickFixContributor {
MISSING_CONSTRUCTOR_KEYWORD.registerFactory(MissingConstructorKeywordFix)
MISSING_CONSTRUCTOR_BRACKETS.registerFactory(MissingConstructorBracketsFix)
ANONYMOUS_FUNCTION_WITH_NAME.registerFactory(RemoveNameFromFunctionExpressionFix)
UNRESOLVED_REFERENCE.registerFactory(ReplaceObsoleteLabelSyntaxFix)
+1 -1
View File
@@ -1,4 +1,4 @@
// "class org.jetbrains.kotlin.idea.intentions.CreateKotlinSubClassIntention" "false"
// ACTION: Create test
abstract class <caret>Base private constructor
abstract class <caret>Base private constructor()
@@ -0,0 +1,4 @@
// "Add empty brackets after primary constructor" "true"
class Fruit private constructor<caret> {
}
@@ -0,0 +1,4 @@
// "Add empty brackets after primary constructor" "true"
class Fruit private constructor(<caret>) {
}
@@ -6741,6 +6741,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
}
}
@TestMetadata("idea/testData/quickfix/missingConstructorBrackets")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class MissingConstructorBrackets extends AbstractQuickFixTest {
public void testAllFilesPresentInMissingConstructorBrackets() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/missingConstructorBrackets"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/missingConstructorBrackets/basic.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/quickfix/modifiers")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)