Skip to content

2.7.1 #75

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 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 2.7.1

- 答案不同地方简易上色

## version 2.6.3

- 今天搬砖增加添加到自定义分类选项
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- [状态栏增加简易计时器](#状态栏增加简易计时器)
- [新增一个 remark 功能](#新增在工作目录存放数据)
- [新增题目自定义分类](#新增在工作目录存放数据)
- [答案不同上色,配置默认不开启](#插件配置项)

# 关于本项目

Expand Down Expand Up @@ -170,6 +171,7 @@
| <font color=red>leetcode-problem-rating.pickOneByRankRangeMax</font> | 随机一题的最大浮动,随机一题最高分(你的竞赛分+本配置)。 | <font color=red>150</font> |
| <font color=red>leetcode-problem-rating.hideScore</font> | 隐藏分数相关的题目。Score:隐藏有分数的题目, NoScore:隐藏没有分数的题目, ScoreRange:隐藏分数范围外的题目 | <font color=red>None</font> |
| <font color=red>leetcode-problem-rating.useVscodeNode</font> | 默认情况下使用 VsCode 自带 Node 环境,不需要额外安装 Node 环境 | <font color=red>true</font> |
| <font color=red>leetcode-problem-rating.answerDiffColor</font> | 答案不同的地方上色 | <font color=red>false</font> |

## 更新日志

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode",
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
"version": "2.6.3",
"version": "2.7.1",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down Expand Up @@ -679,6 +679,12 @@
"scope": "application",
"description": "Use endpoint's translation (if available)"
},
"leetcode-problem-rating.answerDiffColor": {
"type": "boolean",
"default": false,
"scope": "application",
"description": "答案不同地方上色"
},
"leetcode-problem-rating.workspaceFolder": {
"type": "string",
"scope": "machine",
Expand Down
156 changes: 136 additions & 20 deletions src/service/SubmissionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { markdownService } from "./MarkdownService";
import { ISubmitEvent } from "../model/Model";
import { IWebViewOption } from "../model/Model";
import { promptHintMessage } from "../utils/OutputUtils";

import { isAnswerDiffColor } from "../utils/ConfigUtils";
class SubmissionService extends BaseWebViewService {
protected readonly viewType: string = "leetcode.submission";
private result: IResult;
Expand All @@ -34,28 +34,82 @@ class SubmissionService extends BaseWebViewService {
};
}

private sections_filtter(key) {
if (key.substring(0, 6) == "Output") {
return false;
} else if (key.substring(0, 8) == "Expected") {
return false;
} else if (key == "messages") {
return false;
} else if (key == "system_message") {
return false;
}
return true;
}
private getAnswerKey(result) {
let ans;
let exp;
for (const key in result) {
if (key.substring(0, 6) == "Output") {
ans = key;
} else if (key.substring(0, 8) == "Expected") {
exp = key;
}
if (ans != undefined && exp != undefined) {
break;
}
}
let key: Array<any> = [];
key.push(ans);
key.push(exp);
return key;
}

protected getWebviewContent(): string {
const styles: string = markdownService.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" && key !== "system_message")
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));
const body: string = markdownService.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>
`;
let sections: string[] = [];
if (isAnswerDiffColor()) {
sections = Object.keys(this.result)
.filter(this.sections_filtter)
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));

let ans_key: Array<any> = this.getAnswerKey(this.result);
if (ans_key[0] != undefined && ans_key[1] != undefined) {
sections.push(`### Answer\n`);
sections.push(`| ${ans_key[0]} | ${ans_key[1]} | `);
sections.push(`| :---------: | :---------: | `);
let ans = this.result[ans_key[0]];
let exp = this.result[ans_key[1]];
let max_len = Math.max(ans.length, exp.length);
for (let index = 0; index < max_len; index++) {
sections.push(`| ${ans[index] || ""} | ${exp[index] || ""} | `);
}
}
// require("../utils/testHot").test_add_table(sections);
} else {
sections = Object.keys(this.result)
.filter((key: string) => key !== "messages" && key !== "system_message")
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));
}
let body: string = markdownService.render([title, ...messages, ...sections].join("\n"));

let aaa = `
<!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>
`;
return aaa;
}

protected onDidDisposeWebview(): void {
Expand All @@ -76,8 +130,70 @@ class SubmissionService extends BaseWebViewService {
await commands.executeCommand("workbench.action.openGlobalKeybindings", query);
}

private add_color_str(str1, str2) {
let result: Array<string> = [];
let min_len = Math.min(str1.length, str2.length);
let dif_len = 0;
for (let index = 0; index < min_len; index++) {
if (str1[index] != str2[index]) {
dif_len = index;
break;
}
}
let str1_left = str1.substring(0, dif_len);
let str1_right = str1.substring(dif_len);
let str2_left = str2.substring(0, dif_len);
let str2_right = str2.substring(dif_len);
result.push(str1_left + this.getRedPre() + str1_right + this.getRedEnd());
result.push(str2_left + this.getRedPre() + str2_right + this.getRedEnd());

return result;
}

private add_color(temp) {
// let;
let output_key;
let expected_key;
for (const key in temp) {
if (typeof key == "string") {
if (key.substring(0, 6) == "Output") {
output_key = key;
} else if (key.substring(0, 8) == "Expected") {
expected_key = key;
}
if (output_key && expected_key) {
break;
}
}
}
if (output_key && expected_key) {
let output_str = temp[output_key] || [];
let expected_str = temp[expected_key] || [];
let min_len = Math.min(output_str.length, expected_str.length);
for (let index = 0; index < min_len; index++) {
if (output_str[index] != expected_str[index]) {
let temp_result = this.add_color_str(output_str[index], expected_str[index]);
output_str[index] = temp_result[0] || "";
expected_str[index] = temp_result[1] || "";
}
}
}
}

private getRedPre() {
return "__`";
}
private getRedEnd() {
return "`__";
}

private parseResult(raw: string): IResult {
return JSON.parse(raw);
let temp = JSON.parse(raw);
if (isAnswerDiffColor()) {
this.add_color(temp);
}

return temp;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/ConfigUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,7 @@ export async function setDefaultLanguage(): Promise<void> {
leetCodeConfig.update("defaultLanguage", selectedItem.label, true /* Global */);
window.showInformationMessage(`设置默认语言 ${selectedItem.label} 成功`);
}

export function isAnswerDiffColor(): boolean {
return getVsCodeConfig().get<boolean>("answerDiffColor", false);
}
23 changes: 23 additions & 0 deletions src/utils/testHot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function getSubmissionResult() {
return JSON.stringify({
messages: ["Finished"],
system_message: {
fid: "1796",
id: 1904,
qid: 1904,
sub_type: "test",
accepted: true,
},
"Your Input": ['"dfa12321afd"'],
"Output (0 ms)": ["1"],
"Expected Answer": ["2"],
Stdout: [""],
});
}

export function test_add_table(sections) {
sections.push(`\n\n\n### aaaaaa\n`);
sections.push(`| a1a1 | a2a2 |\n| :---------: | :---------: |\n| s1s1 | s2s2 | `);
sections.push(`| __\`aaaaaaaaa\`__ | bbbbbbbbbbb | `);
sections.push(`| __\`ccccccccccccc\`__ | __\`ddddddddddtext\`__ | `);
}