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

Commit 96487df

Browse files
author
chengruilin
committed
refactor: treat with the outputPath
1 parent 3275cde commit 96487df

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,8 @@
250250
},
251251
"leetcode.outputPath": {
252252
"type": "string",
253-
"default": "",
254253
"scope": "application",
255-
"description": "Specifies the relative path to save the problem files."
254+
"description": "Specify the relative path to save the problem files."
256255
}
257256
}
258257
}

src/commands/show.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as fse from "fs-extra";
5+
import * as path from "path";
56
import * as vscode from "vscode";
67
import { LeetCodeNode } from "../explorer/LeetCodeNode";
78
import { leetCodeExecutor } from "../leetCodeExecutor";
@@ -16,16 +17,17 @@ export async function showProblem(node?: LeetCodeNode): Promise<void> {
1617
if (!node) {
1718
return;
1819
}
19-
await showProblemInternal(node.id);
20+
await showProblemInternal(node);
2021
}
2122

2223
export async function searchProblem(): Promise<void> {
2324
if (!leetCodeManager.getUser()) {
2425
promptForSignIn();
2526
return;
2627
}
28+
const problems: IProblem[] = await list.listProblems();
2729
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(
28-
parseProblemsToPicks(list.listProblems()),
30+
parseProblemsToPicks(problems),
2931
{
3032
matchOnDetail: true,
3133
placeHolder: "Select one problem",
@@ -34,12 +36,11 @@ export async function searchProblem(): Promise<void> {
3436
if (!choice) {
3537
return;
3638
}
37-
await showProblemInternal(choice.value);
39+
await showProblemInternal(problems.find((problem: IProblem) => problem.id === choice.value) as IProblem);
3840
}
3941

40-
async function showProblemInternal(id: string): Promise<void> {
42+
async function showProblemInternal(node: IProblem): Promise<void> {
4143
try {
42-
const listProblems: IProblem[] = await list.listProblems();
4344
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
4445
let defaultLanguage: string | undefined = leetCodeConfig.get<string>("defaultLanguage");
4546
if (defaultLanguage && languages.indexOf(defaultLanguage) < 0) {
@@ -51,35 +52,35 @@ async function showProblemInternal(id: string): Promise<void> {
5152
}
5253

5354
let outDir: string = await selectWorkspaceFolder();
54-
const outputPath: string = leetCodeConfig.get<string>("outputPath") || "";
55-
const problem: IProblem | undefined = listProblems.find((item: IProblem) => item.id === id);
56-
switch (outputPath) {
57-
case "": {
58-
break;
59-
}
60-
case "${tag}": {
61-
if (problem) {
62-
outDir = `${outDir}/${problem.tags[0]}`;
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+
const closestTag: string = node.tags.reduce((prev: string, curr: string) => {
61+
return curr.length > prev.length ?
62+
curr :
63+
prev;
64+
}, "");
65+
outDir = path.join(outDir, closestTag);
66+
break;
6367
}
64-
break;
65-
}
66-
case "${language}": {
67-
outDir = `${outDir}/${language}`;
68-
break;
69-
}
70-
case "${difficulty}": {
71-
if (problem) {
72-
outDir = `${outDir}/${problem.difficulty}`;
68+
case "language": {
69+
outDir = path.join(outDir, language);
70+
break;
7371
}
74-
break;
75-
}
76-
default: {
77-
outDir = `${outDir}/${outputPath}`;
78-
break;
72+
case "difficulty": {
73+
outDir = path.join(outDir, node.difficulty);
74+
break;
75+
}
76+
default: {
77+
break;
78+
}
79+
7980
}
8081
}
8182
await fse.ensureDir(outDir);
82-
const result: string = await leetCodeExecutor.showProblem(id, language, outDir);
83+
const result: string = await leetCodeExecutor.showProblem(node.id, language, outDir);
8384
const reg: RegExp = /\* Source Code:\s*(.*)/;
8485
const match: RegExpMatchArray | null = result.match(reg);
8586
if (match && match.length >= 2) {
@@ -108,9 +109,9 @@ async function showProblemInternal(id: string): Promise<void> {
108109
}
109110
}
110111

111-
async function parseProblemsToPicks(p: Promise<IProblem[]>): Promise<Array<IQuickItemEx<string>>> {
112+
async function parseProblemsToPicks(p: IProblem[]): Promise<Array<IQuickItemEx<string>>> {
112113
return new Promise(async (resolve: (res: Array<IQuickItemEx<string>>) => void): Promise<void> => {
113-
const picks: Array<IQuickItemEx<string>> = (await p).map((problem: IProblem) => Object.assign({}, {
114+
const picks: Array<IQuickItemEx<string>> = p.map((problem: IProblem) => Object.assign({}, {
114115
label: `${parseProblemDecorator(problem.state, problem.locked)}${problem.id}.${problem.name}`,
115116
description: "",
116117
detail: `AC rate: ${problem.passRate}, Difficulty: ${problem.difficulty}`,

0 commit comments

Comments
 (0)