// Copyright (c) jdneo. All rights reserved. // Licensed under the MIT license. import { ViewColumn } from "vscode"; import { openKeybindingsEditor, promptHintMessage } from "../utils/uiUtils"; import { ILeetCodeWebviewOption, LeetCodeWebview } from "./LeetCodeWebview"; import { markdownEngine } from "./markdownEngine"; class LeetCodeSubmissionProvider extends LeetCodeWebview { protected readonly viewType: string = "leetcode.submission"; private result: IResult; public show(resultString: string): void { this.result = this.parseResult(resultString); this.showWebviewInternal(); this.showKeybindingsHint(); } protected getWebviewOption(): ILeetCodeWebviewOption { return { title: "Submission", viewColumn: ViewColumn.Two, }; } protected getWebviewContent(): string { const styles: string = markdownEngine.getStyles(); const title: string = `## ${this.result.messages[0]}`; const messages: string[] = this.result.messages.slice(1).map((m: string) => `* ${m}`); const sections: string[] = Object.keys(this.result) .filter((key: string) => key !== "messages") .map((key: string) => [ `### ${key}`, "```", this.result[key].join("\n"), "```", ].join("\n")); const body: string = markdownEngine.render([ title, ...messages, ...sections, ].join("\n")); return ` ${styles} ${body} `; } protected onDidDisposeWebview(): void { super.onDidDisposeWebview(); delete this.result; } private async showKeybindingsHint(): Promise { await promptHintMessage( "hint.commandShortcut", 'You can customize shortcut key bindings in File > Preferences > Keyboard Shortcuts with query "leetcode".', "Open Keybindings", (): Promise => openKeybindingsEditor("leetcode solution"), ); } private parseResult(raw: string): IResult { raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string const regSplit: RegExp = / [√×✔✘vx] ([^]+?)\n(?= [√×✔✘vx] )/g; const regKeyVal: RegExp = /(.+?): ([^]*)/; const result: IResult = { messages: [] }; let entry: RegExpExecArray | null; do { entry = regSplit.exec(raw); if (!entry) { continue; } const kvMatch: RegExpExecArray | null = regKeyVal.exec(entry[1]); if (kvMatch) { const [key, value] = kvMatch.slice(1); if (value) { // Do not show empty string if (!result[key]) { result[key] = []; } result[key].push(value); } } else { result.messages.push(entry[1]); } } while (entry); return result; } } interface IResult { [key: string]: string[]; messages: string[]; } export const leetCodeSubmissionProvider: LeetCodeSubmissionProvider = new LeetCodeSubmissionProvider();