From 4052c50406243079873abf2f16dfba609b8c4927 Mon Sep 17 00:00:00 2001 From: Saadat Su <66662058+sultanalieva-s@users.noreply.github.com> Date: Tue, 27 Jun 2023 01:31:08 +0500 Subject: [PATCH] Code completion plugin: Add Java SDK Transform Completions (Hard Coded) (#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 --- .../main/java/BeamCompletionContributor.java | 18 ++++++--- .../BeamCompletionContributorTestCase.java | 35 ++++++------------ .../src/test/testData/TestCompletions.java | 2 +- .../testData/TestCompletionsWrongClass.java | 36 ------------------ .../testData/TestCompletionsWrongMethod.java | 37 ------------------- 5 files changed, 26 insertions(+), 102 deletions(-) delete mode 100644 plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongClass.java delete mode 100644 plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongMethod.java diff --git a/plugins/beam-code-completion-plugin/src/main/java/BeamCompletionContributor.java b/plugins/beam-code-completion-plugin/src/main/java/BeamCompletionContributor.java index 579aa750fd2f..348668e7df79 100644 --- a/plugins/beam-code-completion-plugin/src/main/java/BeamCompletionContributor.java +++ b/plugins/beam-code-completion-plugin/src/main/java/BeamCompletionContributor.java @@ -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. @@ -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)); + } } } } diff --git a/plugins/beam-code-completion-plugin/src/test/java/BeamCompletionContributorTestCase.java b/plugins/beam-code-completion-plugin/src/test/java/BeamCompletionContributorTestCase.java index 8237238755d0..95a756ae3749 100644 --- a/plugins/beam-code-completion-plugin/src/test/java/BeamCompletionContributorTestCase.java +++ b/plugins/beam-code-completion-plugin/src/test/java/BeamCompletionContributorTestCase.java @@ -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() { @@ -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 lookupElementStrings = myFixture.getLookupElementStrings(); - assertNotNull(lookupElementStrings); - assertEquals(lookupElementStrings.get(0), "TestCompletionElement"); - } - - public void testElementPatternWrongClass() { - myFixture.configureByFile("TestCompletionsWrongClass.java"); - myFixture.complete(CompletionType.BASIC); - List lookupElementStrings = myFixture.getLookupElementStrings(); - assertNotNull(lookupElementStrings); - assertNotSame(lookupElementStrings.get(0), "TestCompletionElement"); - } - - public void testElementPatternWrongMethod() { - myFixture.configureByFile("TestCompletionsWrongMethod.java"); - myFixture.complete(CompletionType.BASIC); - List 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; } } diff --git a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletions.java b/plugins/beam-code-completion-plugin/src/test/testData/TestCompletions.java index c211f12c8a29..17274abd3b01 100644 --- a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletions.java +++ b/plugins/beam-code-completion-plugin/src/test/testData/TestCompletions.java @@ -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); + p.apply(Fi); } } diff --git a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongClass.java b/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongClass.java deleted file mode 100644 index 5f882b3d5b3e..000000000000 --- a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongClass.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.util.Arrays; -import org.apache.beam.sdk.Pipeline; -import org.apache.beam.sdk.io.TextIO; -import org.apache.beam.sdk.options.PipelineOptions; -import org.apache.beam.sdk.options.PipelineOptionsFactory; -import org.apache.beam.sdk.transforms.Count; -import org.apache.beam.sdk.transforms.Filter; -import org.apache.beam.sdk.transforms.FlatMapElements; -import org.apache.beam.sdk.transforms.MapElements; -import org.apache.beam.sdk.values.KV; -import org.apache.beam.sdk.values.TypeDescriptors; - -public class TestCompletionsWrongClass { - public static void main(String[] args) { - String test = "Test"; - test.apply(T); - } -} diff --git a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongMethod.java b/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongMethod.java deleted file mode 100644 index 74feeaa9ded6..000000000000 --- a/plugins/beam-code-completion-plugin/src/test/testData/TestCompletionsWrongMethod.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - -import java.util.Arrays; -import org.apache.beam.sdk.Pipeline; -import org.apache.beam.sdk.io.TextIO; -import org.apache.beam.sdk.options.PipelineOptions; -import org.apache.beam.sdk.options.PipelineOptionsFactory; -import org.apache.beam.sdk.transforms.Count; -import org.apache.beam.sdk.transforms.Filter; -import org.apache.beam.sdk.transforms.FlatMapElements; -import org.apache.beam.sdk.transforms.MapElements; -import org.apache.beam.sdk.values.KV; -import org.apache.beam.sdk.values.TypeDescriptors; - -public class TestCompletions { - public static void main(String[] args) { - PipelineOptions options = PipelineOptionsFactory.create(); - Pipeline p = Pipeline.create(o); - } -} -