Skip to content

fix: get supported languages before fetching codeTemplate (#618) #621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: get supported languages before fetching codeTemplate (#618)
  • Loading branch information
zmen authored and Ethan.Hu committed Aug 24, 2020
commit ebc2e695c5581bb81c67736375664c35967b661f
2 changes: 1 addition & 1 deletion src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ export async function listProblems(): Promise<IProblem[]> {
});
}
}
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 [];
19 changes: 15 additions & 4 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
@@ -105,13 +105,24 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<vo
}
}

async function fetchProblemLanguage(): Promise<string | undefined> {
async function fetchProblemLanguage(node?: IProblem): Promise<string | undefined> {
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<string>("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<void> => {
if (language && !defaultLanguage && leetCodeConfig.get<boolean>("hint.setDefaultLanguage")) {
@@ -133,7 +144,7 @@ async function fetchProblemLanguage(): Promise<string | undefined> {

async function showProblemInternal(node: IProblem): Promise<void> {
try {
const language: string | undefined = await fetchProblemLanguage();
const language: string | undefined = await fetchProblemLanguage(node);
if (!language) {
return;
}
2 changes: 1 addition & 1 deletion src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}