- Notifications
You must be signed in to change notification settings - Fork669
Apply markdown engine to result provider#232
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
0e97e36
4bb9018
983885e
7559af5
7202299
5677450
738ffd9
e5f3f0e
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// Copyright (c) jdneo. All rights reserved. | ||
// Licensed under the MIT license. | ||
import * as _ from "lodash"; | ||
import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode"; | ||
import { markdownEngine } from "./markdownEngine"; | ||
class LeetCodeResultProvider implements Disposable { | ||
@@ -12,19 +14,21 @@ class LeetCodeResultProvider implements Disposable { | ||
this.context = context; | ||
} | ||
public async show(resultString: string): Promise<void> { | ||
if (!this.panel) { | ||
this.panel = window.createWebviewPanel("leetcode.result", "Submission Result", ViewColumn.Two, { | ||
retainContextWhenHidden: true, | ||
enableFindWidget: true, | ||
localResourceRoots: markdownEngine.localResourceRoots, | ||
}); | ||
this.panel.onDidDispose(() => { | ||
this.panel = undefined; | ||
}, null, this.context.subscriptions); | ||
} | ||
const result: IResult = this.parseResult(resultString); | ||
this.panel.webview.html = this.getWebViewContent(result); | ||
this.panel.reveal(ViewColumn.Two); | ||
} | ||
@@ -34,19 +38,67 @@ class LeetCodeResultProvider implements Disposable { | ||
} | ||
} | ||
private parseResult(raw: string): IResult { | ||
raw = raw.concat(" √ "); // Append a dummy sentinel to the end of raw string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What will happen if we don't append There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The RegEx captures anything in between two Worth to note that, with this trick, messages like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Hmm... still feeling the logic here is too tricky for maintaince. It's ok to merge for now. | ||
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: string = _.startCase(kvMatch[1]); | ||
let value: string = kvMatch[2]; | ||
if (!result[key]) { | ||
result[key] = []; | ||
} | ||
if (key === "Testcase") { | ||
value = value.slice(1, -1).replace("\\n", "\n"); | ||
} | ||
result[key].push(value); | ||
} else { | ||
result.messages.push(entry[1]); | ||
} | ||
} while (entry); | ||
return result; | ||
} | ||
private getWebViewContent(result: IResult): string { | ||
const styles: string = markdownEngine.getStylesHTML(); | ||
const title: string = `## ${result.messages[0]}`; | ||
const messages: string[] = result.messages.slice(1).map((m: string) => `* ${m}`); | ||
const sections: string[] = Object.keys(result).filter((k: string) => k !== "messages").map((key: string) => [ | ||
`### ${key}`, | ||
"```", | ||
result[key].join("\n\n"), | ||
"```", | ||
].join("\n")); | ||
const body: string = markdownEngine.render([ | ||
title, | ||
...messages, | ||
...sections, | ||
].join("\n")); | ||
return ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
${styles} | ||
</head> | ||
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4"> | ||
${body} | ||
</body> | ||
</html> | ||
`; | ||
} | ||
} | ||
interface IResult { | ||
[key: string]: string[]; | ||
messages: string[]; | ||
} | ||
export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider(); |