forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetCodeSubmissionProvider.ts
107 lines (96 loc) · 3.74 KB
/
leetCodeSubmissionProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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 `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
${styles}
</head>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${body}
</body>
</html>
`;
}
protected onDidDisposeWebview(): void {
super.onDidDisposeWebview();
delete this.result;
}
private async showKeybindingsHint(): Promise<void> {
await promptHintMessage(
"hint.commandShortcut",
'You can customize shortcut key bindings in File > Preferences > Keyboard Shortcuts with query "leetcode".',
"Open Keybindings",
(): Promise<any> => 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();