diff --git a/base/org.codehaus.groovy26/src/org/apache/groovy/parser/antlr4/AstBuilder.java b/base/org.codehaus.groovy26/src/org/apache/groovy/parser/antlr4/AstBuilder.java index 6749c84ab1..b009790af0 100644 --- a/base/org.codehaus.groovy26/src/org/apache/groovy/parser/antlr4/AstBuilder.java +++ b/base/org.codehaus.groovy26/src/org/apache/groovy/parser/antlr4/AstBuilder.java @@ -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(); diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy index cae55c9ae7..e2c902bfde 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy @@ -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 diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/GroovySemanticReconciler.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/GroovySemanticReconciler.java index 13223a6189..67d0980a21 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/GroovySemanticReconciler.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/GroovySemanticReconciler.java @@ -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"; @@ -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; @@ -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); @@ -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); @@ -323,6 +327,9 @@ private Position newHighlightedPosition(HighlightedTypedPosition pos) { case KEYWORD: style = keywordRefHighlighting; break; + case RESERVED: + style = reservedRefHighlighting; + break; case NUMBER: style = numberRefHighlighting; break; diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/HighlightedTypedPosition.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/HighlightedTypedPosition.java index 4c8c669580..ca82ffb6b7 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/HighlightedTypedPosition.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/HighlightedTypedPosition.java @@ -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. @@ -19,9 +19,9 @@ public class HighlightedTypedPosition extends Position implements Comparable { - 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; diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/SemanticHighlightingReferenceRequestor.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/SemanticHighlightingReferenceRequestor.java index ee6ebcb109..3d17fc0e6d 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/SemanticHighlightingReferenceRequestor.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/editor/highlighting/SemanticHighlightingReferenceRequestor.java @@ -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; @@ -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); }