Skip to content

Commit

Permalink
refact: remove usage of deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen committed Mar 10, 2024
1 parent 9ef6a1d commit dd956f9
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<PsiFile, List<GrammarIssue>> {

Expand Down Expand Up @@ -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) {
Expand All @@ -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> annotation = annotate(holder, issue, range, severity);
annotation.ifPresent(a -> registerFixForAnnotation(a, issue, file));
annotate(holder, issue, range, severity, file);
}
}
}
Expand Down Expand Up @@ -114,33 +115,31 @@ private boolean tokenBelongsToFile(Token t, @NotNull PsiFile file) {
return true;
}

private Optional<Annotation> 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> 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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -198,10 +194,6 @@ public void uninstallListeners() {
}
}

@Override
public void disposeComponent() {
}

@NotNull
@Override
public String getComponentName() {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/antlr/intellij/plugin/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<HighlightInfo> 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<HighlightInfo> 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);
}
}

0 comments on commit dd956f9

Please sign in to comment.