Skip to content

Commit

Permalink
[2020.3 compat] Add except Throwable to runInEdtAndWait method invoca…
Browse files Browse the repository at this point in the history
…tions

this method allows an arbitrary runnable to be added to IntelliJ's event dispatch thread (EDT). [This Commit](JetBrains/intellij-community@370c52b) introduced a refactor where exception invocations from the thread are now visible in the method signature.

* Why not a more specific exception: when running in the EDT, a lot of different exceptions may happen. Hence, even if the runnable is opening a file, it is difficult to narrow down the exception to just Exception types like FileNotFound.

relevant commit: JetBrains/intellij-community@c04bcf7

PiperOrigin-RevId: 361875570
  • Loading branch information
AlexeyGy authored and copybara-github committed Mar 9, 2021
1 parent 96e50d8 commit 1dc2a70
Show file tree
Hide file tree
Showing 48 changed files with 592 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
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.primitives.WorkspacePath;
import com.google.idea.sdkcompat.testframework.fixtures.CompletionAutoPopupTesterAdapter;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.editor.Editor;
import com.intellij.testFramework.fixtures.CompletionAutoPopupTester;
import com.intellij.util.ThrowableRunnable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,11 +33,11 @@
@RunWith(JUnit4.class)
public class ArgumentCompletionContributorTest extends BuildFileIntegrationTestCase {

private CompletionAutoPopupTester completionTester;
private CompletionAutoPopupTesterAdapter completionTester;

@Before
public final void before() {
completionTester = new CompletionAutoPopupTester(testFixture);
completionTester = new CompletionAutoPopupTesterAdapter(testFixture);
}

/** Completion UI testing can't be run on the EDT. */
Expand All @@ -47,64 +48,72 @@ protected boolean runTestsOnEdt() {

@Test
public void testIncompleteFuncall() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(de");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(de".length());

completionTester.typeWithPauses("p");
LookupElement[] completionItems = testFixture.completeBasic();
assertThat(completionItems).isNull();

assertFileContents(
file, "def function(name, deps, srcs):", " # empty function", "function(deps");
});
(ThrowableRunnable<Throwable>)
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(de");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(de".length());

completionTester.typeWithPauses("p");
LookupElement[] completionItems = testFixture.completeBasic();
assertThat(completionItems).isNull();

assertFileContents(
file, "def function(name, deps, srcs):", " # empty function", "function(deps");
});
}

@Test
public void testExistingKeywordArg() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(name = \"lib\")");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(".length());

completionTester.joinAutopopup();
completionTester.joinCompletion();
String[] completionItems = editorTest.getCompletionItemsAsStrings();
assertThat(completionItems).asList().containsAllOf("name", "deps", "srcs", "function");
});
(ThrowableRunnable<Throwable>)
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(name = \"lib\")");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(".length());

completionTester.joinAutopopup();
completionTester.joinCompletion();
String[] completionItems = editorTest.getCompletionItemsAsStrings();
assertThat(completionItems)
.asList()
.containsAllOf("name", "deps", "srcs", "function");
});
}

@Test
public void testNoArgumentCompletionInComment() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(#");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(#".length());

completionTester.typeWithPauses("n");
assertThat(testFixture.getLookup()).isNull();
});
(ThrowableRunnable<Throwable>)
() -> {
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"def function(name, deps, srcs):",
" # empty function",
"function(#");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, "function(#".length());

completionTester.typeWithPauses("n");
assertThat(testFixture.getLookup()).isNull();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
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.primitives.WorkspacePath;
import com.google.idea.sdkcompat.testframework.fixtures.CompletionAutoPopupTesterAdapter;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.editor.Editor;
import com.intellij.testFramework.fixtures.CompletionAutoPopupTester;
import com.intellij.util.ThrowableRunnable;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Before;
Expand All @@ -36,11 +37,11 @@
@RunWith(JUnit4.class)
public class BuildFileAutoCompletionTest extends BuildFileIntegrationTestCase {

private CompletionAutoPopupTester completionTester;
private CompletionAutoPopupTesterAdapter completionTester;

@Before
public final void before() {
completionTester = new CompletionAutoPopupTester(testFixture);
completionTester = new CompletionAutoPopupTesterAdapter(testFixture);
}

/** Completion UI testing can't be run on the EDT. */
Expand All @@ -50,33 +51,33 @@ protected boolean runTestsOnEdt() {
}

@Test
public void testNoPopupAfterNumber() {
public void testNoPopupAfterNumber() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"));
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" testonly = ");
(ThrowableRunnable<Throwable>)
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"));
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" testonly = ");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " testonly = ".length());
Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " testonly = ".length());

