Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/webview/leetCodePreviewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LeetCodePreviewProvider implements Disposable {
}

this.panel.webview.html = await this.provideHtmlContent(node);
this.panel.title = node.name;
this.panel.title = `${node.name}: Preview`;
this.panel.reveal();
}

Expand Down
109 changes: 97 additions & 12 deletions src/webview/leetCodeResultProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@
// Licensed under the MIT license.

import { Disposable, ExtensionContext, ViewColumn, WebviewPanel, window } from "vscode";
import { leetCodeChannel } from "../leetCodeChannel";
import { MarkdownEngine } from "./MarkdownEngine";

class LeetCodeResultProvider implements Disposable {

private context: ExtensionContext;
private panel: WebviewPanel | undefined;
private mdEngine: MarkdownEngine;

public initialize(context: ExtensionContext): void {
this.mdEngine = new MarkdownEngine();
this.context = context;
}

public async show(result: string): Promise<void> {
public async show(resultString: string): Promise<void> {
if (!this.panel) {
this.panel = window.createWebviewPanel("leetcode.result", "LeetCode Results", ViewColumn.Two, {
this.panel = window.createWebviewPanel("leetcode.result", "Submission Result", ViewColumn.Two, {
retainContextWhenHidden: true,
enableFindWidget: true,
localResourceRoots: this.mdEngine.localResourceRoots,
});

this.panel.onDidDispose(() => {
this.panel = undefined;
}, null, this.context.subscriptions);
}

this.panel.webview.html = await this.provideHtmlContent(result);
const result: Result = this.parseResult(resultString);
this.panel.webview.html = this.getWebViewContent(result);
this.panel.reveal(ViewColumn.Two);
}

Expand All @@ -34,19 +40,98 @@ class LeetCodeResultProvider implements Disposable {
}
}

private async provideHtmlContent(result: string): Promise<string> {
return `<!DOCTYPE html>
<html lang="en">
private parseResult(raw: string): Result {
try {
switch (raw[2]) {
case "√": {
const result: AcceptResult = new AcceptResult();
[result.status, raw] = raw.split(/ . (.+)([^]+)/).slice(1);
[result.passed, raw] = raw.split(/\n . (.+)([^]+)/).slice(1);
[result.runtime, raw] = raw.split(/\n . (.+)([^]+)/).slice(1);
[result.memory] = raw.split(/\n . (.+)/).slice(1);
return result;
}
case "×": {
const result: FailedResult = new FailedResult();
[result.status, raw] = raw.split(/ . (.+)([^]+)/).slice(1);
[result.passed, raw] = raw.split(/\n . (.+)([^]+)/).slice(1);
[result.testcase, raw] = raw.split(/\n . testcase: '(.+)'([^]+)/).slice(1);
[result.answer, raw] = raw.split(/\n . answer: (.+)([^]+)/).slice(1);
[result.expected, raw] = raw.split(/\n . expected_answer: (.+)([^]+)/).slice(1);
[result.stdout] = raw.split(/\n . stdout: ([^]+?)\n$/).slice(1);
result.testcase = result.testcase.replace("\\n", "\n");
return result;
}
default: {
throw new TypeError(raw);
}
}
} catch (error) {
leetCodeChannel.appendLine(`Result parsing failed: ${error.message}`);
throw error;
}
}

private getWebViewContent(result: Result): string {
const styles: string = this.mdEngine.getStylesHTML();
let body: string;
if (result instanceof AcceptResult) {
const accpet: AcceptResult = result as AcceptResult;
body = this.mdEngine.render([
`## ${result.status}`,
``,
`* ${result.passed}`,
`* ${accpet.runtime}`,
`* ${accpet.memory}`,
].join("\n"));
} else {
const failed: FailedResult = result as FailedResult;
body = this.mdEngine.render([
`## ${result.status}`,
`* ${result.passed}`,
``,
`### Testcase`, // TODO: add command to copy raw testcase
`\`\`\`\n${failed.testcase}\n\`\`\``,
`### Answer`,
`\`\`\`\n${failed.answer}\n\`\`\``,
`### Expected`,
`\`\`\`\n${failed.expected}\n\`\`\``,
`### Stdout`,
`\`\`\`\n${failed.stdout}\n\`\`\``,
].join("\n"));
}
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LeetCode Results</title>
${styles}
</head>
<body>
<pre>${result.trim()}</pre>
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
${body}
</body>
</html>`;
</html>
`;
}
}

// tslint:disable-next-line:max-classes-per-file
abstract class Result {
public status: string;
public passed: string;
}

// tslint:disable-next-line:max-classes-per-file
class AcceptResult extends Result {
public runtime: string;
public memory: string;
}

// tslint:disable-next-line:max-classes-per-file
class FailedResult extends Result {
public testcase: string;
public answer: string;
public expected: string;
public stdout: string;
}

export const leetCodeResultProvider: LeetCodeResultProvider = new LeetCodeResultProvider();
9 changes: 4 additions & 5 deletions src/webview/leetCodeSolutionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class LeetCodeSolutionProvider implements Disposable {
private context: ExtensionContext;
private panel: WebviewPanel | undefined;
private mdEngine: MarkdownEngine;
private solution: Solution;

public initialize(context: ExtensionContext): void {
this.context = context;
Expand All @@ -30,9 +29,9 @@ class LeetCodeSolutionProvider implements Disposable {
}, null, this.context.subscriptions);
}

this.solution = this.parseSolution(solutionString);
this.panel.title = problem.name;
this.panel.webview.html = this.getWebViewContent(this.solution);
const solution: Solution = this.parseSolution(solutionString);
this.panel.title = `${problem.name}: Solution`;
this.panel.webview.html = this.getWebViewContent(solution);
this.panel.reveal(ViewColumn.Active);
}

Expand Down Expand Up @@ -66,7 +65,7 @@ class LeetCodeSolutionProvider implements Disposable {
`| ${lang} | ${auth} | ${votes} |`,
].join("\n"));
const body: string = this.mdEngine.render(solution.body, {
lang: this.solution.lang,
lang: solution.lang,
host: "https://discuss.leetcode.com/",
});
return `
Expand Down