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
@@ -273,6 +273,8 @@ public interface Errors {
DiagnosticFactory0<KtElement> MISSING_CONSTRUCTOR_KEYWORD = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> MISSING_CONSTRUCTOR_BRACKETS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NON_PRIVATE_CONSTRUCTOR_IN_ENUM = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> NON_PRIVATE_CONSTRUCTOR_IN_SEALED = DiagnosticFactory0.create(ERROR);
@@ -591,6 +591,7 @@ public class DefaultErrorMessages {
MAP.put(ALWAYS_NULL, "The result of the expression is always null");
MAP.put(MISSING_CONSTRUCTOR_KEYWORD, "Use 'constructor' keyword after modifiers of primary constructor");
MAP.put(MISSING_CONSTRUCTOR_BRACKETS, "Constructor requires brackets");
MAP.put(NON_PRIVATE_CONSTRUCTOR_IN_ENUM, "Constructor must be private in enum class");
MAP.put(NON_PRIVATE_CONSTRUCTOR_IN_SEALED, "Constructor must be private in sealed class");
@@ -449,6 +449,10 @@ class DeclarationsChecker(
declaration.modifierList?.let { trace.report(MISSING_CONSTRUCTOR_KEYWORD.on(it)) }
}
if (declaration.valueParameterList == null) {
declaration.getConstructorKeyword()?.let { trace.report(MISSING_CONSTRUCTOR_BRACKETS.on(it)) }
}
if (classOrObject !is KtClass) {
trace.report(CONSTRUCTOR_IN_OBJECT.on(declaration))
}
@@ -0,0 +1,2 @@
class A private <!MISSING_CONSTRUCTOR_BRACKETS!>constructor<!><!SYNTAX!><!> {
}
@@ -0,0 +1,8 @@
package
public final class A {
private constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -12904,6 +12904,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("primaryConstructorMissingBrackets.kt")
public void testPrimaryConstructorMissingBrackets() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingBrackets.kt");
doTest(fileName);
}
@TestMetadata("primaryConstructorMissingKeyword.kt")
public void testPrimaryConstructorMissingKeyword() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/modifiers/primaryConstructorMissingKeyword.kt");
@@ -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)