|
1 | 1 | // Copyright (c) jdneo. All rights reserved.
|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 |
|
| 4 | +import * as os from "os"; |
4 | 5 | import * as path from "path";
|
5 | 6 | import * as vscode from "vscode";
|
6 |
| -import { getWorkspaceFolder } from "./settingUtils"; |
| 7 | +import { IQuickItemEx } from "../shared"; |
| 8 | +import { getWorkspaceConfiguration, getWorkspaceFolder } from "./settingUtils"; |
| 9 | +import { showDirectorySelectDialog } from "./uiUtils"; |
7 | 10 | import * as wsl from "./wslUtils";
|
8 | 11 |
|
9 | 12 | export async function selectWorkspaceFolder(): Promise<string> {
|
10 |
| - const workspaceFolderSetting: string = getWorkspaceFolder(); |
| 13 | + let workspaceFolderSetting: string = getWorkspaceFolder(); |
| 14 | + if (workspaceFolderSetting.trim() === "") { |
| 15 | + workspaceFolderSetting = await determineLeetCodeFolder(); |
| 16 | + if (workspaceFolderSetting === "") { |
| 17 | + // User cancelled |
| 18 | + return workspaceFolderSetting; |
| 19 | + } |
| 20 | + } |
11 | 21 | const workspaceFolders: vscode.WorkspaceFolder[] = vscode.workspace.workspaceFolders || [];
|
12 | 22 | let needAsk: boolean = true;
|
13 | 23 | for (const folder of workspaceFolders) {
|
@@ -70,6 +80,42 @@ function isSubFolder(from: string, to: string): boolean {
|
70 | 80 | return !relative.startsWith("..") && !path.isAbsolute(relative);
|
71 | 81 | }
|
72 | 82 |
|
| 83 | +async function determineLeetCodeFolder(): Promise<string> { |
| 84 | + let result: string; |
| 85 | + const picks: Array<IQuickItemEx<string>> = []; |
| 86 | + picks.push( |
| 87 | + { |
| 88 | + label: `Default location`, |
| 89 | + detail: `${path.join(os.homedir(), ".leetcode")}`, |
| 90 | + value: `${path.join(os.homedir(), ".leetcode")}`, |
| 91 | + }, |
| 92 | + { |
| 93 | + label: "$(file-directory) Browse...", |
| 94 | + value: ":browse", |
| 95 | + }, |
| 96 | + ); |
| 97 | + const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick( |
| 98 | + picks, |
| 99 | + { placeHolder: "Select where you would like to save your LeetCode files" }, |
| 100 | + ); |
| 101 | + if (!choice) { |
| 102 | + result = ""; |
| 103 | + } else if (choice.value === ":browse") { |
| 104 | + const directory: vscode.Uri[] | undefined = await showDirectorySelectDialog(); |
| 105 | + if (!directory || directory.length < 1) { |
| 106 | + result = ""; |
| 107 | + } else { |
| 108 | + result = directory[0].fsPath; |
| 109 | + } |
| 110 | + } else { |
| 111 | + result = choice.value; |
| 112 | + } |
| 113 | + |
| 114 | + getWorkspaceConfiguration().update("workspaceFolder", result, vscode.ConfigurationTarget.Global); |
| 115 | + |
| 116 | + return result; |
| 117 | +} |
| 118 | + |
73 | 119 | enum OpenOption {
|
74 | 120 | openInCurrentWindow = "Open in current window",
|
75 | 121 | openInNewWindow = "Open in new window",
|
|
0 commit comments