-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#6664]: Enable External Workspace completions and tests
Also add a CharFilter that enable: * '@' to filter the lookup elements and not hide the lookup. * '/' to autocomplete selected item Other issues: #505
- Loading branch information
Showing
11 changed files
with
324 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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
/* | ||
* 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 (!lookup.isCompletion()) { | ||
return null; | ||
} | ||
|
||
PsiFile file = lookup.getPsiFile(); | ||
if (file == null || | ||
file.getLanguage() != BuildFileLanguage.INSTANCE || | ||
PsiTreeUtil.getParentOfType(lookup.getPsiElement(), StringLiteral.class) == null) { | ||
return null; | ||
} | ||
|
||
switch (c) { | ||
case '@': | ||
return Result.ADD_TO_PREFIX; | ||
|
||
case '/': | ||
if (lookup.getCurrentItem() instanceof ExternalWorkspaceLookupElement) { | ||
return Result.SELECT_ITEM_AND_FINISH_LOOKUP; | ||
} | ||
} | ||
|
||
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
100 changes: 100 additions & 0 deletions
100
.../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,100 @@ | ||
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.codeInsight.lookup.AutoCompletionPolicy; | ||
import com.intellij.openapi.editor.CaretModel; | ||
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(); | ||
|
||
// if we completed by pressing '/' (since this lookup element should never complete using '/') . | ||
if (context.getCompletionChar() == '/') { | ||
context.setAddCompletionChar(false); | ||
} | ||
|
||
CaretModel caret = context.getEditor().getCaretModel(); | ||
// 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<caret>old_workspace_path//. The following bit will remove `old_workspace_path//` | ||
int replaceStart = context.getTailOffset(); | ||
int replaceEnd = context.getTailOffset(); | ||
|
||
int indexOfPackageStart = literal.getText().lastIndexOf("//"); | ||
if (indexOfPackageStart != -1) { | ||
// shift to be a document offset | ||
replaceEnd = indexOfPackageStart + 2 + literal.getTextOffset(); | ||
} | ||
|
||
document.replaceString(replaceStart, replaceEnd, "//"); | ||
context.commitDocument(); | ||
caret.moveToOffset(replaceStart + 2); | ||
|
||
// 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; | ||
} | ||
|
||
@Override | ||
public AutoCompletionPolicy getAutoCompletionPolicy() { | ||
return AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE; | ||
} | ||
} |
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.