diff --git a/.vscode/launch.json b/.vscode/launch.json index 6f7862ec..8652c55e 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}" ], + "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--disable-extensions" ], "stopOnEntry": false, "sourceMaps": true, "outFiles": [ "${workspaceRoot}/out/src/**/*.js" ], @@ -25,4 +25,4 @@ "preLaunchTask": "npm" } ] -} \ No newline at end of file +} diff --git a/src/commands/show.ts b/src/commands/show.ts index 3aebce8f..3a6df865 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -132,45 +132,61 @@ async function fetchProblemLanguage(): Promise { return language; } -async function showProblemInternal(node: IProblem): Promise { - try { - const language: string | undefined = await fetchProblemLanguage(); - if (!language) { - return; - } - - const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); - const workspaceFolder: string = await selectWorkspaceFolder(); - if (!workspaceFolder) { - return; - } +async function createPath(language:string,node:IProblem):Promise{ + const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode"); + const workspaceFolder: string = await selectWorkspaceFolder(); + if (!workspaceFolder) { + Promise.reject("No workspace is opened."); + } - const fileFolder: string = leetCodeConfig - .get(`filePath.${language}.folder`, leetCodeConfig.get(`filePath.default.folder`, "")) - .trim(); - const fileName: string = leetCodeConfig - .get( - `filePath.${language}.filename`, - leetCodeConfig.get(`filePath.default.filename`) || genFileName(node, language), - ) - .trim(); + const fileFolder: string = leetCodeConfig + .get(`filePath.${language}.folder`, leetCodeConfig.get(`filePath.default.folder`, "")) + .trim(); + const fileName: string = leetCodeConfig + .get( + `filePath.${language}.filename`, + leetCodeConfig.get(`filePath.default.filename`) || genFileName(node, language), + ) + .trim(); - let finalPath: string = path.join(workspaceFolder, fileFolder, fileName); + let finalPath: string = path.join(workspaceFolder, fileFolder, fileName); - if (finalPath) { - finalPath = await resolveRelativePath(finalPath, node, language); - if (!finalPath) { - leetCodeChannel.appendLine("Showing problem canceled by user."); - return; - } + if (finalPath) { + finalPath = await resolveRelativePath(finalPath, node, language); + if (!finalPath) { + leetCodeChannel.appendLine("Showing problem canceled by user."); + Promise.reject("Showing problem canceled by user."); } + } + + finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath; + return finalPath; +} - finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath; +async function showProblemInternal(node: IProblem): Promise { + try { + let language: string | undefined = await fetchProblemLanguage(); + if (!language) { + return; + } + let finalPath: string = await createPath(language,node); const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration(); const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation(); + try { + await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation); + } catch (e) { + if (e instanceof Error) { + //this shows all languages, not just the ones, which are supported for this specific problem + language = await vscode.window.showQuickPick(languages, { placeHolder: "This problem is not supporting your default language, please choose another", ignoreFocusOut: true }); + if (language === undefined) { throw e; } - await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation); + finalPath = await createPath(language,node); + await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation); + } else { + throw e; + } + } const promises: any[] = [ vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }), promptHintMessage( diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index d2332c7a..48a54165 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -107,10 +107,12 @@ class LeetCodeExecutor implements Disposable { if (!needTranslation) { cmd.push("-T"); // use -T to force English version } - + const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd) + if(codeTemplate === undefined) { + throw new Error("language not supported"); + } if (!await fse.pathExists(filePath)) { await fse.createFile(filePath); - const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd); await fse.writeFile(filePath, codeTemplate); } }