Skip to content

Commit ceb6158

Browse files
authored
Merge pull request #75 from ccagml/main
2.7.1
2 parents 1b2ac33 + 4afa205 commit ceb6158

File tree

6 files changed

+176
-21
lines changed

6 files changed

+176
-21
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 2.7.1
2+
3+
- 答案不同地方简易上色
4+
15
## version 2.6.3
26

37
- 今天搬砖增加添加到自定义分类选项

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [状态栏增加简易计时器](#状态栏增加简易计时器)
2727
- [新增一个 remark 功能](#新增在工作目录存放数据)
2828
- [新增题目自定义分类](#新增在工作目录存放数据)
29+
- [答案不同上色,配置默认不开启](#插件配置项)
2930

3031
# 关于本项目
3132

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

174176
## 更新日志
175177

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
5-
"version": "2.6.3",
5+
"version": "2.7.1",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -679,6 +679,12 @@
679679
"scope": "application",
680680
"description": "Use endpoint's translation (if available)"
681681
},
682+
"leetcode-problem-rating.answerDiffColor": {
683+
"type": "boolean",
684+
"default": false,
685+
"scope": "application",
686+
"description": "答案不同地方上色"
687+
},
682688
"leetcode-problem-rating.workspaceFolder": {
683689
"type": "string",
684690
"scope": "machine",

src/service/SubmissionService.ts

+136-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { markdownService } from "./MarkdownService";
1313
import { ISubmitEvent } from "../model/Model";
1414
import { IWebViewOption } from "../model/Model";
1515
import { promptHintMessage } from "../utils/OutputUtils";
16-
16+
import { isAnswerDiffColor } from "../utils/ConfigUtils";
1717
class SubmissionService extends BaseWebViewService {
1818
protected readonly viewType: string = "leetcode.submission";
1919
private result: IResult;
@@ -34,28 +34,82 @@ class SubmissionService extends BaseWebViewService {
3434
};
3535
}
3636

37+
private sections_filtter(key) {
38+
if (key.substring(0, 6) == "Output") {
39+
return false;
40+
} else if (key.substring(0, 8) == "Expected") {
41+
return false;
42+
} else if (key == "messages") {
43+
return false;
44+
} else if (key == "system_message") {
45+
return false;
46+
}
47+
return true;
48+
}
49+
private getAnswerKey(result) {
50+
let ans;
51+
let exp;
52+
for (const key in result) {
53+
if (key.substring(0, 6) == "Output") {
54+
ans = key;
55+
} else if (key.substring(0, 8) == "Expected") {
56+
exp = key;
57+
}
58+
if (ans != undefined && exp != undefined) {
59+
break;
60+
}
61+
}
62+
let key: Array<any> = [];
63+
key.push(ans);
64+
key.push(exp);
65+
return key;
66+
}
67+
3768
protected getWebviewContent(): string {
3869
const styles: string = markdownService.getStyles();
3970
const title: string = `## ${this.result.messages[0]}`;
4071
const messages: string[] = this.result.messages.slice(1).map((m: string) => `* ${m}`);
41-
const sections: string[] = Object.keys(this.result)
42-
.filter((key: string) => key !== "messages" && key !== "system_message")
43-
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));
44-
const body: string = markdownService.render([title, ...messages, ...sections].join("\n"));
45-
return `
46-
<!DOCTYPE html>
47-
<html>
48-
<head>
49-
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
50-
<meta charset="UTF-8">
51-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
52-
${styles}
53-
</head>
54-
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
55-
${body}
56-
</body>
57-
</html>
58-
`;
72+
let sections: string[] = [];
73+
if (isAnswerDiffColor()) {
74+
sections = Object.keys(this.result)
75+
.filter(this.sections_filtter)
76+
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));
77+
78+
let ans_key: Array<any> = this.getAnswerKey(this.result);
79+
if (ans_key[0] != undefined && ans_key[1] != undefined) {
80+
sections.push(`### Answer\n`);
81+
sections.push(`| ${ans_key[0]} | ${ans_key[1]} | `);
82+
sections.push(`| :---------: | :---------: | `);
83+
let ans = this.result[ans_key[0]];
84+
let exp = this.result[ans_key[1]];
85+
let max_len = Math.max(ans.length, exp.length);
86+
for (let index = 0; index < max_len; index++) {
87+
sections.push(`| ${ans[index] || ""} | ${exp[index] || ""} | `);
88+
}
89+
}
90+
// require("../utils/testHot").test_add_table(sections);
91+
} else {
92+
sections = Object.keys(this.result)
93+
.filter((key: string) => key !== "messages" && key !== "system_message")
94+
.map((key: string) => [`### ${key}`, "```", this.result[key].join("\n"), "```"].join("\n"));
95+
}
96+
let body: string = markdownService.render([title, ...messages, ...sections].join("\n"));
97+
98+
let aaa = `
99+
<!DOCTYPE html>
100+
<html>
101+
<head>
102+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
103+
<meta charset="UTF-8">
104+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
105+
${styles}
106+
</head>
107+
<body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
108+
${body}
109+
</body>
110+
</html>
111+
`;
112+
return aaa;
59113
}
60114

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

