From a67e6db0f5e14ffc4348960a73ca4bea623d9d7c Mon Sep 17 00:00:00 2001 From: Bastien Jansen Date: Tue, 9 Jan 2024 08:55:21 +0100 Subject: [PATCH] fix: handle cases when our lexer is instantiated before the ParserDefinition (fixes #681) --- .../intellij/plugin/ANTLRv4ParserDefinition.java | 10 ---------- .../plugin/adaptors/ANTLRv4LexerAdaptor.java | 12 ++++++++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java index 85250314..b1b74bff 100644 --- a/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java +++ b/src/main/java/org/antlr/intellij/plugin/ANTLRv4ParserDefinition.java @@ -10,11 +10,9 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.tree.IFileElementType; import com.intellij.psi.tree.TokenSet; -import org.antlr.intellij.adaptor.lexer.PSIElementTypeFactory; import org.antlr.intellij.plugin.adaptors.ANTLRv4GrammarParser; import org.antlr.intellij.plugin.adaptors.ANTLRv4LexerAdaptor; import org.antlr.intellij.plugin.parser.ANTLRv4Lexer; -import org.antlr.intellij.plugin.parser.ANTLRv4Parser; import org.jetbrains.annotations.NotNull; /** The general interface between IDEA and ANTLR. */ @@ -22,14 +20,6 @@ public class ANTLRv4ParserDefinition implements ParserDefinition { public static final IFileElementType FILE = new IFileElementType(ANTLRv4Language.INSTANCE); - public ANTLRv4ParserDefinition() { - PSIElementTypeFactory.defineLanguageIElementTypes( - ANTLRv4Language.INSTANCE, - ANTLRv4Lexer.tokenNames, - ANTLRv4Parser.ruleNames - ); - } - @NotNull @Override public Lexer createLexer(Project project) { diff --git a/src/main/java/org/antlr/intellij/plugin/adaptors/ANTLRv4LexerAdaptor.java b/src/main/java/org/antlr/intellij/plugin/adaptors/ANTLRv4LexerAdaptor.java index 11ad2286..915b439b 100644 --- a/src/main/java/org/antlr/intellij/plugin/adaptors/ANTLRv4LexerAdaptor.java +++ b/src/main/java/org/antlr/intellij/plugin/adaptors/ANTLRv4LexerAdaptor.java @@ -1,12 +1,24 @@ package org.antlr.intellij.plugin.adaptors; import org.antlr.intellij.adaptor.lexer.ANTLRLexerAdaptor; +import org.antlr.intellij.adaptor.lexer.PSIElementTypeFactory; import org.antlr.intellij.plugin.ANTLRv4Language; import org.antlr.intellij.plugin.parser.ANTLRv4Lexer; +import org.antlr.intellij.plugin.parser.ANTLRv4Parser; import org.antlr.v4.runtime.Lexer; /** Adapt ANTLR needs to intellij */ public class ANTLRv4LexerAdaptor extends ANTLRLexerAdaptor { + + // In case a lexer was created outside our ParserDefinition + static { + PSIElementTypeFactory.defineLanguageIElementTypes( + ANTLRv4Language.INSTANCE, + ANTLRv4Lexer.tokenNames, + ANTLRv4Parser.ruleNames + ); + } + private static final ANTLRv4LexerState INITIAL_STATE = new ANTLRv4LexerState(Lexer.DEFAULT_MODE, null, 0); public ANTLRv4LexerAdaptor(ANTLRv4Lexer lexer) {