use of com.google.idea.blaze.base.io.FileOperationProvider in project intellij by bazelbuild.
the class BuildReferenceManager method resolvePackageLookupElements.
/**
* Finds all child directories. If exactly one is found, continue traversing (and appending to
* LookupElement string) until there are multiple options.<br>
* Used for package path completion suggestions.
*/
public BuildLookupElement[] resolvePackageLookupElements(FileLookupData lookupData) {
String relativePath = lookupData.filePathFragment;
if (!relativePath.equals(FileUtil.toCanonicalUriPath(relativePath))) {
// ignore invalid labels containing './', '../', etc.
return BuildLookupElement.EMPTY_ARRAY;
}
File file = resolveWorkspaceRelativePath(relativePath);
FileOperationProvider provider = FileOperationProvider.getInstance();
String pathFragment = "";
if (file == null || (!provider.isDirectory(file) && !relativePath.endsWith("/"))) {
// we might be partway through a file name. Try the parent directory
relativePath = PathUtil.getParentPath(relativePath);
file = resolveWorkspaceRelativePath(relativePath);
pathFragment = StringUtil.trimStart(lookupData.filePathFragment.substring(relativePath.length()), "/");
}
if (file == null || !provider.isDirectory(file)) {
return BuildLookupElement.EMPTY_ARRAY;
}
VirtualFile vf = VirtualFileSystemProvider.getInstance().getSystem().findFileByPath(file.getPath());
if (vf == null || !vf.isDirectory()) {
return BuildLookupElement.EMPTY_ARRAY;
}
BuildLookupElement[] uniqueLookup = new BuildLookupElement[1];
while (true) {
VirtualFile[] children = vf.getChildren();
if (children == null || children.length == 0) {
return uniqueLookup[0] != null ? uniqueLookup : BuildLookupElement.EMPTY_ARRAY;
}
List<VirtualFile> validChildren = Lists.newArrayListWithCapacity(children.length);
for (VirtualFile child : children) {
ProgressManager.checkCanceled();
if (child.getName().startsWith(pathFragment) && lookupData.acceptFile(project, child)) {
validChildren.add(child);
}
}
if (validChildren.isEmpty()) {
return uniqueLookup[0] != null ? uniqueLookup : BuildLookupElement.EMPTY_ARRAY;
}
if (validChildren.size() > 1) {
return uniqueLookup[0] != null ? uniqueLookup : lookupsForFiles(validChildren, lookupData);
}
// continue traversing while there's only one option
uniqueLookup[0] = lookupForFile(validChildren.get(0), lookupData);
pathFragment = "";
vf = validChildren.get(0);
}
}
use of com.google.idea.blaze.base.io.FileOperationProvider in project intellij by bazelbuild.
the class DirectoryStructure method computeRootDirectoryStructure.
private static DirectoryStructure computeRootDirectoryStructure(Project project, WorkspaceRoot workspaceRoot, ProjectViewSet projectViewSet) throws ExecutionException, InterruptedException {
ListeningExecutorService executorService = FetchExecutor.EXECUTOR;
FileOperationProvider fileOperationProvider = FileOperationProvider.getInstance();
ImportRoots importRoots = ImportRoots.builder(workspaceRoot, Blaze.getBuildSystem(project)).add(projectViewSet).build();
Collection<WorkspacePath> rootDirectories = importRoots.rootDirectories();
Set<WorkspacePath> excludeDirectories = importRoots.excludeDirectories();
List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(rootDirectories.size());
for (WorkspacePath rootDirectory : rootDirectories) {
futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, rootDirectory));
}
ImmutableMap.Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
for (PathStructurePair pair : Futures.allAsList(futures).get()) {
if (pair != null) {
result.put(pair.path, pair.directoryStructure);
}
}
return new DirectoryStructure(result.build());
}
use of com.google.idea.blaze.base.io.FileOperationProvider in project intellij by bazelbuild.
the class DirectoryStructure method walkDirectoryStructure.
private static ListenableFuture<PathStructurePair> walkDirectoryStructure(WorkspaceRoot workspaceRoot, Set<WorkspacePath> excludeDirectories, FileOperationProvider fileOperationProvider, ListeningExecutorService executorService, WorkspacePath workspacePath) {
if (excludeDirectories.contains(workspacePath)) {
return Futures.immediateFuture(null);
}
File file = workspaceRoot.fileForPath(workspacePath);
if (!fileOperationProvider.isDirectory(file)) {
return Futures.immediateFuture(null);
}
ListenableFuture<File[]> childrenFuture = executorService.submit(() -> fileOperationProvider.listFiles(file));
return Futures.transformAsync(childrenFuture, children -> {
if (children == null) {
return Futures.immediateFuture(null);
}
List<ListenableFuture<PathStructurePair>> futures = Lists.newArrayListWithExpectedSize(children.length);
for (File child : children) {
WorkspacePath childWorkspacePath;
try {
childWorkspacePath = workspaceRoot.workspacePathFor(child);
} catch (IllegalArgumentException e) {
// stop at directories with unhandled characters.
continue;
}
futures.add(walkDirectoryStructure(workspaceRoot, excludeDirectories, fileOperationProvider, executorService, childWorkspacePath));
}
return Futures.transform(Futures.allAsList(futures), (Function<List<PathStructurePair>, PathStructurePair>) pairs -> {
Builder<WorkspacePath, DirectoryStructure> result = ImmutableMap.builder();
for (PathStructurePair pair : pairs) {
if (pair != null) {
result.put(pair.path, pair.directoryStructure);
}
}
return new PathStructurePair(workspacePath, new DirectoryStructure(result.build()));
}, executorService);
}, executorService);
}
use of com.google.idea.blaze.base.io.FileOperationProvider in project intellij by bazelbuild.
the class PackageLister method traversePackageRecursively.
private static void traversePackageRecursively(BuildSystemProvider provider, WorkspacePathResolver pathResolver, File dir, List<TargetExpression> output) {
WorkspacePath path = pathResolver.getWorkspacePath(dir);
if (path == null) {
return;
}
if (provider.findBuildFileInDirectory(dir) != null) {
output.add(TargetExpression.allFromPackageNonRecursive(path));
}
FileOperationProvider fileOperationProvider = FileOperationProvider.getInstance();
File[] children = fileOperationProvider.listFiles(dir);
if (children == null) {
return;
}
for (File child : children) {
if (fileOperationProvider.isDirectory(child)) {
traversePackageRecursively(provider, pathResolver, child, output);
}
}
}
use of com.google.idea.blaze.base.io.FileOperationProvider in project intellij by bazelbuild.
the class BazelBuildSystemProvider method findBuildFileInDirectory.
@Nullable
@Override
public File findBuildFileInDirectory(File directory) {
FileOperationProvider provider = FileOperationProvider.getInstance();
File child = new File(directory, "BUILD");
if (provider.exists(child)) {
return child;
}
child = new File(directory, "BUILD.bazel");
if (provider.exists(child)) {
return child;
}
return null;
}
Aggregations