suggest AddEmptyRecordHeaderFix in case of no record header

GitOrigin-RevId: ac6a585e1ccdb091a02d49d2f8bf2e55bf2114cb
This commit is contained in:
Roman.Ivanov
2019-12-24 08:41:04 +00:00
committed by intellij-monorepo-bot
parent ca80fb7215
commit 2cc8b2c194
8 changed files with 94 additions and 2 deletions
@@ -483,4 +483,7 @@ public abstract class QuickFixFactory {
@NotNull
public abstract IntentionAction createAddParameterListFix(PsiMethod method);
@NotNull
public abstract IntentionAction createAddEmptyRecordHeaderFix(PsiClass record);
}
@@ -890,8 +890,10 @@ public class HighlightClassUtil {
PsiIdentifier identifier = psiClass.getNameIdentifier();
if (identifier == null) return null;
if (header == null) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier)
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier)
.descriptionAndTooltip(JavaErrorMessages.message("record.no.header")).create();
QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createAddEmptyRecordHeaderFix(psiClass));
return info;
}
return null;
}
@@ -361,4 +361,6 @@ convert.variable.to.field.in.anonymous.class.fix.name=Move ''{0}'' into anonymou
change.method.parameters.text=Change method parameters to ''{0}''
change.method.parameters.family=Change method parameters
add.default.branch.to.variable.initializing.switch.fix.name=Add ''default'' branch to the ''switch'' statement which initializes ''{0}''
add.default.branch.to.variable.initializing.switch.fix.name=Add ''default'' branch to the ''switch'' statement which initializes ''{0}''
add.empty.record.header=Insert '()'
@@ -0,0 +1,53 @@
// 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.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class AddEmptyRecordHeaderFix extends LocalQuickFixAndIntentionActionOnPsiElement {
private final SmartPsiElementPointer<PsiClass> myRecordPointer;
public AddEmptyRecordHeaderFix(@NotNull PsiClass record) {
super(record);
this.myRecordPointer = SmartPointerManager.createPointer(record);
}
@Override
public void invoke(@NotNull Project project,
@NotNull PsiFile file,
@Nullable Editor editor,
@NotNull PsiElement startElement,
@NotNull PsiElement endElement) {
PsiClass record = myRecordPointer.dereference();
if (record == null || !record.isRecord() || record.getRecordHeader() != null) return;
PsiTypeParameterList typeParameterList = record.getTypeParameterList();
if (typeParameterList == null) return;
record.addAfter(createEmptyRecordHeader(project), typeParameterList);
}
private static PsiRecordHeader createEmptyRecordHeader(Project project) {
PsiJavaFile file = (PsiJavaFile)PsiFileFactory.getInstance(project).createFileFromText(JavaLanguage.INSTANCE, "record __DUMMY(){}");
return file.getClasses()[0].getRecordHeader();
}
@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getText() {
return getFamilyName();
}
@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
@Override
public String getFamilyName() {
return QuickFixBundle.message("add.empty.record.header");
}
}
@@ -913,4 +913,10 @@ public class QuickFixFactoryImpl extends QuickFixFactory {
public IntentionAction createAddParameterListFix(PsiMethod method) {
return new AddParameterListFix(method);
}
@NotNull
@Override
public IntentionAction createAddEmptyRecordHeaderFix(PsiClass psiClass) {
return new AddEmptyRecordHeaderFix(psiClass);
}
}
@@ -0,0 +1,3 @@
// "Insert '()'" "true"
record A() {}
@@ -0,0 +1,3 @@
// "Insert '()'" "true"
record <caret>A {}
@@ -0,0 +1,20 @@
// Copyright 2000-2018 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;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
public class AddEmptyRecordHeaderTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return LightJavaCodeInsightFixtureTestCase.JAVA_14;
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/addEmptyRecordHeader";
}
}