From 58ef9d898e3faa18a6ae7656392023182380884a Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Sat, 22 Feb 2020 20:57:51 +0800 Subject: [PATCH 1/2] Better config how to show the problem description --- package.json | 19 ++++++++++++++++++- src/commands/show.ts | 26 +++++++++++++++----------- src/leetCodeExecutor.ts | 4 ++-- src/shared.ts | 7 +++++++ src/utils/settingUtils.ts | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 59c5927b..6397ef3a 100644 --- a/package.json +++ b/package.json @@ -300,11 +300,28 @@ "scope": "application", "description": "Default language for solving the problems." }, + "leetcode.showDescription": { + "type": "string", + "default": "In Webview", + "enum": [ + "In Webview", + "In File Comment", + "Both", + "None" + ], + "enumDescriptions": [ + "Show the problem description in a new webview window", + "Show the problem description in the file's comment" + ], + "scope": "application", + "description": "Specify where to show the description." + }, "leetcode.showCommentDescription": { "type": "boolean", "default": false, "scope": "application", - "description": "Include problem description in comments." + "description": "[Deprecated] Include problem description in comments.", + "deprecationMessage": "This setting will be deprecated in 0.17.0, please use 'leetcode.showDescription' instead" }, "leetcode.hint.setDefaultLanguage": { "type": "boolean", diff --git a/src/commands/show.ts b/src/commands/show.ts index 3de4f89a..1265e6f7 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -12,6 +12,8 @@ import { leetCodeExecutor } from "../leetCodeExecutor"; import { leetCodeManager } from "../leetCodeManager"; import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared"; import { genFileExt, genFileName, getNodeIdFromFile } from "../utils/problemUtils"; +import * as settingUtils from "../utils/settingUtils"; +import { IDescriptionConfiguration } from "../utils/settingUtils"; import { DialogOptions, DialogType, openSettingsEditor, promptForOpenOutputChannel, promptForSignIn, promptHintMessage } from "../utils/uiUtils"; import { getActiveFilePath, selectWorkspaceFolder } from "../utils/workspaceUtils"; import * as wsl from "../utils/wslUtils"; @@ -166,28 +168,30 @@ async function showProblemInternal(node: IProblem): Promise { finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath; - await leetCodeExecutor.showProblem(node, language, finalPath, leetCodeConfig.get("showCommentDescription")); - await Promise.all([ + const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration(); + await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment); + const promises: any[] = [ vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }), - movePreviewAsideIfNeeded(node), promptHintMessage( "hint.commentDescription", - 'You can generate the code file with problem description in the comments by enabling "leetcode.showCommentDescription".', + 'You can config how to show the problem description through "leetcode.showDescription".', "Open settings", - (): Promise => openSettingsEditor("leetcode.showCommentDescription"), + (): Promise => openSettingsEditor("leetcode.showDescription"), ), - ]); + ]; + if (descriptionConfig.showInWebview) { + promises.push(showDescriptionView(node)); + } + + await Promise.all(promises); } catch (error) { await promptForOpenOutputChannel(`${error} Please open the output channel for details.`, DialogType.error); } } -async function movePreviewAsideIfNeeded(node: IProblem): Promise { - if (vscode.workspace.getConfiguration("leetcode").get("enableSideMode", true)) { - return previewProblem(node, true); - } +async function showDescriptionView(node: IProblem): Promise { + return previewProblem(node, vscode.workspace.getConfiguration("leetcode").get("enableSideMode", true)); } - async function parseProblemsToPicks(p: Promise): Promise>> { return new Promise(async (resolve: (res: Array>) => void): Promise => { const picks: Array> = (await p).map((problem: IProblem) => Object.assign({}, { diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index 271ca9a4..4c0aa312 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -86,8 +86,8 @@ class LeetCodeExecutor implements Disposable { ); } - public async showProblem(problemNode: IProblem, language: string, filePath: string, detailed: boolean = false): Promise { - const templateType: string = detailed ? "-cx" : "-c"; + public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false): Promise { + const templateType: string = showDescriptionInComment ? "-cx" : "-c"; if (!await fse.pathExists(filePath)) { await fse.createFile(filePath); diff --git a/src/shared.ts b/src/shared.ts index 5f1039e4..d9d156d8 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -105,3 +105,10 @@ export const supportedPlugins: string[] = [ "solution.discuss", "leetcode.cn", ]; + +export enum DescriptionConfiguration { + InWebView = "In Webview", + InFileComment = "In File Comment", + Both = "Both", + None = "None", +} diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index 388f31b8..0aa72538 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -2,6 +2,7 @@ // Licensed under the MIT license. import { workspace, WorkspaceConfiguration } from "vscode"; +import { DescriptionConfiguration } from "../shared"; export function getWorkspaceConfiguration(): WorkspaceConfiguration { return workspace.getConfiguration("leetcode"); @@ -18,3 +19,41 @@ export function getWorkspaceFolder(): string { export function getEditorShortcuts(): string[] { return getWorkspaceConfiguration().get("editor.shortcuts", ["submit", "test"]); } + +export function getDescriptionConfiguration(): IDescriptionConfiguration { + const setting: string = getWorkspaceConfiguration().get("showDescription", DescriptionConfiguration.InWebView); + const config: IDescriptionConfiguration = { + showInComment: false, + showInWebview: true, + }; + switch (setting) { + case DescriptionConfiguration.Both: + config.showInComment = true; + config.showInWebview = true; + break; + case DescriptionConfiguration.None: + config.showInComment = false; + config.showInWebview = false; + break; + case DescriptionConfiguration.InFileComment: + config.showInComment = true; + config.showInWebview = false; + break; + case DescriptionConfiguration.InFileComment: + config.showInComment = true; + config.showInWebview = false; + break; + } + + // To be compatible with the deprecated setting: + if (getWorkspaceConfiguration().get("showCommentDescription")) { + config.showInComment = true; + } + + return config; +} + +export interface IDescriptionConfiguration { + showInComment: boolean; + showInWebview: boolean; +} From c86e09619a406b209f482d4f68e08677f61d0340 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Sun, 23 Feb 2020 11:55:44 +0800 Subject: [PATCH 2/2] Fix the test cases --- src/utils/settingUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index 0aa72538..8c94a95f 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -39,9 +39,9 @@ export function getDescriptionConfiguration(): IDescriptionConfiguration { config.showInComment = true; config.showInWebview = false; break; - case DescriptionConfiguration.InFileComment: - config.showInComment = true; - config.showInWebview = false; + case DescriptionConfiguration.InWebView: + config.showInComment = false; + config.showInWebview = true; break; }