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);
}
}
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;
}
Aggregations