diff --git a/src/commands/list.ts b/src/commands/list.ts index 5cb44137..3b6aefd5 100644 --- a/src/commands/list.ts +++ b/src/commands/list.ts @@ -36,7 +36,7 @@ export async function listProblems(): Promise { }); } } - return problems.reverse(); + return problems.sort((p1: IProblem, p2: IProblem) => +p1.id - +p2.id); } catch (error) { await promptForOpenOutputChannel("Failed to list problems. Please open the output channel for details.", DialogType.error); return []; diff --git a/src/commands/show.ts b/src/commands/show.ts index 70474426..0cd05c6c 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -105,13 +105,24 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise { +async function fetchProblemLanguage(node?: IProblem): Promise { + let supportLangs: string[] | null = null; + if (node) { + const problemDescription: string = await leetCodeExecutor.getDescription(node.id); + const matchLang: RegExpMatchArray | null = problemDescription.match(/\n(Langs:[\w\s]+)\n/); + supportLangs = matchLang && matchLang[1] + ? matchLang[1].slice(6).split(/\s+/).filter(Boolean) + : null; + } + if (!supportLangs) { + supportLangs = languages.slice(); + } const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); let defaultLanguage: string | undefined = leetCodeConfig.get("defaultLanguage"); - if (defaultLanguage && languages.indexOf(defaultLanguage) < 0) { + if (defaultLanguage && supportLangs.indexOf(defaultLanguage) < 0) { defaultLanguage = undefined; } - const language: string | undefined = defaultLanguage || await vscode.window.showQuickPick(languages, { placeHolder: "Select the language you want to use", ignoreFocusOut: true }); + const language: string | undefined = defaultLanguage || await vscode.window.showQuickPick(supportLangs, { placeHolder: "Select the language you want to use", ignoreFocusOut: true }); // fire-and-forget default language query (async (): Promise => { if (language && !defaultLanguage && leetCodeConfig.get("hint.setDefaultLanguage")) { @@ -133,7 +144,7 @@ async function fetchProblemLanguage(): Promise { async function showProblemInternal(node: IProblem): Promise { try { - const language: string | undefined = await fetchProblemLanguage(); + const language: string | undefined = await fetchProblemLanguage(node); if (!language) { return; } diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index 5157b6c1..afad5b99 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -99,8 +99,8 @@ class LeetCodeExecutor implements Disposable { const templateType: string = showDescriptionInComment ? "-cx" : "-c"; if (!await fse.pathExists(filePath)) { - await fse.createFile(filePath); const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, templateType, "-l", language]); + await fse.createFile(filePath); await fse.writeFile(filePath, codeTemplate); } }