completionTester.typeWithPauses("1");
assertThat(currentLookupStrings()).isEmpty();
});
completionTester.typeWithPauses("1");
assertThat(currentLookupStrings()).isEmpty();
});
}

private List<String> currentLookupStrings() {
LookupImpl lookup = completionTester.getLookup();
if (lookup == null) {
return ImmutableList.of();
}
return lookup
.getItems()
.stream()
return lookup.getItems().stream()
.map(LookupElement::getLookupString)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
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.primitives.WorkspacePath;
import com.google.idea.sdkcompat.testframework.fixtures.CompletionAutoPopupTesterAdapter;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.editor.Editor;
import com.intellij.testFramework.fixtures.CompletionAutoPopupTester;
import com.intellij.util.ThrowableRunnable;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Before;
Expand All @@ -36,11 +37,11 @@
@RunWith(JUnit4.class)
public class BuildLabelAutoCompletionTest extends BuildFileIntegrationTestCase {

private CompletionAutoPopupTester completionTester;
private CompletionAutoPopupTesterAdapter completionTester;

@Before
public final void before() {
completionTester = new CompletionAutoPopupTester(testFixture);
completionTester = new CompletionAutoPopupTesterAdapter(testFixture);
}

/** Completion UI testing can't be run on the EDT. */
Expand All @@ -50,74 +51,79 @@ protected boolean runTestsOnEdt() {
}

@Test
public void testPopupAutocompleteAfterSlash() {
public void testPopupAutocompleteAfterSlash() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"));
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = [''],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['".length());

completionTester.typeWithPauses("/");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo'");
});
(ThrowableRunnable<Throwable>)
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"));
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = [''],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['".length());

completionTester.typeWithPauses("/");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo'");
});
}

@Test
public void testPopupAutocompleteAfterColon() {
public void testPopupAutocompleteAfterColon() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"), "java_library(name = 'target')");
BuildFile file =
(ThrowableRunnable<Throwable>)
() -> {
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = ['//java/com/foo'],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['//java/com/foo".length());

completionTester.typeWithPauses(":");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo:target'");
});
new WorkspacePath("java/com/foo/BUILD"), "java_library(name = 'target')");
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = ['//java/com/foo'],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['//java/com/foo".length());

completionTester.typeWithPauses(":");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo:target'");
});
}

@Test
public void testPopupAutocompleteAfterLetter() {
// test for IntelliJ's standard autocomplete popup trigger
public void testPopupAutocompleteAfterLetter() throws Throwable {
// #api202: remove redundant cast "(ThrowableRunnable<Throwable>)"
completionTester.runWithAutoPopupEnabled(
() -> {
createBuildFile(new WorkspacePath("java/com/foo/BUILD"), "java_library(name = 'target')");
BuildFile file =
(ThrowableRunnable<Throwable>)
() -> {
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = ['//'],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['//".length());

completionTester.typeWithPauses("j");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo'");
});
new WorkspacePath("java/com/foo/BUILD"), "java_library(name = 'target')");
BuildFile file =
createBuildFile(
new WorkspacePath("BUILD"),
"java_library(",
" name = 'lib',",
" srcs = ['//'],");

Editor editor = editorTest.openFileInEditor(file.getVirtualFile());
editorTest.setCaretPosition(editor, 2, " srcs = ['//".length());

completionTester.typeWithPauses("j");
assertThat(currentLookupStrings()).containsExactly("'//java/com/foo'");
});
}

private List<String> currentLookupStrings() {
LookupImpl lookup = completionTester.getLookup();
if (lookup == null) {
return ImmutableList.of();
}
return lookup
.getItems()
.stream()
return lookup.getItems().stream()
.map(LookupElement::getLookupString)
.collect(Collectors.toList());
}
Expand Down
Loading

0 comments on commit 1dc2a70

Please sign in to comment.