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 19, 2023
1 parent cd9a2c8 commit 01ba5a6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@
public class GrammarElementRefTest extends LightPlatformCodeInsightFixtureTestCase {
public void testFindUsagesOfLexerRule() {
Collection<UsageInfo> ruleUsages = myFixture.testFindUsages("SimpleGrammar.g4");
assertEquals(4, ruleUsages.size());
assertEquals(3, ruleUsages.size());
}

public void testFindUsagesOfParserRule() {
Collection<UsageInfo> ruleUsages = myFixture.testFindUsages("SimpleGrammar2.g4");
assertEquals(2, ruleUsages.size());
assertEquals(1, ruleUsages.size());
}

public void testHighlightUsagesOfLexerRule() {
RangeHighlighter[] usages = myFixture.testHighlightUsages("SimpleGrammar.g4");

assertEquals(5, usages.length);
assertEquals(4, usages.length);
}

public void testHighlightUsagesOfParserRule() {
RangeHighlighter[] usages = myFixture.testHighlightUsages("SimpleGrammar2.g4");

assertEquals(3, usages.length);
assertEquals(2, usages.length);
}

public void testReferenceToLexerRule() {
Expand Down

0 comments on commit 01ba5a6

Please sign in to comment.