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 2020/10/26 15:50:12 UTC

[GitHub] [netbeans] JaroslavTulach commented on a change in pull request #2493: Infrastructure to display simple confirmations/questions in LSP client.

JaroslavTulach commented on a change in pull request #2493:
URL: https://github.com/apache/netbeans/pull/2493#discussion_r512051491



##########
File path: java/java.lsp.server/nbcode/integration/src/org/netbeans/modules/nbcode/integration/LspDialogDisplayer.java
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.nbcode.integration;
+
+import org.netbeans.modules.java.lsp.server.ui.AbstractDialogDisplayer;
+import org.openide.DialogDisplayer;
+import org.openide.util.lookup.ServiceProvider;
+
+/**
+ *
+ * @author sdedic
+ */
+//@ServiceProvider(service = DialogDisplayer.class, position = 1000)

Review comment:
       Reasonable, given we after feature freeze. This functionality will certainly be needed in the future. For 12.2 we'd rather delay using it, unless necessary.

##########
File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
##########
@@ -86,39 +109,56 @@ public static void launchServer(InputStream in, OutputStream out) {
         }
     }
     
-    private static Launcher<NbCodeLanguageClient> createLauncher(LanguageServerImpl server, InputStream in, OutputStream out) {
+    private static Launcher<NbCodeLanguageClient> createLauncher(LanguageServerImpl server, InputStream in, OutputStream out,
+            Function<MessageConsumer, MessageConsumer> processor) {
         return new LSPLauncher.Builder<NbCodeLanguageClient>()
             .setLocalService(server)
             .setRemoteInterface(NbCodeLanguageClient.class)
             .setInput(in)
             .setOutput(out)
-            .wrapMessages(new ConsumeWithLookup(server.getSessionLookup())::attachLookup)
+            .wrapMessages(processor)
             .create();
     }
     
+    static final ThreadLocal<NbCodeLanguageClient>   DISPATCHERS = new ThreadLocal<>();
+    
     /**
      * Processes message while the default Lookup is set to 
      * {@link LanguageServerImpl#getSessionLookup()}.
      */
     private static class ConsumeWithLookup {
         private final Lookup sessionLookup;
-
+        private NbCodeLanguageClient client;
+        
         public ConsumeWithLookup(Lookup sessionLookup) {
             this.sessionLookup = sessionLookup;
         }
         
+        synchronized void attachClient(NbCodeLanguageClient client) {
+            this.client = client;
+        }
+        
         public MessageConsumer attachLookup(MessageConsumer delegate) {
             return new MessageConsumer() {
                 @Override
                 public void consume(Message msg) throws MessageIssueException, JsonRpcException {
-                    Lookups.executeWith(sessionLookup, () -> {
-                        delegate.consume(msg);
-                    });
+                    try {
+                        DISPATCHERS.set(client);
+                        Lookups.executeWith(sessionLookup, () -> {
+                            delegate.consume(msg);
+                        });
+                    } finally {
+                        DISPATCHERS.remove();
+                    }
                 }
             };
         }
     }
     
+    private static final RequestProcessor SERVER_INIT_RP = new RequestProcessor(LanguageServerImpl.class.getName(), 10);
+    private static final Map<NbCodeClientWrapper, Future> initializations = new WeakHashMap<>();

Review comment:
       Is the `initializations` field read anywhere? I can't find it (and that'd be good, as I don't think it is needed).

##########
File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/ui/NotifyDescriptorAdapter.java
##########
@@ -0,0 +1,322 @@
+/*
+ * 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.ui;
+
+import java.awt.Component;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import javax.accessibility.Accessible;
+import javax.accessibility.AccessibleContext;
+import javax.accessibility.AccessibleIcon;
+import javax.accessibility.AccessibleText;
+import javax.swing.Icon;
+import javax.swing.JButton;
+import org.eclipse.lsp4j.MessageActionItem;
+import org.eclipse.lsp4j.MessageType;
+import org.eclipse.lsp4j.ShowMessageRequestParams;
+import org.openide.NotifyDescriptor;
+import org.openide.util.NbBundle;
+
+/**
+ * Adapts a {@link NotifyDescriptor} to a {@link ShowMessageRequestParams} call.
+ * @author sdedic
+ */
+class NotifyDescriptorAdapter {
+    private static final Logger LOG = Logger.getLogger(NotifyDescriptorAdapter.class.getName());
+    
+    private final UIContext client;
+    private final NotifyDescriptor  descriptor;
+    private final Map<MessageActionItem, Object> item2Option = new LinkedHashMap<>();
+    private final Map<String, Object> text2Option = new HashMap<>();
+    private final Map<Object, List<ActionListener>> optionListeners = new HashMap<>();
+    private final Map<Object, JButton> option2Button = new HashMap<>();
+    
+    private static final Set<String> warnedClasses = new HashSet<>();
+    
+    private ShowMessageRequestParams request;
+
+    private static final Object[] YES_NO_CANCEL = new Object[] {
+        NotifyDescriptor.YES_OPTION,
+        NotifyDescriptor.NO_OPTION,
+        NotifyDescriptor.CANCEL_OPTION
+    };
+    
+    private static final Object[] YES_NO = new Object[] {
+        NotifyDescriptor.YES_OPTION,
+        NotifyDescriptor.NO_OPTION,
+    };
+    
+    private static final Object[] OK_CANCEL = new Object[] {
+        NotifyDescriptor.OK_OPTION,
+        NotifyDescriptor.CANCEL_OPTION
+    };
+    
+    private static final Object[] JUST_OK = new Object[] {
+        NotifyDescriptor.OK_OPTION
+    };
+    
+    public NotifyDescriptorAdapter(NotifyDescriptor descriptor, UIContext client) {
+        this.descriptor = descriptor;
+        this.client = client;
+    }
+    
+    private MessageType translateMessageType() {
+        switch(descriptor.getMessageType()) {
+            case NotifyDescriptor.ERROR_MESSAGE:
+                return MessageType.Error;
+            case NotifyDescriptor.WARNING_MESSAGE:
+                return MessageType.Warning;
+            
+            case NotifyDescriptor.QUESTION_MESSAGE:
+            case NotifyDescriptor.PLAIN_MESSAGE:
+            case NotifyDescriptor.INFORMATION_MESSAGE:
+                return MessageType.Info;
+            default:
+                return MessageType.Log;
+        }
+    }
+    
+    /**
+     * Strip HTML from the message; VSCode standard showMessage does not support HTML.
+     * @param original
+     * @return 
+     */
+    private String translateText(String original) {

Review comment:
       I believe the translation of the text away from HTML shall only happen if it starts with `<html>`.

##########
File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java
##########
@@ -86,39 +109,56 @@ public static void launchServer(InputStream in, OutputStream out) {
         }
     }
     
-    private static Launcher<NbCodeLanguageClient> createLauncher(LanguageServerImpl server, InputStream in, OutputStream out) {
+    private static Launcher<NbCodeLanguageClient> createLauncher(LanguageServerImpl server, InputStream in, OutputStream out,
+            Function<MessageConsumer, MessageConsumer> processor) {
         return new LSPLauncher.Builder<NbCodeLanguageClient>()
             .setLocalService(server)
             .setRemoteInterface(NbCodeLanguageClient.class)
             .setInput(in)
             .setOutput(out)
-            .wrapMessages(new ConsumeWithLookup(server.getSessionLookup())::attachLookup)
+            .wrapMessages(processor)
             .create();
     }
     
+    static final ThreadLocal<NbCodeLanguageClient>   DISPATCHERS = new ThreadLocal<>();
+    
     /**
      * Processes message while the default Lookup is set to 
      * {@link LanguageServerImpl#getSessionLookup()}.
      */
     private static class ConsumeWithLookup {
         private final Lookup sessionLookup;
-
+        private NbCodeLanguageClient client;
+        
         public ConsumeWithLookup(Lookup sessionLookup) {
             this.sessionLookup = sessionLookup;
         }
         
+        synchronized void attachClient(NbCodeLanguageClient client) {
+            this.client = client;
+        }
+        
         public MessageConsumer attachLookup(MessageConsumer delegate) {
             return new MessageConsumer() {
                 @Override
                 public void consume(Message msg) throws MessageIssueException, JsonRpcException {
-                    Lookups.executeWith(sessionLookup, () -> {
-                        delegate.consume(msg);
-                    });
+                    try {
+                        DISPATCHERS.set(client);
+                        Lookups.executeWith(sessionLookup, () -> {
+                            delegate.consume(msg);
+                        });
+                    } finally {
+                        DISPATCHERS.remove();
+                    }
                 }
             };
         }
     }
     
+    private static final RequestProcessor SERVER_INIT_RP = new RequestProcessor(LanguageServerImpl.class.getName(), 10);

Review comment:
       Throughput 10!? Isn't opening of project sequential anyway?




----------------------------------------------------------------
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.

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