From 980c7f261edc4e4eed0d30056de05b3018112265 Mon Sep 17 00:00:00 2001 From: Dusan Balek Date: Tue, 14 May 2024 10:58:50 +0200 Subject: [PATCH] LSP: Allow for invocation of 'nbls.new.from.template' command passing template as parameter. --- .../lsp/server/protocol/LspTemplateUI.java | 25 +++++++++++++++---- java/java.lsp.server/vscode/src/extension.ts | 11 +++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspTemplateUI.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspTemplateUI.java index 2c5ae8967323..891bfa328de6 100644 --- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspTemplateUI.java +++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/protocol/LspTemplateUI.java @@ -106,7 +106,7 @@ static CompletableFuture createProject(String templates, NbCodeLanguageC } private CompletableFuture templateUI(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) { - CompletionStage findTemplate = findTemplate(templates, client); + CompletionStage findTemplate = findTemplate(templates, client, params); CompletionStage findTargetFolder = findTargetForTemplate(findTemplate, client, params); return findTargetFolder.thenCombine(findTemplate, (target, source) -> { final FileObject templateFileObject = source.getPrimaryFile(); @@ -138,8 +138,8 @@ private CompletableFuture templateUI(DataFolder templates, NbCodeLanguag } private CompletableFuture projectUI(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) { - CompletionStage findTemplate = findTemplate(templates, client); - CompletionStage> findTargetFolderAndName = findTargetAndNameForProject(findTemplate, client, params); + CompletionStage findTemplate = findTemplate(templates, client, params); + CompletionStage> findTargetFolderAndName = findTargetAndNameForProject(findTemplate, client); CompletionStage> findTemplateAndPackage = findTemplate.thenCombine(findPackage(findTargetFolderAndName, client), Pair::of); return findTargetFolderAndName.thenCombineAsync(findTemplateAndPackage, (targetAndName, templateAndPackage) -> { try { @@ -200,7 +200,7 @@ public CompletionStage apply(String path) { }); } - private static CompletionStage> findTargetAndNameForProject(CompletionStage findTemplate, NbCodeLanguageClient client, ExecuteCommandParams params) { + private static CompletionStage> findTargetAndNameForProject(CompletionStage findTemplate, NbCodeLanguageClient client) { return findTemplate.thenCompose(__ -> client.workspaceFolders()).thenCompose(folders -> { class VerifyNonExistingFolder implements Function>> { @Override @@ -245,7 +245,22 @@ private static String suggestWorkspaceRoot(List folders) throws return suggestion; } - private static CompletionStage findTemplate(DataFolder templates, NbCodeLanguageClient client) { + private static CompletionStage findTemplate(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) { + List args = params.getArguments(); + if (!args.isEmpty()) { + Object arg = args.get(0); + String path = arg instanceof JsonPrimitive ? ((JsonPrimitive) arg).getAsString() : arg != null ? arg.toString() : null; + if (path != null) { + FileObject fo = templates.getPrimaryFile().getFileObject(path); + if (fo != null) { + try { + return CompletableFuture.completedStage(DataObject.find(fo)); + } catch (DataObjectNotFoundException ex) { + throw raise(RuntimeException.class, ex); + } + } + } + } final List categories = quickPickTemplates(templates); final CompletionStage> pickGroup = client.showQuickPick(new ShowQuickPickParams(Bundle.CTL_TemplateUI_SelectGroup(), categories)); final CompletionStage group = pickGroup.thenApply((selectedGroups) -> { diff --git a/java/java.lsp.server/vscode/src/extension.ts b/java/java.lsp.server/vscode/src/extension.ts index fa61aef06833..7a0e4f19bb82 100644 --- a/java/java.lsp.server/vscode/src/extension.ts +++ b/java/java.lsp.server/vscode/src/extension.ts @@ -450,12 +450,17 @@ export function activate(context: ExtensionContext): VSNetBeansAPI { }); // register commands - context.subscriptions.push(commands.registerCommand(COMMAND_PREFIX + '.workspace.new', async (ctx) => { + context.subscriptions.push(commands.registerCommand(COMMAND_PREFIX + '.workspace.new', async (ctx, template) => { let c : LanguageClient = await client; const commands = await vscode.commands.getCommands(); if (commands.includes(COMMAND_PREFIX + '.new.from.template')) { - // first give the context, then the open-file hint in the case the context is not specific enough - const res = await vscode.commands.executeCommand(COMMAND_PREFIX + '.new.from.template', contextUri(ctx)?.toString(), vscode.window.activeTextEditor?.document?.uri?.toString()); + // first give the template (if present), then the context, and then the open-file hint in the case the context is not specific enough + const params = []; + if (typeof template === 'string') { + params.push(template); + } + params.push(contextUri(ctx)?.toString(), vscode.window.activeTextEditor?.document?.uri?.toString()); + const res = await vscode.commands.executeCommand(COMMAND_PREFIX + '.new.from.template', ...params); if (typeof res === 'string') { let newFile = vscode.Uri.parse(res as string);