lahodaj commented on code in PR #8514:
URL: https://github.com/apache/netbeans/pull/8514#discussion_r2120241919


##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java:
##########
@@ -1049,44 +1051,53 @@ private void collectProjectCandidates(FileObject fo, 
List<FileObject> candidates
 
         private void initializeOptions() {
             getWorkspaceProjects().thenAccept(projects -> {
-                ConfigurationItem item = new ConfigurationItem();
-                // PENDING: what about doing just one roundtrip to the client- 
we may request multiple ConfiguratonItems in one message ?
-                
item.setSection(client.getNbCodeCapabilities().getConfigurationPrefix() + 
NETBEANS_JAVA_HINTS);
-                client.configuration(new 
ConfigurationParams(Collections.singletonList(item))).thenAccept(c -> {
-                    if (c != null && !c.isEmpty() && c.get(0) instanceof 
JsonObject) {
-                        
textDocumentService.updateJavaHintPreferences((JsonObject) c.get(0));
-                    }
-                    else {
-                        textDocumentService.hintsSettingsRead = true;
-                        textDocumentService.reRunDiagnostics();
-                    }
-                });
-                
item.setSection(client.getNbCodeCapabilities().getConfigurationPrefix() + 
NETBEANS_PROJECT_JDKHOME);
-                client.configuration(new 
ConfigurationParams(Collections.singletonList(item))).thenAccept(c -> {
-                    JsonPrimitive newProjectJDKHomePath = null;
+                List<String> defaultConfigs = List.of(NETBEANS_JAVA_HINTS, 
NETBEANS_PROJECT_JDKHOME);
+                List<String> projectConfigs = List.of(NETBEANS_FORMAT, 
NETBEANS_JAVA_IMPORTS);
 
-                    if (c != null && !c.isEmpty() && c.get(0) instanceof 
JsonPrimitive) {
-                        newProjectJDKHomePath = (JsonPrimitive) c.get(0);
-                    } else {
-                    }
-                    
textDocumentService.updateProjectJDKHome(newProjectJDKHomePath);
-                });
+                AtomicReference<FileObject> projectDirectory = new 
AtomicReference<>(null);

Review Comment:
   The use of `AtomicReference` here is probably unnecessary, correct? Can it 
be simply `FileObject projectDirectory`? It seems to be assign on one place 
only.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ClientConfigurationManager.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.JsonElement;
+import com.google.gson.JsonObject;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.WeakHashMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.BiConsumer;
+import org.eclipse.lsp4j.ConfigurationItem;
+import org.eclipse.lsp4j.ConfigurationParams;
+import org.eclipse.lsp4j.services.LanguageClient;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Lookup;
+
+/**
+ * Manages configuration settings for LSP clients
+ *
+ * @author atalati
+ */
+public class ClientConfigurationManager {
+
+    final WeakHashMap<LanguageClient, ConfigValueCache> clientCaches = new 
WeakHashMap<>();
+
+    private ClientConfigurationManager() {
+    }
+
+    public static ClientConfigurationManager getInstance() {
+        return SingletonHolder.INSTANCE;
+    }
+
+    private static class SingletonHolder {
+
+        private static final ClientConfigurationManager INSTANCE = new 
ClientConfigurationManager();
+    }
+
+    public void registerClient(LanguageClient client) {
+        clientCaches.put(client, new ConfigValueCache());
+    }
+
+    public void registerConfigChangeListener(NbCodeLanguageClient client, 
String section, BiConsumer<String, JsonElement> consumer) {
+        ConfigValueCache cache = clientCaches.get(client);
+        if (cache != null) {
+            cache.registerListener(section, consumer);
+        }
+    }
+
+    public void registerConfigCache(NbCodeLanguageClient client, String 
section) {

Review Comment:
   I wonder what is the purpose of this registering. Can the `ConfigValueCache` 
be simply registered on the first query?



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ConfigValueCache.java:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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.JsonElement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.BiConsumer;
+import org.eclipse.lsp4j.ConfigurationItem;
+import org.eclipse.lsp4j.ConfigurationParams;
+
+/**
+ * Cache for configuration values with support for hierarchical keys and change
+ * notifications
+ *
+ * @author atalati
+ */
+public class ConfigValueCache {
+
+    private final ConcurrentHashMap<String, Object> rootCache = new 
ConcurrentHashMap<>();
+    private static final String ROOT_KEY_VALUE = "";
+
+    public void registerListener(String section, BiConsumer<String, 
JsonElement> listener) {
+        ConfigData configData = getCachedConfigData(section);
+        if (configData != null) {
+            configData.setConsumer(listener);
+        } else {
+            setConfigInTree(section, new ConfigData(listener));
+        }
+    }
+
+    public void registerCache(String section) {
+        ConfigData configData = getCachedConfigData(section);
+        if (configData == null) {
+            setConfigInTree(section, new ConfigData());
+        }
+    }
+
+    public JsonElement getConfigValue(String section, String scope) {
+        ConfigData configData = getCachedConfigData(section);
+        if (configData == null) {
+            return null;
+        } else if (scope == null) {
+            return configData.getRootValue();
+        }
+        return configData.getScopedValue(scope);
+    }
+
+    public void updateCache(NbCodeLanguageClient client, String section, 
Object cacheValue, JsonElement tree) {
+        if (tree == null || cacheValue == null) {
+            return;
+        }
+
+        ConfigData rootData;
+        ConfigValueCache currentCache = null;
+
+        if (isConfigDataInstance(cacheValue)) {
+            rootData = (ConfigData) cacheValue;
+        } else if (isConfigValueInstance(cacheValue)) {
+            currentCache = (ConfigValueCache) cacheValue;
+            rootData = currentCache.getRootCacheValue();
+        } else {
+            return;
+        }
+
+        if (rootData != null) {
+            rootData.setRootValue(tree);
+            try {
+                BiConsumer<String, JsonElement> consumer = 
rootData.getConsumer();
+                if (consumer != null) {
+                    consumer.accept(section, tree);
+                }
+            } catch (RuntimeException e) {

Review Comment:
   Catching and throwing away an exception like this is usually wrong. If 
there's something wrong, and the exception happens, we will never find out, 
things will just appear broken.
   
   In cases there's a valid reason to continue after a callback fails with an 
exception, the exception should be logged, so that at least from the log we can 
find out what happened.
   
   In cases we don't see a valid reason why the callback would fail, I would 
simply not catch the error.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/ClientConfigurationManager.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.JsonElement;
+import com.google.gson.JsonObject;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.WeakHashMap;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.BiConsumer;
+import org.eclipse.lsp4j.ConfigurationItem;
+import org.eclipse.lsp4j.ConfigurationParams;
+import org.eclipse.lsp4j.services.LanguageClient;
+import org.netbeans.api.project.FileOwnerQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.openide.filesystems.FileObject;
+import org.openide.filesystems.FileUtil;
+import org.openide.util.Lookup;
+
+/**
+ * Manages configuration settings for LSP clients
+ *
+ * @author atalati
+ */
+public class ClientConfigurationManager {
+
+    final WeakHashMap<LanguageClient, ConfigValueCache> clientCaches = new 
WeakHashMap<>();

Review Comment:
   This is not necessarily wrong, but I wonder if `NbCodeLanguageClient` should 
hold an instance of `ClientConfigurationManagement`. On the call sites it would 
then be like:
   ```
   
client.getConfigurationManagement().registerConfigChangeListener("<section>", 
consumer);
   ```
   
   and it seems it could be more clearly non-leaking, and also even easier for 
to use.



-- 
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-unsubscr...@netbeans.apache.org

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


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

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

Reply via email to