Skip to content

Commit

Permalink
feat: Can select folder to save the LeetCode files (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Jul 12, 2019
1 parent a00dac5 commit e1f3206
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
| `leetcode.defaultLanguage` | Specify the default language used to solve the problem. Supported languages are: `bash`, `c`, `cpp`, `csharp`, `golang`, `java`, `javascript`, `kotlin`, `mysql`, `php`, `python`,`python3`,`ruby`,`rust`, `scala`,`swift` | `N/A` |
| `leetcode.useWsl` | Specify whether to use WSL or not | `false` |
| `leetcode.endpoint` | Specify the active endpoint. Supported endpoints are: `leetcode`, `leetcode-cn` | `leetcode` |
| `leetcode.workspaceFolder` | Specify the path of the workspace folder to store the problem files. | `${home}/.leetcode` |
| `leetcode.workspaceFolder` | Specify the path of the workspace folder to store the problem files. | `""` |
| `leetcode.outputFolder` | Specify the relative path to save the problem files. Besides using customized path, there are also several reserved words which can be used here: <ul><li>`${tag}`: Categorize the problem according to their tags.<li>`${language}`: Categorize the problem according to their language.</li><li>`${difficulty}`: Categorize the problem according to their difficulty.</li></ul>For example: `problem-${tag}-${difficulty}` | N/A |
| `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` |
| **(Deprecated)** `leetcode.enableShortcuts` | Specify whether the submit and test shortcuts in editor or not. | `true` |
Expand Down
2 changes: 1 addition & 1 deletion docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
| `leetcode.defaultLanguage` | 指定答题时使用的默认语言,可选语言有:`bash`, `c`, `cpp`, `csharp`, `golang`, `java`, `javascript`, `kotlin`, `mysql`, `php`, `python`,`python3`,`ruby`, `rust`, `scala`,`swift` | `N/A` |
| `leetcode.useWsl` | 指定是否启用 WSL | `false` |
| `leetcode.endpoint` | 指定使用的终端,可用终端有:`leetcode`, `leetcode-cn` | `leetcode` |
| `leetcode.workspaceFolder` | 指定保存文件的工作区目录 | `${home}/.leetcode` |
| `leetcode.workspaceFolder` | 指定保存文件的工作区目录 | `""` |
| `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:<ul><li>`${tag}`: 根据题目的类别进行分类。<li>`${language}`: 根据题目的语言进行分类。</li><li>`${difficulty}`: 根据题目的难度进行分类。</li></ul>例如:`problem-${tag}-${difficulty}` | N/A |
| `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` |
| **(Deprecated)** `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
"type": "string",
"scope": "application",
"description": "The path of the workspace folder to store the problem files.",
"default": "${home}/.leetcode"
"default": ""
},
"leetcode.outputFolder": {
"type": "string",
Expand Down
4 changes: 1 addition & 3 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import * as os from "os";
import { workspace, WorkspaceConfiguration } from "vscode";

export function getWorkspaceConfiguration(): WorkspaceConfiguration {
Expand All @@ -13,8 +12,7 @@ export function shouldHideSolvedProblem(): boolean {
}

export function getWorkspaceFolder(): string {
const rawWorkspaceFolder: string = getWorkspaceConfiguration().get<string>("workspaceFolder", "${home}/.leetcode");
return rawWorkspaceFolder.replace(/\${home}/i, os.homedir());
return getWorkspaceConfiguration().get<string>("workspaceFolder", "");
}

export function getEditorShortcuts(): string[] {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/uiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ export async function showFileSelectDialog(): Promise<vscode.Uri[] | undefined>
return await vscode.window.showOpenDialog(options);
}

export async function showDirectorySelectDialog(): Promise<vscode.Uri[] | undefined> {
const defaultUri: vscode.Uri | undefined = vscode.workspace.rootPath ? vscode.Uri.file(vscode.workspace.rootPath) : undefined;
const options: vscode.OpenDialogOptions = {
defaultUri,
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
openLabel: "Select",
};
return await vscode.window.showOpenDialog(options);
}

export async function openUrl(url: string): Promise<void> {
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(url));
}
Expand Down
50 changes: 48 additions & 2 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { getWorkspaceFolder } from "./settingUtils";
import { IQuickItemEx } from "../shared";
import { getWorkspaceConfiguration, getWorkspaceFolder } from "./settingUtils";
import { showDirectorySelectDialog } from "./uiUtils";
import * as wsl from "./wslUtils";

export async function selectWorkspaceFolder(): Promise<string> {
const workspaceFolderSetting: string = getWorkspaceFolder();
let workspaceFolderSetting: string = getWorkspaceFolder();
if (workspaceFolderSetting.trim() === "") {
workspaceFolderSetting = await determineLeetCodeFolder();
if (workspaceFolderSetting === "") {
// User cancelled
return workspaceFolderSetting;
}
}
const workspaceFolders: vscode.WorkspaceFolder[] = vscode.workspace.workspaceFolders || [];
let needAsk: boolean = true;
for (const folder of workspaceFolders) {
Expand Down Expand Up @@ -70,6 +80,42 @@ function isSubFolder(from: string, to: string): boolean {
return !relative.startsWith("..") && !path.isAbsolute(relative);
}

async function determineLeetCodeFolder(): Promise<string> {
let result: string;
const picks: Array<IQuickItemEx<string>> = [];
picks.push(
{
label: `Default location`,
detail: `${path.join(os.homedir(), ".leetcode")}`,
value: `${path.join(os.homedir(), ".leetcode")}`,
},
{
label: "$(file-directory) Browse...",
value: ":browse",
},
);
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(
picks,
{ placeHolder: "Select where you would like to save your LeetCode files" },
);
if (!choice) {
result = "";
} else if (choice.value === ":browse") {
const directory: vscode.Uri[] | undefined = await showDirectorySelectDialog();
if (!directory || directory.length < 1) {
result = "";
} else {
result = directory[0].fsPath;
}
} else {
result = choice.value;
}

getWorkspaceConfiguration().update("workspaceFolder", result, vscode.ConfigurationTarget.Global);

return result;
}

enum OpenOption {
openInCurrentWindow = "Open in current window",
openInNewWindow = "Open in new window",
Expand Down

0 comments on commit e1f3206

Please sign in to comment.