From 23a5098cc1dfa9b6a1af0b4b6ee9de2faf8484e3 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Sat, 29 Jun 2019 19:11:04 +0800 Subject: [PATCH 1/3] feat: Can specify the workspace folder to save the files --- README.md | 3 +++ docs/README_zh-CN.md | 3 ++- package.json | 6 +++++ src/commands/show.ts | 4 +++ src/utils/settingUtils.ts | 6 +++++ src/utils/workspaceUtils.ts | 54 +++++++++++++++++++++++++++++-------- 6 files changed, 64 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index cc0bffea..4234864e 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,8 @@ - Directly click on the problem or right click the problem in the `LeetCode Explorer` and select `Preview Problem` to see the problem description. - Select `Show Problem` to directly open the file with the problem description. + > Note:You can specify the path of the workspace folder to store the problem files by updating the setting `leetcode.workspaceFolder`. The default value is:**$HOME/.leetcode/**. + > Note: If no folder is opened in VS Code, the extension will save the problem files in **$HOME/.leetcode/**. > You can specify whether including the problem description in comments or not by updating the setting `leetcode.showCommentDescription`. @@ -121,6 +123,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.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: 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` | diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 0a3e23e5..6d90edb6 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -73,7 +73,7 @@ - 直接点击题目或者在 `LeetCode Explorer` 中**右键**题目并选择 `Preview Problem` 可查看题目描述 - 选择 `Show Problem` 可直接进行答题。 - > 注意:若当前 VS Code 没有已打开的文件夹,则生成的题目文件会存储于 **$HOME/.leetcode/** 目录下。 + > 注意:你可以通过更新配置项 `leetcode.workspaceFolder` 来指定保存题目文件所用的工作区路径。默认工作区路径为:**$HOME/.leetcode/**。 > 注意:你可以通过更新配置项 `leetcode.showCommentDescription` 来指定是否要在注释中包含题目描述。 @@ -121,6 +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.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:例如:`problem-${tag}-${difficulty}` | N/A | | `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` | | **(Deprecated)** `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` | diff --git a/package.json b/package.json index 0ba3e9aa..5f90e490 100644 --- a/package.json +++ b/package.json @@ -310,6 +310,12 @@ ], "description": "Endpoint of the user account." }, + "leetcode.workspaceFolder": { + "type": "string", + "scope": "application", + "description": "The path of the workspace folder to store the problem files.", + "default": "${home}/.leetcode" + }, "leetcode.outputFolder": { "type": "string", "scope": "application", diff --git a/src/commands/show.ts b/src/commands/show.ts index 7dceffed..896516a9 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -128,6 +128,10 @@ async function showProblemInternal(node: IProblem): Promise { const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); let outDir: string = await selectWorkspaceFolder(); + if (!outDir) { + return; + } + let relativePath: string = (leetCodeConfig.get("outputFolder", "")).trim(); if (relativePath) { relativePath = await resolveRelativePath(relativePath, node, language); diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index 3cc48ea7..b7cb5919 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -1,6 +1,7 @@ // 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 { @@ -11,6 +12,11 @@ export function shouldHideSolvedProblem(): boolean { return getWorkspaceConfiguration().get("hideSolved", false); } +export function getWorkspaceFolder(): string { + const rawWorkspaceFolder: string = getWorkspaceConfiguration().get("workspaceFolder", "${home}/.leetcode"); + return rawWorkspaceFolder.replace(/\${home}/i, os.homedir()); +} + export function getEditorShortcuts(): string[] { return getWorkspaceConfiguration().get("editor.shortcuts", ["submit", "test"]); } diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index f2f2d16b..2cd0ff19 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -1,26 +1,47 @@ // 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 * as wsl from "./wslUtils"; export async function selectWorkspaceFolder(): Promise { - let folder: vscode.WorkspaceFolder | undefined; - if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) { - if (vscode.workspace.workspaceFolders.length > 1) { - folder = await vscode.window.showWorkspaceFolderPick({ - placeHolder: "Select the working directory you wish to use", - }); - } else { - folder = vscode.workspace.workspaceFolders[0]; + const workspaceFolderSetting: string = getWorkspaceFolder(); + const workspaceFolders: vscode.WorkspaceFolder[] = vscode.workspace.workspaceFolders || []; + let needAsk: boolean = true; + for (const folder of workspaceFolders) { + if (isSubFolder(folder.uri.fsPath, workspaceFolderSetting)) { + needAsk = false; } } - const workFolder: string = folder ? folder.uri.fsPath : path.join(os.homedir(), ".leetcode"); + if (needAsk) { + const choice: string | undefined = await vscode.window.showQuickPick( + [ + OpenOption.openInCurrentWindow, + OpenOption.openInNewWindow, + OpenOption.addToWorkspace, + ], + { placeHolder: "Select how you would like to open your workspace folder" }, + ); - return wsl.useWsl() ? wsl.toWslPath(workFolder) : workFolder; + switch (choice) { + case OpenOption.openInCurrentWindow: + await vscode.commands.executeCommand("vscode.openFolder", vscode.Uri.file(workspaceFolderSetting), false); + return ""; + case OpenOption.openInNewWindow: + await vscode.commands.executeCommand("vscode.openFolder", vscode.Uri.file(workspaceFolderSetting), true); + return ""; + case OpenOption.addToWorkspace: + vscode.workspace.updateWorkspaceFolders(workspaceFolders.length, 0, { uri: vscode.Uri.file(workspaceFolderSetting) }); + break; + default: + return ""; + } + } + + return wsl.useWsl() ? wsl.toWslPath(workspaceFolderSetting) : workspaceFolderSetting; } export async function getActiveFilePath(uri?: vscode.Uri): Promise { @@ -40,3 +61,14 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise Date: Sat, 29 Jun 2019 19:12:40 +0800 Subject: [PATCH 2/3] Update the document --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 4234864e..a46fe59c 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,6 @@ > Note:You can specify the path of the workspace folder to store the problem files by updating the setting `leetcode.workspaceFolder`. The default value is:**$HOME/.leetcode/**. - > Note: If no folder is opened in VS Code, the extension will save the problem files in **$HOME/.leetcode/**. - > You can specify whether including the problem description in comments or not by updating the setting `leetcode.showCommentDescription`. > You can switch the default language by triggering the command: `LeetCode: Switch Default Language`. From 08821525e4d77a303b723b18f2238b05f13295df Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Thu, 11 Jul 2019 16:18:33 +0800 Subject: [PATCH 3/3] Fix the bug to resolve the relative path --- src/utils/workspaceUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/workspaceUtils.ts b/src/utils/workspaceUtils.ts index 2cd0ff19..df7df0ad 100644 --- a/src/utils/workspaceUtils.ts +++ b/src/utils/workspaceUtils.ts @@ -64,7 +64,7 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise