Search in sources :

Example 6 with FileOperationProvider

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuildLookupElement(com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 7 with FileOperationProvider

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());
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 8 with FileOperationProvider

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);
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) Blaze(com.google.idea.blaze.base.settings.Blaze) ImportRoots(com.google.idea.blaze.base.sync.projectview.ImportRoots) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File) Builder(com.google.common.collect.ImmutableMap.Builder) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Lists(com.google.common.collect.Lists) ProjectViewSet(com.google.idea.blaze.base.projectview.ProjectViewSet) WorkspaceRoot(com.google.idea.blaze.base.model.primitives.WorkspaceRoot) Project(com.intellij.openapi.project.Project) WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FetchExecutor(com.google.idea.blaze.base.prefetch.FetchExecutor) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Builder(com.google.common.collect.ImmutableMap.Builder) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) File(java.io.File)

Example 9 with FileOperationProvider

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);
        }
    }
}
Also used : WorkspacePath(com.google.idea.blaze.base.model.primitives.WorkspacePath) FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File)

Example 10 with FileOperationProvider

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;
}
Also used : FileOperationProvider(com.google.idea.blaze.base.io.FileOperationProvider) File(java.io.File) Nullable(javax.annotation.Nullable)

Aggregations

FileOperationProvider (com.google.idea.blaze.base.io.FileOperationProvider)15 File (java.io.File)10 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 IOException (java.io.IOException)5 Nullable (javax.annotation.Nullable)5 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 ImportRoots (com.google.idea.blaze.base.sync.projectview.ImportRoots)3 PsiFile (com.intellij.psi.PsiFile)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Lists (com.google.common.collect.Lists)2 Futures (com.google.common.util.concurrent.Futures)2 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)2 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 WorkspaceRoot (com.google.idea.blaze.base.model.primitives.WorkspaceRoot)2 ProjectViewSet (com.google.idea.blaze.base.projectview.ProjectViewSet)2 Project (com.intellij.openapi.project.Project)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2