Unverified Commit 09d83f0b authored by Svatopluk Dedic's avatar Svatopluk Dedic Committed by GitHub
Browse files

Merge pull request #2725 from entlicher/vscodeext_CodelensOutputFixes

[NETBEANS-5326] LSP codelens output fixes
parents f6dfdf30 cb0f5dbb
......@@ -118,11 +118,14 @@ public final class NbSourceProvider {
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;
}
}
}
}
......
......@@ -27,7 +27,6 @@ import java.util.function.BiConsumer;
import java.util.function.Supplier;
import org.eclipse.lsp4j.debug.StoppedEventArguments;
import org.eclipse.lsp4j.debug.TerminatedEventArguments;
import org.eclipse.lsp4j.debug.ThreadEventArguments;
import org.netbeans.api.debugger.DebuggerEngine;
import org.netbeans.api.debugger.DebuggerManager;
......@@ -63,15 +62,6 @@ public final class NbThreads {
}
private void initThreads(DebugAdapterContext context, JPDADebugger debugger) {
debugger.addPropertyChangeListener(JPDADebugger.PROP_STATE, evt -> {
int newState = (int) evt.getNewValue();
switch (newState) {
case JPDADebugger.STATE_DISCONNECTED:
//debugger.removePropertyChangeListener(this);
context.getClient().terminated(new TerminatedEventArguments());
break;
}
});
DebuggerEngine engine = debugger.getSession().getCurrentEngine();
if (engine == null) {
debugger.getSession().addPropertyChangeListener(Session.PROP_CURRENT_LANGUAGE, new PropertyChangeListener() {
......
......@@ -39,6 +39,7 @@ import org.eclipse.lsp4j.debug.OutputEventArguments;
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;
......@@ -58,8 +59,9 @@ public final class NbLaunchRequestHandler {
public CompletableFuture<Void> launch(Map<String, Object> launchArguments, DebugAdapterContext context) {
CompletableFuture<Void> resultFuture = new CompletableFuture<>();
boolean noDebug = (Boolean) launchArguments.getOrDefault("noDebug", Boolean.FALSE);
activeLaunchHandler = noDebug ? new NbLaunchWithoutDebuggingDelegate((daContext) -> handleTerminatedEvent(daContext))
: new NbLaunchWithDebuggingDelegate();
Consumer<DebugAdapterContext> terminateHandle = (daContext) -> handleTerminatedEvent(daContext);
activeLaunchHandler = noDebug ? new NbLaunchWithoutDebuggingDelegate(terminateHandle)
: new NbLaunchWithDebuggingDelegate(terminateHandle);
// validation
List<String> modulePaths = (List<String>) launchArguments.getOrDefault("modulePaths", Collections.emptyList());
List<String> classPaths = (List<String>) launchArguments.getOrDefault("classPaths", Collections.emptyList());
......@@ -112,6 +114,19 @@ public final class NbLaunchRequestHandler {
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);
......@@ -161,7 +176,7 @@ public final class NbLaunchRequestHandler {
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.
}
......
......@@ -19,6 +19,7 @@
package org.netbeans.modules.java.lsp.server.debugging.launch;
import java.util.Map;
import java.util.function.Consumer;
import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext;
......@@ -28,6 +29,18 @@ import org.netbeans.modules.java.lsp.server.debugging.DebugAdapterContext;
*/
public final class NbLaunchWithDebuggingDelegate extends NbLaunchDelegate {
private final Consumer<DebugAdapterContext> onFinishCallback;
NbLaunchWithDebuggingDelegate(Consumer<DebugAdapterContext> onFinishCallback) {
this.onFinishCallback = onFinishCallback;
}
@Override
protected void notifyFinished(DebugAdapterContext ctx, boolean success) {
super.notifyFinished(ctx, success);
onFinishCallback.accept(ctx);
}
@Override
public void postLaunch(Map<String, Object> launchArguments, DebugAdapterContext context) {
context.getClient().initialized();
......
......@@ -322,8 +322,7 @@ public final class Server {
capabilities.setDocumentHighlightProvider(true);
capabilities.setReferencesProvider(true);
List<String> 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 @@ public final class Server {
}
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 ";
......
......@@ -1375,7 +1375,7 @@ public class TextDocumentServiceImpl implements TextDocumentService, LanguageCli
Utils.createPosition(cc.getCompilationUnit(), method.end().getOffset()));
List<Object> 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 class TextDocumentServiceImpl implements TextDocumentService, LanguageCli
Range range = Utils.treeRange(cc, tree);
List<Object> 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),
......
......@@ -112,33 +112,6 @@ public final class WorkspaceServiceImpl implements WorkspaceService, LanguageCli
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 final class WorkspaceServiceImpl implements WorkspaceService, LanguageCli
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<List<? extends SymbolInformation>> symbol(WorkspaceSymbolParams params) {
String query = params.getQuery();
......
......@@ -38,9 +38,9 @@
"integrity": "sha512-HI5l+f38o93x81mbOWZ1IEzj87rGCHfN4A4QyCU1MuViT5Slvlo5F+YVvmBFHfZsEGi0Lo8TghWU2Ew6vBILNA=="
},
"@types/vscode": {
"version": "1.47.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.47.0.tgz",
"integrity": "sha512-nJA37ykkz9FYA0ZOQUSc3OZnhuzEW2vUhUEo4MiduUo82jGwwcLfyvmgd/Q7b0WrZAAceojGhZybg319L24bTA==",
"version": "1.49.0",
"resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.49.0.tgz",
"integrity": "sha512-wfNQmLmm1VdMBr6iuNdprWmC1YdrgZ9dQzadv+l2eSjJlElOdJw8OTm4RU4oGTBcfvG6RZI2jOcppkdSS18mZw==",
"dev": true
},
"agent-base": {
......
......@@ -21,7 +21,7 @@
"multi-root ready"
],
"engines": {
"vscode": "^1.47.0"
"vscode": "^1.49.0"
},
"activationEvents": [
"onLanguage:java",
......@@ -175,7 +175,7 @@
"nbjavac": "node ./out/nbcode.js -J-Dnetbeans.close=true --modules --install .*nbjavac.*"
},
"devDependencies": {
"@types/vscode": "^1.47.0",
"@types/vscode": "^1.49.0",
"@types/glob": "^7.1.1",
"@types/mocha": "^7.0.2",
"@types/node": "^13.11.0",
......
......@@ -211,20 +211,29 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
]);
}
}));
context.subscriptions.push(commands.registerCommand('java.debug.codelens', async (uri, methodName) => {
const runCodelens = async (uri : any, methodName : string, noDebug : boolean) => {
const editor = window.activeTextEditor;
if (editor) {
const docUri = editor.document.uri;
const workspaceFolder = vscode.workspace.getWorkspaceFolder(docUri);
const debugConfig = {
const debugConfig : vscode.DebugConfiguration = {
type: "java8+",
name: "CodeLens Debug",
request: "launch",
mainClass: uri,
singleMethod: methodName,
};
await vscode.debug.startDebugging(workspaceFolder, debugConfig).then();
const debugOptions : vscode.DebugSessionOptions = {
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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment