Search in sources :

Example 1 with BuildLookupElement

use of com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement 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 2 with BuildLookupElement

use of com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement in project intellij by bazelbuild.

the class LabelReference method getSkylarkExtensionLookups.

private BuildLookupElement[] getSkylarkExtensionLookups(String labelString) {
    if (!skylarkExtensionReference(myElement)) {
        return BuildLookupElement.EMPTY_ARRAY;
    }
    String packagePrefix = LabelUtils.getPackagePathComponent(labelString);
    BuildFile parentFile = myElement.getContainingFile();
    if (parentFile == null) {
        return BuildLookupElement.EMPTY_ARRAY;
    }
    BlazePackage containingPackage = BlazePackage.getContainingPackage(parentFile);
    if (containingPackage == null) {
        return BuildLookupElement.EMPTY_ARRAY;
    }
    BuildFile referencedBuildFile = LabelUtils.getReferencedBuildFile(containingPackage.buildFile, packagePrefix);
    // Directories before the colon are already covered.
    // We're only concerned with package-local directories.
    boolean hasColon = labelString.indexOf(':') != -1;
    VirtualFileFilter filter = file -> ("bzl".equals(file.getExtension()) && !file.getPath().equals(parentFile.getFilePath())) || (hasColon && file.isDirectory());
    FileLookupData lookupData = FileLookupData.packageLocalFileLookup(labelString, myElement, referencedBuildFile, filter);
    return lookupData != null ? getReferenceManager().resolvePackageLookupElements(lookupData) : BuildLookupElement.EMPTY_ARRAY;
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) ArrayUtil(com.intellij.util.ArrayUtil) IncorrectOperationException(com.intellij.util.IncorrectOperationException) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) StringLiteral(com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral) PsiUtils(com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils) TextRange(com.intellij.openapi.util.TextRange) BlazePackage(com.google.idea.blaze.base.lang.buildfile.search.BlazePackage) BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) ASTNode(com.intellij.lang.ASTNode) LabelRuleLookupElement(com.google.idea.blaze.base.lang.buildfile.completion.LabelRuleLookupElement) BuildLookupElement(com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement) PsiReferenceBase(com.intellij.psi.PsiReferenceBase) Label(com.google.idea.blaze.base.model.primitives.Label) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) BlazeFileType(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile.BlazeFileType) Argument(com.google.idea.blaze.base.lang.buildfile.psi.Argument) LoadStatement(com.google.idea.blaze.base.lang.buildfile.psi.LoadStatement) Nullable(javax.annotation.Nullable) ResolveUtil(com.google.idea.blaze.base.lang.buildfile.search.ResolveUtil) VirtualFileFilter(com.intellij.openapi.vfs.VirtualFileFilter) BlazePackage(com.google.idea.blaze.base.lang.buildfile.search.BlazePackage)

Aggregations

BuildLookupElement (com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement)2 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 PsiFile (com.intellij.psi.PsiFile)2 FileOperationProvider (com.google.idea.blaze.base.io.FileOperationProvider)1 LabelRuleLookupElement (com.google.idea.blaze.base.lang.buildfile.completion.LabelRuleLookupElement)1 Argument (com.google.idea.blaze.base.lang.buildfile.psi.Argument)1 BlazeFileType (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile.BlazeFileType)1 FuncallExpression (com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression)1 LoadStatement (com.google.idea.blaze.base.lang.buildfile.psi.LoadStatement)1 StringLiteral (com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral)1 PsiUtils (com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils)1 BlazePackage (com.google.idea.blaze.base.lang.buildfile.search.BlazePackage)1 ResolveUtil (com.google.idea.blaze.base.lang.buildfile.search.ResolveUtil)1 Label (com.google.idea.blaze.base.model.primitives.Label)1 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 VirtualFileFilter (com.intellij.openapi.vfs.VirtualFileFilter)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReferenceBase (com.intellij.psi.PsiReferenceBase)1