From de60ebfcc2374a82b1fc4e50b6b10687b2c94012 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 25 Oct 2017 11:39:27 +0200 Subject: [PATCH] SSR: significantly speed up some java searches --- .../compiler/JavaCompilingVisitor.java | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java index 7b821aecc8de..cdaaa28af348 100644 --- a/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java +++ b/java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/compiler/JavaCompilingVisitor.java @@ -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 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());