Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3a688cd

Browse files
committed
Refactoring
1 parent f6352cd commit 3a688cd

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

src/commands/show.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as fse from "fs-extra";
55
import * as path from "path";
66
import * as vscode from "vscode";
77
import { LeetCodeNode } from "../explorer/LeetCodeNode";
8+
import { leetCodeChannel } from "../leetCodeChannel";
89
import { leetCodeExecutor } from "../leetCodeExecutor";
910
import { leetCodeManager } from "../leetCodeManager";
1011
import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared";
@@ -25,9 +26,8 @@ export async function searchProblem(): Promise<void> {
2526
promptForSignIn();
2627
return;
2728
}
28-
const problems: IProblem[] = await list.listProblems();
2929
const choice: IQuickItemEx<IProblem> | undefined = await vscode.window.showQuickPick(
30-
parseProblemsToPicks(problems),
30+
parseProblemsToPicks(list.listProblems()),
3131
{
3232
matchOnDetail: true,
3333
placeHolder: "Select one problem",
@@ -52,42 +52,18 @@ async function showProblemInternal(node: IProblem): Promise<void> {
5252
}
5353

5454
let outDir: string = await selectWorkspaceFolder();
55-
const outputPathCfg: string = leetCodeConfig.get<string>("outputPath") || "";
56-
const outputPath: RegExpMatchArray | null = outputPathCfg.match(/\$\{(.*?)\}/);
57-
if (outputPath) {
58-
switch (outputPath[1].toLowerCase()) {
59-
case "tag":
60-
let tag: string | undefined;
61-
if (node.tags.length === 1) {
62-
tag = node.tags[0];
63-
} else {
64-
tag = await vscode.window.showQuickPick(
65-
node.tags,
66-
{
67-
matchOnDetail: true,
68-
placeHolder: "Select one tag",
69-
},
70-
);
71-
}
72-
if (!tag) {
73-
return;
74-
}
75-
outDir = path.join(outDir, tag);
76-
break;
77-
case "language":
78-
outDir = path.join(outDir, language);
79-
break;
80-
case "difficulty":
81-
outDir = path.join(outDir, node.difficulty);
82-
break;
83-
default: {
84-
break;
85-
}
86-
55+
let relativePath: string = (leetCodeConfig.get<string>("outputPath") || "").trim();
56+
const matchResult: RegExpMatchArray | null = relativePath.match(/\$\{(.*?)\}/);
57+
if (matchResult) {
58+
const resolvedPath: string | undefined = await resolveRelativePath(matchResult[1].toLocaleLowerCase(), node, language);
59+
if (!resolvedPath) {
60+
leetCodeChannel.appendLine("No tag is picked, skip showing the problem.");
61+
return;
8762
}
88-
} else {
89-
outDir = path.join(outDir, outputPathCfg);
63+
relativePath = resolvedPath;
9064
}
65+
66+
outDir = path.join(outDir, relativePath);
9167
await fse.ensureDir(outDir);
9268
const result: string = await leetCodeExecutor.showProblem(node.id, language, outDir);
9369
const reg: RegExp = /\* Source Code:\s*(.*)/;
@@ -114,13 +90,13 @@ async function showProblemInternal(node: IProblem): Promise<void> {
11490
}
11591
}
11692
} catch (error) {
117-
await promptForOpenOutputChannel("Failed to fetch the problem information. Please open the output channel for details.", DialogType.error);
93+
await promptForOpenOutputChannel("Failed to show the problem. Please open the output channel for details.", DialogType.error);
11894
}
11995
}
12096

121-
async function parseProblemsToPicks(p: IProblem[]): Promise<Array<IQuickItemEx<IProblem>>> {
97+
async function parseProblemsToPicks(p: Promise<IProblem[]>): Promise<Array<IQuickItemEx<IProblem>>> {
12298
return new Promise(async (resolve: (res: Array<IQuickItemEx<IProblem>>) => void): Promise<void> => {
123-
const picks: Array<IQuickItemEx<IProblem>> = p.map((problem: IProblem) => Object.assign({}, {
99+
const picks: Array<IQuickItemEx<IProblem>> = (await p).map((problem: IProblem) => Object.assign({}, {
124100
label: `${parseProblemDecorator(problem.state, problem.locked)}${problem.id}.${problem.name}`,
125101
description: "",
126102
detail: `AC rate: ${problem.passRate}, Difficulty: ${problem.difficulty}`,
@@ -140,3 +116,28 @@ function parseProblemDecorator(state: ProblemState, locked: boolean): string {
140116
return locked ? "$(lock) " : "";
141117
}
142118
}
119+
120+
async function resolveRelativePath(value: string, node: IProblem, selectedLanguage: string): Promise<string | undefined> {
121+
switch (value) {
122+
case "tag":
123+
if (node.tags.length === 1) {
124+
return node.tags[0];
125+
}
126+
return await vscode.window.showQuickPick(
127+
node.tags,
128+
{
129+
matchOnDetail: true,
130+
placeHolder: "Select one tag",
131+
ignoreFocusOut: true,
132+
},
133+
);
134+
case "language":
135+
return selectedLanguage;
136+
case "difficulty":
137+
return node.difficulty;
138+
default:
139+
const errorMsg: string = `The config '${value}' is not supported.`;
140+
leetCodeChannel.appendLine(errorMsg);
141+
throw new Error(errorMsg);
142+
}
143+
}

0 commit comments

Comments
 (0)