Skip to content

Commit

Permalink
[bazelbuild#6664] Resolve a workspace label across external repositor…
Browse files Browse the repository at this point in the history
…ies - 4/n
  • Loading branch information
mtoader committed Aug 30, 2024
1 parent b738318 commit b82cfeb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

/** Converts a blaze label into an absolute path, then resolves that path to a PsiElements */
public class LabelReference extends PsiReferenceBase<StringLiteral> {

public LabelReference(StringLiteral element, boolean soft) {
super(element, new TextRange(0, element.getTextLength()), soft);
super(element, soft);
}

@Override
public @NotNull TextRange getRangeInElement() {
int index = myElement.getText().indexOf(':');
return TextRange.create(index + 1, myElement.getTextLength());
}

@Nullable
Expand All @@ -52,6 +61,7 @@ public PsiElement resolve() {
* - data file (.java, .txt, etc.)
* - glob contents
*/
// PsiElement references = myElement.getReferences()[2].resolve();
return resolveTarget(myElement.getStringContents());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import com.google.common.base.Functions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.devtools.intellij.model.ProjectData;
import com.google.idea.blaze.base.ideinfo.ProtoWrapper;
import com.google.idea.blaze.base.model.primitives.ExternalWorkspace;

import javax.annotation.Nullable;

public final class ExternalWorkspaceData implements ProtoWrapper<ProjectData.ExternalWorkspaceData> {
public ImmutableMap<String, ExternalWorkspace> workspaces;

Expand Down Expand Up @@ -43,4 +46,9 @@ public ProjectData.ExternalWorkspaceData toProto() {
public static ExternalWorkspaceData fromProto(ProjectData.ExternalWorkspaceData proto) {
return new ExternalWorkspaceData(proto.getWorkspacesList().stream().map(ExternalWorkspace::fromProto).collect(ImmutableList.toImmutableList()));
}

@Nullable
public ExternalWorkspace getByRepoName(String name) {
return Maps.filterValues(workspaces, w -> w.repoName() != null && w.repoName().equals(name)).values().stream().findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.idea.blaze.base.bazel.BuildSystemProvider;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.ExternalWorkspaceData;
import com.google.idea.blaze.base.model.primitives.ExternalWorkspace;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetName;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
Expand Down Expand Up @@ -169,8 +171,8 @@ private static Label deriveLabel(
TargetName.createIfValid(
FileUtil.getRelativePath(workspace.root.fileForPath(packagePath), file));
return targetName != null
? Label.create(workspace.externalWorkspaceName, packagePath, targetName)
: null;
? Label.create(workspace.externalWorkspaceName, packagePath, targetName)
: null;
}

private static WorkspacePath getPackagePath(
Expand All @@ -193,33 +195,41 @@ public static File getExternalSourceRoot(BlazeProjectData projectData) {

@Nullable
private static synchronized WorkspaceRoot getExternalWorkspaceRootsFile(String workspaceName,
Project project) {
Project project) {
if (Blaze.getBuildSystemName(project) == BuildSystemName.Blaze) {
return null;
}
logger.debug("getExternalWorkspaceRootsFile for " + workspaceName);
Map<String, WorkspaceRoot> workspaceRootCache = SyncCache.getInstance(project)
.get(WorkspaceHelper.class, (p, data) -> new ConcurrentHashMap<String, WorkspaceRoot>());
Map<String, WorkspaceRoot> workspaceRootCache =
SyncCache.getInstance(project)
.get(WorkspaceHelper.class, (p, data) -> new ConcurrentHashMap<>());

//the null cache value case could happen when the blazeProjectData is null.
if(workspaceRootCache == null) {
if (workspaceRootCache == null) {
return null;
}

WorkspaceRoot root = null;
if (workspaceRootCache.containsKey(workspaceName)) {
root = workspaceRootCache.get(workspaceName);
} else if (getBlazeProjectData(project) != null) {
File externalDir = new File(getBlazeProjectData(project).getBlazeInfo().getOutputBase(),
"external/" + workspaceName);
return workspaceRootCache.get(workspaceName);
}

BlazeProjectData blazeProjectData = getBlazeProjectData(project);
if (blazeProjectData != null) {
File workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspaceName);

if (externalDir.exists() || isInTestMode()) {
root = new WorkspaceRoot(externalDir);
ExternalWorkspace workspace = blazeProjectData.getExternalWorkspaceData().getByRepoName(workspaceName);
if (workspace != null) {
workspaceDir = new File(blazeProjectData.getBlazeInfo().getOutputBase(), "external/" + workspace.name());
}

if (workspaceDir.exists() || isInTestMode()) {
WorkspaceRoot root = new WorkspaceRoot(workspaceDir);
workspaceRootCache.put(workspaceName, root);
return root;
}
}

return root;
return null;
}

//The unit test use the TempFileSystem to create VirtualFile which does not exist on disk.
Expand Down

0 comments on commit b82cfeb

Please sign in to comment.