Make custom RegExp inspection meta data dialog more generic to make it reusable

GitOrigin-RevId: 174520233d03405c5693ba3e1017e027cffa3cbe
This commit is contained in:
Bas Leijdekkers
2023-04-18 15:01:38 +02:00
committed by intellij-monorepo-bot
parent 73412b84ed
commit 02b517002d
5 changed files with 61 additions and 61 deletions

View File

@@ -1,4 +1,4 @@
action.add.pattern.text=Add pattern\u2026
action.add.pattern.text=Add Pattern\u2026
action.add.regexp.replace.template.text=Add RegExp Replace Template\u2026
action.add.regexp.search.template.text=Add RegExp Search Template\u2026
button.enable.replace=Enable Replace
@@ -25,12 +25,11 @@ color.settings.redundant.escape.sequence=Redundant escape sequence
color.settings.title.regexp=RegExp
dialog.message.inspection.with.name.exists.warning=Inspection with name ''{0}'' already exists
dialog.message.name.must.not.be.empty=Name must not be empty
dialog.message.suppress.id.already.in.use.by.another.inspection=Suppress ID ''{0}'' is already in use by another inspection
dialog.message.suppress.id.must.match.regex.za.z=Suppress ID must match regex [a-zA-Z_0-9.-]+
dialog.title.custom.regexp.inspection=Custom RegExp Inspection
doc.property.block.stands.for.0=Property block stands for {0}
doc.property.block.stands.for.characters.not.matching.0=Property block stands for characters not matching {0}
edit.metadata.button=Edit Metadata...
edit.metadata.button=Edit Metadata\u2026
error.0.repetition.not.allowed.inside.lookbehind={0} repetition not allowed inside lookbehind
error.alternation.alternatives.needs.to.have.the.same.length.inside.lookbehind=Alternation alternatives need to have the same length inside lookbehind
error.atomic.groups.are.not.supported.in.this.regex.dialect=Atomic groups are not supported in this regex dialect
@@ -186,5 +185,6 @@ tooltip.matches=Expression and example match!
tooltip.more.input.expected=More example input expected
tooltip.no.match=Expression and example do not match
tooltip.pattern.is.too.complex=Regular expression pattern is too complex
unnamed.inspection=Unnamed inspection
warning.duplicate.character.0.inside.character.class=Duplicate character ''{0}'' inside character class
warning.duplicate.predefined.character.class.0.inside.character.class=Duplicate predefined character class ''{0}'' inside character class

View File

@@ -164,10 +164,15 @@ public class CustomRegExpFakeInspection extends LocalInspectionTool {
return;
}
final CustomRegExpInspection inspection = getRegExpInspection(profile);
final MetaDataDialog dialog = new MetaDataDialog(project, inspection, myConfiguration, false);
final InspectionMetaDataDialog dialog = inspection.createMetaDataDialog(project, myConfiguration);
if (!dialog.showAndGet()) {
return;
}
myConfiguration.setName(dialog.getName());
myConfiguration.setDescription(dialog.getDescription());
myConfiguration.setSuppressId(dialog.getSuppressId());
myConfiguration.setProblemDescriptor(dialog.getProblemDescriptor());
inspection.updateConfiguration(myConfiguration);
profile.setModified(true);
profile.getProfileManager().fireProfileChanged(profile);

View File

