You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by pa...@apache.org on 2023/06/26 20:31:17 UTC

[beam] branch master updated: Code completion plugin: Add Java SDK Transform Completions (Hard Coded) (#27168)

This is an automated email from the ASF dual-hosted git repository.

pabloem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 4052c504062 Code completion plugin: Add Java SDK Transform Completions (Hard Coded) (#27168)
4052c504062 is described below

commit 4052c50406243079873abf2f16dfba609b8c4927
Author: Saadat Su <66...@users.noreply.github.com>
AuthorDate: Tue Jun 27 01:31:08 2023 +0500

    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 <pa...@users.noreply.github.com>
---
 .../src/main/java/BeamCompletionContributor.java   | 18 ++++++++---
 .../java/BeamCompletionContributorTestCase.java    | 35 +++++++-------------
 .../src/test/testData/TestCompletions.java         |  2 +-
 .../test/testData/TestCompletionsWrongClass.java   | 36 ---------------------
 .../test/testData/TestCompletionsWrongMethod.java  | 37 ----------------------
 5 files changed, 26 insertions(+), 102 deletions(-)

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 579aa750fd2..348668e7df7 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 @@ import org.jetbrains.annotations.NotNull;
  * 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 class BeamCompletionContributor extends CompletionContributor {
                     );
 
     /**
-     * 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 8237238755d..95a756ae374 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 @@ public class BeamCompletionContributorTestCase extends LightJavaCodeInsightFixtu
         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;
     }
 }
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 c211f12c8a2..17274abd3b0 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<caret>);
+        p.apply(Fi<caret>);
     }
 }
 
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 5f882b3d5b3..00000000000
--- 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<caret>);
-    }
-}
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 74feeaa9ded..00000000000
--- 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<caret>);
-    }
-}
-