Skip to content
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

Better config how to show the problem description #519

Merged
merged 2 commits into from
Feb 23, 2020
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 15 additions & 11 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -166,28 +168,30 @@ async function showProblemInternal(node: IProblem): Promise<void> {

finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;

await leetCodeExecutor.showProblem(node, language, finalPath, leetCodeConfig.get<boolean>("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<any> => openSettingsEditor("leetcode.showCommentDescription"),
(): Promise<any> => 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<void> {
if (vscode.workspace.getConfiguration("leetcode").get<boolean>("enableSideMode", true)) {
return previewProblem(node, true);
}
async function showDescriptionView(node: IProblem): Promise<void> {
return previewProblem(node, vscode.workspace.getConfiguration("leetcode").get<boolean>("enableSideMode", true));
}

async function parseProblemsToPicks(p: Promise<IProblem[]>): Promise<Array<IQuickItemEx<IProblem>>> {
return new Promise(async (resolve: (res: Array<IQuickItemEx<IProblem>>) => void): Promise<void> => {
const picks: Array<IQuickItemEx<IProblem>> = (await p).map((problem: IProblem) => Object.assign({}, {
Expand Down
4 changes: 2 additions & 2 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class LeetCodeExecutor implements Disposable {
);
}

public async showProblem(problemNode: IProblem, language: string, filePath: string, detailed: boolean = false): Promise<void> {
const templateType: string = detailed ? "-cx" : "-c";
public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false): Promise<void> {
const templateType: string = showDescriptionInComment ? "-cx" : "-c";

if (!await fse.pathExists(filePath)) {
await fse.createFile(filePath);
Expand Down
7 changes: 7 additions & 0 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
39 changes: 39 additions & 0 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -18,3 +19,41 @@ export function getWorkspaceFolder(): string {
export function getEditorShortcuts(): string[] {
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
}

export function getDescriptionConfiguration(): IDescriptionConfiguration {
const setting: string = getWorkspaceConfiguration().get<string>("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.InWebView:
config.showInComment = false;
config.showInWebview = true;
break;
}

// To be compatible with the deprecated setting:
if (getWorkspaceConfiguration().get<boolean>("showCommentDescription")) {
config.showInComment = true;
}

return config;
}

export interface IDescriptionConfiguration {
showInComment: boolean;
showInWebview: boolean;
}