From dd956f9151b67f6a332715b6bac2782604955260 Mon Sep 17 00:00:00 2001 From: Bastien Jansen Date: Sun, 10 Mar 2024 17:14:42 +0100 Subject: [PATCH] refact: remove usage of deprecated methods --- .../plugin/ANTLRv4ExternalAnnotator.java | 43 +++++++-------- .../plugin/ANTLRv4ParserDefinition.java | 4 -- .../plugin/ANTLRv4PluginController.java | 12 +--- .../java/org/antlr/intellij/plugin/Icons.java | 12 ++-- .../preview/ParseTreeContextualMenu.java | 4 +- .../plugin/preview/ShowAmbigTreesDialog.java | 11 ++-- .../templates/ANTLRLiveTemplateContext.java | 6 +- .../plugin/ANTLRv4ExternalAnnotatorTest.java | 55 ++++++++++--------- 8 files changed, 71 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotator.java b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotator.java index c3a08e92..7ef7e151 100644 --- a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotator.java +++ b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotator.java @@ -1,14 +1,13 @@ package org.antlr.intellij.plugin; -import com.intellij.codeInsight.intention.IntentionAction; -import com.intellij.lang.annotation.Annotation; +import com.intellij.lang.annotation.AnnotationBuilder; import com.intellij.lang.annotation.AnnotationHolder; import com.intellij.lang.annotation.ExternalAnnotator; +import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiFile; -import org.antlr.intellij.plugin.actions.AnnotationIntentActionsFactory; import org.antlr.intellij.plugin.validation.GrammarIssue; import org.antlr.intellij.plugin.validation.GrammarIssuesCollector; import org.antlr.runtime.ANTLRFileStream; @@ -20,7 +19,8 @@ import org.jetbrains.annotations.Nullable; import java.util.List; -import java.util.Optional; + +import static org.antlr.intellij.plugin.actions.AnnotationIntentActionsFactory.getFix; public class ANTLRv4ExternalAnnotator extends ExternalAnnotator> { @@ -62,8 +62,10 @@ public void apply(@NotNull PsiFile file, } private void annotateFileIssue(@NotNull PsiFile file, @NotNull AnnotationHolder holder, GrammarIssue issue) { - Annotation annotation = holder.createWarningAnnotation(file, issue.getAnnotation()); - annotation.setFileLevelAnnotation(true); + holder.newAnnotation(HighlightSeverity.WARNING, issue.getAnnotation()) + .fileLevel() + .range(file) + .create(); } private void annotateIssue(@NotNull PsiFile file, @NotNull AnnotationHolder holder, GrammarIssue issue) { @@ -72,8 +74,7 @@ private void annotateIssue(@NotNull PsiFile file, @NotNull AnnotationHolder hold TextRange range = getTokenRange((CommonToken) t, file); ErrorSeverity severity = getIssueSeverity(issue); - Optional annotation = annotate(holder, issue, range, severity); - annotation.ifPresent(a -> registerFixForAnnotation(a, issue, file)); + annotate(holder, issue, range, severity, file); } } } @@ -114,33 +115,31 @@ private boolean tokenBelongsToFile(Token t, @NotNull PsiFile file) { return true; } - private Optional annotate(@NotNull AnnotationHolder holder, GrammarIssue issue, TextRange range, ErrorSeverity severity) { + private void annotate(@NotNull AnnotationHolder holder, GrammarIssue issue, TextRange range, ErrorSeverity severity, @NotNull PsiFile file) { + AnnotationBuilder annotationBuilder = null; switch ( severity ) { case ERROR: case ERROR_ONE_OFF: case FATAL: - return Optional.of(holder.createErrorAnnotation(range, issue.getAnnotation())); - + annotationBuilder = holder.newAnnotation(HighlightSeverity.ERROR, issue.getAnnotation()).range(range); + break; case WARNING: - return Optional.of(holder.createWarningAnnotation(range, issue.getAnnotation())); - + annotationBuilder = holder.newAnnotation(HighlightSeverity.WARNING, issue.getAnnotation()).range(range); + break; case WARNING_ONE_OFF: case INFO: /* When trying to remove the deprecation warning, you will need something like this: AnnotationBuilder builder = holder.newAnnotation(HighlightSeverity.WEAK_WARNING, issue.getAnnotation()).range(range); */ - return Optional.of(holder.createWeakWarningAnnotation(range, issue.getAnnotation())); - + annotationBuilder = holder.newAnnotation(HighlightSeverity.WEAK_WARNING, issue.getAnnotation()).range(range); default: break; } - return Optional.empty(); - } - static void registerFixForAnnotation(Annotation annotation, GrammarIssue issue, PsiFile file) { - TextRange textRange = new TextRange(annotation.getStartOffset(), annotation.getEndOffset()); - Optional intentionAction = AnnotationIntentActionsFactory.getFix(textRange, issue.getMsg().getErrorType(), file); - intentionAction.ifPresent(annotation::registerFix); + if (annotationBuilder != null) { + getFix(range, issue.getMsg().getErrorType(), file) + .ifPresent(annotationBuilder::withFix); + annotationBuilder.create(); + } } - } diff --git a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java index b1b74bff..856b62be 100644 --- a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java +++ b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java @@ -57,10 +57,6 @@ public PsiFile createFile(FileViewProvider viewProvider) { return new ANTLRv4FileRoot(viewProvider); } - public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { - return SpaceRequirements.MAY; - } - /** Convert from internal parse node (AST they call it) to final PSI node. This * converts only internal rule nodes apparently, not leaf nodes. Leaves * are just tokens I guess. diff --git a/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java b/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java index f19b58e9..65d1e924 100644 --- a/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java +++ b/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java @@ -4,7 +4,7 @@ import com.intellij.execution.filters.TextConsoleBuilderFactory; import com.intellij.execution.ui.ConsoleView; import com.intellij.ide.plugins.IdeaPluginDescriptor; -import com.intellij.ide.plugins.PluginManager; +import com.intellij.ide.plugins.PluginManagerCore; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.ProjectComponent; import com.intellij.openapi.diagnostic.Logger; @@ -114,13 +114,9 @@ public static ANTLRv4PluginController getInstance(Project project) { return pc; } - @Override - public void initComponent() { - } - @Override public void projectOpened() { - IdeaPluginDescriptor plugin = PluginManager.getPlugin(PluginId.getId(PLUGIN_ID)); + IdeaPluginDescriptor plugin = PluginManagerCore.getPlugin(PluginId.getId(PLUGIN_ID)); String version = "unknown"; if ( plugin!=null ) { version = plugin.getVersion(); @@ -198,10 +194,6 @@ public void uninstallListeners() { } } - @Override - public void disposeComponent() { - } - @NotNull @Override public String getComponentName() { diff --git a/src/main/java/org/antlr/intellij/plugin/Icons.java b/src/main/java/org/antlr/intellij/plugin/Icons.java index 464ef009..52da7c2b 100644 --- a/src/main/java/org/antlr/intellij/plugin/Icons.java +++ b/src/main/java/org/antlr/intellij/plugin/Icons.java @@ -6,17 +6,17 @@ import javax.swing.*; public class Icons { - public static final Icon FILE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/antlr.png"); - public static final Icon LEXER_RULE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/lexer-rule.png"); - public static final Icon PARSER_RULE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/parser-rule.png"); - public static final Icon MODE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/mode.png"); + public static final Icon FILE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/antlr.png", Icons.class); + public static final Icon LEXER_RULE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/lexer-rule.png", Icons.class); + public static final Icon PARSER_RULE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/parser-rule.png", Icons.class); + public static final Icon MODE = IconLoader.getIcon("/icons/org/antlr/intellij/plugin/mode.png", Icons.class); public static Icon getToolWindow() { // IntelliJ 2018.2+ has monochrome icons for tool windows so let's use one too if (ApplicationInfo.getInstance().getBuild().getBaselineVersion() >= 182) { - return IconLoader.getIcon("/icons/org/antlr/intellij/plugin/toolWindowAntlr.svg"); + return IconLoader.getIcon("/icons/org/antlr/intellij/plugin/toolWindowAntlr.svg", Icons.class); } - return IconLoader.getIcon("/icons/org/antlr/intellij/plugin/antlr.png"); + return IconLoader.getIcon("/icons/org/antlr/intellij/plugin/antlr.png", Icons.class); } } diff --git a/src/main/java/org/antlr/intellij/plugin/preview/ParseTreeContextualMenu.java b/src/main/java/org/antlr/intellij/plugin/preview/ParseTreeContextualMenu.java index cc8060e1..1e90194e 100644 --- a/src/main/java/org/antlr/intellij/plugin/preview/ParseTreeContextualMenu.java +++ b/src/main/java/org/antlr/intellij/plugin/preview/ParseTreeContextualMenu.java @@ -14,7 +14,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileWrapper; import com.intellij.ui.JBColor; -import com.intellij.util.ui.UIUtil; +import com.intellij.util.ui.ImageUtil; import org.apache.commons.lang3.StringUtils; import org.jfree.svg.SVGGraphics2D; @@ -78,7 +78,7 @@ private static JMenuItem createExportMenuItem(UberTreeViewer parseTreeViewer, St private static void exportToImage(UberTreeViewer parseTreeViewer, File file, boolean useTransparentBackground, String imageFormat) { int imageType = useTransparentBackground ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB; - BufferedImage bi = UIUtil.createImage(parseTreeViewer.getWidth(), parseTreeViewer.getHeight(), imageType); + BufferedImage bi = ImageUtil.createImage(parseTreeViewer.getWidth(), parseTreeViewer.getHeight(), imageType); Graphics graphics = bi.getGraphics(); if (!useTransparentBackground) { diff --git a/src/main/java/org/antlr/intellij/plugin/preview/ShowAmbigTreesDialog.java b/src/main/java/org/antlr/intellij/plugin/preview/ShowAmbigTreesDialog.java index 79583e78..2f5cb0ec 100644 --- a/src/main/java/org/antlr/intellij/plugin/preview/ShowAmbigTreesDialog.java +++ b/src/main/java/org/antlr/intellij/plugin/preview/ShowAmbigTreesDialog.java @@ -53,12 +53,11 @@ public ShowAmbigTreesDialog() { public static JBPopup createAmbigTreesPopup(final PreviewState previewState, final AmbiguityInfo ambigInfo) { - final JBList list = new JBList("Show all phrase interpretations"); - list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); - JBPopupFactory factory = JBPopupFactory.getInstance(); - PopupChooserBuilder builder = factory.createListPopupBuilder(list); - builder.setItemChoosenCallback(() -> popupAmbigTreesDialog(previewState, ambigInfo)); - return builder.createPopup(); + return JBPopupFactory.getInstance() + .createPopupChooserBuilder(List.of("Show all phrase interpretations")) + .setSelectionMode(ListSelectionModel.SINGLE_SELECTION) + .setItemChosenCallback((str) -> popupAmbigTreesDialog(previewState, ambigInfo)) + .createPopup(); } public static void popupAmbigTreesDialog(PreviewState previewState, AmbiguityInfo ambigInfo) { diff --git a/src/main/java/org/antlr/intellij/plugin/templates/ANTLRLiveTemplateContext.java b/src/main/java/org/antlr/intellij/plugin/templates/ANTLRLiveTemplateContext.java index 84de3a8e..65c1453e 100644 --- a/src/main/java/org/antlr/intellij/plugin/templates/ANTLRLiveTemplateContext.java +++ b/src/main/java/org/antlr/intellij/plugin/templates/ANTLRLiveTemplateContext.java @@ -1,5 +1,6 @@ package org.antlr.intellij.plugin.templates; +import com.intellij.codeInsight.template.TemplateActionContext; import com.intellij.codeInsight.template.TemplateContextType; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; @@ -20,7 +21,10 @@ public ANTLRLiveTemplateContext(@NotNull @NonNls String id, protected abstract boolean isInContext(@NotNull PsiFile file, @NotNull PsiElement element, int offset); @Override - public boolean isInContext(@NotNull PsiFile file, int offset) { + public boolean isInContext(TemplateActionContext context) { + PsiFile file = context.getFile(); + int offset = context.getStartOffset(); + // offset is where cursor or insertion point is I guess if ( !PsiUtilBase.getLanguageAtOffset(file, offset).isKindOf(ANTLRv4Language.INSTANCE) ) { return false; diff --git a/src/test/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotatorTest.java b/src/test/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotatorTest.java index 20719749..52e0064d 100644 --- a/src/test/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotatorTest.java +++ b/src/test/java/org/antlr/intellij/plugin/ANTLRv4ExternalAnnotatorTest.java @@ -1,45 +1,50 @@ package org.antlr.intellij.plugin; -import com.google.common.collect.Iterables; -import com.intellij.lang.annotation.Annotation; -import com.intellij.lang.annotation.HighlightSeverity; -import com.intellij.psi.PsiFile; -import org.antlr.intellij.plugin.validation.CreateRuleFix; +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.testFramework.fixtures.BasePlatformTestCase; import org.antlr.intellij.plugin.validation.AddTokenDefinitionFix; -import org.antlr.intellij.plugin.validation.GrammarIssue; -import org.antlr.v4.tool.ANTLRMessage; -import org.antlr.v4.tool.ErrorType; +import org.antlr.intellij.plugin.validation.CreateRuleFix; import org.junit.Assert; -import org.junit.Test; -import org.mockito.Mockito; -public class ANTLRv4ExternalAnnotatorTest { +import java.util.List; +import java.util.Objects; - @Test - public void shouldRegisterTokenDefinitionQuickFix() { +public class ANTLRv4ExternalAnnotatorTest extends BasePlatformTestCase { + + @Override + protected void tearDown() throws Exception { + TestUtils.tearDownIgnoringObjectNotDisposedException(super::tearDown); + } + + public void testShouldRegisterTokenDefinitionQuickFix() { // given: - Annotation annotation = new Annotation(0,0, HighlightSeverity.WARNING, "msg", "tooltip"); + myFixture.configureByText("test.g4", "grammar test; rule: TOKEN;"); // when: - ANTLRv4ExternalAnnotator.registerFixForAnnotation(annotation, new GrammarIssue(new ANTLRMessage(ErrorType.IMPLICIT_TOKEN_DEFINITION)), null); + List result = myFixture.doHighlighting(); // then: - Annotation.QuickFixInfo quickFix = Iterables.getOnlyElement(annotation.getQuickFixes()); - Assert.assertTrue(quickFix.quickFix instanceof AddTokenDefinitionFix); + var quickFix = result.stream() + .map(el -> el.findRegisteredQuickFix((action, range) -> action)) + .filter(Objects::nonNull) + .findFirst().orElseThrow(); + + Assert.assertTrue(quickFix.getAction() instanceof AddTokenDefinitionFix); } - @Test - public void shouldRegisterCreateRuleQuickFix() { + public void testShouldRegisterCreateRuleQuickFix() { // given: - Annotation annotation = new Annotation(0,0, HighlightSeverity.WARNING, "msg", "tooltip"); - PsiFile file = Mockito.mock(PsiFile.class); - Mockito.when(file.getText()).thenReturn("sample text"); + myFixture.configureByText("test.g4", "grammar test; rule: undefined_rule;"); // when: - ANTLRv4ExternalAnnotator.registerFixForAnnotation(annotation, new GrammarIssue(new ANTLRMessage(ErrorType.UNDEFINED_RULE_REF)), file); + List result = myFixture.doHighlighting(); // then: - Annotation.QuickFixInfo quickFix = Iterables.getOnlyElement(annotation.getQuickFixes()); - Assert.assertTrue(quickFix.quickFix instanceof CreateRuleFix); + var quickFix = result.stream() + .map(el -> el.findRegisteredQuickFix((action, range) -> action)) + .filter(Objects::nonNull) + .findFirst().orElseThrow(); + + Assert.assertTrue(quickFix.getAction() instanceof CreateRuleFix); } } \ No newline at end of file