sdedic commented on a change in pull request #3025:
URL: https://github.com/apache/netbeans/pull/3025#discussion_r662349084



##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ProjectConfigurationCompletion.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of project configurations in launch.json.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 100)
+public class ProjectConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    private static final String CONFIG_TYPE = "java8+";     // NOI18N
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return projectSupplier.get().thenApply(p -> 
createConfigurationsCompletion(p));
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        if ("launchConfiguration".equals(attribute)) {      // NOI18N
+            return projectSupplier.get().thenApply(p -> 
createLaunchConfigCompletion(p));
+        } else {
+            return CompletableFuture.completedFuture(Collections.emptyList());
+        }
+    }
+
+    @NbBundle.Messages({"# {0} - Configuration name", 
"LBL_LaunchJavaConfig=Launch Java: {0}",
+                        "# {0} - Configuration name", 
"LBL_LaunchJavaConfig_desc=Launch a Java 8+ application using {0}."})
+    private static List<CompletionItem> createConfigurationsCompletion(Project 
p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();
+        List<CompletionItem> completionItems = new 
ArrayList<>(configurations.size() - 1);
+        boolean skipFirst = true;
+        for (ProjectConfiguration c : configurations) {
+            if (skipFirst) {
+                skipFirst = false;
+                continue;
+            }
+            String configDisplayName = c.getDisplayName();
+            String launchName = Bundle.LBL_LaunchJavaConfig(configDisplayName);
+            CompletionItem ci = new CompletionItem("Java 8+: " + launchName);  
 // NOI18N
+            StringWriter sw = new StringWriter();
+            try (JsonWriter w = new JsonWriter(sw)) {
+                w.setIndent("\t");                                          // 
NOI18N
+                w.beginObject();
+                w.name("name").value(launchName);                           // 
NOI18N
+                w.name("type").value(CONFIG_TYPE);                          // 
NOI18N
+                w.name("request").value("launch");                          // 
NOI18N
+                w.name("mainClass").value("${file}");                       // 
NOI18N
+                w.name("launchConfiguration").value(configDisplayName);     // 
NOI18N

Review comment:
       I wonder if we shouldn't finally add nonlocalizable IDs to the 
configuration API.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/AttachConfigurationCompletion.java
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.debugging.attach;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.eclipse.lsp4j.InsertTextFormat;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.java.lsp.server.Utils;
+import 
org.netbeans.modules.java.lsp.server.protocol.LaunchConfigurationCompletion;
+import org.openide.util.Exceptions;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of debugger attach configurations.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 200)
+public class AttachConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return CompletableFuture.supplyAsync(() -> {
+            return createCompletion(AttachConfigurations.get());
+        }, AttachConfigurations.RP);
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.supplyAsync(() -> {
+            return createAttributesCompletion(AttachConfigurations.get(), 
currentAttributes);
+        }, AttachConfigurations.RP);
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    private static List<CompletionItem> createCompletion(AttachConfigurations 
attachConfigurations) {
+        return 
attachConfigurations.getConfigurations().stream().map(configAttrs -> 
createCompletion(configAttrs)).collect(Collectors.toList());
+    }
+
+    private static CompletionItem createCompletion(ConfigurationAttributes 
configAttrs) {
+        CompletionItem ci = new CompletionItem("Java 8+: " + 
configAttrs.getName());    // NOI18N
+        StringWriter sw = new StringWriter();
+        try (JsonWriter w = new JsonWriter(sw)) {
+            w.setIndent("\t");                                              // 
NOI18N
+            w.beginObject();
+            w.name("name").value("${1:" + 
Utils.escapeCompletionSnippetSpecialChars(configAttrs.getName()) + "}"); // 
NOI18N
+            w.name("type").value(AttachConfigurations.CONFIG_TYPE);         // 
NOI18N
+            w.name("request").value(AttachConfigurations.CONFIG_REQUEST);   // 
NOI18N
+            int locationIndex = 2;
+            for (Map.Entry<String, ConfigurationAttribute> entry : 
configAttrs.getAttributes().entrySet()) {
+                ConfigurationAttribute ca = entry.getValue();
+                if (ca.isMustSpecify()) {
+                    String value = ca.getDefaultValue();
+                    if (value.startsWith("${command:")) { // Do not suggest to 
customize values provided by commands    // NOI18N
+                        value = 
Utils.escapeCompletionSnippetSpecialChars(encode2JSON(value));
+                    } else {
+                        value = "${" + (locationIndex++) + (value.isEmpty() ? 
"}" : ":" + Utils.escapeCompletionSnippetSpecialChars(encode2JSON(value)) + 
"}"); // NOI18N
+                    }
+                    // We have pre-encoded the value in order not to encode 
the completion snippet escape characters
+                    w.name(entry.getKey()).jsonValue("\"" + value + "\"");
+                }
+            }
+            w.endObject();
+            w.flush();
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        ci.setInsertText(sw.toString());
+        ci.setInsertTextFormat(InsertTextFormat.Snippet);
+        ci.setDocumentation(configAttrs.getDescription());
+        return ci;
+    }
+
+    private static String encode2JSON(String value) {
+        if (value.isEmpty()) {
+            return value;
+        }
+        StringWriter sw = new StringWriter();
+        try (JsonWriter w = new JsonWriter(sw)) {
+            w.beginArray();
+            w.value(value);
+            w.endArray();
+            w.flush();
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        String encoded = sw.toString();
+        // We have ["value"], remove the array and quotes
+        return encoded.substring(2, encoded.length() - 2);
+    }
+
+    private static List<CompletionItem> 
createAttributesCompletion(AttachConfigurations attachConfigurations, 
Map<String, Object> currentAttributes) {
+        List<CompletionItem> completionItems = null;
+        ConfigurationAttributes currentConfiguration = 
attachConfigurations.findConfiguration(currentAttributes);
+        if (currentConfiguration != null) {
+            Map<String, ConfigurationAttribute> attributes = 
currentConfiguration.getAttributes();
+            for (Map.Entry<String, ConfigurationAttribute> entry : 
attributes.entrySet()) {
+                String attrName = entry.getKey();
+                if (!currentAttributes.containsKey(attrName)) {
+                    StringWriter sw = new StringWriter();
+                    try (JsonWriter w = new JsonWriter(sw)) {
+                        w.beginObject();
+                        
w.name(attrName).value(entry.getValue().getDefaultValue());
+                        w.endObject();
+                        w.flush();
+                    } catch (IOException ex) {
+                        Exceptions.printStackTrace(ex);
+                    }
+                    CompletionItem ci = new CompletionItem(attrName);
+                    String text = sw.toString();
+                    text = text.substring(1, text.length() - 1); // Remove { 
and }
+                    ci.setInsertText(text);
+                    ci.setDocumentation(entry.getValue().getDescription());
+                    if (completionItems == null) {
+                        completionItems = new ArrayList<>(3);
+                    }
+                    completionItems.add(ci);
+                }
+            }
+        }
+        if (completionItems != null) {
+            return completionItems;
+        } else {
+            return Collections.emptyList();
+        }
+    }
+
+    /*

Review comment:
       Leftover ?

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ProjectConfigurationCompletion.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of project configurations in launch.json.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 100)
+public class ProjectConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    private static final String CONFIG_TYPE = "java8+";     // NOI18N
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return projectSupplier.get().thenApply(p -> 
createConfigurationsCompletion(p));
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        if ("launchConfiguration".equals(attribute)) {      // NOI18N
+            return projectSupplier.get().thenApply(p -> 
createLaunchConfigCompletion(p));
+        } else {
+            return CompletableFuture.completedFuture(Collections.emptyList());
+        }
+    }
+
+    @NbBundle.Messages({"# {0} - Configuration name", 
"LBL_LaunchJavaConfig=Launch Java: {0}",
+                        "# {0} - Configuration name", 
"LBL_LaunchJavaConfig_desc=Launch a Java 8+ application using {0}."})
+    private static List<CompletionItem> createConfigurationsCompletion(Project 
p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();
+        List<CompletionItem> completionItems = new 
ArrayList<>(configurations.size() - 1);
+        boolean skipFirst = true;
+        for (ProjectConfiguration c : configurations) {
+            if (skipFirst) {
+                skipFirst = false;
+                continue;
+            }
+            String configDisplayName = c.getDisplayName();
+            String launchName = Bundle.LBL_LaunchJavaConfig(configDisplayName);
+            CompletionItem ci = new CompletionItem("Java 8+: " + launchName);  
 // NOI18N
+            StringWriter sw = new StringWriter();
+            try (JsonWriter w = new JsonWriter(sw)) {
+                w.setIndent("\t");                                          // 
NOI18N
+                w.beginObject();
+                w.name("name").value(launchName);                           // 
NOI18N
+                w.name("type").value(CONFIG_TYPE);                          // 
NOI18N
+                w.name("request").value("launch");                          // 
NOI18N
+                w.name("mainClass").value("${file}");                       // 
NOI18N
+                w.name("launchConfiguration").value(configDisplayName);     // 
NOI18N
+                w.endObject();
+                w.flush();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+            ci.setInsertText(sw.toString());
+            
ci.setDocumentation(Bundle.LBL_LaunchJavaConfig_desc(configDisplayName));
+            completionItems.add(ci);
+        }
+        return completionItems;
+    }
+
+    private List<CompletionItem> createLaunchConfigCompletion(Project p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();
+        List<CompletionItem> completionItems = new 
ArrayList<>(configurations.size() - 1);
+        boolean skipFirst = true;
+        for (ProjectConfiguration c : configurations) {
+            if (skipFirst) {
+                skipFirst = false;
+                continue;
+            }
+            String configDisplayName = c.getDisplayName();
+            CompletionItem ci = new CompletionItem(configDisplayName);
+            ci.setInsertText("\"" + configDisplayName + "\"");

Review comment:
       The presets do not use weird chars as quotes, but for the future, better 
escape possible `"` when creating a String literal.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java
##########
@@ -241,6 +247,45 @@
             case Server.JAVA_FIND_DEBUG_PROCESS_TO_ATTACH: {
                 return AttachConfigurations.findProcessAttachTo(client);
             }
+            case Server.JAVA_PROJECT_CONFIGURATION_COMPLETION: {

Review comment:
       Maybe document the supported formats of the request in a comment.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java
##########
@@ -206,4 +206,35 @@ public static synchronized FileObject fromUri(String uri) 
throws MalformedURLExc
     private static File getCacheDir() {
         return Places.getCacheSubfile("java-server");
     }
+
+    private static final char[] SNIPPET_ESCAPE_CHARS = new char[] { '\\', '$', 
'}' };

Review comment:
       OK, `\` must come first :)

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/Utils.java
##########
@@ -206,4 +206,35 @@ public static synchronized FileObject fromUri(String uri) 
throws MalformedURLExc
     private static File getCacheDir() {
         return Places.getCacheSubfile("java-server");
     }
+
+    private static final char[] SNIPPET_ESCAPE_CHARS = new char[] { '\\', '$', 
'}' };
+    /**
+     * Escape special characters in a completion snippet. Characters '$' and 
'}'
+     * are escaped via backslash.
+     */
+    public static String escapeCompletionSnippetSpecialChars(String text) {
+        if (text.isEmpty()) {

Review comment:
       check for `null`

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ProjectConfigurationCompletion.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of project configurations in launch.json.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 100)
+public class ProjectConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    private static final String CONFIG_TYPE = "java8+";     // NOI18N
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return projectSupplier.get().thenApply(p -> 
createConfigurationsCompletion(p));
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        if ("launchConfiguration".equals(attribute)) {      // NOI18N
+            return projectSupplier.get().thenApply(p -> 
createLaunchConfigCompletion(p));
+        } else {
+            return CompletableFuture.completedFuture(Collections.emptyList());
+        }
+    }
+
+    @NbBundle.Messages({"# {0} - Configuration name", 
"LBL_LaunchJavaConfig=Launch Java: {0}",
+                        "# {0} - Configuration name", 
"LBL_LaunchJavaConfig_desc=Launch a Java 8+ application using {0}."})
+    private static List<CompletionItem> createConfigurationsCompletion(Project 
p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();

Review comment:
       Check for `null`.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ProjectConfigurationCompletion.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of project configurations in launch.json.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 100)
+public class ProjectConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    private static final String CONFIG_TYPE = "java8+";     // NOI18N
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return projectSupplier.get().thenApply(p -> 
createConfigurationsCompletion(p));
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        if ("launchConfiguration".equals(attribute)) {      // NOI18N
+            return projectSupplier.get().thenApply(p -> 
createLaunchConfigCompletion(p));
+        } else {
+            return CompletableFuture.completedFuture(Collections.emptyList());
+        }
+    }
+
+    @NbBundle.Messages({"# {0} - Configuration name", 
"LBL_LaunchJavaConfig=Launch Java: {0}",
+                        "# {0} - Configuration name", 
"LBL_LaunchJavaConfig_desc=Launch a Java 8+ application using {0}."})
+    private static List<CompletionItem> createConfigurationsCompletion(Project 
p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();
+        List<CompletionItem> completionItems = new 
ArrayList<>(configurations.size() - 1);
+        boolean skipFirst = true;
+        for (ProjectConfiguration c : configurations) {
+            if (skipFirst) {
+                skipFirst = false;
+                continue;
+            }
+            String configDisplayName = c.getDisplayName();
+            String launchName = Bundle.LBL_LaunchJavaConfig(configDisplayName);
+            CompletionItem ci = new CompletionItem("Java 8+: " + launchName);  
 // NOI18N
+            StringWriter sw = new StringWriter();
+            try (JsonWriter w = new JsonWriter(sw)) {
+                w.setIndent("\t");                                          // 
NOI18N
+                w.beginObject();
+                w.name("name").value(launchName);                           // 
NOI18N
+                w.name("type").value(CONFIG_TYPE);                          // 
NOI18N
+                w.name("request").value("launch");                          // 
NOI18N
+                w.name("mainClass").value("${file}");                       // 
NOI18N
+                w.name("launchConfiguration").value(configDisplayName);     // 
NOI18N
+                w.endObject();
+                w.flush();
+            } catch (IOException ex) {
+                Exceptions.printStackTrace(ex);
+            }
+            ci.setInsertText(sw.toString());
+            
ci.setDocumentation(Bundle.LBL_LaunchJavaConfig_desc(configDisplayName));
+            completionItems.add(ci);
+        }
+        return completionItems;
+    }
+
+    private List<CompletionItem> createLaunchConfigCompletion(Project p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();

Review comment:
       Check for `null`.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LaunchConfigurationCompletion.java
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+
+import org.netbeans.api.project.Project;
+
+/**
+ * Provider of launch configurations completion. Run/Debug launch and Debugger
+ * attach configurations can be provided.
+ *
+ * @author Martin Entlicher
+ */
+public interface LaunchConfigurationCompletion {
+
+    /**
+     * Provide configurations of Run/Debug actions.
+     *
+     * @param projectSupplier Supplier of the relevant project
+     * @return a list of completion items

Review comment:
       I'd document that the impl cannot return `null` even in case of 
unsupported projects, LSP code relies on that; or improve LSP code to handle 
possible `null`s.

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ProjectConfigurationCompletion.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.protocol;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.netbeans.api.project.Project;
+import org.netbeans.spi.project.ProjectConfiguration;
+import org.netbeans.spi.project.ProjectConfigurationProvider;
+import org.openide.util.Exceptions;
+import org.openide.util.NbBundle;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of project configurations in launch.json.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 100)
+public class ProjectConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    private static final String CONFIG_TYPE = "java8+";     // NOI18N
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return projectSupplier.get().thenApply(p -> 
createConfigurationsCompletion(p));
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        if ("launchConfiguration".equals(attribute)) {      // NOI18N
+            return projectSupplier.get().thenApply(p -> 
createLaunchConfigCompletion(p));
+        } else {
+            return CompletableFuture.completedFuture(Collections.emptyList());
+        }
+    }
+
+    @NbBundle.Messages({"# {0} - Configuration name", 
"LBL_LaunchJavaConfig=Launch Java: {0}",
+                        "# {0} - Configuration name", 
"LBL_LaunchJavaConfig_desc=Launch a Java 8+ application using {0}."})
+    private static List<CompletionItem> createConfigurationsCompletion(Project 
p) {
+        ProjectConfigurationProvider<ProjectConfiguration> provider = 
p.getLookup().lookup(ProjectConfigurationProvider.class);
+        Collection<ProjectConfiguration> configurations = 
provider.getConfigurations();
+        List<CompletionItem> completionItems = new 
ArrayList<>(configurations.size() - 1);
+        boolean skipFirst = true;
+        for (ProjectConfiguration c : configurations) {
+            if (skipFirst) {
+                skipFirst = false;
+                continue;
+            }
+            String configDisplayName = c.getDisplayName();
+            String launchName = Bundle.LBL_LaunchJavaConfig(configDisplayName);
+            CompletionItem ci = new CompletionItem("Java 8+: " + launchName);  
 // NOI18N

Review comment:
       Constant or localizable String

##########
File path: 
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/attach/AttachConfigurationCompletion.java
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.
+ */
+package org.netbeans.modules.java.lsp.server.debugging.attach;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import org.eclipse.lsp4j.CompletionItem;
+import org.eclipse.lsp4j.InsertTextFormat;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.java.lsp.server.Utils;
+import 
org.netbeans.modules.java.lsp.server.protocol.LaunchConfigurationCompletion;
+import org.openide.util.Exceptions;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ * Completion of debugger attach configurations.
+ *
+ * @author Martin Entlicher
+ */
+@ServiceProvider(service = LaunchConfigurationCompletion.class, position = 200)
+public class AttachConfigurationCompletion implements 
LaunchConfigurationCompletion {
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
configurations(Supplier<CompletableFuture<Project>> projectSupplier) {
+        return CompletableFuture.supplyAsync(() -> {
+            return createCompletion(AttachConfigurations.get());
+        }, AttachConfigurations.RP);
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributes(Supplier<CompletableFuture<Project>> projectSupplier, Map<String, 
Object> currentAttributes) {
+        return CompletableFuture.supplyAsync(() -> {
+            return createAttributesCompletion(AttachConfigurations.get(), 
currentAttributes);
+        }, AttachConfigurations.RP);
+    }
+
+    @Override
+    public CompletableFuture<List<CompletionItem>> 
attributeValues(Supplier<CompletableFuture<Project>> projectSupplier, 
Map<String, Object> currentAttributes, String attribute) {
+        return CompletableFuture.completedFuture(Collections.emptyList());
+    }
+
+    private static List<CompletionItem> createCompletion(AttachConfigurations 
attachConfigurations) {
+        return 
attachConfigurations.getConfigurations().stream().map(configAttrs -> 
createCompletion(configAttrs)).collect(Collectors.toList());
+    }
+
+    private static CompletionItem createCompletion(ConfigurationAttributes 
configAttrs) {
+        CompletionItem ci = new CompletionItem("Java 8+: " + 
configAttrs.getName());    // NOI18N
+        StringWriter sw = new StringWriter();
+        try (JsonWriter w = new JsonWriter(sw)) {
+            w.setIndent("\t");                                              // 
NOI18N
+            w.beginObject();
+            w.name("name").value("${1:" + 
Utils.escapeCompletionSnippetSpecialChars(configAttrs.getName()) + "}"); // 
NOI18N
+            w.name("type").value(AttachConfigurations.CONFIG_TYPE);         // 
NOI18N
+            w.name("request").value(AttachConfigurations.CONFIG_REQUEST);   // 
NOI18N
+            int locationIndex = 2;
+            for (Map.Entry<String, ConfigurationAttribute> entry : 
configAttrs.getAttributes().entrySet()) {
+                ConfigurationAttribute ca = entry.getValue();
+                if (ca.isMustSpecify()) {
+                    String value = ca.getDefaultValue();
+                    if (value.startsWith("${command:")) { // Do not suggest to 
customize values provided by commands    // NOI18N
+                        value = 
Utils.escapeCompletionSnippetSpecialChars(encode2JSON(value));
+                    } else {
+                        value = "${" + (locationIndex++) + (value.isEmpty() ? 
"}" : ":" + Utils.escapeCompletionSnippetSpecialChars(encode2JSON(value)) + 
"}"); // NOI18N
+                    }
+                    // We have pre-encoded the value in order not to encode 
the completion snippet escape characters
+                    w.name(entry.getKey()).jsonValue("\"" + value + "\"");
+                }
+            }
+            w.endObject();
+            w.flush();
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        ci.setInsertText(sw.toString());
+        ci.setInsertTextFormat(InsertTextFormat.Snippet);
+        ci.setDocumentation(configAttrs.getDescription());
+        return ci;
+    }
+
+    private static String encode2JSON(String value) {
+        if (value.isEmpty()) {
+            return value;
+        }
+        StringWriter sw = new StringWriter();
+        try (JsonWriter w = new JsonWriter(sw)) {
+            w.beginArray();
+            w.value(value);
+            w.endArray();
+            w.flush();
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+        }
+        String encoded = sw.toString();
+        // We have ["value"], remove the array and quotes
+        return encoded.substring(2, encoded.length() - 2);
+    }
+
+    private static List<CompletionItem> 
createAttributesCompletion(AttachConfigurations attachConfigurations, 
Map<String, Object> currentAttributes) {
+        List<CompletionItem> completionItems = null;
+        ConfigurationAttributes currentConfiguration = 
attachConfigurations.findConfiguration(currentAttributes);
+        if (currentConfiguration != null) {
+            Map<String, ConfigurationAttribute> attributes = 
currentConfiguration.getAttributes();
+            for (Map.Entry<String, ConfigurationAttribute> entry : 
attributes.entrySet()) {
+                String attrName = entry.getKey();
+                if (!currentAttributes.containsKey(attrName)) {
+                    StringWriter sw = new StringWriter();
+                    try (JsonWriter w = new JsonWriter(sw)) {
+                        w.beginObject();
+                        
w.name(attrName).value(entry.getValue().getDefaultValue());

Review comment:
       Is escaping unnecessary here ?

##########
File path: java/java.lsp.server/vscode/src/launchConfigurations.ts
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.
+ */
+'use strict';
+
+import { commands, CompletionItem, CompletionList, ExtensionContext, 
languages, ProviderResult, SnippetString } from 'vscode';
+import { InsertTextFormat } from 'vscode-languageclient';
+import * as jsoncp from 'jsonc-parser';
+
+export function registerCompletion(context: ExtensionContext) {
+   context.subscriptions.push(languages.registerCompletionItemProvider({ 
language: 'jsonc', pattern: '**/launch.json' }, {
+        provideCompletionItems(document, position, cancelToken) {
+            const sourceText = document.getText();
+            const root = jsoncp.parseTree(sourceText);
+            if (root) {
+                const offset = document.offsetAt(position);
+                const currentNode = jsoncp.findNodeAtOffset(root, offset);
+                if (currentNode) {
+                    const path = jsoncp.getNodePath(currentNode);
+                    if (path.length >= 1 && 'configurations' == path[0]) {
+                        const uri = document.uri.toString();
+                        let completionItems: 
ProviderResult<CompletionList<CompletionItem>> | CompletionItem[];
+                        if (path.length == 1) {
+                            // Get all configurations:
+                            completionItems = 
commands.executeCommand('java.project.configuration.completion', uri);
+                        } else {
+                            let node: jsoncp.Node = currentNode;
+                            if (currentNode.type == 'property' && 
currentNode.parent) {
+                                let propName = 
currentNode.children?.[0]?.value;
+                                node = currentNode.parent;
+                                let attributesMap = getAttributes(node);
+                                // Get possible values of property 'propName':
+                                completionItems = 
commands.executeCommand('java.project.configuration.completion', uri, 
attributesMap, propName);

Review comment:
       OK, if `propName` is undef ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to