From 02b517002daf99d3aee8e2489f8ccd1e144da884 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 18 Apr 2023 15:01:38 +0200 Subject: [PATCH] Make custom RegExp inspection meta data dialog more generic to make it reusable GitOrigin-RevId: 174520233d03405c5693ba3e1017e027cffa3cbe --- .../messages/RegExpBundle.properties | 6 +- .../custom/CustomRegExpFakeInspection.java | 7 +- .../custom/CustomRegExpInspection.java | 20 +++++ ...log.java => InspectionMetaDataDialog.java} | 79 ++++++------------- ...StructuralSearchProfileActionProvider.java | 10 ++- 5 files changed, 61 insertions(+), 61 deletions(-) rename RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/{MetaDataDialog.java => InspectionMetaDataDialog.java} (56%) diff --git a/RegExpSupport/resources/messages/RegExpBundle.properties b/RegExpSupport/resources/messages/RegExpBundle.properties index 47831a7e9fd6..f710adc82b28 100644 --- a/RegExpSupport/resources/messages/RegExpBundle.properties +++ b/RegExpSupport/resources/messages/RegExpBundle.properties @@ -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 \ No newline at end of file diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpFakeInspection.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpFakeInspection.java index deed1cb35983..f5698c8ae39f 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpFakeInspection.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpFakeInspection.java @@ -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); diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpInspection.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpInspection.java index 260a2390a193..27d0f234ed92 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpInspection.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/CustomRegExpInspection.java @@ -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 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; diff --git a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/MetaDataDialog.java b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/InspectionMetaDataDialog.java similarity index 56% rename from RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/MetaDataDialog.java rename to RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/InspectionMetaDataDialog.java index 5d7310df5683..c262b6ee243d 100644 --- a/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/MetaDataDialog.java +++ b/RegExpSupport/src/org/intellij/lang/regexp/inspection/custom/InspectionMetaDataDialog.java @@ -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 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 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 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 doValidateAll() { + protected @NotNull List doValidateAll() { final List warnings = new SmartList<>(); - final List 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(); } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java index b71f760a5dbb..6f43d2ed32bb 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/inspection/StructuralSearchProfileActionProvider.java @@ -141,13 +141,17 @@ public class StructuralSearchProfileActionProvider extends InspectionProfileActi final RegExpInspectionConfiguration.InspectionPattern pattern = dialog.getPattern(); final InspectionProfileModifiableModel profile = myPanel.getProfile(); final CustomRegExpInspection inspection = InspectionProfileUtil.getCustomRegExpInspection(profile); - final RegExpInspectionConfiguration configuration = new RegExpInspectionConfiguration("new inspection"); - configuration.addPattern(pattern); final Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) return; - final MetaDataDialog metaDataDialog = new MetaDataDialog(project, inspection, configuration, true); + final InspectionMetaDataDialog metaDataDialog = inspection.createMetaDataDialog(project, null); if (!metaDataDialog.showAndGet()) return; + final RegExpInspectionConfiguration configuration = new RegExpInspectionConfiguration(metaDataDialog.getName()); + configuration.addPattern(pattern); + configuration.setDescription(metaDataDialog.getDescription()); + configuration.setSuppressId(metaDataDialog.getSuppressId()); + configuration.setProblemDescriptor(metaDataDialog.getProblemDescriptor()); + configuration.setUuid(null); inspection.addConfiguration(configuration); CustomRegExpInspection.addInspectionToProfile(project, profile, configuration);