From 4c72b12c6a50b380babfcd91c3ebe0dafd471eec Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Mon, 24 Oct 2016 17:46:45 +0300 Subject: [PATCH] function expression searcher: find most likely files only among of files where occurrences was found --- .../JavaFunctionalExpressionSearcher.java | 29 ++++++++++--------- .../lambda/FindFunctionalInterfaceTest.java | 2 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java index e401087d5a86..b23e14b0fb01 100644 --- a/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java @@ -84,7 +84,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase p.getElementToSearch().getManager()); manager.startBatchFilesProcessingMode(); try { - processOffsets(descriptors, (file, offsets) -> { + processOffsets(descriptors, project, (file, offsets) -> { fileCount.incrementAndGet(); exprCount.addAndGet(offsets.size()); return processFile(consumer, descriptors, file, offsets); @@ -99,9 +99,9 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase getFilesToSearchInPsi(PsiClass samClass) { + public static Set getFilesToSearchInPsi(PsiClass samClass, Project project) { Set result = new HashSet<>(); - processOffsets(calcDescriptors(new SearchParameters(samClass, samClass.getUseScope())), (file, offsets) -> result.add(file)); + processOffsets(calcDescriptors(new SearchParameters(samClass, samClass.getUseScope())), project, (file, offsets) -> result.add(file)); return result; } @@ -134,8 +134,9 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase getLikelyFiles(List descriptors) { - return JBIterable.from(descriptors).flatMap(SamDescriptor::getMostLikelyFiles).toSet(); + private static Set getLikelyFiles(List descriptors, Collection candidateFiles, Project project) { + final GlobalSearchScope candidateFilesScope = GlobalSearchScope.filesScope(project, candidateFiles); + return JBIterable.from(descriptors).flatMap(descriptor -> descriptor.getMostLikelyFiles(candidateFilesScope)).toSet(); } @NotNull @@ -156,12 +157,14 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase descriptors, PairProcessor> processor) { + private static void processOffsets(List descriptors, Project project, PairProcessor> processor) { if (descriptors.isEmpty()) return; List samClasses = ContainerUtil.map(descriptors, d -> d.samClass); MultiMap allCandidates = getAllOccurrences(descriptors); - for (VirtualFile vFile : putLikelyFilesFirst(descriptors, allCandidates.keySet())) { + if (allCandidates.isEmpty()) return; + + for (VirtualFile vFile : putLikelyFilesFirst(descriptors, allCandidates.keySet(), project)) { List toLoad = filterInapplicable(samClasses, vFile, allCandidates.get(vFile)); if (!toLoad.isEmpty()) { LOG.trace("To load " + vFile.getPath() + " with values: " + toLoad); @@ -173,9 +176,9 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase putLikelyFilesFirst(List descriptors, Set allFiles) { - Set orderedFiles = new LinkedHashSet<>(allFiles); - orderedFiles.retainAll(getLikelyFiles(descriptors)); + private static Set putLikelyFilesFirst(List descriptors, Set allFiles, Project project) { + Set orderedFiles = new LinkedHashSet<>(allFiles.size()); + orderedFiles.addAll(getLikelyFiles(descriptors, allFiles, project)); orderedFiles.addAll(allFiles); return orderedFiles; } @@ -306,7 +309,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase getMostLikelyFiles() { + private Set getMostLikelyFiles(GlobalSearchScope searchScope) { Set files = ContainerUtil.newLinkedHashSet(); ReadAction.run(() -> { if (!samClass.isValid()) return; @@ -317,7 +320,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase likelyNames = ContainerUtil.newLinkedHashSet(className); StubIndex.getInstance().processElements(JavaMethodParameterTypesIndex.getInstance().getKey(), className, - project, effectiveUseScope, PsiMethod.class, method -> { + project, searchScope, PsiMethod.class, method -> { ProgressManager.checkCanceled(); likelyNames.add(method.getName()); return true; @@ -326,7 +329,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase processor = Processors.cancelableCollectProcessor(files); for (String word : likelyNames) { - helper.processFilesWithText(effectiveUseScope, UsageSearchContext.IN_CODE, true, word, processor); + helper.processFilesWithText(searchScope, UsageSearchContext.IN_CODE, true, word, processor); } }); return files; diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/FindFunctionalInterfaceTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/FindFunctionalInterfaceTest.java index c02e3c770fec..801acf286bbb 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/FindFunctionalInterfaceTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/FindFunctionalInterfaceTest.java @@ -172,7 +172,7 @@ public class FindFunctionalInterfaceTest extends LightCodeInsightFixtureTestCase "}"); assertSize(1, FunctionalExpressionSearch.search(sam).findAll()); - for (VirtualFile file : JavaFunctionalExpressionSearcher.getFilesToSearchInPsi(sam)) { + for (VirtualFile file : JavaFunctionalExpressionSearcher.getFilesToSearchInPsi(sam, getProject())) { assertFalse(file.getName(), file.getName().startsWith("_")); } }