From d28e5b202d9b0846cdf1c84328b108a9a995c1d2 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Wed, 15 May 2019 20:07:58 +0800 Subject: [PATCH 1/3] feat: Support interpolation for outputFolder settings --- src/commands/show.ts | 64 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/commands/show.ts b/src/commands/show.ts index 77256dbf..f8139b0a 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -107,14 +107,12 @@ async function showProblemInternal(node: IProblem): Promise { const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); let outDir: string = await selectWorkspaceFolder(); let relativePath: string = (leetCodeConfig.get("outputFolder", "")).trim(); - const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/); - if (matchResult) { - const resolvedPath: string | undefined = await resolveRelativePath(matchResult[1].toLocaleLowerCase(), node, language); - if (!resolvedPath) { + if (relativePath) { + relativePath = await resolveRelativePath(relativePath, node, language); + if (!relativePath) { leetCodeChannel.appendLine("Showing problem canceled by user."); return; } - relativePath = resolvedPath; } outDir = path.join(outDir, relativePath); @@ -166,27 +164,39 @@ function parseProblemDecorator(state: ProblemState, locked: boolean): string { } } -async function resolveRelativePath(value: string, node: IProblem, selectedLanguage: string): Promise { - switch (value) { - case "tag": - if (node.tags.length === 1) { - return node.tags[0]; - } - return await vscode.window.showQuickPick( - node.tags, - { - matchOnDetail: true, - placeHolder: "Multiple tags available, please select one", - ignoreFocusOut: true, - }, - ); - case "language": - return selectedLanguage; - case "difficulty": - return node.difficulty; - default: - const errorMsg: string = `The config '${value}' is not supported.`; - leetCodeChannel.appendLine(errorMsg); - throw new Error(errorMsg); +async function resolveRelativePath(relativePath: string, node: IProblem, selectedLanguage: string): Promise { + if (/\$\{tag\}/i.test(relativePath)) { + const tag: string | undefined = await resolveTagForProblem(node); + if (!tag) { + return ""; + } + relativePath = relativePath.replace(/\$\{tag\}/i, tag); } + + relativePath = relativePath.replace(/\$\{language\}/i, selectedLanguage); + relativePath = relativePath.replace(/\$\{difficulty\}/i, node.difficulty.toLocaleLowerCase()); + + // Check if there is any unsupported configuration + const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/); + if (matchResult && matchResult.length >= 1) { + const errorMsg: string = `The config '${matchResult[1]}' is not supported.`; + leetCodeChannel.appendLine(errorMsg); + throw new Error(errorMsg); + } + + return relativePath; +} + +async function resolveTagForProblem(problem: IProblem): Promise { + if (problem.tags.length === 1) { + return problem.tags[0]; + } + return await vscode.window.showQuickPick( + problem.tags, + { + matchOnDetail: true, + placeHolder: "Multiple tags available, please select one", + ignoreFocusOut: true, + }, + ); } From a5f2aa7fe0b1bc2892da0d13f5399db6914874ca Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Wed, 15 May 2019 20:10:57 +0800 Subject: [PATCH 2/3] Update readme --- README.md | 2 +- docs/README_zh-CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ab08b3b2..64f62c19 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,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.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:
  • `${tag}`: Categorize the problem according to their tags.
  • `${language}`: Categorize the problem according to their language.
  • `${difficulty}`: Categorize the problem according to their difficulty.
| N/A | +| `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:
  • `${tag}`: Categorize the problem according to their tags.
  • `${language}`: Categorize the problem according to their language.
  • `${difficulty}`: Categorize the problem according to their difficulty.
For example: `problem-${tag}-${difficulty}` | N/A | | `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` | | `leetcode.enableShortcuts` | Specify whether the submit and test shortcuts in editor or not. | `true` | | `leetcode.enableSideMode` | Specify whether `preview`, `solution` and `submission` tab should be grouped into the second editor column when solving a problem. | `true` | diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 98b04748..39ae2683 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -140,7 +140,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.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:
  • `${tag}`: 根据题目的类别进行分类。
  • `${language}`: 根据题目的语言进行分类。
  • `${difficulty}`: 根据题目的难度进行分类。
| N/A | +| `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:
  • `${tag}`: 根据题目的类别进行分类。
  • `${language}`: 根据题目的语言进行分类。
  • `${difficulty}`: 根据题目的难度进行分类。
例如:`problem-${tag}-${difficulty}` | N/A | | `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` | | `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` | | `leetcode.enableSideMode` | 指定在解决一道题时,是否将`问题预览`、`高票答案`与`提交结果`窗口集中在编辑器的第二栏。 | `true` | From bb78e17799774265b9776cbe8df2a077ee1c9722 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Wed, 15 May 2019 20:14:53 +0800 Subject: [PATCH 3/3] Use global flag --- src/commands/show.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/commands/show.ts b/src/commands/show.ts index f8139b0a..6277241f 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -170,11 +170,11 @@ async function resolveRelativePath(relativePath: string, node: IProblem, selecte if (!tag) { return ""; } - relativePath = relativePath.replace(/\$\{tag\}/i, tag); + relativePath = relativePath.replace(/\$\{tag\}/ig, tag); } - relativePath = relativePath.replace(/\$\{language\}/i, selectedLanguage); - relativePath = relativePath.replace(/\$\{difficulty\}/i, node.difficulty.toLocaleLowerCase()); + relativePath = relativePath.replace(/\$\{language\}/ig, selectedLanguage); + relativePath = relativePath.replace(/\$\{difficulty\}/ig, node.difficulty.toLocaleLowerCase()); // Check if there is any unsupported configuration const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/);