mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Handle compact constructor in regular class: quick-fix to add () (IDEA-228460)
GitOrigin-RevId: c019e19e8023c0209f6e5ed54f8a486689bb0e0a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
e29a0e3102
commit
662cb5deae
@@ -480,4 +480,7 @@ public abstract class QuickFixFactory {
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createWrapSwitchRuleStatementsIntoBlockFix(PsiSwitchLabeledRuleStatement rule);
|
||||
|
||||
@NotNull
|
||||
public abstract IntentionAction createAddParameterListFix(PsiMethod method);
|
||||
}
|
||||
+15
-7
@@ -1937,7 +1937,18 @@ public class HighlightMethodUtil {
|
||||
public static HighlightInfo checkRecordConstructorDeclaration(PsiMethod method) {
|
||||
if (!method.isConstructor()) return null;
|
||||
PsiClass aClass = method.getContainingClass();
|
||||
if (aClass == null || !aClass.isRecord()) return null;
|
||||
if (aClass == null) return null;
|
||||
PsiIdentifier identifier = method.getNameIdentifier();
|
||||
if (identifier == null) return null;
|
||||
if (!aClass.isRecord()) {
|
||||
if (JavaPsiRecordUtil.isCompactConstructor(method)) {
|
||||
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(
|
||||
identifier).descriptionAndTooltip(JavaErrorMessages.message("compact.constructor.in.regular.class")).create();
|
||||
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddParameterListFix(method));
|
||||
return info;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (JavaPsiRecordUtil.isCanonicalConstructor(method)) {
|
||||
PsiParameter[] parameters = method.getParameterList().getParameters();
|
||||
PsiRecordComponent[] components = aClass.getRecordComponents();
|
||||
@@ -1961,12 +1972,9 @@ public class HighlightMethodUtil {
|
||||
// Non-canonical constructor
|
||||
PsiMethodCallExpression call = JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(method);
|
||||
if (call == null || JavaPsiConstructorUtil.isSuperConstructorCall(call)) {
|
||||
PsiIdentifier identifier = method.getNameIdentifier();
|
||||
if (identifier != null) {
|
||||
String message = JavaErrorMessages.message("record.no.constructor.call.in.non.canonical");
|
||||
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier)
|
||||
.descriptionAndTooltip(message).create();
|
||||
}
|
||||
String message = JavaErrorMessages.message("record.no.constructor.call.in.non.canonical");
|
||||
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier)
|
||||
.descriptionAndTooltip(message).create();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
|
||||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.JavaPsiRecordUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AddParameterListFix extends LocalQuickFixAndIntentionActionOnPsiElement {
|
||||
public AddParameterListFix(PsiMethod method) {
|
||||
super(method);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
if (!(startElement instanceof PsiMethod)) return;
|
||||
PsiMethod method = (PsiMethod)startElement;
|
||||
method.getParameterList()
|
||||
.replace(JavaPsiFacade.getElementFactory(project).createParameterList(ArrayUtil.EMPTY_STRING_ARRAY, PsiType.EMPTY_ARRAY));
|
||||
}
|
||||
|
||||
@Nls(capitalization = Nls.Capitalization.Sentence)
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return getFamilyName();
|
||||
}
|
||||
|
||||
@Nls(capitalization = Nls.Capitalization.Sentence)
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Insert '()'";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project,
|
||||
@NotNull PsiFile file,
|
||||
@Nullable Editor editor,
|
||||
@NotNull PsiElement startElement,
|
||||
@NotNull PsiElement endElement) {
|
||||
return super.isAvailable(project, file, editor, startElement, endElement) &&
|
||||
startElement instanceof PsiMethod && JavaPsiRecordUtil.isCompactConstructor((PsiMethod)startElement);
|
||||
}
|
||||
}
|
||||
+6
@@ -907,4 +907,10 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
public IntentionAction createWrapSwitchRuleStatementsIntoBlockFix(PsiSwitchLabeledRuleStatement rule) {
|
||||
return new WrapSwitchRuleStatementsIntoBlockFix(rule);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IntentionAction createAddParameterListFix(PsiMethod method) {
|
||||
return new AddParameterListFix(method);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,16 @@ public class JavaPsiRecordUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param method to check
|
||||
* @param method method to check
|
||||
* @return true if given method is a compact constructor (has no parameter list),
|
||||
* regardless whether it's declared in the record or not
|
||||
*/
|
||||
public static boolean isCompactConstructor(@NotNull PsiMethod method) {
|
||||
return method.isConstructor() && method.getParameterList().textMatches("");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param method method to check
|
||||
* @return true if given method is a canonical constructor for a record class
|
||||
*/
|
||||
public static boolean isCanonicalConstructor(@NotNull PsiMethod method) {
|
||||
|
||||
@@ -484,6 +484,7 @@ record.special.method.throws={0} cannot declare thrown exceptions
|
||||
record.canonical.constructor=Canonical constructor
|
||||
record.accessor=Record component accessor
|
||||
record.component.not.initialized=Record component ''{0}'' might not be initialized in canonical constructor
|
||||
compact.constructor.in.regular.class=Parameter list expected
|
||||
|
||||
feature.generics=Generics
|
||||
feature.annotations=Annotations
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class NotRecord {
|
||||
public <error descr="Parameter list expected">NotRecord</error> {
|
||||
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Insert '()'" "true"
|
||||
class NotRecord {
|
||||
public NotRecord() {
|
||||
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Insert '()'" "true"
|
||||
class NotRecord {
|
||||
public <caret>NotRecord {
|
||||
|
||||
}
|
||||
}
|
||||
+3
@@ -27,6 +27,9 @@ public class LightRecordsHighlightingTest extends LightJavaCodeInsightFixtureTes
|
||||
public void testRecordConstructors() {
|
||||
doTest();
|
||||
}
|
||||
public void testRecordCompactConstructors() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
|
||||
public class AddParameterListTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/addParameterList";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user