@@ -14,6 +14,7 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.UnknownFileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
@@ -27,6 +28,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Function;
import static com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
@@ -205,6 +207,24 @@ public class CustomRegExpInspection extends LocalInspectionTool implements Dynam
return myConfigurations;
}
@NotNull
public InspectionMetaDataDialog createMetaDataDialog(Project project, @Nullable RegExpInspectionConfiguration configuration) {
Function<String, @Nullable @NlsContexts.DialogMessage String> nameValidator = name -> {
for (RegExpInspectionConfiguration current : myConfigurations) {
if ((configuration == null || !configuration.getUuid().equals(current.getUuid())) &&
current.getName().equals(name)) {
return RegExpBundle.message("dialog.message.inspection.with.name.exists.warning", name);
}
}
return null;
};
if (configuration == null) {
return new InspectionMetaDataDialog(project, nameValidator);
}
return new InspectionMetaDataDialog(project, nameValidator, configuration.getName(), configuration.getDescription(),
configuration.getProblemDescriptor(), configuration.getSuppressId());
}
private static class CustomRegExpQuickFix implements LocalQuickFix {
private final int myStartOffset;
private final int myEndOffset;

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.intellij.lang.regexp.inspection.custom;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.fileTypes.FileType;
@@ -9,6 +8,7 @@ import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.EditorTextField;
@@ -22,34 +22,40 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Pattern;
public class MetaDataDialog extends DialogWrapper {
public class InspectionMetaDataDialog extends DialogWrapper {
private final Pattern mySuppressIdPattern = Pattern.compile(LocalInspectionTool.VALID_ID_PATTERN);
private final CustomRegExpInspection myInspection;
@NotNull private final RegExpInspectionConfiguration myConfiguration;
private final boolean myNewInspection;
private final Function<String, @Nullable @NlsContexts.DialogMessage String> myNameValidator;
private final JTextField myNameTextField;
private final JTextField myProblemDescriptorTextField;
private final EditorTextField myDescriptionTextArea;
private final JTextField mySuppressIdTextField;
public MetaDataDialog(Project project, @NotNull CustomRegExpInspection inspection, @NotNull RegExpInspectionConfiguration configuration, boolean newInspection) {
super((Project)null);
myInspection = inspection;
public InspectionMetaDataDialog(@NotNull Project project,
@NotNull Function<String, @Nullable @NlsContexts.DialogMessage String> nameValidator) {
this(project, nameValidator, null, null, null, null);
}
myConfiguration = configuration;
myNewInspection = newInspection;
myNameTextField = new JTextField(configuration.getName());
myProblemDescriptorTextField = new JTextField(configuration.getProblemDescriptor());
public InspectionMetaDataDialog(@NotNull Project project,
@NotNull Function<String, @Nullable @NlsContexts.DialogMessage String> nameValidator,
@NlsSafe String name,
@NlsSafe String description,
@NlsSafe String problemDescriptor,
@NlsSafe String suppressId) {
super(project);
myNameTextField = new JTextField(name == null ? RegExpBundle.message("unnamed.inspection") : name);
myProblemDescriptorTextField = new JTextField(problemDescriptor);
final FileType htmlFileType = FileTypeManager.getInstance().getStdFileType("HTML");
myDescriptionTextArea = new EditorTextField(ObjectUtils.notNull(configuration.getDescription(), ""), project, htmlFileType);
myDescriptionTextArea = new EditorTextField(ObjectUtils.notNull(description, ""), project, htmlFileType);
myDescriptionTextArea.setOneLineMode(false);
myDescriptionTextArea.setFont(EditorFontType.getGlobalPlainFont());
myDescriptionTextArea.setPreferredSize(new Dimension(375, 125));
myDescriptionTextArea.setMinimumSize(new Dimension(200, 50));
mySuppressIdTextField = new JTextField(configuration.getSuppressId());
mySuppressIdTextField = new JTextField(suppressId);
myNameValidator = nameValidator;
setTitle(RegExpBundle.message("dialog.title.custom.regexp.inspection"));
init();
}
@@ -60,24 +66,16 @@ public class MetaDataDialog extends DialogWrapper {
}
@Override
protected @NotNull java.util.List<ValidationInfo> doValidateAll() {
protected @NotNull List<ValidationInfo> doValidateAll() {
final List<ValidationInfo> warnings = new SmartList<>();
final List<RegExpInspectionConfiguration> configurations = myInspection.getConfigurations();
final String name = getName();
if (StringUtil.isEmpty(name)) {
warnings.add(new ValidationInfo(RegExpBundle.message("dialog.message.name.must.not.be.empty"), myNameTextField));
}
else {
for (RegExpInspectionConfiguration configuration : configurations) {
if (myNewInspection) {
if (configuration.getName().equals(name)) {
warnings.add(new ValidationInfo(RegExpBundle.message("dialog.message.inspection.with.name.exists.warning", name), myNameTextField));
break;
}
} else if (!configuration.getUuid().equals(myConfiguration.getUuid()) && configuration.getName().equals(name)) {
warnings.add(new ValidationInfo(RegExpBundle.message("dialog.message.inspection.with.name.exists.warning", name), myNameTextField));
break;
}
String errorMessage = myNameValidator.apply(name);
if (errorMessage != null) {
warnings.add(new ValidationInfo(errorMessage, myNameTextField));
}
}
final String suppressId = getSuppressId();
@@ -85,44 +83,17 @@ public class MetaDataDialog extends DialogWrapper {
if (!mySuppressIdPattern.matcher(suppressId).matches()) {
warnings.add(new ValidationInfo(RegExpBundle.message("dialog.message.suppress.id.must.match.regex.za.z"), mySuppressIdTextField));
}
else {
final HighlightDisplayKey key = HighlightDisplayKey.findById(suppressId);
if (key != null && key != HighlightDisplayKey.find(myConfiguration.getUuid())) {
warnings.add(new ValidationInfo(
RegExpBundle.message("dialog.message.suppress.id.already.in.use.by.another.inspection", suppressId), mySuppressIdTextField));
}
else {
for (RegExpInspectionConfiguration configuration : configurations) {
if (suppressId.equals(configuration.getSuppressId()) && !myConfiguration.getUuid().equals(configuration.getUuid())) {
warnings.add(new ValidationInfo(
RegExpBundle.message("dialog.message.suppress.id.already.in.use.by.another.inspection", suppressId), mySuppressIdTextField));
break;
}
}
}
}
}
return warnings;
}
@Override
protected void doOKAction() {
super.doOKAction();
if (getOKAction().isEnabled()) {
myConfiguration.setName(getName());
myConfiguration.setDescription(getDescription());
myConfiguration.setSuppressId(getSuppressId());
myConfiguration.setProblemDescriptor(getProblemDescriptor());
}
}
@Nullable
@Override
protected JComponent createCenterPanel() {
return new FormBuilder()
.addLabeledComponent(RegExpBundle.message("label.inspection.name"), myNameTextField, true)
.addLabeledComponent(RegExpBundle.message("label.problem.tool.tip"), myProblemDescriptorTextField, true)
.addLabeledComponentFillVertically(RegExpBundle.message("label.description"), myDescriptionTextArea)
.addLabeledComponent(RegExpBundle.message("label.problem.tool.tip"), myProblemDescriptorTextField, true)
.addLabeledComponent(RegExpBundle.message("label.suppress.id"), mySuppressIdTextField)
.getPanel();
}