Skip to content

Commit

Permalink
fix: Do not complete ignored directories
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebarrau committed Oct 31, 2023
1 parent 2ed261e commit a53d909
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.idea.blaze.base.model.primitives.TargetName;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.sync.projectview.ImportRoots;
import com.google.idea.blaze.base.sync.workspace.WorkspaceHelper;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverProvider;
Expand Down Expand Up @@ -53,8 +54,13 @@ public static BuildReferenceManager getInstance(Project project) {

private final Project project;

private IgnoredDirectories ignoredDirectories;

public BuildReferenceManager(Project project) {
this.project = project;
if (ImportRoots.forProjectSafe(project) != null) {
this.ignoredDirectories = new IgnoredDirectories(ImportRoots.forProjectSafe(project));
}
}

/** Finds the PSI element associated with the given label. */
Expand Down Expand Up @@ -158,6 +164,7 @@ public BuildLookupElement[] resolvePackageLookupElements(FileLookupData lookupDa
if (vf == null || !vf.isDirectory()) {
return BuildLookupElement.EMPTY_ARRAY;
}

BuildLookupElement[] uniqueLookup = new BuildLookupElement[1];
while (true) {
VirtualFile[] children = vf.getChildren();
Expand All @@ -167,7 +174,9 @@ public BuildLookupElement[] resolvePackageLookupElements(FileLookupData lookupDa
List<VirtualFile> validChildren = Lists.newArrayListWithCapacity(children.length);
for (VirtualFile child : children) {
ProgressManager.checkCanceled();
if (child.getName().startsWith(pathFragment) && lookupData.acceptFile(project, child)) {
if (child.getName().startsWith(pathFragment)
&& lookupData.acceptFile(project, child)
&& (ignoredDirectories == null || !ignoredDirectories.shouldIgnore(child))) {
validChildren.add(child);
}
}
Expand Down Expand Up @@ -256,4 +265,21 @@ private File resolveParentDirectory(WorkspacePath packagePath, TargetName target
String rulePathParent = PathUtil.getParentPath(targetName.toString());
return new File(packageFile, rulePathParent);
}

private class IgnoredDirectories {
private ImportRoots importRoots;

public IgnoredDirectories(ImportRoots importRoots) {
this.importRoots = importRoots;
}

public Boolean shouldIgnore(VirtualFile vf) {
for (WorkspacePath excludeDirectory : importRoots.excludeDirectories()) {
if (vf.getName().startsWith(excludeDirectory.relativePath())) {
return true;
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static FileLookupData packageLocalFileLookup(
private final QuoteType quoteType;
@Nullable private final VirtualFileFilter fileFilter;

private FileLookupData(
protected FileLookupData(
String originalLabel,
@Nullable BuildFile containingFile,
@Nullable String containingPackage,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.google.idea.blaze.base.lang.buildfile.references;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.base.Joiner;
import com.google.idea.blaze.base.MockProjectViewManager;
import com.google.idea.blaze.base.lang.buildfile.BuildFileIntegrationTestCase;
import com.google.idea.blaze.base.lang.buildfile.completion.BuildLookupElement;
import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.projectview.parser.ProjectViewParser;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Arrays;
import java.util.List;

@RunWith(JUnit4.class)
public class BuildReferenceManagerTest extends BuildFileIntegrationTestCase {
private MockProjectViewManager projectViewManager;

@Test
public void testResolvePackageLookupElementsIgnoresIgnoredDirectories() {
projectViewManager = new MockProjectViewManager(getProject());
setProjectView("directories:", " .", "derive_targets_from_directories: true");

workspace.createDirectory(new WorkspacePath(".ijwb"));
workspace.createDirectory(new WorkspacePath("src"));
workspace.createDirectory(new WorkspacePath("src/go"));
workspace.createFile(new WorkspacePath("src/go/BUILD.bazel"), "");
workspace.createFile(new WorkspacePath("src/go/main.go"), "package main\n");
BuildFile topTelevelBuildFile = createBuildFile(new WorkspacePath("BUILD.bazel"), "");

BuildReferenceManager manager = new BuildReferenceManager(getProject());
FileLookupData nonLocalLookupData =
FileLookupData.nonLocalFileLookup(
"//", topTelevelBuildFile, QuoteType.NoQuotes, FileLookupData.PathFormat.NonLocal);
assertThat(nonLocalLookupData).isNotNull();
List<String> results =
Arrays.stream(manager.resolvePackageLookupElements(nonLocalLookupData))
.map(BuildLookupElement::getLookupString)
.toList();
assertThat(results).containsAllIn(new String[] {"//src/go"});
assertThat(results).containsNoneIn(new String[] {".ijwb"});

String originalLabel = "//:";
Label packageLabel = topTelevelBuildFile.getPackageLabel();
assertThat(packageLabel).isNotNull();
String basePackagePath = packageLabel.blazePackage().relativePath();
String filePath = basePackagePath + "/" + LabelUtils.getRuleComponent(originalLabel);
FileLookupData localLookupData =
new FileLookupData(
originalLabel,
topTelevelBuildFile,
basePackagePath,
filePath,
FileLookupData.PathFormat.PackageLocal,
QuoteType.NoQuotes,
null);
List<String> otherResults =
Arrays.stream(manager.resolvePackageLookupElements(localLookupData))
.map(BuildLookupElement::getLookupString)
.toList();
assertThat(otherResults).containsAllIn(new String[] {"//:src"});
assertThat(otherResults).containsNoneIn(new String[] {":.ijwb"});
}

protected void setProjectView(String... contents) {
BlazeContext context = BlazeContext.create();
ProjectViewParser projectViewParser =
new ProjectViewParser(context, new WorkspacePathResolverImpl(workspaceRoot));
projectViewParser.parseProjectView(Joiner.on("\n").join(contents));

ProjectViewSet result = projectViewParser.getResult();
assertThat(result.getProjectViewFiles()).isNotEmpty();
projectViewManager.setProjectView(result);
}
}

0 comments on commit a53d909

Please sign in to comment.