Skip to content

Commit

Permalink
vscode: Better options on project location (#5924)
Browse files Browse the repository at this point in the history
This always asks for the user to select a location for the project.
If VScode is already in a folder it will default to that location.

Then at the end the user is asked if they wish to open the project in a new window, the current window or current window, but new workspace.

Typescript tweaked to be less hardcoded strings with potential errors from typos.
  • Loading branch information
NigelBreslaw authored Aug 25, 2024
1 parent 1479097 commit 4ca3760
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions editors/vscode/src/quick_picks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import * as fs from "fs-extra";
import simpleGit from "simple-git";
import * as path from "node:path";

// Flow
// (1) What language? (2) What directory? (3) What name? (4) Open in current window or new window?

export async function newProject(context: vscode.ExtensionContext) {
type Language = "Node (JavaScript/TypeScript)" | "C++" | "Rust";
const node = "Node (JavaScript/TypeScript)";
const cpp = "C++";
const rust = "Rust";
const LANGUAGES = [node, cpp, rust] as const;
type Language = typeof LANGUAGES[number];

const language = await vscode.window.showQuickPick(
["Node (JavaScript/TypeScript)", "C++", "Rust"],
LANGUAGES,
{
placeHolder: "What language do you want to use?",
},
Expand All @@ -23,13 +30,13 @@ export async function newProject(context: vscode.ExtensionContext) {

let repoUrl: string | undefined;
switch (language) {
case "Node (JavaScript/TypeScript)":
case node:
repoUrl = "https://github.com/slint-ui/slint-nodejs-template";
break;
case "C++":
case cpp:
repoUrl = "https://github.com/slint-ui/slint-cpp-template";
break;
case "Rust":
case rust:
repoUrl = "https://github.com/slint-ui/slint-rust-template";
break;
default:
Expand All @@ -38,25 +45,25 @@ export async function newProject(context: vscode.ExtensionContext) {
}

let workspacePath: string | undefined =
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;

if (!workspacePath) {
const folderUris = await vscode.window.showOpenDialog({
canSelectFolders: true,
canSelectMany: false,
openLabel: "Select Folder",
});

if (!folderUris || folderUris.length === 0) {
vscode.window.showErrorMessage(
"Please select a folder to place the project in.",
);
return;
}
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;

const folderUris = await vscode.window.showOpenDialog({
canSelectFolders: true,
canSelectMany: false,
openLabel: "Open",
defaultUri: workspacePath ? vscode.Uri.file(workspacePath) : undefined,
title: "Choose Location for New Slint Project"
});

workspacePath = folderUris[0].fsPath;
if (!folderUris || folderUris.length === 0) {
vscode.window.showErrorMessage(
"Please select a folder to place the project in.",
);
return;
}

workspacePath = folderUris[0].fsPath;

const projectName = await vscode.window.showInputBox({
prompt: "Enter the name of the new project",
});
Expand Down Expand Up @@ -96,7 +103,39 @@ export async function newProject(context: vscode.ExtensionContext) {
await fs.writeJson(settingsFilePath, settingsContent, { spaces: 2 });

const uri = vscode.Uri.file(projectPath);
await vscode.commands.executeCommand("vscode.openFolder", uri, true);

const openNewWindow = 'Open in New Window';
const openCurrentWindow = 'Open in Current Window';
const addToWorkspace = 'Add to Workspace';
const PROJECT_OPTIONS = [openNewWindow, openCurrentWindow, addToWorkspace] as const;
type ProjectOption = typeof PROJECT_OPTIONS[number];

const choice = await vscode.window.showInformationMessage(
`How Would You Like to Open Project "${projectName}"?`,
{ modal: true },
openNewWindow,
openCurrentWindow,
addToWorkspace
) as ProjectOption;

switch (choice) {
case openNewWindow:
await vscode.commands.executeCommand("vscode.openFolder", uri, true);
break;
case openCurrentWindow:
await vscode.commands.executeCommand("vscode.openFolder", uri, false);
break;
case addToWorkspace:
await vscode.workspace.updateWorkspaceFolders(
vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0,
null,
{ uri }
);
break;
default:
// If the user closes the dialog without choosing, default to opening in a new window
await vscode.commands.executeCommand("vscode.openFolder", uri, true);
}
} catch (err: unknown) {
const errorMessage =
err instanceof Error ? err.message : "Unknown error";
Expand Down

0 comments on commit 4ca3760

Please sign in to comment.