From cf30670c24d446134b12f32efa21a21ab0896fe0 Mon Sep 17 00:00:00 2001 From: Martin Entlicher Date: Wed, 3 Feb 2021 21:03:32 +0100 Subject: [PATCH] Change the Run code lens action to start through debugging with noDebug=true. --- .../server/debugging/NbSourceProvider.java | 13 ++-- .../launch/NbLaunchRequestHandler.java | 16 ++++- .../java/lsp/server/protocol/Server.java | 5 +- .../protocol/TextDocumentServiceImpl.java | 4 +- .../server/protocol/WorkspaceServiceImpl.java | 67 ------------------- java/java.lsp.server/vscode/src/extension.ts | 13 +++- 6 files changed, 37 insertions(+), 81 deletions(-) diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/NbSourceProvider.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/NbSourceProvider.java index 630324820ba3..9e598bd243f5 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/NbSourceProvider.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/NbSourceProvider.java @@ -118,11 +118,14 @@ public static Source convertDebuggerSourceToClient(String fullyQualifiedName, St String uri = sourceProvider.getSourceFileURI(fullyQualifiedName, relativeSourcePath); if (uri == null || uri.isEmpty()) { - for (String path : context.getSourcePaths()) { - Path fullpath = Paths.get(path, relativeSourcePath); - if (Files.isRegularFile(fullpath)) { - uri = fullpath.toString(); - break; + String[] sourcePaths = context.getSourcePaths(); + if (sourcePaths != null) { + for (String path : sourcePaths) { + Path fullpath = Paths.get(path, relativeSourcePath); + if (Files.isRegularFile(fullpath)) { + uri = fullpath.toString(); + break; + } } } } diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java index c6f7c84296a3..bebc49f9141e 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchRequestHandler.java @@ -39,6 +39,7 @@ import org.eclipse.lsp4j.debug.Source; import org.eclipse.lsp4j.debug.TerminatedEventArguments; import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode; +import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext; import org.netbeans.modules.java.lsp.server.debugging.NbSourceProvider; import org.netbeans.modules.java.lsp.server.debugging.utils.ErrorUtilities; @@ -113,6 +114,19 @@ public CompletableFuture launch(Map launchArguments, Debug ResponseErrorCode.serverErrorStart); return resultFuture; } + if (!launchArguments.containsKey("sourcePaths")) { + ClassPath sourceCP = ClassPath.getClassPath(file, ClassPath.SOURCE); + if (sourceCP != null) { + FileObject[] roots = sourceCP.getRoots(); + String[] sourcePaths = new String[roots.length]; + for (int i = 0; i < roots.length; i++) { + sourcePaths[i] = roots[i].getPath(); + } + context.setSourcePaths(sourcePaths); + } + } else { + context.setSourcePaths((String[]) launchArguments.get("sourcePaths")); + } String singleMethod = (String)launchArguments.get("singleMethod"); activeLaunchHandler.nbLaunch(file, singleMethod, context, !noDebug, new OutputListener(context)).thenRun(() -> { activeLaunchHandler.postLaunch(launchArguments, context); @@ -162,7 +176,7 @@ private static OutputEventArguments convertToOutputEventArguments(String message protected void handleTerminatedEvent(DebugAdapterContext context) { CompletableFuture.runAsync(() -> { try { - waitForDebuggeeConsole.get(5, TimeUnit.SECONDS); + waitForDebuggeeConsole.get(1, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { // do nothing. } diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java index 2a9b5b18468c..a4a5b1dcbd7e 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/Server.java @@ -322,8 +322,7 @@ private InitializeResult constructInitResponse(JavaSource src) { capabilities.setDocumentHighlightProvider(true); capabilities.setReferencesProvider(true); List commands = new ArrayList<>(Arrays.asList( - JAVA_BUILD_WORKSPACE, GRAALVM_PAUSE_SCRIPT, - JAVA_TEST_SINGLE_METHOD, JAVA_RUN_MAIN_METHOD)); + JAVA_BUILD_WORKSPACE, GRAALVM_PAUSE_SCRIPT)); for (CodeGenerator codeGenerator : Lookup.getDefault().lookupAll(CodeGenerator.class)) { commands.addAll(codeGenerator.getCommands()); } @@ -419,8 +418,6 @@ protected LanguageClient client() { } public static final String JAVA_BUILD_WORKSPACE = "java.build.workspace"; - public static final String JAVA_TEST_SINGLE_METHOD = "java.test.single.method"; - public static final String JAVA_RUN_MAIN_METHOD = "java.run.main.method"; public static final String GRAALVM_PAUSE_SCRIPT = "graalvm.pause.script"; static final String INDEXING_COMPLETED = "Indexing completed."; static final String NO_JAVA_SUPPORT = "Cannot initialize Java support on JDK "; diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java index d7ef2ebc526e..2e0b0bcd3e63 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/TextDocumentServiceImpl.java @@ -1375,7 +1375,7 @@ public CompletableFuture> codeLens(CodeLensParams param Utils.createPosition(cc.getCompilationUnit(), method.end().getOffset())); List arguments = Arrays.asList(new Object[]{method.method().getFile().toURI(), method.method().getMethodName()}); lens.add(new CodeLens(range, - new Command("Run test", Server.JAVA_TEST_SINGLE_METHOD, arguments), + new Command("Run test", "java.run.codelens", arguments), null)); lens.add(new CodeLens(range, new Command("Debug test", "java.debug.codelens", arguments), @@ -1391,7 +1391,7 @@ public Void visitMethod(MethodTree tree, Void p) { Range range = Utils.treeRange(cc, tree); List arguments = Collections.singletonList(params.getTextDocument().getUri()); lens.add(new CodeLens(range, - new Command("Run main", Server.JAVA_RUN_MAIN_METHOD, arguments), + new Command("Run main", "java.run.codelens", arguments), null)); lens.add(new CodeLens(range, new Command("Debug main", "java.debug.codelens", arguments), diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java index 4e65d5150dd9..a7de5eb65036 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/WorkspaceServiceImpl.java @@ -112,33 +112,6 @@ public CompletableFuture executeCommand(ExecuteCommandParams params) { progressOfCompilation.checkStatus(); return progressOfCompilation.getFinishFuture(); } - case Server.JAVA_TEST_SINGLE_METHOD: - CommandProgress progressOfCommand = new CommandProgress(); - String uriStr = ((JsonPrimitive) params.getArguments().get(0)).getAsString(); - FileObject file; - try { - file = URLMapper.findFileObject(new URL(uriStr)); - } catch (MalformedURLException ex) { - Exceptions.printStackTrace(ex); - return CompletableFuture.completedFuture(true); - } - String methodName = ((JsonPrimitive) params.getArguments().get(1)).getAsString(); - SingleMethod method = new SingleMethod(file, methodName); - runSingleMethodCommand(method, SingleMethod.COMMAND_RUN_SINGLE_METHOD, progressOfCommand); - progressOfCommand.checkStatus(); - return progressOfCommand.getFinishFuture(); - case Server.JAVA_RUN_MAIN_METHOD: - progressOfCommand = new CommandProgress(); - uriStr = ((JsonPrimitive) params.getArguments().get(0)).getAsString(); - try { - file = URLMapper.findFileObject(new URL(uriStr)); - } catch (MalformedURLException ex) { - Exceptions.printStackTrace(ex); - return CompletableFuture.completedFuture(true); - } - runSingleFile(file, ActionProvider.COMMAND_RUN_SINGLE, progressOfCommand); - progressOfCommand.checkStatus(); - return progressOfCommand.getFinishFuture(); default: for (CodeGenerator codeGenerator : Lookup.getDefault().lookupAll(CodeGenerator.class)) { if (codeGenerator.getCommands().contains(command)) { @@ -149,46 +122,6 @@ public CompletableFuture executeCommand(ExecuteCommandParams params) { throw new UnsupportedOperationException("Command not supported: " + params.getCommand()); } - @NbBundle.Messages("No_Method_Found=No method found") - private void runSingleMethodCommand(SingleMethod singleMethod, String command, CommandProgress progressOfCommand) { - if (singleMethod == null) { - StatusDisplayer.getDefault().setStatusText(Bundle.No_Method_Found()); - progressOfCommand.getFinishFuture().complete(true); - } else { - Mutex.EVENT.readAccess(new Runnable() { - @Override - public void run() { - Project owner = FileOwnerQuery.getOwner(singleMethod.getFile()); - if (owner != null) { - ActionProvider ap = owner.getLookup().lookup(ActionProvider.class); - if (ap != null) { - if (Arrays.asList(ap.getSupportedActions()).contains(command) && ap.isActionEnabled(command, Lookups.singleton(singleMethod))) { - ap.invokeAction(command, Lookups.fixed(singleMethod, progressOfCommand)); - } - } - } - } - }); - } - } - - private void runSingleFile(FileObject file, String command, CommandProgress progressOfCommand) { - Mutex.EVENT.readAccess(new Runnable() { - @Override - public void run() { - Project owner = FileOwnerQuery.getOwner(file); - if (owner != null) { - ActionProvider ap = owner.getLookup().lookup(ActionProvider.class); - if (ap != null) { - if (Arrays.asList(ap.getSupportedActions()).contains(command) && ap.isActionEnabled(command, Lookups.singleton(file))) { - ap.invokeAction(command, Lookups.fixed(file, progressOfCommand)); - } - } - } - } - }); - } - @Override public CompletableFuture> symbol(WorkspaceSymbolParams params) { String query = params.getQuery(); diff --git a/java/java.lsp.server/vscode/src/extension.ts b/java/java.lsp.server/vscode/src/extension.ts index e43f6ad494ee..2e0895336ce3 100644 --- a/java/java.lsp.server/vscode/src/extension.ts +++ b/java/java.lsp.server/vscode/src/extension.ts @@ -211,7 +211,7 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { ]); } })); - context.subscriptions.push(commands.registerCommand('java.debug.codelens', async (uri, methodName) => { + const runCodelens = async (uri, methodName, noDebug) => { const editor = window.activeTextEditor; if (editor) { const docUri = editor.document.uri; @@ -223,8 +223,17 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { mainClass: uri, singleMethod: methodName, }; - await vscode.debug.startDebugging(workspaceFolder, debugConfig).then(); + const debugOptions = { + noDebug: noDebug, + } + await vscode.debug.startDebugging(workspaceFolder, debugConfig, debugOptions).then(); } + }; + context.subscriptions.push(commands.registerCommand('java.run.codelens', async (uri, methodName) => { + await runCodelens(uri, methodName, true); + })); + context.subscriptions.push(commands.registerCommand('java.debug.codelens', async (uri, methodName) => { + await runCodelens(uri, methodName, false); })); return Object.freeze({ version : API_VERSION