Skip to content

feat: Can specify the workspace folder to save the files #360

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

Merged
merged 3 commits into from
Jul 11, 2019
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
- 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: If no folder is opened in VS Code, the extension will save the problem files in **$HOME/.leetcode/**.
> 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/**.

> You can specify whether including the problem description in comments or not by updating the setting `leetcode.showCommentDescription`.

Expand Down Expand Up @@ -121,6 +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.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
3 changes: 2 additions & 1 deletion docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
- 直接点击题目或者在 `LeetCode Explorer` 中**右键**题目并选择 `Preview Problem` 可查看题目描述
- 选择 `Show Problem` 可直接进行答题。

> 注意:若当前 VS Code 没有已打开的文件夹,则生成的题目文件会存储于 **$HOME/.leetcode/** 目录下
> 注意:你可以通过更新配置项 `leetcode.workspaceFolder` 来指定保存题目文件所用的工作区路径。默认工作区路径为:**$HOME/.leetcode/**。

> 注意:你可以通过更新配置项 `leetcode.showCommentDescription` 来指定是否要在注释中包含题目描述。

Expand Down Expand Up @@ -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` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:<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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ async function showProblemInternal(node: IProblem): Promise<void> {

const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
let outDir: string = await selectWorkspaceFolder();
if (!outDir) {
return;
}

let relativePath: string = (leetCodeConfig.get<string>("outputFolder", "")).trim();
if (relativePath) {
relativePath = await resolveRelativePath(relativePath, node, language);
Expand Down
6 changes: 6 additions & 0 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,6 +12,11 @@ export function shouldHideSolvedProblem(): boolean {
return getWorkspaceConfiguration().get<boolean>("hideSolved", false);
}

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

export function getEditorShortcuts(): string[] {
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
}
54 changes: 43 additions & 11 deletions src/utils/workspaceUtils.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<string | undefined> {
Expand All @@ -40,3 +61,14 @@ export async function getActiveFilePath(uri?: vscode.Uri): Promise<string | unde
}
return wsl.useWsl() ? wsl.toWslPath(textEditor.document.uri.fsPath) : textEditor.document.uri.fsPath;
}

function isSubFolder(from: string, to: string): boolean {
const relative: string = path.relative(from, to);
return !!relative && !relative.startsWith("..") && !path.isAbsolute(relative);
}

enum OpenOption {
openInCurrentWindow = "Open in current window",
openInNewWindow = "Open in new window",
addToWorkspace = "Add to workspace",
}