SSR: significantly speed up some java searches

This commit is contained in:
Bas Leijdekkers
2017-10-25 11:41:55 +02:00
parent 01288a9bf0
commit de60ebfcc2
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.impl.matcher.compiler;
import com.intellij.dupLocator.iterators.NodeIterator;
@@ -33,12 +19,14 @@ import com.intellij.structuralsearch.impl.matcher.handlers.*;
import com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator;
import com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -53,6 +41,9 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
private static final Pattern ourPattern2 = Pattern.compile("/\\*" + COMMENT + "\\*/", Pattern.DOTALL);
private static final Pattern ourPattern3 = Pattern.compile("/\\*\\*" + COMMENT + "\\*/", Pattern.DOTALL);
private static final Set<String> excludedKeywords = ContainerUtil.newHashSet(PsiKeyword.CLASS, PsiKeyword.INTERFACE, PsiKeyword.ENUM,
PsiKeyword.THROWS, PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS);
public JavaCompilingVisitor(GlobalCompilingVisitor compilingVisitor) {
this.myCompilingVisitor = compilingVisitor;
}
@@ -337,15 +328,30 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
public void visitClass(PsiClass psiClass) {
super.visitClass(psiClass);
CompiledPattern pattern = myCompilingVisitor.getContext().getPattern();
final CompileContext context = myCompilingVisitor.getContext();
final CompiledPattern pattern = context.getPattern();
final MatchingHandler handler = pattern.getHandler(psiClass);
if (needsSupers(psiClass, handler)) {
((JavaCompiledPattern)pattern).setRequestsSuperInners(true);
}
handleReferenceText(psiClass.getName(), myCompilingVisitor.getContext());
handleReferenceText(psiClass.getName(), context);
GlobalCompilingVisitor.setFilter(handler, ClassFilter.getInstance());
if (!(handler instanceof SubstitutionHandler) || ((SubstitutionHandler)handler).getMinOccurs() > 0) {
if (psiClass.isInterface()) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord(PsiKeyword.INTERFACE, true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
else if (psiClass.isEnum()) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord(PsiKeyword.ENUM, true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
else {
GlobalCompilingVisitor.addFilesToSearchForGivenWord(PsiKeyword.INTERFACE, false, GlobalCompilingVisitor.OccurenceKind.CODE, context);
GlobalCompilingVisitor.addFilesToSearchForGivenWord(PsiKeyword.ENUM, false, GlobalCompilingVisitor.OccurenceKind.CODE, context);
GlobalCompilingVisitor.addFilesToSearchForGivenWord(PsiKeyword.CLASS, true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
}
}
private void createAndSetSubstitutionHandlerFromReference(final PsiElement expr, final String referenceText, boolean classQualifier) {
@@ -405,9 +411,21 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
public void visitElement(PsiElement element) {
myCompilingVisitor.handle(element);
super.visitElement(element);
final CompileContext context = myCompilingVisitor.getContext();
if (element instanceof PsiMethodReferenceExpression) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord("::", true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
else if (element instanceof PsiLambdaExpression) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord("->", true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
else if (element instanceof PsiKeyword) {
final String keyword = element.getText();
if (!excludedKeywords.contains(keyword)) {
GlobalCompilingVisitor.addFilesToSearchForGivenWord(keyword, true, GlobalCompilingVisitor.OccurenceKind.CODE, context);
}
}
}
private void handleReference(PsiJavaCodeReferenceElement reference) {
if (shouldOccur(reference)) {
handleReferenceText(reference.getReferenceName(), myCompilingVisitor.getContext());