Skip to content

Commit

Permalink
revert the change of result page (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Apr 2, 2019
1 parent d935977 commit 43feaa3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 65 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- Add a setting `leetcode.nodePath` to specify the `Node.js` executable path [#227](https://github.com/jdneo/vscode-leetcode/issues/227)

## Changed
- Improve the submission result page
- Update the activity bar icon, See: [#225](https://github.com/jdneo/vscode-leetcode/pull/225)

## Fixed
Expand Down
76 changes: 12 additions & 64 deletions src/webview/leetCodeResultProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// 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 {

Expand All @@ -14,21 +12,19 @@ class LeetCodeResultProvider implements Disposable {
this.context = context;
}

public async show(resultString: string): Promise<void> {
public async show(result: string): Promise<void> {
if (!this.panel) {
this.panel = window.createWebviewPanel("leetcode.result", "Submission Result", ViewColumn.Two, {
this.panel = window.createWebviewPanel("leetcode.result", "LeetCode Results", 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.webview.html = await this.provideHtmlContent(result);
this.panel.reveal(ViewColumn.Two);
}

Expand All @@ -38,67 +34,19 @@ class LeetCodeResultProvider implements Disposable {
}
}

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: 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>
private async provideHtmlContent(result: string): Promise<string> {
return `<!DOCTYPE html>
<html lang="en">
<head>
${styles}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode Results</title>
</head>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${body}
<body>
<pre>${result.trim()}</pre>
</body>
</html>
`;
</html>`;
}
}

interface IResult {
[key: string]: string[];
messages: string[];
}

export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider();

0 comments on commit 43feaa3

Please sign in to comment.