highlighting for keyword in Groovy via annotator

This commit is contained in:
Maxim.Medvedev
2012-05-12 19:54:06 +04:00
parent 86fe64c310
commit 359b946d21
24 changed files with 226 additions and 104 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -130,6 +130,29 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator {
((GroovyPsiElement)element).accept(this);
myHolder = null;
}
else {
final IElementType token = element.getNode().getElementType();
if (TokenSets.KEYWORDS.contains(token)) {
highlightKeyword(element, holder, token);
}
}
}
private static void highlightKeyword(PsiElement element, AnnotationHolder holder, IElementType token) {
final PsiElement parent = element.getParent();
if (parent instanceof GrArgumentLabel) return; //don't highlight: print (void:'foo')
if (PsiTreeUtil.getParentOfType(element, GrCodeReferenceElement.class) != null) {
if (token == GroovyTokenTypes.kDEF || token == GroovyTokenTypes.kIN || token == GroovyTokenTypes.kAS) {
return; //It is allowed to name packages 'as', 'in' or 'def'
}
}
else if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression)parent).getReferenceNameElement()) {
return; //don't highlight foo.def
}
final Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(DefaultHighlighter.KEYWORD);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -193,7 +193,7 @@ public class GroovySyntaxHighlighter extends SyntaxHighlighterBase implements Gr
fillMap(ATTRIBUTES, tLINE_COMMENTS, DefaultHighlighter.LINE_COMMENT);
fillMap(ATTRIBUTES, tBLOCK_COMMENTS, DefaultHighlighter.BLOCK_COMMENT);
fillMap(ATTRIBUTES, tBAD_CHARACTERS, DefaultHighlighter.BAD_CHARACTER);
fillMap(ATTRIBUTES, tKEYWORDS, DefaultHighlighter.KEYWORD);
//fillMap(ATTRIBUTES, tKEYWORDS, DefaultHighlighter.KEYWORD);
fillMap(ATTRIBUTES, tNUMBERS, DefaultHighlighter.NUMBER);
fillMap(ATTRIBUTES, tGSTRINGS, DefaultHighlighter.GSTRING);
fillMap(ATTRIBUTES, tSTRINGS, DefaultHighlighter.STRING);
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,8 @@
package org.jetbrains.plugins.groovy.lang.lexer;
import com.intellij.lexer.Lexer;
import com.intellij.lexer.LookAheadLexer;
import com.intellij.lexer.MergingLexerAdapter;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import static com.intellij.psi.TokenType.WHITE_SPACE;
@@ -43,53 +41,4 @@ public class GroovyLexer extends LookAheadLexer {
public GroovyLexer() {
super(new MergingLexerAdapter(new GroovyFlexLexer(), tokensToMerge));
}
@Override
protected void lookAhead(Lexer baseLexer) {
final IElementType type = baseLexer.getTokenType();
if (TokenSets.DOTS.contains(type) || type == kIMPORT || type == kPACKAGE) {
addToken(type);
baseLexer.advance();
while (baseLexer.getTokenType() == WHITE_SPACE) {
addToken(WHITE_SPACE);
baseLexer.advance();
}
if (TokenSets.DOTS.contains(type) && baseLexer.getTokenType()==mAT) {
addToken(mAT);
baseLexer.advance();
while (baseLexer.getTokenType() == WHITE_SPACE) {
addToken(WHITE_SPACE);
baseLexer.advance();
}
}
final IElementType token = baseLexer.getTokenType();
if (shouldBeIdent(type, token)) {
addToken(mIDENT);
baseLexer.advance();
}
else {
addToken(token);
baseLexer.advance();
}
}
else {
super.lookAhead(baseLexer);
}
}
private static boolean shouldBeIdent(IElementType type, IElementType token) {
if (!TokenSets.KEYWORDS.contains(token)) return false;
if (TokenSets.DOTS.contains(type)) {
return token != kTHIS && token != kSUPER && token != kNEW;
}
else if (type == kIMPORT) {
return token != kSTATIC;
}
else if (type == kPACKAGE) {
return true;
}
return false;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -201,4 +201,5 @@ public abstract class TokenSets {
);
public static final TokenSet SHIFT_SIGNS = TokenSet.create(COMPOSITE_LSHIFT_SIGN, COMPOSITE_RSHIFT_SIGN, COMPOSITE_TRIPLE_SHIFT_SIGN);
public static TokenSet CODE_REFERENCE_ELEMENT_NAME_TOKENS = TokenSet.create(mIDENT, kDEF, kIN, kAS);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -124,7 +124,7 @@ public class PrimaryExpression implements GroovyElementTypes {
if (TokenSets.BUILT_IN_TYPE.contains(builder.getTokenType())) {
ParserUtils.eatElement(builder, BUILT_IN_TYPE);
}
else if (mIDENT == builder.getTokenType()) {
else if (TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS.contains(builder.getTokenType())) {
name = builder.getTokenText();
ReferenceElement.parse(builder, false, true, false, true, true);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package org.jetbrains.plugins.groovy.lang.parser.parsing.statements.imports;
import com.intellij.lang.PsiBuilder;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.parser.parsing.statements.typeDefinitions.ReferenceElement;
import org.jetbrains.plugins.groovy.lang.parser.parsing.util.ParserUtils;
@@ -31,7 +32,7 @@ public class ImportReference implements GroovyElementTypes {
public static boolean parse(PsiBuilder builder) {
if (!mIDENT.equals(builder.getTokenType())) {
if (!TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS.contains(builder.getTokenType())) {
return false;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.jetbrains.plugins.groovy.lang.parser.parsing.statements.typeDefiniti
import com.intellij.codeInsight.completion.CompletionInitializationContext;
import com.intellij.lang.PsiBuilder;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.parser.parsing.types.TypeArguments;
import org.jetbrains.plugins.groovy.lang.parser.parsing.util.ParserUtils;
@@ -97,7 +98,7 @@ public class ReferenceElement implements GroovyElementTypes {
String lastIdentifier = builder.getTokenText();
if (!ParserUtils.getToken(builder, mIDENT)) {
if (!ParserUtils.getToken(builder, TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS)) {
internalTypeMarker.rollbackTo();
return fail;
}
@@ -127,7 +128,7 @@ public class ReferenceElement implements GroovyElementTypes {
lastIdentifier = builder.getTokenText();
if (!ParserUtils.getToken(builder, mIDENT)) {
if (!ParserUtils.getToken(builder, TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS)) {
internalTypeMarker.rollbackTo();
return fail;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.jetbrains.plugins.groovy.lang.parser.parsing.toplevel.packaging;
import com.intellij.lang.PsiBuilder;
import com.intellij.lang.PsiBuilder.Marker;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.parser.GroovyParser;
import org.jetbrains.plugins.groovy.lang.parser.parsing.auxiliary.modifiers.Modifiers;
@@ -40,9 +41,11 @@ public class PackageDefinition implements GroovyElementTypes {
pMarker.rollbackTo();
return false;
}
if (builder.getTokenType() == mIDENT) {
if (TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS.contains(builder.getTokenType())) {
ReferenceElement.parseForPackage(builder);
} else {
}
else {
builder.error(GroovyBundle.message("identifier.expected"));
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@ import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.GrReferenceAdjuster;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
@@ -60,11 +59,6 @@ public abstract class GrReferenceElementImpl<Q extends PsiElement> extends Groov
return null;
}
@Nullable
public PsiElement getReferenceNameElement() {
return findChildByType(GroovyTokenTypes.mIDENT);
}
public PsiElement getElement() {
return this;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.completion.GroovyCompletionUtil;
import org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocReferenceElement;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
@@ -92,6 +93,11 @@ public class GrCodeReferenceElementImpl extends GrReferenceElementImpl<GrCodeRef
return (GrCodeReferenceElement) findChildByType(GroovyElementTypes.REFERENCE_ELEMENT);
}
@Override
public PsiElement getReferenceNameElement() {
return findChildByType(TokenSets.CODE_REFERENCE_ELEMENT_NAME_TOKENS);
}
enum ReferenceKind {
CLASS,
CLASS_OR_PACKAGE,
@@ -216,6 +222,8 @@ public class GrCodeReferenceElementImpl extends GrReferenceElementImpl<GrCodeRef
case STATIC_MEMBER_FQ:
case CLASS_OR_PACKAGE:
if (resolve() instanceof PsiPackage) return true;
case CLASS:
case CLASS_IN_QUALIFIED_NEW:
}
final GrCodeReferenceElement qualifier = getQualifier();
return qualifier != null && ((GrCodeReferenceElementImpl)qualifier).isFullyQualified();
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -393,4 +393,64 @@ Groovy script
PsiElement(()('(')
PsiElement())(')')'''
}
void testRefElementsWithKeywords() {
checkParsing('''\
def a = new def.as.Foo()
def b = new foo.as.in.Foo()
''', '''\
Groovy script
Variable definitions
Modifiers
PsiElement(def)('def')
PsiWhiteSpace(' ')
Variable
PsiElement(identifier)('a')
PsiWhiteSpace(' ')
PsiElement(=)('=')
PsiWhiteSpace(' ')
NEW expression
PsiElement(new)('new')
PsiWhiteSpace(' ')
Reference element
Reference element
Reference element
PsiElement(def)('def')
PsiElement(.)('.')
PsiElement(as)('as')
PsiElement(.)('.')
PsiElement(identifier)('Foo')
Arguments
PsiElement(()('(')
PsiElement())(')')
PsiElement(new line)('\\n')
Variable definitions
Modifiers
PsiElement(def)('def')
PsiWhiteSpace(' ')
Variable
PsiElement(identifier)('b')
PsiWhiteSpace(' ')
PsiElement(=)('=')
PsiWhiteSpace(' ')
NEW expression
PsiElement(new)('new')
PsiWhiteSpace(' ')
Reference element
Reference element
Reference element
Reference element
PsiElement(identifier)('foo')
PsiElement(.)('.')
PsiElement(as)('as')
PsiElement(.)('.')
PsiElement(in)('in')
PsiElement(.)('.')
PsiElement(identifier)('Foo')
Arguments
PsiElement(()('(')
PsiElement())(')')
PsiElement(new line)('\\n')
''')
}
}
@@ -1,6 +1,17 @@
/*
* Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
* Use is subject to license terms.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.lang.parser;
@@ -143,6 +154,7 @@ public class StatementsParsingTest extends GroovyParsingTestCase {
public void testTypedef$classes$class6() throws Throwable { doTest(); }
public void testTypedef$classes$class7() throws Throwable { doTest(); }
public void testTypedef$classes$class8() throws Throwable { doTest(); }
public void testTypedef$classes$class9() throws Throwable { doTest(); }
public void testTypedef$classes$errors$classerr1() throws Throwable { doTest(); }
public void testTypedef$classes$errors$classerr2() throws Throwable { doTest(); }
public void testTypedef$classes$errors$classerr3() throws Throwable { doTest(); }
@@ -1,6 +1,21 @@
class <info>A</info> {
static private class <info>B</info> {
int <info>i</info>
B () { this.<info descr="null">i</info> = 42 }
}
<info descr="null">class</info> <info descr="null">A</info> {
<info descr="null">static</info> <info descr="null">private</info> <info descr="null">class</info> <info descr="null">B</info> {
<info descr="null">int</info> <info descr="null">i</info>
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
B () { <info descr="null">this</info>.<info descr="null">i</info> = 42 }
}
}
@@ -1,15 +1,30 @@
def <info textAttributesKey="Groovy method declaration">method</info>(int <info textAttributesKey="Groovy parameter">param1</info>, int <info textAttributesKey="Groovy reassigned parameter">param2</info>) {
int <info textAttributesKey="Groovy var">var1</info> = 0
int <info textAttributesKey="Groovy reassigned var">var2</info> = 1
int <info textAttributesKey="Groovy reassigned var">var3</info> = 1
if (<info textAttributesKey="Groovy parameter">param1</info> == 1) {
<info textAttributesKey="Groovy reassigned parameter">param2</info> = <info textAttributesKey="Groovy reassigned var">var2</info> = 2
<info textAttributesKey="Groovy reassigned var">var3</info>++
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
<info descr="null">def</info> <info descr="null">method</info>(<info descr="null">int</info> <info descr="null">param1</info>, <info descr="null">int</info> <info descr="null">param2</info>) {
<info descr="null">int</info> <info descr="null">var1</info> = 0
<info descr="null">int</info> <info descr="null">var2</info> = 1
<info descr="null">int</info> <info descr="null">var3</info> = 1
<info descr="null">if</info> (<info descr="null">param1</info> == 1) {
<info descr="null">param2</info> = <info descr="null">var2</info> = 2
<info descr="null">var3</info>++
}
<info textAttributesKey="Method call">println</info> <info textAttributesKey="Groovy var">var1</info> +
<info textAttributesKey="Groovy reassigned var">var2</info> +
<info textAttributesKey="Groovy parameter">param1</info> +
<info textAttributesKey="Groovy reassigned parameter">param2</info>
<info descr="null">println</info> <info descr="null">var1</info> +
<info descr="null">var2</info> +
<info descr="null">param1</info> +
<info descr="null">param2</info>
}
<info textAttributesKey="Method call">method</info>(239, 42)
<info descr="null">method</info>(239, 42)
@@ -16,7 +16,7 @@ Groovy script
Reference expression
PsiElement(identifier)('foo')
PsiElement(.)('.')
PsiElement(identifier)('class')
PsiElement(class)('class')
PsiElement())(')')
PsiElement(new line)('\n')
PsiElement(def)('def')
@@ -12,4 +12,4 @@ Groovy script
PsiElement(Integer)('2')
PsiElement())(')')
PsiElement(?.)('?.')
PsiElement(identifier)('class')
PsiElement(class)('class')
@@ -13,7 +13,7 @@ Groovy script
PsiElement(Integer)('2')
PsiElement())(')')
PsiElement(?.)('?.')
PsiElement(identifier)('class')
PsiElement(class)('class')
Arguments
PsiElement(()('(')
Literal
@@ -10,4 +10,4 @@ Groovy script
'this' reference expression
PsiElement(this)('this')
PsiElement(.)('.')
PsiElement(identifier)('final')
PsiElement(final)('final')
@@ -11,6 +11,6 @@ Groovy script
Reference element
PsiElement(identifier)('a')
PsiElement(.)('.')
PsiElement(identifier)('def')
PsiElement(def)('def')
PsiElement(.)('.')
PsiElement(identifier)('b')
@@ -10,4 +10,4 @@ Groovy script
Reference element
PsiElement(identifier)('a')
PsiElement(.)('.')
PsiElement(identifier)('def')
PsiElement(def)('def')
@@ -10,4 +10,4 @@ Groovy script
Reference element
PsiElement(identifier)('a')
PsiElement(.)('.')
PsiElement(identifier)('as')
PsiElement(as)('as')
@@ -7,4 +7,4 @@ Groovy script
PsiElement(package)('package')
PsiWhiteSpace(' ')
Reference element
PsiElement(identifier)('as')
PsiElement(as)('as')
@@ -8,6 +8,6 @@ Groovy script
PsiWhiteSpace(' ')
Reference element
Reference element
PsiElement(identifier)('def')
PsiElement(def)('def')
PsiElement(.)('.')
PsiElement(identifier)('as')
PsiElement(as)('as')
@@ -0,0 +1,40 @@
class a extends def.b implements as.foo.def.in.c {}
-----
Groovy script
Class definition
Modifiers
<empty list>
PsiElement(class)('class')
PsiWhiteSpace(' ')
PsiElement(identifier)('a')
PsiWhiteSpace(' ')
Extends clause
PsiElement(extends)('extends')
PsiWhiteSpace(' ')
Reference element
Reference element
PsiElement(def)('def')
PsiElement(.)('.')
PsiElement(identifier)('b')
PsiWhiteSpace(' ')
Implements clause
PsiElement(implements)('implements')
PsiWhiteSpace(' ')
Reference element
Reference element
Reference element
Reference element
Reference element
PsiElement(as)('as')
PsiElement(.)('.')
PsiElement(identifier)('foo')
PsiElement(.)('.')
PsiElement(def)('def')
PsiElement(.)('.')
PsiElement(in)('in')
PsiElement(.)('.')
PsiElement(identifier)('c')
PsiWhiteSpace(' ')
Type definition body
PsiElement({)('{')
PsiElement(})('}')