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


##########
java/java.lsp.server/vscode/src/extension.ts:
##########
@@ -486,6 +486,47 @@ export function activate(context: ExtensionContext): 
VSNetBeansAPI {
             throw `Client ${c} doesn't support new project`;
         }
     }));
+    context.subscriptions.push(commands.registerCommand(COMMAND_PREFIX + 
'.goto.test', async (ctx) => {

Review Comment:
   We should probably come up with a consistent naming scheme for such command 
wrappers, or for the "data providing commands" . "goto.test" vs. "go.to.test" 
similarity can lead to errors.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/commands/TestOppositesCommandProvider.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.commands;
+
+import com.google.gson.JsonPrimitive;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import org.netbeans.api.gototest.TestOppositesLocator;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.spi.lsp.CommandProvider;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.filesystems.FileObject;
+import org.openide.util.lookup.ServiceProvider;
+
+@ServiceProvider(service=CommandProvider.class)
+public class TestOppositesCommandProvider implements CommandProvider {
+
+    private static final String NBLS_GO_TO_TEST = "nbls.go.to.test";
+    private static final Set<String> COMMANDS = new 
HashSet<>(Arrays.asList(NBLS_GO_TO_TEST));
+
+    @Override
+    public Set<String> getCommands() {
+        return COMMANDS;
+    }
+
+    @Override
+    public CompletableFuture<Object> runCommand(String command, List<Object> 
arguments) {
+        switch (command) {
+            case NBLS_GO_TO_TEST: {
+                try {
+                    String source = ((JsonPrimitive) 
arguments.get(0)).getAsString();
+                    FileObject file;
+                    file = Utils.fromUri(source);
+                    return 
TestOppositesLocator.getDefault().findOpposites(file, -1).thenApply(locations 
-> {
+                        Set<FileObject> result = new LinkedHashSet<>();
+
+                        if (locations.getErrorMessage() != null) {

Review Comment:
   Question: why the errors (info messages) are not just relayed to the client 
(e.g. vscode) for presentation ?



##########
java/java.lsp.server/vscode/src/extension.ts:
##########
@@ -486,6 +486,47 @@ export function activate(context: ExtensionContext): 
VSNetBeansAPI {
             throw `Client ${c} doesn't support new project`;
         }
     }));
+    context.subscriptions.push(commands.registerCommand(COMMAND_PREFIX + 
'.goto.test', async (ctx) => {
+        let c: LanguageClient = await client;
+        const commands = await vscode.commands.getCommands();
+        if (commands.includes(COMMAND_PREFIX + '.go.to.test')) {
+            try {
+                const res: Array<string> = await 
vscode.commands.executeCommand(COMMAND_PREFIX + '.go.to.test', 
contextUri(ctx)?.toString());
+                if (res?.length) {
+                    if (res.length === 1) {
+                        let file = vscode.Uri.parse(res[0]);
+                        await vscode.window.showTextDocument(file, { preview: 
false });
+                    } else {
+                        const namePathMapping: { [key: string]: string } = {}
+                        res.forEach(fp => {
+                            const fileName = path.basename(fp);
+                            namePathMapping[fileName] = fp
+                        });
+                        const selected = await 
window.showQuickPick(Object.keys(namePathMapping), {
+                            title: 'Select files to open',
+                            placeHolder: 'Test files or source files 
associated to each other',
+                            canPickMany: true
+                        });
+                        if (selected) {
+                            for await (const filePath of selected) {
+                                let file = vscode.Uri.parse(filePath);
+                                await vscode.window.showTextDocument(file, { 
preview: false });
+                            }
+                        } else {
+                            vscode.window.showInformationMessage("No file 
selected");
+                        }
+                    }
+                }
+                else {
+                    throw new Error("No corresponding file found");

Review Comment:
   The error is immediately caught a few lines below.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/commands/TestOppositesCommandProvider.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.commands;
+
+import com.google.gson.JsonPrimitive;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import org.netbeans.api.gototest.TestOppositesLocator;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.spi.lsp.CommandProvider;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.filesystems.FileObject;
+import org.openide.util.lookup.ServiceProvider;
+
+@ServiceProvider(service=CommandProvider.class)
+public class TestOppositesCommandProvider implements CommandProvider {
+
+    private static final String NBLS_GO_TO_TEST = "nbls.go.to.test";
+    private static final Set<String> COMMANDS = new 
HashSet<>(Arrays.asList(NBLS_GO_TO_TEST));
+
+    @Override
+    public Set<String> getCommands() {
+        return COMMANDS;
+    }
+
+    @Override
+    public CompletableFuture<Object> runCommand(String command, List<Object> 
arguments) {
+        switch (command) {
+            case NBLS_GO_TO_TEST: {
+                try {
+                    String source = ((JsonPrimitive) 
arguments.get(0)).getAsString();
+                    FileObject file;
+                    file = Utils.fromUri(source);
+                    return 
TestOppositesLocator.getDefault().findOpposites(file, -1).thenApply(locations 
-> {
+                        Set<FileObject> result = new LinkedHashSet<>();
+
+                        if (locations.getErrorMessage() != null) {
+                            DialogDisplayer.getDefault().notify(
+                                    new 
NotifyDescriptor.Message(locations.getErrorMessage(), 
NotifyDescriptor.ERROR_MESSAGE));
+                        } else {
+                            locations.getProviderErrors()
+                                     .stream()
+                                     .forEach(msg -> {
+                                         DialogDisplayer.getDefault().notify(

Review Comment:
   `.notify()` will block until "OK", effectively blocking the RequestProcessor 
in `TestOppositesLocator` from serving other requests. This might be harmful as 
users tend to ignore the notifications / errors in vscode. Use nonblocking 
`notifyLater`.



##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/commands/TestOppositesCommandProvider.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.commands;
+
+import com.google.gson.JsonPrimitive;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import org.netbeans.api.gototest.TestOppositesLocator;
+import org.netbeans.modules.java.lsp.server.Utils;
+import org.netbeans.spi.lsp.CommandProvider;
+import org.openide.DialogDisplayer;
+import org.openide.NotifyDescriptor;
+import org.openide.filesystems.FileObject;
+import org.openide.util.lookup.ServiceProvider;
+
+@ServiceProvider(service=CommandProvider.class)
+public class TestOppositesCommandProvider implements CommandProvider {
+
+    private static final String NBLS_GO_TO_TEST = "nbls.go.to.test";
+    private static final Set<String> COMMANDS = new 
HashSet<>(Arrays.asList(NBLS_GO_TO_TEST));
+
+    @Override
+    public Set<String> getCommands() {
+        return COMMANDS;
+    }
+
+    @Override
+    public CompletableFuture<Object> runCommand(String command, List<Object> 
arguments) {
+        switch (command) {
+            case NBLS_GO_TO_TEST: {
+                try {
+                    String source = ((JsonPrimitive) 
arguments.get(0)).getAsString();
+                    FileObject file;
+                    file = Utils.fromUri(source);
+                    return 
TestOppositesLocator.getDefault().findOpposites(file, -1).thenApply(locations 
-> {
+                        Set<FileObject> result = new LinkedHashSet<>();
+
+                        if (locations.getErrorMessage() != null) {
+                            DialogDisplayer.getDefault().notify(
+                                    new 
NotifyDescriptor.Message(locations.getErrorMessage(), 
NotifyDescriptor.ERROR_MESSAGE));
+                        } else {
+                            locations.getProviderErrors()
+                                     .stream()
+                                     .forEach(msg -> {
+                                         DialogDisplayer.getDefault().notify(
+                                                 new 
NotifyDescriptor.Message(msg, NotifyDescriptor.INFORMATION_MESSAGE));
+                                         });
+                            locations.getLocations().stream().map(l -> 
l.getFileObject()).forEach(result::add);

Review Comment:
   The client does not receive positional information, offset is discarded: it 
will open a file, but won't be able to scroll down past comments to the actual 
test/tested class declaration. I'd suggest to report back the Location 
structure and let the client to process the data.



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