Skip to content

Commit

Permalink
Don't include declarations in usages (fixes #629).
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Rodionov <[email protected]>
  • Loading branch information
ris58h committed Jul 7, 2023
1 parent cd9a2c8 commit 7227cbc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ANTLRv4ASTFactory extends ASTFactory {
ruleElementTypeToPsiFactory.put(ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_modeSpec), ModeSpecNode.Factory.INSTANCE);
ruleElementTypeToPsiFactory.put(ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_action), AtAction.Factory.INSTANCE);
ruleElementTypeToPsiFactory.put(ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_identifier), TokenSpecNode.Factory.INSTANCE);
ruleElementTypeToPsiFactory.put(ANTLRv4TokenTypes.RULE_ELEMENT_TYPES.get(ANTLRv4Parser.RULE_tokensSpec), TokensSpecNode.Factory.INSTANCE);
}

/** Create a FileElement for root or a parse tree CompositeElement (not
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.antlr.intellij.plugin.psi;

import com.intellij.psi.PsiReference;
import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.tree.IElementType;

Expand All @@ -11,6 +12,16 @@ public GrammarElementRefNode(IElementType type, CharSequence text) {
super(type, text);
}

protected abstract boolean isDeclaration();

@Override
public PsiReference getReference() {
if (isDeclaration()) {
return null;
}
return new GrammarElementRef(this, getText());
}

@Override
public String getName() {
return getText();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package org.antlr.intellij.plugin.psi;

import com.intellij.psi.PsiReference;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;

public class LexerRuleRefNode extends GrammarElementRefNode {
public LexerRuleRefNode(IElementType type, CharSequence text) {
super(type, text);
}

@Override
public PsiReference getReference() {
return new GrammarElementRef(this, getText());
protected boolean isDeclaration() {
return getParent() instanceof LexerRuleSpecNode
|| PsiTreeUtil.getParentOfType(this, TokensSpecNode.class) != null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.antlr.intellij.plugin.psi;

import com.intellij.psi.PsiReference;
import com.intellij.psi.tree.IElementType;

public class ParserRuleRefNode extends GrammarElementRefNode {
Expand All @@ -9,7 +8,7 @@ public ParserRuleRefNode(IElementType type, CharSequence text) {
}

@Override
public PsiReference getReference() {
return new GrammarElementRef(this, getText());
protected boolean isDeclaration() {
return getParent() instanceof ParserRuleSpecNode;
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/antlr/intellij/plugin/psi/TokensSpecNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.antlr.intellij.plugin.psi;

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.antlr.intellij.adaptor.parser.PsiElementFactory;
import org.jetbrains.annotations.NotNull;

public class TokensSpecNode extends ASTWrapperPsiElement {
public TokensSpecNode(@NotNull ASTNode node) {
super(node);
}

public static class Factory implements PsiElementFactory {
public static TokensSpecNode.Factory INSTANCE = new TokensSpecNode.Factory();

@Override
public PsiElement createElement(ASTNode node) {
return new TokensSpecNode(node);
}
}
}

0 comments on commit 7227cbc

Please sign in to comment.