From e3fa9d83202affb3012d270b1f67b686dd03d828 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 25 Jul 2012 22:01:56 +0200 Subject: [PATCH] groovy highlighting performance: use walking visitor --- .../annotator/GrReferenceHighlighter.java | 162 ++++++++---------- .../local/GroovyPostHighlightingPass.java | 8 +- .../lang/editor/GroovyImportOptimizer.java | 27 +-- 3 files changed, 90 insertions(+), 107 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java index 6da3f86bf93d..59ddb6cbdee4 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GrReferenceHighlighter.java @@ -1,88 +1,74 @@ -/* - * 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.editor.colors.TextAttributesKey; -import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.psi.PsiElement; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; -import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; -import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor; -import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation; -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 GrReferenceHighlighter extends TextEditorHighlightingPass { - @NotNull private final GroovyFile myFile; - @Nullable private List myInfos = null; - - protected GrReferenceHighlighter(@Nullable Document document, @NotNull GroovyFile file) { - super(file.getProject(), document); - myFile = file; - } - - @Override - public void doCollectInformation(@NotNull ProgressIndicator progress) { - myInfos = new ArrayList(); - myFile.accept(new GroovyRecursiveElementVisitor() { - @Override - public void visitReferenceExpression(GrReferenceExpression referenceExpression) { - super.visitReferenceExpression(referenceExpression); - visit(referenceExpression); - } - - private void visit(GrReferenceElement element) { - final PsiElement resolved = element.resolve(); - final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(resolved); - if (attribute != null) { - final PsiElement refNameElement = GrHighlightUtil.getElementToHighlight(element); - myInfos.add(HighlightInfo.createHighlightInfo(HighlightInfoType.INFORMATION, refNameElement, null, attribute)); - } - } - - @Override - public void visitCodeReferenceElement(GrCodeReferenceElement refElement) { - super.visitCodeReferenceElement(refElement); - visit(refElement); - } - - @Override - public void visitAnnotation(GrAnnotation annotation) { - super.visitAnnotation(annotation); - visit(annotation.getClassReference()); - } - }); - } - - @Override - public void doApplyInformationToEditor() { - if (myInfos == null) return; - UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), myInfos, getColorsScheme(), getId()); - } -} +/* + * 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.editor.colors.TextAttributesKey; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiRecursiveElementWalkingVisitor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Max Medvedev + */ +public class GrReferenceHighlighter extends TextEditorHighlightingPass { + @NotNull private final GroovyFile myFile; + @Nullable private List myInfos = null; + + protected GrReferenceHighlighter(@Nullable Document document, @NotNull GroovyFile file) { + super(file.getProject(), document); + myFile = file; + } + + @Override + public void doCollectInformation(@NotNull ProgressIndicator progress) { + myInfos = new ArrayList(); + myFile.accept(new PsiRecursiveElementWalkingVisitor() { + @Override + public void visitElement(PsiElement element) { + super.visitElement(element); + if (element instanceof GrReferenceElement) { + visit((GrReferenceElement)element); + } + } + private void visit(GrReferenceElement element) { + final PsiElement resolved = element.resolve(); + final TextAttributesKey attribute = GrHighlightUtil.getDeclarationHighlightingAttribute(resolved); + if (attribute != null) { + final PsiElement refNameElement = GrHighlightUtil.getElementToHighlight(element); + myInfos.add(HighlightInfo.createHighlightInfo(HighlightInfoType.INFORMATION, refNameElement, null, attribute)); + } + } + }); + } + + @Override + public void doApplyInformationToEditor() { + if (myInfos == null) return; + UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), myInfos, getColorsScheme(), getId()); + } +} diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java index 0b332bc32bc8..8f5a2d5a3f25 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/local/GroovyPostHighlightingPass.java @@ -56,7 +56,6 @@ import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement; import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; @@ -109,7 +108,6 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass { }; final List unusedDeclarations = new ArrayList(); - final Set unusedImports = new HashSet(PsiUtil.getValidImportStatements(myFile)); final Map usedParams = new HashMap(); myFile.accept(new PsiRecursiveElementWalkingVisitor() { @@ -156,13 +154,11 @@ public class GroovyPostHighlightingPass extends TextEditorHighlightingPass { } } - for (GrImportStatement used : GroovyImportOptimizer.findUsedImports(myFile)) { - unusedImports.remove(used); - } - super.visitElement(element); } }); + final Set unusedImports = new HashSet(PsiUtil.getValidImportStatements(myFile)); + unusedImports.removeAll(GroovyImportOptimizer.findUsedImports(myFile)); myUnusedImports = unusedImports; if (deadCodeEnabled) { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/editor/GroovyImportOptimizer.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/editor/GroovyImportOptimizer.java index 31cf84c594d5..88fee646dbc2 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/editor/GroovyImportOptimizer.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/editor/GroovyImportOptimizer.java @@ -29,9 +29,11 @@ import gnu.trove.TObjectIntProcedure; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings; -import org.jetbrains.plugins.groovy.lang.psi.*; +import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement; import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; @@ -40,7 +42,8 @@ import org.jetbrains.plugins.groovy.lang.resolve.processors.ResolverProcessor; import java.util.*; -import static org.jetbrains.plugins.groovy.lang.editor.GroovyImportHelper.*; +import static org.jetbrains.plugins.groovy.lang.editor.GroovyImportHelper.isImplicitlyImported; +import static org.jetbrains.plugins.groovy.lang.editor.GroovyImportHelper.processImports; /** * @author ven @@ -72,16 +75,14 @@ public class GroovyImportOptimizer implements ImportOptimizer { @Nullable final Map annotations) { if (!(file instanceof GroovyFile)) return; - ((GroovyFile)file).accept(new GroovyRecursiveElementVisitor() { - public void visitCodeReferenceElement(GrCodeReferenceElement refElement) { - visitRefElement(refElement); - super.visitCodeReferenceElement(refElement); - } - - public void visitReferenceExpression(GrReferenceExpression referenceExpression) { - visitRefElement(referenceExpression); - super.visitReferenceExpression(referenceExpression); - } + ((GroovyFile)file).accept(new PsiRecursiveElementWalkingVisitor() { + @Override + public void visitElement(PsiElement element) { + super.visitElement(element); + if (element instanceof GrReferenceElement) { + visitRefElement((GrReferenceElement)element); + } + } private void visitRefElement(GrReferenceElement refElement) { final GroovyResolveResult[] resolveResults = refElement.multiResolve(false);