Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit token highlighting #383

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions src/org/intellij/grammar/editor/BnfAnnotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ final class BnfAnnotator implements Annotator, DumbAware {
@Override
public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {
PsiElement parent = psiElement.getParent();
if (parent instanceof BnfRule && ((BnfRule)parent).getId() == psiElement) {
addRuleHighlighting((BnfRule)parent, psiElement, annotationHolder);
if (parent instanceof BnfRule rule && rule.getId() == psiElement) {
addRuleHighlighting(rule, psiElement, annotationHolder);
}
else if (parent instanceof BnfAttr && ((BnfAttr)parent).getId() == psiElement) {
else if (parent instanceof BnfAttr attr && attr.getId() == psiElement) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(psiElement)
.textAttributes(BnfSyntaxHighlighter.ATTRIBUTE)
Expand All @@ -47,9 +47,9 @@ else if (parent instanceof BnfModifier) {
.textAttributes(BnfSyntaxHighlighter.KEYWORD)
.create();
}
else if (parent instanceof BnfListEntry && ((BnfListEntry)parent).getId() == psiElement) {
boolean hasValue = ((BnfListEntry)parent).getLiteralExpression() != null;
BnfAttr attr = PsiTreeUtil.getParentOfType(parent, BnfAttr.class);
else if (parent instanceof BnfListEntry listEntry && listEntry.getId() == psiElement) {
boolean hasValue = listEntry.getLiteralExpression() != null;
BnfAttr attr = PsiTreeUtil.getParentOfType(listEntry, BnfAttr.class);
KnownAttribute<?> attribute = attr != null ? KnownAttribute.getCompatibleAttribute(attr.getName()) : null;
if (attribute == KnownAttribute.METHODS && !hasValue) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
Expand All @@ -58,48 +58,56 @@ else if (parent instanceof BnfListEntry && ((BnfListEntry)parent).getId() == psi
.create();
}
}
else if (psiElement instanceof BnfReferenceOrToken) {
else if (psiElement instanceof BnfReferenceOrToken refOrToken) {
if (parent instanceof BnfAttr) {
String text = psiElement.getText();
String text = refOrToken.getText();
if ("true".equals(text) || "false".equals(text)) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(psiElement)
.range(refOrToken)
.textAttributes(BnfSyntaxHighlighter.KEYWORD)
.create();
return;
}
}
PsiReference reference = psiElement.getReference();
PsiReference reference = refOrToken.getReference();
Object resolve = reference == null ? null : reference.resolve();
if (resolve instanceof BnfRule) {
addRuleHighlighting((BnfRule)resolve, psiElement, annotationHolder);
if (resolve instanceof BnfRule rule) {
addRuleHighlighting(rule, refOrToken, annotationHolder);
}
else if (resolve instanceof BnfAttr) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(psiElement)
.range(refOrToken)
.textAttributes(BnfSyntaxHighlighter.ATTRIBUTE)
.create();
}
else if (GrammarUtil.isExternalReference(psiElement)) {
if (resolve == null && parent instanceof BnfExternalExpression && ((BnfExternalExpression)parent).getArguments().isEmpty() &&
ParserGeneratorUtil.Rule.isMeta(ParserGeneratorUtil.Rule.of((BnfReferenceOrToken)psiElement))) {
else if (GrammarUtil.isExternalReference(refOrToken)) {
if (resolve == null && parent instanceof BnfExternalExpression extExpr && extExpr.getArguments().isEmpty() &&
ParserGeneratorUtil.Rule.isMeta(ParserGeneratorUtil.Rule.of(refOrToken))) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(parent)
.textAttributes(BnfSyntaxHighlighter.META_PARAM)
.create();
}
else {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(psiElement)
.range(refOrToken)
.textAttributes(BnfSyntaxHighlighter.EXTERNAL)
.create();
}
}
else if (resolve == null) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(psiElement)
.textAttributes(BnfSyntaxHighlighter.TOKEN)
.create();
var text = refOrToken.getId().getText();
if (RuleGraphHelper.getTokenNameToTextMap((BnfFile)refOrToken.getContainingFile()).containsKey(text)) {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(refOrToken)
.textAttributes(BnfSyntaxHighlighter.EXPLICIT_TOKEN)
.create();
} else {
annotationHolder.newSilentAnnotation(HighlightSeverity.INFORMATION)
.range(refOrToken)
.textAttributes(BnfSyntaxHighlighter.IMPLICIT_TOKEN)
.create();
}
}
}
else if (psiElement instanceof BnfStringLiteralExpression) {
Expand Down
17 changes: 10 additions & 7 deletions src/org/intellij/grammar/editor/BnfColorSettingsPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.intellij.openapi.options.colors.ColorSettingsPage;
import org.intellij.grammar.BnfIcons;
import org.intellij.grammar.GrammarKitBundle;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

Expand All @@ -33,7 +34,8 @@ final class BnfColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("String", STRING),
new AttributesDescriptor("Number", NUMBER),
new AttributesDescriptor("Keyword", KEYWORD),
new AttributesDescriptor("Token", TOKEN),
new AttributesDescriptor("Explicit token", EXPLICIT_TOKEN),
new AttributesDescriptor("Implicit token", IMPLICIT_TOKEN),
new AttributesDescriptor("Rule", RULE),
new AttributesDescriptor("Attribute", ATTRIBUTE),
new AttributesDescriptor("Meta rule", META_RULE),
Expand Down Expand Up @@ -76,7 +78,7 @@ public Icon getIcon() {
}

@Override
public @NotNull String getDemoText() {
public @NotNull @Language("HTML") String getDemoText() {
return """
/*
* Sample grammar
Expand All @@ -94,11 +96,11 @@ public Icon getIcon() {
}
// Grammar rules
<r>root</r> ::= <r>header</r> <r>content</r>
<r>header</r> ::= <t>DECLARE</t> <r>reference</r>
<r>header</r> ::= <it>DECLARE</it> <r>reference</r>
<k>external</k> <r>reference</r> ::= <e>parseReference</e>
<k>private</k> <k>meta</k> <mr>comma_list</mr> ::= <pin><s>'('</s></pin> <mp><<p>></mp> (<pin><s>','</s></pin> <mp><<p>></mp>) * ')'
<k>private</k> <r>content</r> ::= <pin><t>AS</t></pin> <<<mr>comma_list</mr> <ru><r>element</r></ru>>> {<a>pin</a>=1}
<ru><r>element</r></ru> ::= <r>reference</r> [ {<pa>'+'</pa> | <pa>'-'</pa>} <r>reference</r> <t>ONLY</t>?] {<a>recoverWhile</a>=<r>element_recover</r>}
<k>private</k> <k>meta</k> <mr>comma_list</mr> ::= <pin><t>LEFT_PAREN</t></pin> <mp><<p>></mp> (<pin><s>','</s></pin> <mp><<p>></mp>) * <t>RIGHT_PAREN</t>
<k>private</k> <r>content</r> ::= <pin><it>AS</it></pin> <<<mr>comma_list</mr> <ru><r>element</r></ru>>> {<a>pin</a>=1}
<ru><r>element</r></ru> ::= <r>reference</r> [ {<pa>'+'</pa> | <pa>'-'</pa>} <r>reference</r> <it>ONLY</it>?] {<a>recoverWhile</a>=<r>element_recover</r>}
<k>private</k> <r>element_recover</r> ::= !(',' | ')')

""";
Expand All @@ -112,7 +114,8 @@ public Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMa
map.put("mr", META_RULE);
map.put("a", ATTRIBUTE);
map.put("pa", PATTERN);
map.put("t", TOKEN);
map.put("t", EXPLICIT_TOKEN);
map.put("it", IMPLICIT_TOKEN);
map.put("k", KEYWORD);
map.put("e", EXTERNAL);
map.put("pin", PIN_MARKER);
Expand Down
3 changes: 2 additions & 1 deletion src/org/intellij/grammar/editor/BnfSyntaxHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class BnfSyntaxHighlighter extends SyntaxHighlighterBase {
public static final TextAttributesKey PATTERN = createTextAttributesKey("BNF_PATTERN", DefaultLanguageHighlighterColors.INSTANCE_FIELD);
public static final TextAttributesKey NUMBER = createTextAttributesKey("BNF_NUMBER", DefaultLanguageHighlighterColors.NUMBER);
public static final TextAttributesKey KEYWORD = createTextAttributesKey("BNF_KEYWORD", DefaultLanguageHighlighterColors.MARKUP_ATTRIBUTE);
public static final TextAttributesKey TOKEN = createTextAttributesKey("BNF_TOKEN", DefaultLanguageHighlighterColors.STRING);
public static final TextAttributesKey EXPLICIT_TOKEN = createTextAttributesKey("BNF_TOKEN", DefaultLanguageHighlighterColors.STRING);
public static final TextAttributesKey IMPLICIT_TOKEN = createTextAttributesKey("BNF_IMPLICIT_TOKEN", DefaultLanguageHighlighterColors.STATIC_FIELD);
public static final TextAttributesKey RULE = createTextAttributesKey("BNF_RULE", DefaultLanguageHighlighterColors.KEYWORD);
public static final TextAttributesKey META_RULE = createTextAttributesKey("BNF_META_RULE", DefaultLanguageHighlighterColors.KEYWORD);
public static final TextAttributesKey META_PARAM = createTextAttributesKey("BNF_META_RULE_PARAM");
Expand Down