Skip to content

Commit

Permalink
[bazelbuild#6665] Rest - 3/n
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoader committed Aug 28, 2024
1 parent 17c26da commit 55299d0
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 14 deletions.
17 changes: 9 additions & 8 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions base/src/META-INF/blaze-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@
<codeInsight.lineMarkerProvider
language="BUILD"
implementationClass="com.google.idea.blaze.base.query.MacroLineMarkerProvider"/>
<lookup.charFilter implementation="com.google.idea.blaze.base.lang.buildfile.references.ExternalWorkspaceReferenceFragment$CharFilter"/>
<runLineMarkerContributor
language="BUILD"
implementationClass="com.google.idea.blaze.base.run.producers.BuildFileRunLineMarkerContributor"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.google.idea.blaze.base.lang.buildfile.completion;

import com.google.idea.blaze.base.lang.buildfile.references.QuoteType;
import com.google.idea.blaze.base.model.primitives.ExternalWorkspace;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.AutoCompletionPolicy;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class WorkspaceLookupElement extends BuildLookupElement {

final ExternalWorkspace workspace;
public WorkspaceLookupElement(ExternalWorkspace workspace) {
super(workspace.repoName(), QuoteType.NoQuotes);
this.workspace = workspace;
}

@Override
public String getLookupString() {
if (workspace.repoName() == null || workspace.repoName().isEmpty()) {
return "@" + workspace.name();
}

return "@" + workspace.repoName();
}

@Override
protected @Nullable String getTypeText() {
return workspace.name();
}

@Override
protected @Nullable String getTailText() {
return "//";
}

@Override
public @Nullable Icon getIcon() {
return PlatformIcons.IMPORT_ICON;
}

protected String getItemText() {
if (workspace.repoName() == null || workspace.repoName().isEmpty()) {
return "@" + workspace.name();
}

return "@" + workspace.repoName();
}

@Override
public void handleInsert(InsertionContext context) {
switch (context.getCompletionChar()) {
case Lookup.REPLACE_SELECT_CHAR:
case Lookup.NORMAL_SELECT_CHAR:
}
super.handleInsert(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,29 @@
*/
package com.google.idea.blaze.base.lang.buildfile.references;

import com.google.idea.blaze.base.lang.buildfile.completion.WorkspaceLookupElement;
import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile;
import com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression;
import com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral;
import com.google.idea.blaze.base.lang.buildfile.psi.util.PsiUtils;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.ExternalWorkspaceData;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

/** The external workspace component of a label (between '@' and '//') */
Expand All @@ -42,15 +50,18 @@ public ExternalWorkspaceReferenceFragment(LabelReference labelReference) {
@Override
public TextRange getRangeInElement() {
String rawText = myElement.getText();
boolean valid = LabelUtils.getExternalWorkspaceComponent(myElement.getStringContents()) != null;
String elementStringContents = myElement.getStringContents();
int packagePrefixIndex = rawText.indexOf("//");
boolean valid = elementStringContents.startsWith("@") || packagePrefixIndex != -1;
if (!valid) {
// we also want to check if the original element is empty since we want to show the package prefix
// variants at the time too
return TextRange.EMPTY_RANGE;
}
int endIndex = rawText.indexOf("//");
if (endIndex == -1) {
endIndex = rawText.length() - 1;
if (packagePrefixIndex == -1) {
packagePrefixIndex = rawText.length() - 1;
}
return new TextRange(1, endIndex);
return new TextRange(1, packagePrefixIndex);
}

@Nullable
Expand Down Expand Up @@ -84,6 +95,12 @@ private static BuildFile resolveProjectWorkspaceFile(Project project) {

@Override
public Object[] getVariants() {
BlazeProjectDataManager manager = BlazeProjectDataManager.getInstance(myElement.getProject());
BlazeProjectData blazeProjectData = manager.getBlazeProjectData();
if (blazeProjectData != null) {
ExternalWorkspaceData externalWorkspaceData = blazeProjectData.getExternalWorkspaceData();
return externalWorkspaceData.workspaces.values().stream().map(WorkspaceLookupElement::new).toArray();
}
return EMPTY_ARRAY;
}

Expand All @@ -109,4 +126,19 @@ private PsiElement handleRename(String newStringContents) {
PsiUtils.createNewLabel(myElement.getProject(), newStringContents));
return myElement;
}

public final static class CharFilter extends com.intellij.codeInsight.lookup.CharFilter {

public CharFilter() {}

@Nullable
public com.intellij.codeInsight.lookup.CharFilter.Result acceptChar(char c, int prefixLength, @NotNull Lookup lookup) {

if (lookup.isCompletion() && '@' == c && PsiTreeUtil.getParentOfType(lookup.getPsiElement(), StringLiteral.class) != null) {
return Result.ADD_TO_PREFIX;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral;
import com.google.idea.blaze.base.lang.buildfile.search.BlazePackage;
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;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.sync.workspace.WorkspaceHelper;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NullableLazyValue;
Expand All @@ -36,6 +38,7 @@
import icons.BlazeIcons;
import javax.annotation.Nullable;
import javax.swing.Icon;
import java.io.File;

/** The data relevant to finding file lookups. */
public class FileLookupData {
Expand Down Expand Up @@ -69,6 +72,25 @@ public static FileLookupData nonLocalFileLookup(
// it's a package-local reference
return null;
}

BuildFile buildFile = containingFile;
if (StringUtil.startsWith(originalLabel, "@") && containingFile != null) {
String externalWorkspace = LabelUtils.getExternalWorkspaceComponent(originalLabel);
if (externalWorkspace != null) {
File file = WorkspaceHelper.resolveBlazePackage(buildFile.getProject(), Label.create(externalWorkspace, WorkspacePath.createIfValid(""), TargetName.create("__pkg__")));
if (file != null) {
BuildFile bibi = BuildReferenceManager.getInstance(buildFile.getProject()).findBuildFile(file);
if (bibi != null) {
buildFile = bibi;
}
}

// BuildFile workspaceBuildFile = BuildReferenceManager.getInstance(containingFile.getProject()).resolveLabel(Label.create(originalLabel));
// if (workspaceBuildFile != null) {
// buildFile = workspaceBuildFile;
// }
}
}
// handle the single '/' case by calling twice.
String relativePath = StringUtil.trimStart(StringUtil.trimStart(originalLabel, "/"), "/");
if (relativePath.startsWith("/")) {
Expand All @@ -77,7 +99,7 @@ public static FileLookupData nonLocalFileLookup(
boolean onlyDirectories = pathFormat != PathFormat.NonLocalWithoutInitialBackslashes;
VirtualFileFilter filter = vf -> !onlyDirectories || vf.isDirectory();
return new FileLookupData(
originalLabel, containingFile, null, relativePath, pathFormat, quoteType, filter);
originalLabel, buildFile, null, relativePath, pathFormat, quoteType, filter);
}

@Nullable
Expand Down

0 comments on commit 55299d0

Please sign in to comment.