From 8ca4b4a86aeeec8aabdc5bddeeb3e31802ca9f1a Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Fri, 15 Jun 2012 15:19:43 +0400 Subject: [PATCH] Separate keyword highlighting --- plugins/groovy/src/META-INF/plugin.xml | 3 + .../groovy/annotator/GroovyAnnotator.java | 23 ----- .../annotator/KeywordHighlightFactory.java | 44 +++++++++ .../groovy/annotator/KeywordHighlighter.java | 94 +++++++++++++++++++ 4 files changed, 141 insertions(+), 23 deletions(-) create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlightFactory.java create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlighter.java diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 1d302ffb98a1..7561c19d5b88 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -1347,6 +1347,9 @@ org.jetbrains.plugins.groovy.codeInspection.local.GroovyUnusedImportsPassFactory + + org.jetbrains.plugins.groovy.annotator.KeywordHighlightFactory + org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicManager org.jetbrains.plugins.groovy.annotator.intentions.dynamic.DynamicManagerImpl diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java index 352f44cf2dd6..2173bb5b9549 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java @@ -137,29 +137,6 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator { } 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 diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlightFactory.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlightFactory.java new file mode 100644 index 000000000000..64582fe22579 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlightFactory.java @@ -0,0 +1,44 @@ +/* + * 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.annotator; + +import com.intellij.codeHighlighting.TextEditorHighlightingPass; +import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory; +import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar; +import com.intellij.openapi.components.AbstractProjectComponent; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; + +/** + * @author Max Medvedev + */ +public class KeywordHighlightFactory extends AbstractProjectComponent implements TextEditorHighlightingPassFactory { + protected KeywordHighlightFactory(Project project) { + super(project); + + TextEditorHighlightingPassRegistrar.getInstance(project).registerTextEditorHighlightingPass(this, null, null, false, -1); + } + + + @Override + public TextEditorHighlightingPass createHighlightingPass(@NotNull PsiFile file, @NotNull Editor editor) { + if (!(file instanceof GroovyFile)) return null; + return new KeywordHighlighter((GroovyFile)file, editor.getDocument()); + } +} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlighter.java new file mode 100644 index 000000000000..600c86638322 --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/KeywordHighlighter.java @@ -0,0 +1,94 @@ +/* + * 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.annotator; + +import com.intellij.codeHighlighting.TextEditorHighlightingPass; +import com.intellij.codeInsight.daemon.impl.HighlightInfo; +import com.intellij.codeInsight.daemon.impl.HighlightInfoType; +import com.intellij.codeInsight.daemon.impl.UpdateHighlightersUtil; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiRecursiveElementVisitor; +import com.intellij.psi.tree.IElementType; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.highlighter.DefaultHighlighter; +import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; +import org.jetbrains.plugins.groovy.lang.lexer.TokenSets; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Max Medvedev + */ +public class KeywordHighlighter extends TextEditorHighlightingPass { + private final GroovyFile myFile; + + private List toHighlight; + + protected KeywordHighlighter(GroovyFile file, Document document) { + super(file.getProject(), document); + myFile = file; + } + + @Override + public void doCollectInformation(@NotNull ProgressIndicator progress) { + final List result = new ArrayList(); + myFile.accept(new PsiRecursiveElementVisitor() { + @Override + public void visitElement(PsiElement element) { + IElementType tokenType = element.getNode().getElementType(); + if (TokenSets.KEYWORDS.contains(tokenType)) { + highlightKeyword(element, result, tokenType); + } + else { + super.visitElement(element); + } + } + }); + toHighlight = result; + } + + private static void highlightKeyword(PsiElement element, List result, 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 + } + + result.add(HighlightInfo.createHighlightInfo(HighlightInfoType.INFORMATION, element, null, DefaultHighlighter.KEYWORD_ATTRIBUTES)); + } + + + @Override + public void doApplyInformationToEditor() { + if (toHighlight == null) return; + UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), toHighlight, getColorsScheme(), + getId()); + } +}