groovy highlighting performance: use walking visitor

This commit is contained in:
peter
2012-07-25 22:17:00 +02:00
parent d36b2ecf44
commit e3fa9d8320
3 changed files with 90 additions and 107 deletions
@@ -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<HighlightInfo> 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<HighlightInfo>();
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<HighlightInfo> 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<HighlightInfo>();
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());
}
}
@@ -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<HighlightInfo> unusedDeclarations = new ArrayList<HighlightInfo>();
final Set<GrImportStatement> unusedImports = new HashSet<GrImportStatement>(PsiUtil.getValidImportStatements(myFile));
final Map<GrParameter, Boolean> usedParams = new HashMap<GrParameter, Boolean>();
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<GrImportStatement> unusedImports = new HashSet<GrImportStatement>(PsiUtil.getValidImportStatements(myFile));
unusedImports.removeAll(GroovyImportOptimizer.findUsedImports(myFile));
myUnusedImports = unusedImports;
if (deadCodeEnabled) {
@@ -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<String, String> 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);