Skip to content

Commit fc80f68

Browse files
authored
feat: Support interpolation for outputFolder settings (#323)
1 parent d01a8f0 commit fc80f68

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
| `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` |
141141
| `leetcode.useWsl` | Specify whether to use WSL or not | `false` |
142142
| `leetcode.endpoint` | Specify the active endpoint. Supported endpoints are: `leetcode`, `leetcode-cn` | `leetcode` |
143-
| `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> | N/A |
143+
| `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 |
144144
| `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` |
145145
| `leetcode.enableShortcuts` | Specify whether the submit and test shortcuts in editor or not. | `true` |
146146
| `leetcode.enableSideMode` | Specify whether `preview`, `solution` and `submission` tab should be grouped into the second editor column when solving a problem. | `true` |

Diff for: docs/README_zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
| `leetcode.defaultLanguage` | 指定答题时使用的默认语言,可选语言有:`bash`, `c`, `cpp`, `csharp`, `golang`, `java`, `javascript`, `kotlin`, `mysql`, `php`, `python`,`python3`,`ruby`, `rust`, `scala`,`swift` | `N/A` |
141141
| `leetcode.useWsl` | 指定是否启用 WSL | `false` |
142142
| `leetcode.endpoint` | 指定使用的终端,可用终端有:`leetcode`, `leetcode-cn` | `leetcode` |
143-
| `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:<ul><li>`${tag}`: 根据题目的类别进行分类。<li>`${language}`: 根据题目的语言进行分类。</li><li>`${difficulty}`: 根据题目的难度进行分类。</li></ul> | N/A |
143+
| `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:<ul><li>`${tag}`: 根据题目的类别进行分类。<li>`${language}`: 根据题目的语言进行分类。</li><li>`${difficulty}`: 根据题目的难度进行分类。</li></ul>例如:`problem-${tag}-${difficulty}` | N/A |
144144
| `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` |
145145
| `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` |
146146
| `leetcode.enableSideMode` | 指定在解决一道题时,是否将`问题预览``高票答案``提交结果`窗口集中在编辑器的第二栏。 | `true` |

Diff for: src/commands/show.ts

+37-27
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,12 @@ async function showProblemInternal(node: IProblem): Promise<void> {
107107
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
108108
let outDir: string = await selectWorkspaceFolder();
109109
let relativePath: string = (leetCodeConfig.get<string>("outputFolder", "")).trim();
110-
const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/);
111-
if (matchResult) {
112-
const resolvedPath: string | undefined = await resolveRelativePath(matchResult[1].toLocaleLowerCase(), node, language);
113-
if (!resolvedPath) {
110+
if (relativePath) {
111+
relativePath = await resolveRelativePath(relativePath, node, language);
112+
if (!relativePath) {
114113
leetCodeChannel.appendLine("Showing problem canceled by user.");
115114
return;
116115
}
117-
relativePath = resolvedPath;
118116
}
119117

120118
outDir = path.join(outDir, relativePath);
@@ -166,27 +164,39 @@ function parseProblemDecorator(state: ProblemState, locked: boolean): string {
166164
}
167165
}
168166

169-
async function resolveRelativePath(value: string, node: IProblem, selectedLanguage: string): Promise<string | undefined> {
170-
switch (value) {
171-
case "tag":
172-
if (node.tags.length === 1) {
173-
return node.tags[0];
174-
}
175-
return await vscode.window.showQuickPick(
176-
node.tags,
177-
{
178-
matchOnDetail: true,
179-
placeHolder: "Multiple tags available, please select one",
180-
ignoreFocusOut: true,
181-
},
182-
);
183-
case "language":
184-
return selectedLanguage;
185-
case "difficulty":
186-
return node.difficulty;
187-
default:
188-
const errorMsg: string = `The config '${value}' is not supported.`;
189-
leetCodeChannel.appendLine(errorMsg);
190-
throw new Error(errorMsg);
167+
async function resolveRelativePath(relativePath: string, node: IProblem, selectedLanguage: string): Promise<string> {
168+
if (/\$\{tag\}/i.test(relativePath)) {
169+
const tag: string | undefined = await resolveTagForProblem(node);
170+
if (!tag) {
171+
return "";
172+
}
173+
relativePath = relativePath.replace(/\$\{tag\}/ig, tag);
191174
}
175+
176+
relativePath = relativePath.replace(/\$\{language\}/ig, selectedLanguage);
177+
relativePath = relativePath.replace(/\$\{difficulty\}/ig, node.difficulty.toLocaleLowerCase());
178+
179+
// Check if there is any unsupported configuration
180+
const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/);
181+
if (matchResult && matchResult.length >= 1) {
182+
const errorMsg: string = `The config '${matchResult[1]}' is not supported.`;
183+
leetCodeChannel.appendLine(errorMsg);
184+
throw new Error(errorMsg);
185+
}
186+
187+
return relativePath;
188+
}
189+
190+
async function resolveTagForProblem(problem: IProblem): Promise<string | undefined> {
191+
if (problem.tags.length === 1) {
192+
return problem.tags[0];
193+
}
194+
return await vscode.window.showQuickPick(
195+
problem.tags,
196+
{
197+
matchOnDetail: true,
198+
placeHolder: "Multiple tags available, please select one",
199+
ignoreFocusOut: true,
200+
},
201+
);
192202
}

0 commit comments

Comments
 (0)