From bd79dd819bc13922a05c7760313e671ff8a61714 Mon Sep 17 00:00:00 2001 From: wangzihang Date: Mon, 9 Jan 2023 17:46:51 +0800 Subject: [PATCH] modify the code template --- package.json | 16 ++++++++++++---- src/commands/show.ts | 7 ++++++- src/leetCodeExecutor.ts | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index db0d475..d7b1556 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "vscode-leetcode", - "displayName": "LeetCode", + "name": "vscode-leetcode-wzh", + "displayName": "LeetCode-wzh", "description": "Solve LeetCode problems in VS Code", - "version": "0.18.1", - "author": "Sheng Chen", + "version": "0.18.1-wzh", + "author": "Sheng Chen (Modified by wzh)", "publisher": "LeetCode", "license": "MIT", "icon": "resources/LeetCode.png", @@ -700,6 +700,14 @@ "Acceptance Rate (Descending)" ], "description": "Sorting strategy for problems list." + }, + "leetcode.header": { + "type": "array", + "items": { + "type": "string" + }, + "scope": "application", + "description": "Add your custom codes at the top of source files." } } } diff --git a/src/commands/show.ts b/src/commands/show.ts index 3aebce8..92c3426 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -170,7 +170,12 @@ async function showProblemInternal(node: IProblem): Promise { const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration(); const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation(); - await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation); + const header: string[] = leetCodeConfig.get("header", []); + let header_str: string = header.join("\n").trim(); + if (header_str.length > 0) { + header_str += "\n"; + } + await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment, needTranslation, header_str); const promises: any[] = [ vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }), promptHintMessage( diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index d2332c7..a87f208 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -100,7 +100,7 @@ class LeetCodeExecutor implements Disposable { return await this.executeCommandEx(this.nodeExecutable, cmd); } - public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise { + public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean, header: string = ""): Promise { const templateType: string = showDescriptionInComment ? "-cx" : "-c"; const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.id, templateType, "-l", language]; @@ -111,7 +111,7 @@ class LeetCodeExecutor implements Disposable { if (!await fse.pathExists(filePath)) { await fse.createFile(filePath); const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd); - await fse.writeFile(filePath, codeTemplate); + await fse.writeFile(filePath, header + codeTemplate); } }