Skip to content

Commit

Permalink
Fix for #667: Add support for highlighting reserved type name 'var'
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 6, 2018
1 parent 9674090 commit 1388e61
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2154,9 +2154,14 @@ public DeclarationListStatement visitVariableDeclaration(VariableDeclarationCont

for (DeclarationExpression e : declarationExpressionList) {
VariableExpression variableExpression = (VariableExpression) e.getLeftExpression();

modifierManager.processVariableExpression(variableExpression);
modifierManager.attachAnnotations(e);
// GRECLIPSE add
ModifierNode var = modifierManager.get(VAR);
if (var != null) {
e.setNodeMetaData("reserved.type.name", var);
}
// GRECLIPSE end
}

int size = declarationExpressionList.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,32 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
new HighlightedTypedPosition(contents.indexOf('it'), 2, GROOVY_CALL))
}

@Test
void testVarKeyword1() {
String contents = '''\
def abc = null
int ijk = null
var xyz = null
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('abc'), 3, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('ijk'), 3, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('xyz'), 3, VARIABLE),
new HighlightedTypedPosition(contents.indexOf('var'), 3, isParrotParser() ? RESERVED : UNKNOWN))
}

@Test
void testVarKeyword2() {
String contents = '''\
var var = null
'''.stripIndent()

assertHighlighting(contents,
new HighlightedTypedPosition(contents.indexOf('var'), 3, isParrotParser() ? RESERVED : UNKNOWN),
new HighlightedTypedPosition(contents.lastIndexOf('var'), 3, VARIABLE))
}

@Test
void testThisAndSuper() {
// the keywords super and this are identified/highlighted by GroovyTagScanner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class GroovySemanticReconciler implements IJavaReconcilingListener {
private static final String NUMBER_HIGHLIGHT_PREFERENCE = "semanticHighlighting.number";
private static final String COMMENT_HIGHLIGHT_PREFERENCE = "java_single_line_comment";
private static final String KEYWORD_HIGHLIGHT_PREFERENCE = PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_KEYWORDS_COLOR.replaceFirst("\\.color$", "");
private static final String RESERVED_HIGHLIGHT_PREFERENCE = PreferenceConstants.GROOVY_EDITOR_HIGHLIGHT_PRIMITIVES_COLOR.replaceFirst("\\.color$", "");
private static final String VARIABLE_HIGHLIGHT_PREFERENCE = "semanticHighlighting.localVariable";
private static final String PARAMETER_HIGHLIGHT_PREFERENCE = "semanticHighlighting.parameterVariable";
private static final String ANNOTATION_HIGHLIGHT_PREFERENCE = "semanticHighlighting.annotationElementReference";
Expand Down Expand Up @@ -106,6 +107,7 @@ public class GroovySemanticReconciler implements IJavaReconcilingListener {
private Object regexpRefHighlighting;
private Object commentRefHighlighting;
private Object keywordRefHighlighting;
private Object reservedRefHighlighting;
private Object undefinedRefHighlighting;
private Object deprecatedRefHighlighting;

Expand All @@ -131,6 +133,7 @@ public GroovySemanticReconciler() {
Color tagKeyColor = loadColorFrom(prefs, ANNOTATION_HIGHLIGHT_PREFERENCE);
Color commentColor = loadColorFrom(prefs, COMMENT_HIGHLIGHT_PREFERENCE);
Color keywordColor = loadColorFrom(prefs, KEYWORD_HIGHLIGHT_PREFERENCE);
Color reservedColor = loadColorFrom(prefs, RESERVED_HIGHLIGHT_PREFERENCE);
Color variableColor = loadColorFrom(prefs, VARIABLE_HIGHLIGHT_PREFERENCE);
Color parameterColor = loadColorFrom(prefs, PARAMETER_HIGHLIGHT_PREFERENCE);
Color objectFieldColor = loadColorFrom(prefs, OBJECT_FIELD_HIGHLIGHT_PREFERENCE);
Expand All @@ -146,6 +149,7 @@ public GroovySemanticReconciler() {
regexpRefHighlighting = newHighlightingStyle(stringColor, SWT.ITALIC | loadStyleFrom(prefs, STRING_HIGHLIGHT_PREFERENCE));
commentRefHighlighting = newHighlightingStyle(commentColor);
keywordRefHighlighting = newHighlightingStyle(keywordColor, loadStyleFrom(prefs, KEYWORD_HIGHLIGHT_PREFERENCE));
reservedRefHighlighting = newHighlightingStyle(reservedColor, loadStyleFrom(prefs, RESERVED_HIGHLIGHT_PREFERENCE));
deprecatedRefHighlighting = newHighlightingStyle(null, loadStyleFrom(prefs, DEPRECATED_HIGHLIGHT_PREFERENCE));
undefinedRefHighlighting = newHighlightingStyle(null, TextAttribute.UNDERLINE);

Expand Down Expand Up @@ -323,6 +327,9 @@ private Position newHighlightedPosition(HighlightedTypedPosition pos) {
case KEYWORD:
style = keywordRefHighlighting;
break;
case RESERVED:
style = reservedRefHighlighting;
break;
case NUMBER:
style = numberRefHighlighting;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,9 @@

public class HighlightedTypedPosition extends Position implements Comparable<Position> {

public static enum HighlightKind {
COMMENT, KEYWORD, NUMBER, REGEXP, MAP_KEY, TAG_KEY, FIELD, STATIC_FIELD, STATIC_VALUE, PARAMETER, VARIABLE,
CTOR, METHOD, STATIC_METHOD, CTOR_CALL, GROOVY_CALL, METHOD_CALL, STATIC_CALL, DEPRECATED, UNKNOWN
public enum HighlightKind {
KEYWORD, RESERVED, NUMBER, REGEXP, MAP_KEY, TAG_KEY, FIELD, STATIC_FIELD, STATIC_VALUE, PARAMETER, VARIABLE,
CTOR, METHOD, STATIC_METHOD, CTOR_CALL, GROOVY_CALL, METHOD_CALL, STATIC_CALL, COMMENT, DEPRECATED, UNKNOWN
}

public final HighlightKind kind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.GStringExpression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
Expand Down Expand Up @@ -167,9 +168,14 @@ public VisitStatus acceptASTNode(ASTNode node, TypeLookupResult result, IJavaEle
} else if (node instanceof MapEntryExpression) {
pos = handleMapEntryExpression((MapEntryExpression) node);

} else if (node instanceof DeclarationExpression) {
ASTNode var = node.getNodeMetaData("reserved.type.name");
if (var != null) {
pos = new HighlightedTypedPosition(var.getStart(), var.getLength(), HighlightKind.RESERVED);
}
} else if (DEBUG) {
String type = node.getClass().getSimpleName();
if (!type.matches("ClassNode|(Class|Binary|ArgumentList|Closure(List)?|Declaration|Property|List|Map)Expression"))
if (!type.matches("ClassNode|(Class|Binary|ArgumentList|Closure(List)?|Property|List|Map)Expression"))
System.err.println("found: " + type);
}

Expand Down

0 comments on commit 1388e61

Please sign in to comment.