mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
FilesScope should be smart enough to make a decision about searching in
libraries
This commit is contained in:
@@ -306,7 +306,7 @@ public class FindManagerTest extends DaemonAnalyzerTestCase {
|
||||
findModel.setGlobal(true);
|
||||
findModel.setMultipleFiles(true);
|
||||
findModel.setCustomScope(true);
|
||||
findModel.setCustomScope(new GlobalSearchScope.FilesScope(myProject, ContainerUtil.list(nonProjectFile)));
|
||||
findModel.setCustomScope(GlobalSearchScope.filesScope(myProject, ContainerUtil.list(nonProjectFile)));
|
||||
|
||||
assertSize(1, findUsages(findModel));
|
||||
}
|
||||
|
||||
@@ -310,11 +310,33 @@ public abstract class GlobalSearchScope extends SearchScope implements ProjectAw
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Please consider using this#filesWithLibrariesScope or this#filesWithoutLibrariesScope for optimization
|
||||
*/
|
||||
@NotNull
|
||||
public static GlobalSearchScope filesScope(@NotNull Project project, @NotNull Collection<VirtualFile> files) {
|
||||
return filesScope(project, files, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimization. By default FilesScope makes a decision about searching in libraries by checking that
|
||||
* at least one file is placed out of module roots. So if you're sure about files placement you can explicitly say FilesScope whether
|
||||
* it should include libraries or not in order to avoid checking each file.
|
||||
* Also, if you have a lot of files it might be faster to always search in libraries.
|
||||
*/
|
||||
@NotNull
|
||||
public static GlobalSearchScope filesWithoutLibrariesScope(@NotNull Project project, @NotNull Collection<VirtualFile> files) {
|
||||
if (files.isEmpty()) return EMPTY_SCOPE;
|
||||
return new FilesScope(project, files, false);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GlobalSearchScope filesWithLibrariesScope(@NotNull Project project, @NotNull Collection<VirtualFile> files) {
|
||||
if (files.isEmpty()) return EMPTY_SCOPE;
|
||||
return new FilesScope(project, files, true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static GlobalSearchScope filesScope(@NotNull Project project, @NotNull Collection<VirtualFile> files, @Nullable final String displayName) {
|
||||
if (files.isEmpty()) return EMPTY_SCOPE;
|
||||
@@ -721,11 +743,18 @@ public abstract class GlobalSearchScope extends SearchScope implements ProjectAw
|
||||
}
|
||||
|
||||
public static class FilesScope extends GlobalSearchScope implements Iterable<VirtualFile> {
|
||||
private final Collection<VirtualFile> myFiles; // files can be out of project roots
|
||||
private final Collection<VirtualFile> myFiles;
|
||||
private Boolean myHasFilesOutOfProjectRoots;
|
||||
|
||||
public FilesScope(final Project project, @NotNull Collection<VirtualFile> files) {
|
||||
public FilesScope(@Nullable Project project, @NotNull Collection<VirtualFile> files) {
|
||||
this(project, files, null);
|
||||
}
|
||||
|
||||
// Optimization
|
||||
private FilesScope(@Nullable Project project, @NotNull Collection<VirtualFile> files, @Nullable Boolean hasFilesOutOfProjectRoots) {
|
||||
super(project);
|
||||
myFiles = files;
|
||||
myHasFilesOutOfProjectRoots = hasFilesOutOfProjectRoots;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -745,7 +774,7 @@ public abstract class GlobalSearchScope extends SearchScope implements ProjectAw
|
||||
|
||||
@Override
|
||||
public boolean isSearchInLibraries() {
|
||||
return false;
|
||||
return hasFilesOutOfProjectRoots();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -757,11 +786,28 @@ public abstract class GlobalSearchScope extends SearchScope implements ProjectAw
|
||||
public int hashCode() {
|
||||
return myFiles.hashCode();
|
||||
}
|
||||
|
||||
private boolean hasFilesOutOfProjectRoots() {
|
||||
if (myHasFilesOutOfProjectRoots == null) {
|
||||
long i = System.currentTimeMillis();
|
||||
myHasFilesOutOfProjectRoots = false;
|
||||
Project project = getProject();
|
||||
if (project != null && !project.isDefault()) {
|
||||
for (VirtualFile file : myFiles) {
|
||||
if (FileIndexFacade.getInstance(project).getModuleForFile(file) == null) {
|
||||
myHasFilesOutOfProjectRoots = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return myHasFilesOutOfProjectRoots;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
List<VirtualFile> files = myFiles.size() <= 20 ? new ArrayList<VirtualFile>(myFiles) : new ArrayList<VirtualFile>(myFiles).subList(0,20);
|
||||
return "Files: ("+ files +")";
|
||||
return "Files: ("+ files +"); search in libraries: " + (myHasFilesOutOfProjectRoots != null ? myHasFilesOutOfProjectRoots : "unknown");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-8
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2014 JetBrains s.r.o.
|
||||
* Copyright 2000-2015 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.
|
||||
@@ -250,13 +250,7 @@ public class PredefinedSearchScopeProviderImpl extends PredefinedSearchScopeProv
|
||||
}
|
||||
});
|
||||
if (!selectedFiles.isEmpty()) {
|
||||
return new DelegatingGlobalSearchScope(GlobalSearchScope.filesScope(project, selectedFiles)) {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Selected Files";
|
||||
}
|
||||
};
|
||||
return GlobalSearchScope.filesScope(project, selectedFiles, "Selected Files");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user