Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LSP: New from Template invocation should accept template as an optional parameter #7379

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static CompletableFuture<Object> createProject(String templates, NbCodeLanguageC
}

private CompletableFuture<Object> templateUI(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) {
CompletionStage<DataObject> findTemplate = findTemplate(templates, client);
CompletionStage<DataObject> findTemplate = findTemplate(templates, client, params);
CompletionStage<DataFolder> findTargetFolder = findTargetForTemplate(findTemplate, client, params);
return findTargetFolder.thenCombine(findTemplate, (target, source) -> {
final FileObject templateFileObject = source.getPrimaryFile();
Expand Down Expand Up @@ -138,8 +138,8 @@ private CompletableFuture<Object> templateUI(DataFolder templates, NbCodeLanguag
}

private CompletableFuture<Object> projectUI(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) {
CompletionStage<DataObject> findTemplate = findTemplate(templates, client);
CompletionStage<Pair<DataFolder, String>> findTargetFolderAndName = findTargetAndNameForProject(findTemplate, client, params);
CompletionStage<DataObject> findTemplate = findTemplate(templates, client, params);
CompletionStage<Pair<DataFolder, String>> findTargetFolderAndName = findTargetAndNameForProject(findTemplate, client);
CompletionStage<Pair<DataObject, String>> findTemplateAndPackage = findTemplate.thenCombine(findPackage(findTargetFolderAndName, client), Pair::of);
return findTargetFolderAndName.thenCombineAsync(findTemplateAndPackage, (targetAndName, templateAndPackage) -> {
try {
Expand Down Expand Up @@ -200,7 +200,7 @@ public CompletionStage<DataFolder> apply(String path) {
});
}

private static CompletionStage<Pair<DataFolder, String>> findTargetAndNameForProject(CompletionStage<DataObject> findTemplate, NbCodeLanguageClient client, ExecuteCommandParams params) {
private static CompletionStage<Pair<DataFolder, String>> findTargetAndNameForProject(CompletionStage<DataObject> findTemplate, NbCodeLanguageClient client) {
return findTemplate.thenCompose(__ -> client.workspaceFolders()).thenCompose(folders -> {
class VerifyNonExistingFolder implements Function<String, CompletionStage<Pair<DataFolder,String>>> {
@Override
Expand Down Expand Up @@ -245,7 +245,22 @@ private static String suggestWorkspaceRoot(List<WorkspaceFolder> folders) throws
return suggestion;
}

private static CompletionStage<DataObject> findTemplate(DataFolder templates, NbCodeLanguageClient client) {
private static CompletionStage<DataObject> findTemplate(DataFolder templates, NbCodeLanguageClient client, ExecuteCommandParams params) {
List<Object> 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<QuickPickItem> categories = quickPickTemplates(templates);
final CompletionStage<List<QuickPickItem>> pickGroup = client.showQuickPick(new ShowQuickPickParams(Bundle.CTL_TemplateUI_SelectGroup(), categories));
final CompletionStage<DataFolder> group = pickGroup.thenApply((selectedGroups) -> {
Expand Down
11 changes: 8 additions & 3 deletions java/java.lsp.server/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading