Skip to content

Commit

Permalink
refact: replace usages of deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bjansen committed Nov 6, 2024
1 parent 61a557f commit 199144e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.lang.annotation.Annotation;
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;
Expand Down Expand Up @@ -62,8 +63,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())
.range(file)
.fileLevel()
.create();
}

private void annotateIssue(@NotNull PsiFile file, @NotNull AnnotationHolder holder, GrammarIssue issue) {
Expand All @@ -72,8 +75,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,27 +116,22 @@ private boolean tokenBelongsToFile(Token t, @NotNull PsiFile file) {
return true;
}

private Optional<Annotation> annotate(@NotNull AnnotationHolder holder, GrammarIssue issue, TextRange range, ErrorSeverity severity) {
switch ( severity ) {
case ERROR:
case ERROR_ONE_OFF:
case FATAL:
return Optional.of(holder.createErrorAnnotation(range, issue.getAnnotation()));

case WARNING:
return Optional.of(holder.createWarningAnnotation(range, issue.getAnnotation()));

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()));

default:
break;
private void annotate(@NotNull AnnotationHolder holder, GrammarIssue issue, TextRange range, ErrorSeverity severity, @NotNull PsiFile file) {
var annotation = switch (severity) {
case ERROR, ERROR_ONE_OFF, FATAL -> holder.newAnnotation(HighlightSeverity.ERROR, issue.getAnnotation());
case WARNING -> holder.newAnnotation(HighlightSeverity.WARNING, issue.getAnnotation());
case WARNING_ONE_OFF, INFO -> holder.newAnnotation(HighlightSeverity.WEAK_WARNING, issue.getAnnotation());
};

annotation = annotation.range(range);

var fix = fixForAnnotation(range, issue, file);

if ( fix!=null ) {
annotation = annotation.withFix(fix);
}
return Optional.empty();

annotation.create();
}

static void registerFixForAnnotation(Annotation annotation, GrammarIssue issue, PsiFile file) {
Expand All @@ -143,4 +140,10 @@ static void registerFixForAnnotation(Annotation annotation, GrammarIssue issue,
intentionAction.ifPresent(annotation::registerFix);
}

static IntentionAction fixForAnnotation(TextRange range, GrammarIssue issue, PsiFile file) {
TextRange textRange = new TextRange(range.getStartOffset(), range.getEndOffset());
return AnnotationIntentActionsFactory.getFix(textRange, issue.getMsg().getErrorType(), file)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public PsiFile createFile(FileViewProvider viewProvider) {
return new ANTLRv4FileRoot(viewProvider);
}

public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
@Override
public SpaceRequirements spaceExistenceTypeBetweenTokens(ASTNode left, ASTNode right) {
return SpaceRequirements.MAY;
}

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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.antlr.intellij.plugin.configdialogs;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
Expand All @@ -15,7 +14,7 @@ public class ANTLRv4GrammarPropertiesComponent implements PersistentStateCompone
private ANTLRv4GrammarPropertiesStore mySettings = new ANTLRv4GrammarPropertiesStore();

public static ANTLRv4GrammarPropertiesComponent getInstance(Project project) {
return ServiceManager.getService(project, ANTLRv4GrammarPropertiesComponent.class);
return project.getService(ANTLRv4GrammarPropertiesComponent.class);
}

@NotNull
Expand Down
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 @@ -2,18 +2,21 @@

import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.ui.Gray;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBList;
import com.intellij.ui.components.JBPanel;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import org.antlr.intellij.plugin.parsing.ParsingUtils;
import org.antlr.intellij.plugin.parsing.PreviewInterpreterRuleContext;
import org.antlr.v4.gui.TreeViewer;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserInterpreter;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.AmbiguityInfo;
import org.antlr.v4.runtime.atn.LookaheadEventInfo;
import org.antlr.v4.runtime.misc.Interval;
Expand Down Expand Up @@ -53,12 +56,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 Expand Up @@ -104,13 +106,11 @@ public static void popupAmbigTreesDialog(PreviewState previewState, AmbiguityInf

public static JBPopup createLookaheadTreesPopup(final PreviewState previewState,
final LookaheadEventInfo lookaheadInfo) {
final JBList list = new JBList("Show all lookahead interpretations");
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JBPopupFactory factory = JBPopupFactory.getInstance();
PopupChooserBuilder builder = factory.createListPopupBuilder(list);
builder.setItemChoosenCallback(() -> popupLookaheadTreesDialog(previewState, lookaheadInfo));

return builder.createPopup();
return JBPopupFactory.getInstance()
.createPopupChooserBuilder(List.of("Show all lookahead interpretations"))
.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
.setItemChosenCallback(str -> popupLookaheadTreesDialog(previewState, lookaheadInfo))
.createPopup();
}

public static void popupLookaheadTreesDialog(PreviewState previewState, LookaheadEventInfo lookaheadInfo) {
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 @@ -16,7 +17,10 @@ public ANTLRLiveTemplateContext(@NotNull String presentableName)
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) {
var file = context.getFile();
var 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

0 comments on commit 199144e

Please sign in to comment.