forked from bazelbuild/intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bazelbuild#6664]: Enable External Workspace completions and tests
Also add a CharFilter impl that allows `@` to filter the lookup elements and not hide the lookup Other issues: bazelbuild#505
- Loading branch information
Showing
11 changed files
with
280 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
base/src/com/google/idea/blaze/base/lang/buildfile/completion/BuildLabelCharFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024 The Bazel Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.idea.blaze.base.lang.buildfile.completion; | ||
|
||
import com.google.idea.blaze.base.lang.buildfile.language.BuildFileLanguage; | ||
import com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral; | ||
import com.intellij.codeInsight.lookup.CharFilter; | ||
import com.intellij.codeInsight.lookup.Lookup; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** Allows '@' to be typed (and appended to completion) inside a label from a build file. */ | ||
public final class BuildLabelCharFilter extends CharFilter { | ||
@Nullable | ||
public CharFilter.Result acceptChar(char c, int prefixLength, @NotNull Lookup lookup) { | ||
if ('@' == c && lookup.isCompletion()) { | ||
|
||
// check that we are in a proper place to complete. Unfortunately the CharFilter extension points are not | ||
// langauge and / or location aware so we need the below hack to | ||
PsiFile file = lookup.getPsiFile(); | ||
if (file != null && | ||
file.getLanguage() == BuildFileLanguage.INSTANCE && | ||
PsiTreeUtil.getParentOfType(lookup.getPsiElement(), StringLiteral.class) != null) { | ||
return Result.ADD_TO_PREFIX; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
.../com/google/idea/blaze/base/lang/buildfile/completion/ExternalWorkspaceLookupElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.google.idea.blaze.base.lang.buildfile.completion; | ||
|
||
import com.google.idea.blaze.base.lang.buildfile.psi.StringLiteral; | ||
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.openapi.editor.Document; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.PlatformIcons; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import javax.swing.*; | ||
|
||
public class ExternalWorkspaceLookupElement extends BuildLookupElement { | ||
private final ExternalWorkspace workspace; | ||
|
||
public ExternalWorkspaceLookupElement(ExternalWorkspace workspace) { | ||
super('@' + workspace.repoName(), QuoteType.NoQuotes); | ||
this.workspace = workspace; | ||
} | ||
|
||
@Override | ||
public String getLookupString() { | ||
return super.getItemText() + "//"; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
protected String getTypeText() { | ||
return !workspace.repoName().equals(workspace.name()) ? '@' + workspace.name() : null; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
protected String getTailText() { | ||
return "//"; | ||
} | ||
|
||
@Override | ||
public @Nullable Icon getIcon() { | ||
return PlatformIcons.LIBRARY_ICON; | ||
} | ||
|
||
@Override | ||
public void handleInsert(InsertionContext context) { | ||
StringLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), StringLiteral.class, false); | ||
if (literal == null) { | ||
super.handleInsert(context); | ||
return; | ||
} | ||
|
||
Document document = context.getDocument(); | ||
context.commitDocument(); | ||
// find an remove trailing package path after insert / replace. | ||
// current element text looks like `@workspace//`. If this is complete inside an existing workspace name the | ||
// result would look like: @workspace//old_workspace_path//. The following bit will remove `old_workspace_path//` | ||
int indexOfPackageStart = literal.getText().lastIndexOf("//"); | ||
if (indexOfPackageStart != -1) { | ||
// shift to be a document offset | ||
indexOfPackageStart += literal.getTextOffset() + 2; | ||
if (context.getSelectionEndOffset() < indexOfPackageStart) { | ||
document.deleteString(context.getSelectionEndOffset(), indexOfPackageStart); | ||
context.commitDocument(); | ||
} | ||
} | ||
|
||
// handle auto insertion of end quote. This will have to be if the completion is triggered inside a `"@<caret` bit | ||
if (insertClosingQuotes()) { | ||
QuoteType quoteType = literal.getQuoteType(); | ||
if (quoteType != QuoteType.NoQuotes && !literal.getText().endsWith(quoteType.quoteString)) { | ||
document.insertString(literal.getTextOffset() + literal.getTextLength(), quoteType.quoteString); | ||
context.commitDocument(); | ||
} | ||
} | ||
|
||
super.handleInsert(context); | ||
} | ||
|
||
@Override | ||
public boolean requiresCommittedDocuments() { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.