diff --git a/CHANGELOG.md b/CHANGELOG.md index 08a0d80..4a42e35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## version 2.7.1 + +- 答案不同地方简易上色 + ## version 2.6.3 - 今天搬砖增加添加到自定义分类选项 diff --git a/README.md b/README.md index a44d3b4..8606863 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ - [状态栏增加简易计时器](#状态栏增加简易计时器) - [新增一个 remark 功能](#新增在工作目录存放数据) - [新增题目自定义分类](#新增在工作目录存放数据) +- [答案不同上色,配置默认不开启](#插件配置项) # 关于本项目 @@ -170,6 +171,7 @@ | leetcode-problem-rating.pickOneByRankRangeMax | 随机一题的最大浮动,随机一题最高分(你的竞赛分+本配置)。 | 150 | | leetcode-problem-rating.hideScore | 隐藏分数相关的题目。Score:隐藏有分数的题目, NoScore:隐藏没有分数的题目, ScoreRange:隐藏分数范围外的题目 | None | | leetcode-problem-rating.useVscodeNode | 默认情况下使用 VsCode 自带 Node 环境,不需要额外安装 Node 环境 | true | +| leetcode-problem-rating.answerDiffColor | 答案不同的地方上色 | false | ## 更新日志 diff --git a/package.json b/package.json index 2c672d7..2ee533e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/service/SubmissionService.ts b/src/service/SubmissionService.ts index 9d596d1..94a0e76 100644 --- a/src/service/SubmissionService.ts +++ b/src/service/SubmissionService.ts @@ -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; @@ -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 = []; + 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 ` - - - - - - - ${styles} - - - ${body} - - - `; + 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 = 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 = ` + + + + + + + ${styles} + + + ${body} + + +`; + return aaa; } protected onDidDisposeWebview(): void { @@ -76,8 +130,70 @@ class SubmissionService extends BaseWebViewService { await commands.executeCommand("workbench.action.openGlobalKeybindings", query); } + private add_color_str(str1, str2) { + let result: Array = []; + 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; } } diff --git a/src/utils/ConfigUtils.ts b/src/utils/ConfigUtils.ts index b787ee7..384836c 100644 --- a/src/utils/ConfigUtils.ts +++ b/src/utils/ConfigUtils.ts @@ -366,3 +366,7 @@ export async function setDefaultLanguage(): Promise { leetCodeConfig.update("defaultLanguage", selectedItem.label, true /* Global */); window.showInformationMessage(`设置默认语言 ${selectedItem.label} 成功`); } + +export function isAnswerDiffColor(): boolean { + return getVsCodeConfig().get("answerDiffColor", false); +} diff --git a/src/utils/testHot.ts b/src/utils/testHot.ts new file mode 100644 index 0000000..5b6e1a6 --- /dev/null +++ b/src/utils/testHot.ts @@ -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\`__ | `); +}