133+
private add_color_str(str1, str2) {
134+
let result: Array<string> = [];
135+
let min_len = Math.min(str1.length, str2.length);
136+
let dif_len = 0;
137+
for (let index = 0; index < min_len; index++) {
138+
if (str1[index] != str2[index]) {
139+
dif_len = index;
140+
break;
141+
}
142+
}
143+
let str1_left = str1.substring(0, dif_len);
144+
let str1_right = str1.substring(dif_len);
145+
let str2_left = str2.substring(0, dif_len);
146+
let str2_right = str2.substring(dif_len);
147+
result.push(str1_left + this.getRedPre() + str1_right + this.getRedEnd());
148+
result.push(str2_left + this.getRedPre() + str2_right + this.getRedEnd());
149+
150+
return result;
151+
}
152+
153+
private add_color(temp) {
154+
// let;
155+
let output_key;
156+
let expected_key;
157+
for (const key in temp) {
158+
if (typeof key == "string") {
159+
if (key.substring(0, 6) == "Output") {
160+
output_key = key;
161+
} else if (key.substring(0, 8) == "Expected") {
162+
expected_key = key;
163+
}
164+
if (output_key && expected_key) {
165+
break;
166+
}
167+
}
168+
}
169+
if (output_key && expected_key) {
170+
let output_str = temp[output_key] || [];
171+
let expected_str = temp[expected_key] || [];
172+
let min_len = Math.min(output_str.length, expected_str.length);
173+
for (let index = 0; index < min_len; index++) {
174+
if (output_str[index] != expected_str[index]) {
175+
let temp_result = this.add_color_str(output_str[index], expected_str[index]);
176+
output_str[index] = temp_result[0] || "";
177+
expected_str[index] = temp_result[1] || "";
178+
}
179+
}
180+
}
181+
}
182+
183+
private getRedPre() {
184+
return "__`";
185+
}
186+
private getRedEnd() {
187+
return "`__";
188+
}
189+
79190
private parseResult(raw: string): IResult {
80-
return JSON.parse(raw);
191+
let temp = JSON.parse(raw);
192+
if (isAnswerDiffColor()) {
193+
this.add_color(temp);
194+
}
195+
196+
return temp;
81197
}
82198
}
83199

src/utils/ConfigUtils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,7 @@ export async function setDefaultLanguage(): Promise<void> {
366366
leetCodeConfig.update("defaultLanguage", selectedItem.label, true /* Global */);
367367
window.showInformationMessage(`设置默认语言 ${selectedItem.label} 成功`);
368368
}
369+
370+
export function isAnswerDiffColor(): boolean {
371+
return getVsCodeConfig().get<boolean>("answerDiffColor", false);
372+
}

src/utils/testHot.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function getSubmissionResult() {
2+
return JSON.stringify({
3+
messages: ["Finished"],
4+
system_message: {
5+
fid: "1796",
6+
id: 1904,
7+
qid: 1904,
8+
sub_type: "test",
9+
accepted: true,
10+
},
11+
"Your Input": ['"dfa12321afd"'],
12+
"Output (0 ms)": ["1"],
13+
"Expected Answer": ["2"],
14+
Stdout: [""],
15+
});
16+
}
17+
18+
export function test_add_table(sections) {
19+
sections.push(`\n\n\n### aaaaaa\n`);
20+
sections.push(`| a1a1 | a2a2 |\n| :---------: | :---------: |\n| s1s1 | s2s2 | `);
21+
sections.push(`| __\`aaaaaaaaa\`__ | bbbbbbbbbbb | `);
22+
sections.push(`| __\`ccccccccccccc\`__ | __\`ddddddddddtext\`__ | `);
23+
}

0 commit comments

Comments
 (0)