You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/06/02 09:51:11 UTC

[GitHub] [netbeans] sdedic commented on a diff in pull request #4159: LSP: Initial support for multi-step inputs added.

sdedic commented on code in PR #4159:
URL: https://github.com/apache/netbeans/pull/4159#discussion_r887635353


##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/input/InputBoxStep.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.input;
+
+import java.util.Objects;
+import org.eclipse.lsp4j.jsonrpc.validation.NonNull;
+import org.eclipse.lsp4j.util.Preconditions;
+import org.eclipse.xtext.xbase.lib.Pure;
+import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+public class InputBoxStep extends ShowInputBoxParams {

Review Comment:
   `final`, if 'our' API ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/input/InputBoxStep.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.input;
+
+import java.util.Objects;
+import org.eclipse.lsp4j.jsonrpc.validation.NonNull;
+import org.eclipse.lsp4j.util.Preconditions;
+import org.eclipse.xtext.xbase.lib.Pure;
+import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+public class InputBoxStep extends ShowInputBoxParams {
+
+    /**
+     * An optional total step count.
+     */
+    private int	totalSteps;

Review Comment:
   What about this step's number ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/input/LspInputServiceImpl.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.input;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import org.eclipse.lsp4j.jsonrpc.messages.Either;
+import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+@JsonSegment("input")
+public class LspInputServiceImpl implements InputService {
+
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>>> stepCallbacks = new HashMap<>();
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<String>>> validateCallbacks = new HashMap<>();
+
+    public String registerInput(Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>> stepCallback, Function<InputCallbackParams, CompletableFuture<String>> validateCallback) {

Review Comment:
   I would propose adding an 2-method interface, maybe with `default` validate method returning null.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/ui/NotifyDescriptorAdapter.java:
##########
@@ -346,7 +346,7 @@ public <T extends NotifyDescriptor> CompletableFuture<T> clientNotifyCompletion(
             for (NotifyDescriptor.QuickPick.Item item : qp.getItems()) {
                 items.put(new QuickPickItem(item.getLabel(), item.getDescription(), null, item.isSelected(), null), item);
             }
-            ShowQuickPickParams params = new ShowQuickPickParams(qp.getTitle(), qp.isMultipleSelection(), new ArrayList<>(items.keySet()));
+            ShowQuickPickParams params = new ShowQuickPickParams(null, qp.getTitle(), qp.isMultipleSelection(), new ArrayList<>(items.keySet()));

Review Comment:
   Q: could be `NotifyDescriptor.QuickPick.text` be used as a placeholder ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java:
##########
@@ -876,6 +889,7 @@ protected LanguageClient client() {
             });
             sessionServices.add(new WorkspaceUIContext(client));
             sessionServices.add(treeService.getNodeRegistry());
+            sessionServices.add(inputService);

Review Comment:
   Nitpick: I'd add "server interface" in the InputServiceImpl, so server code can look it up by that interface rather than impl class



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/input/LspInputServiceImpl.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.input;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import org.eclipse.lsp4j.jsonrpc.messages.Either;
+import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+@JsonSegment("input")
+public class LspInputServiceImpl implements InputService {
+
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>>> stepCallbacks = new HashMap<>();
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<String>>> validateCallbacks = new HashMap<>();
+
+    public String registerInput(Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>> stepCallback, Function<InputCallbackParams, CompletableFuture<String>> validateCallback) {
+        String id = "ID:" + System.identityHashCode(stepCallback);

Review Comment:
   Note that identity hashcode may not be unique throughout the time. Unlikely, but can happen. Why not identify by only-incementing AtomicInteger ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/input/LspInputServiceImpl.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.input;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import org.eclipse.lsp4j.jsonrpc.messages.Either;
+import org.eclipse.lsp4j.jsonrpc.services.JsonSegment;
+
+/**
+ *
+ * @author Dusan Balek
+ */
+@JsonSegment("input")
+public class LspInputServiceImpl implements InputService {
+
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>>> stepCallbacks = new HashMap<>();
+    private final Map<String, Function<InputCallbackParams, CompletableFuture<String>>> validateCallbacks = new HashMap<>();
+
+    public String registerInput(Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>> stepCallback, Function<InputCallbackParams, CompletableFuture<String>> validateCallback) {
+        String id = "ID:" + System.identityHashCode(stepCallback);
+        stepCallbacks.put(id, stepCallback);
+        if (validateCallback != null) {
+            validateCallbacks.put(id, validateCallback);
+        }
+        return id;
+    }
+
+    public void unregisterInput(String inputId) {
+        stepCallbacks.remove(inputId);
+        validateCallbacks.remove(inputId);
+    }
+
+    @Override
+    public CompletableFuture<Either<QuickPickStep, InputBoxStep>> step(InputCallbackParams params) {
+        Function<InputCallbackParams, CompletableFuture<Either<QuickPickStep, InputBoxStep>>> callback = stepCallbacks.get(params.getInputId());
+        return callback != null ? callback.apply(params) : CompletableFuture.completedFuture(null);

Review Comment:
   Maybe we can save an `unregister` call requirement for the service client, if we check `apply` return value here ?
   Something like 
   ```
   retValue.handle( (ex, step) -> { 
       if (ex != null || step == null)  {
         unregisterInput(params.getInputId());
      }
   });
   ```
   ?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/NbCodeLanguageClient.java:
##########
@@ -69,6 +75,15 @@ public interface NbCodeLanguageClient extends LanguageClient {
     @JsonRequest("window/showInputBox")
     public CompletableFuture<String> showInputBox(@NonNull ShowInputBoxParams params);
 
+    /**
+     * Shows a mutli-step input using QuickPicks and InputBoxes.
+     *
+     * @param params input parameters
+     * @return collected input values and selected items
+     */
+    @JsonRequest("window/showMultiStepInput")
+    public CompletableFuture<Map<String, Either<List<QuickPickItem>, String>>> showMultiStepInput(@NonNull ShowMutliStepInputParams params);

Review Comment:
   OK at this moment, but if there's a chance that there could be more step "styles", it could be more appropriate to make a class that would hold the alternatives instead of `Either`.
   
   An observation: since the incoming request is associated to the appropriate handler by ID, so the process is not stateless REST; it could be possible to avoid travel of past data (Map<stepId, Either...>) server <=> client at all, as the handler can accumulate the data in an object server-side.



-- 
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: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

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