This is an automated email from the ASF dual-hosted git repository. dbalek pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 717f17db2a Modified to ensure opened file focused. (#5930) 717f17db2a is described below commit 717f17db2a12cdf56675c82f7dcd56e2ad3ef77a Author: Dusan Balek <dusan.ba...@oracle.com> AuthorDate: Tue May 9 14:50:06 2023 +0200 Modified to ensure opened file focused. (#5930) --- .../modules/java/lsp/server/protocol/Server.java | 7 +- .../lsp/server/protocol/WorkspaceServiceImpl.java | 91 +++++++++++----------- java/java.lsp.server/vscode/src/extension.ts | 35 +-------- 3 files changed, 54 insertions(+), 79 deletions(-) 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 d10758d913..4945aa243f 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 @@ -76,6 +76,7 @@ import org.eclipse.lsp4j.WorkDoneProgressParams; import org.eclipse.lsp4j.WorkspaceFolder; import org.eclipse.lsp4j.WorkspaceFoldersOptions; import org.eclipse.lsp4j.WorkspaceServerCapabilities; +import org.eclipse.lsp4j.WorkspaceSymbolOptions; import org.eclipse.lsp4j.jsonrpc.Endpoint; import org.eclipse.lsp4j.jsonrpc.JsonRpcException; import org.eclipse.lsp4j.jsonrpc.Launcher; @@ -793,7 +794,6 @@ public final class Server { JAVA_PROJECT_CONFIGURATION_COMPLETION, JAVA_PROJECT_RESOLVE_PROJECT_PROBLEMS, JAVA_SUPER_IMPLEMENTATION, - JAVA_SOURCE_FOR, JAVA_CLEAR_PROJECT_CACHES, NATIVE_IMAGE_FIND_DEBUG_PROCESS_TO_ATTACH, JAVA_PROJECT_INFO @@ -802,7 +802,9 @@ public final class Server { commands.addAll(codeActionsProvider.getCommands()); } capabilities.setExecuteCommandProvider(new ExecuteCommandOptions(new ArrayList<>(commands))); - capabilities.setWorkspaceSymbolProvider(true); + WorkspaceSymbolOptions wsOpts = new WorkspaceSymbolOptions(); + wsOpts.setResolveProvider(true); + capabilities.setWorkspaceSymbolProvider(wsOpts); capabilities.setCodeLensProvider(new CodeLensOptions(false)); RenameOptions renOpt = new RenameOptions(); renOpt.setPrepareProvider(true); @@ -965,7 +967,6 @@ public final class Server { public static final String JAVA_GET_PROJECT_PACKAGES = "java.get.project.packages"; public static final String JAVA_LOAD_WORKSPACE_TESTS = "java.load.workspace.tests"; public static final String JAVA_SUPER_IMPLEMENTATION = "java.super.implementation"; - public static final String JAVA_SOURCE_FOR = "java.source.for"; public static final String GRAALVM_PAUSE_SCRIPT = "graalvm.pause.script"; public static final String JAVA_RUN_PROJECT_ACTION = "java.project.run.action"; 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 1a4464573e..6924f13899 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 @@ -399,50 +399,6 @@ public final class WorkspaceServiceImpl implements WorkspaceService, LanguageCli String uri = ((JsonPrimitive) params.getArguments().get(0)).getAsString(); Position pos = gson.fromJson(gson.toJson(params.getArguments().get(1)), Position.class); return (CompletableFuture)((TextDocumentServiceImpl)server.getTextDocumentService()).superImplementations(uri, pos); - case Server.JAVA_SOURCE_FOR: { - CompletableFuture<Object> result = new CompletableFuture<>(); - try { - String sourceUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString(); - if (sourceUri == null || !sourceUri.startsWith(SOURCE_FOR)) { - throw new IllegalArgumentException("Invalid uri: " + sourceUri); - } - sourceUri = URLDecoder.decode(sourceUri.substring(SOURCE_FOR.length()), StandardCharsets.UTF_8.toString()); - int qIdx = sourceUri.lastIndexOf('?'); - int hIdx = sourceUri.lastIndexOf('#'); - if (qIdx < 0 || hIdx < 0 || hIdx <= qIdx) { - throw new IllegalArgumentException("Invalid uri: " + sourceUri); - } - String rootUri = sourceUri.substring(0, qIdx); - FileObject root = URLMapper.findFileObject(URI.create(rootUri).toURL()); - if (root == null) { - throw new IllegalStateException("Unable to find root: " + rootUri); - } - ElementHandle typeHandle = ElementHandleAccessor.getInstance().create(ElementKind.valueOf(sourceUri.substring(qIdx + 1, hIdx)), sourceUri.substring(hIdx + 1)); - CompletableFuture<ElementOpen.Location> location = ElementOpen.getLocation(ClasspathInfo.create(root), typeHandle, typeHandle.getQualifiedName().replace('.', '/') + ".class"); - location.exceptionally(ex -> { - result.completeExceptionally(ex); - return null; - }).thenAccept(loc -> { - if (loc != null) { - ShowDocumentParams sdp = new ShowDocumentParams(Utils.toUri(loc.getFileObject())); - Position position = Utils.createPosition(loc.getFileObject(), loc.getStartOffset()); - sdp.setSelection(new Range(position, position)); - client.showDocument(sdp).thenAccept(res -> { - if (res.isSuccess()) { - result.complete(null); - } else { - result.completeExceptionally(new IllegalStateException("Cannot open source for: " + typeHandle.getQualifiedName())); - } - }); - } else if (!result.isCompletedExceptionally()) { - result.completeExceptionally(new IllegalStateException("Cannot find source for: " + typeHandle.getQualifiedName())); - } - }); - } catch (Throwable t) { - result.completeExceptionally(t); - } - return result; - } case Server.JAVA_FIND_PROJECT_CONFIGURATIONS: { String fileUri = ((JsonPrimitive) params.getArguments().get(0)).getAsString(); @@ -1059,6 +1015,53 @@ public final class WorkspaceServiceImpl implements WorkspaceService, LanguageCli return result; } + @Override + public CompletableFuture<WorkspaceSymbol> resolveWorkspaceSymbol(WorkspaceSymbol workspaceSymbol) { + String sourceUri = workspaceSymbol.getLocation().isLeft() ? workspaceSymbol.getLocation().getLeft().getUri() : workspaceSymbol.getLocation().getRight().getUri(); + if (!sourceUri.startsWith(SOURCE_FOR)) { + return CompletableFuture.completedFuture(workspaceSymbol); + } + CompletableFuture<WorkspaceSymbol> result = new CompletableFuture<>(); + try { + sourceUri = URLDecoder.decode(sourceUri.substring(SOURCE_FOR.length()), StandardCharsets.UTF_8.toString()); + int qIdx = sourceUri.lastIndexOf('?'); + int hIdx = sourceUri.lastIndexOf('#'); + if (qIdx < 0 || hIdx < 0 || hIdx <= qIdx) { + throw new IllegalArgumentException("Invalid uri: " + sourceUri); + } + String rootUri = sourceUri.substring(0, qIdx); + FileObject root = URLMapper.findFileObject(URI.create(rootUri).toURL()); + if (root == null) { + throw new IllegalStateException("Unable to find root: " + rootUri); + } + ElementHandle typeHandle = ElementHandleAccessor.getInstance().create(ElementKind.valueOf(sourceUri.substring(qIdx + 1, hIdx)), sourceUri.substring(hIdx + 1)); + CompletableFuture<ElementOpen.Location> location = ElementOpen.getLocation(ClasspathInfo.create(root), typeHandle, typeHandle.getQualifiedName().replace('.', '/') + ".class"); + location.exceptionally(ex -> { + result.completeExceptionally(ex); + return null; + }).thenAccept(loc -> { + if (loc != null) { + ShowDocumentParams sdp = new ShowDocumentParams(Utils.toUri(loc.getFileObject())); + Position position = Utils.createPosition(loc.getFileObject(), loc.getStartOffset()); + sdp.setSelection(new Range(position, position)); + client.showDocument(sdp).thenAccept(res -> { + if (res.isSuccess()) { + workspaceSymbol.setLocation(Either.forLeft(new Location(Utils.toUri(loc.getFileObject()), new Range(position, position)))); + result.complete(workspaceSymbol); + } else { + result.completeExceptionally(new IllegalStateException("Cannot open source for: " + typeHandle.getQualifiedName())); + } + }); + } else if (!result.isCompletedExceptionally()) { + result.completeExceptionally(new IllegalStateException("Cannot find source for: " + typeHandle.getQualifiedName())); + } + }); + } catch (Throwable t) { + result.completeExceptionally(t); + } + return result; + } + private Location tree2Location(CompilationInfo info, TreePath path) { return new Location(Utils.toUri(info.getFileObject()), Utils.treeRange(info, path.getLeaf())); diff --git a/java/java.lsp.server/vscode/src/extension.ts b/java/java.lsp.server/vscode/src/extension.ts index 459d567fa3..8a05ba4dbc 100644 --- a/java/java.lsp.server/vscode/src/extension.ts +++ b/java/java.lsp.server/vscode/src/extension.ts @@ -409,10 +409,6 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('java+', debugDescriptionFactory)); context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('nativeimage', debugDescriptionFactory)); - // register content provider - let sourceForContentProvider = new NetBeansSourceForContentProvider(context); - context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('sourceFor', sourceForContentProvider)); - // initialize Run Configuration initializeRunConfiguration().then(initialized => { if (initialized) { @@ -484,7 +480,9 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { wrapProjectActionWithProgress('clean', undefined, 'Cleaning...', log, true, args); })); context.subscriptions.push(commands.registerCommand('java.open.type', () => { - wrapCommandWithProgress('java.quick.open', 'Opening type...', log, true); + wrapCommandWithProgress('java.quick.open', 'Opening type...', log, true).then(() => { + commands.executeCommand('workbench.action.focusActiveEditorGroup'); + }); })); context.subscriptions.push(commands.registerCommand('java.goto.super.implementation', async () => { if (window.activeTextEditor?.document.languageId !== "java") { @@ -1466,30 +1464,3 @@ class NetBeansConfigurationNativeResolver implements vscode.DebugConfigurationPr return config; } } - -class NetBeansSourceForContentProvider implements vscode.TextDocumentContentProvider { - - private uri: vscode.Uri | undefined; - - constructor(context: ExtensionContext) { - context.subscriptions.push(vscode.window.onDidChangeVisibleTextEditors(eds => { - eds.forEach(ed => { - if (this.uri && this.uri.toString() === ed.document.uri.toString()) { - ed.hide(); - this.uri = undefined; - } - }); - })); - } - - provideTextDocumentContent(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<string> { - this.uri = uri; - vscode.window.withProgress({location: ProgressLocation.Notification, title: 'Finding source...', cancellable: false}, () => { - return vscode.commands.executeCommand('java.source.for', uri.toString()).then(() => { - }, (reason: any) => { - vscode.window.showErrorMessage(reason.data); - }); - }); - return Promise.resolve(''); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists