Skip to content

Commit

Permalink
Code completion plugin: Add Java SDK Transform Completions (Hard Code…
Browse files Browse the repository at this point in the history
…d) (apache#27168)

* [Code Completion Plugin] Define Element Pattern for java sdk

* Code Completion Plugin: Add java sdk transform completions

* Verifying that tests are running as expected :) Sorry!

* Update BeamCompletionContributorTestCase.java

---------

Co-authored-by: Pablo <[email protected]>
  • Loading branch information
sultanalieva-s and pabloem authored Jun 26, 2023
1 parent ed762d9 commit 4052c50
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
* suggestions specifically for Apache Beam pipelines.
*/
public class BeamCompletionContributor extends CompletionContributor {
public static final String[] beamJavaSDKTransforms = {
"Filter", "FlatMapElements", "Keys", "KvSwap", "MapElements", "ParDo",
"Partition", "Regex", "Reify", "ToString", "WithKeys", "WithTimestamps",
"Values", "ApproximateQuantiles", "ApproximateUnique", "CoGroupByKey", "Combine",
"CombineWithContext", "Count", "Distinct", "GroupByKey", "GroupIntoBatches",
"HllCount", "Latest", "Max", "Mean", "Min", "Sample", "Sum", "Top",
"Create", "Flatten", "PAssert", "View", "Window"
};

/**
* A pattern condition that matches method call expressions with the name "apply" in the context of the
* Apache Beam `Pipeline` class.
Expand Down Expand Up @@ -75,15 +84,14 @@ public boolean accepts(@NotNull PsiMethodCallExpression psiMethodCallExpression,
);

/**
* Fills the completion variants for the given completion parameters and result set.
* Fills the completion variants with Transforms from Java SDK.
*/
@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull CompletionResultSet result) {
final PsiElement position = parameters.getPosition();
PrefixMatcher matcher = result.getPrefixMatcher();
PsiElement parent = position.getParent();
if (AFTER_METHOD_CALL_PATTERN.accepts(parameters.getPosition())){
result.addElement(LookupElementBuilder.create("TestCompletionElement"));
for (String transform: beamJavaSDKTransforms) {
result.addElement(LookupElementBuilder.create(transform).appendTailText(" org.apache.beam.sdk.transforms", true));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;

import java.util.List;

// Each test method test a feature as a whole rather than each function or method.
public class BeamCompletionContributorTestCase extends LightJavaCodeInsightFixtureTestCase {
@Override
protected String getTestDataPath() {
Expand All @@ -34,27 +34,16 @@ protected String getTestDataPath() {
return new DefaultLightProjectDescriptor().withRepositoryLibrary("org.apache.beam:beam-sdks-java-core:2.48.0");
}

public void testElementPatternIsTriggered() {
public void testCompletionsSuccess() throws Throwable {
myFixture.configureByFile("TestCompletions.java");
myFixture.complete(CompletionType.BASIC);
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
assertNotNull(lookupElementStrings);
assertEquals(lookupElementStrings.get(0), "TestCompletionElement");
}

public void testElementPatternWrongClass() {
myFixture.configureByFile("TestCompletionsWrongClass.java");
myFixture.complete(CompletionType.BASIC);
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
assertNotNull(lookupElementStrings);
assertNotSame(lookupElementStrings.get(0), "TestCompletionElement");
}

public void testElementPatternWrongMethod() {
myFixture.configureByFile("TestCompletionsWrongMethod.java");
myFixture.complete(CompletionType.BASIC);
List<String> lookupElementStrings = myFixture.getLookupElementStrings();
assertNotNull(lookupElementStrings);
assertNotSame(lookupElementStrings.get(0), "TestCompletionElement");
LookupElement[] result = myFixture.completeBasic();
LookupElement scenarioOutlineLookupElement = null;
for (LookupElement lookupElement : result) {
if (lookupElement.getLookupString().equals("Filter")) {
scenarioOutlineLookupElement = lookupElement;
break;
}
}
assert scenarioOutlineLookupElement != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TestCompletions {
public static void main(String[] args) {
PipelineOptions options = PipelineOptionsFactory.create();
Pipeline p = Pipeline.create(options);
p.apply(T<caret>);
p.apply(Fi<caret>);
}
}

This file was deleted.

This file was deleted.

0 comments on commit 4052c50

Please sign in to comment.