-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable External Workspace completions and tests
- Loading branch information
Showing
7 changed files
with
231 additions
and
21 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
84 changes: 84 additions & 0 deletions
84
.../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,84 @@ | ||
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.PsiElement; | ||
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.repositoryName(), QuoteType.NoQuotes); | ||
this.workspace = workspace; | ||
} | ||
|
||
@Override | ||
public String getLookupString() { | ||
return super.getItemText() + "//"; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
protected String getTypeText() { | ||
return !workspace.repositoryName().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) { | ||
Document document = context.getDocument(); | ||
|
||
StringLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), StringLiteral.class, false); | ||
if (literal == null) { | ||
super.handleInsert(context); | ||
return; | ||
} | ||
|
||
// 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
89 changes: 89 additions & 0 deletions
89
...com/google/idea/blaze/base/lang/buildfile/completion/ExternalWorkspaceCompletionTest.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,89 @@ | ||
package com.google.idea.blaze.base.lang.buildfile.completion; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.idea.blaze.base.ExternalWorkspaceFixture; | ||
import com.google.idea.blaze.base.lang.buildfile.BuildFileIntegrationTestCase; | ||
import com.google.idea.blaze.base.lang.buildfile.psi.BuildFile; | ||
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.WorkspacePath; | ||
import com.intellij.codeInsight.CodeInsightSettings; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.psi.PsiFile; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class ExternalWorkspaceCompletionTest extends BuildFileIntegrationTestCase { | ||
protected ExternalWorkspaceFixture workspaceOne; | ||
protected ExternalWorkspaceFixture workspaceTwoMapped; | ||
|
||
@Override | ||
protected ExternalWorkspaceData mockExternalWorkspaceData() { | ||
workspaceOne = new ExternalWorkspaceFixture( | ||
ExternalWorkspace.create("workspace_one", "workspace_one"), fileSystem); | ||
|
||
workspaceTwoMapped = new ExternalWorkspaceFixture( | ||
ExternalWorkspace.create("workspace_two", "com_workspace_two"), fileSystem); | ||
|
||
return ExternalWorkspaceData.create( | ||
ImmutableList.of(workspaceOne.w, workspaceTwoMapped.w)); | ||
} | ||
|
||
@Test | ||
public void testEmptyLabelCompletion() throws Throwable { | ||
PsiFile file = testFixture.configureByText("BUILD", "'<caret>'"); | ||
|
||
String[] strings = editorTest.getCompletionItemsAsStrings(); | ||
assertThat(strings).hasLength(2); | ||
assertThat(strings) | ||
.asList() | ||
.containsExactly("@com_workspace_two//", "@workspace_one//"); | ||
} | ||
|
||
@Test | ||
public void testCompleteWillIncludeSlashes () { | ||
PsiFile file = testFixture.configureByText("BUILD", "'@com<caret>'"); | ||
assertThat(editorTest.completeIfUnique()).isTrue(); | ||
|
||
assertFileContents(file, "'@com_workspace_two//'"); | ||
} | ||
|
||
@Test | ||
public void testCompleteWillFixUpRemainingSlashed () { | ||
PsiFile file = testFixture.configureByText("BUILD", "'@com<caret>//'"); | ||
assertThat(editorTest.completeIfUnique()).isTrue(); | ||
|
||
assertFileContents(file, "'@com_workspace_two//'"); | ||
} | ||
|
||
@Test | ||
public void testCompleteWillAlwaysReplaceWorkspace() { | ||
PsiFile file = testFixture.configureByText("BUILD", "'@com<caret>xxxx//'"); | ||
assertThat(editorTest.completeIfUnique()).isTrue(); | ||
|
||
assertFileContents(file, "'@com_workspace_two//'"); | ||
} | ||
|
||
@Test | ||
public void testCompleteWillRespectAutoQuoting() { | ||
boolean old = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE; | ||
try { | ||
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE = false; | ||
PsiFile file = testFixture.configureByText("BUILD", "'@com<caret>"); | ||
|
||
assertThat(editorTest.completeIfUnique()).isTrue(); | ||
assertFileContents(file, "'@com_workspace_two//"); | ||
|
||
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE = true; | ||
file = testFixture.configureByText("BUILD", "'@com<caret>"); | ||
|
||
assertThat(editorTest.completeIfUnique()).isTrue(); | ||
assertFileContents(file, "'@com_workspace_two//'"); | ||
|
||
} finally { | ||
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE = old; | ||
} | ||
} | ||
} |
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