Skip to content

Commit 884c795

Browse files
committed
feat: add generate doc
1 parent 9a4b564 commit 884c795

File tree

6 files changed

+107
-18
lines changed

6 files changed

+107
-18
lines changed

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode",
33
"displayName": "LeetCode",
44
"description": "Solve LeetCode problems in VS Code",
5-
"version": "0.19.1",
5+
"version": "0.20.1",
66
"author": "convexwf",
77
"publisher": "LeetCode",
88
"license": "MIT",
@@ -32,6 +32,7 @@
3232
"onCommand:leetcode.manageSessions",
3333
"onCommand:leetcode.refreshExplorer",
3434
"onCommand:leetcode.pickOne",
35+
"onCommand:leetcode.generateDocumentation",
3536
"onCommand:leetcode.showProblem",
3637
"onCommand:leetcode.showDocumentation",
3738
"onCommand:leetcode.previewProblem",
@@ -83,6 +84,11 @@
8384
"title": "Pick One",
8485
"category": "LeetCode"
8586
},
87+
{
88+
"command": "leetcode.generateDocumentation",
89+
"title": "Generate Documentation",
90+
"category": "LeetCode"
91+
},
8692
{
8793
"command": "leetcode.showProblem",
8894
"title": "Show Problem",

src/commands/show.ts

+78
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,91 @@ export async function pickOne(): Promise<void> {
5252
await showProblemInternal(randomProblem);
5353
}
5454

55+
export async function generateDocumentation(): Promise<void> {
56+
const ids: string | undefined = await vscode.window.showInputBox({
57+
prompt: "Enter problem id. e.g. 1, 1-10, 1,2,3, 1,2,3-10",
58+
ignoreFocusOut: true,
59+
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
60+
});
61+
if (!ids) {
62+
return;
63+
}
64+
const idArray: string[] = ids.split(",");
65+
const nodeArray: IProblem[] = [];
66+
for (const id of idArray) {
67+
const _id: string = id.trim();
68+
if (_id.search("-") > 0) {
69+
const range: string[] = _id.split("-");
70+
const start: number = parseInt(range[0], 10);
71+
const end: number = parseInt(range[1], 10);
72+
if (start > 0 && end > 0 && start <= end) {
73+
for (let i: number = start; i <= end; i++) {
74+
console.log(`Show documentation: ${i}`);
75+
const node: IProblem | undefined = explorerNodeManager.getNodeById(i.toString());
76+
if (node) {
77+
nodeArray.push(node);
78+
}
79+
}
80+
}
81+
}
82+
else {
83+
console.log(`Show documentation: ${_id}`);
84+
const node: IProblem | undefined = explorerNodeManager.getNodeById(_id);
85+
if (node) {
86+
nodeArray.push(node);
87+
}
88+
}
89+
}
90+
91+
for (const node of nodeArray) {
92+
const finalPath: string = await assureDoucumentation(node);
93+
if (!finalPath) {
94+
continue;
95+
}
96+
try {
97+
await leetCodeExecutor.showDocumentationInternal(node, finalPath);
98+
}
99+
catch (error) {
100+
console.log(error);
101+
vscode.window.showErrorMessage(error);
102+
}
103+
}
104+
}
105+
55106
export async function showProblem(node?: LeetCodeNode): Promise<void> {
56107
if (!node) {
57108
return;
58109
}
59110
await showProblemInternal(node);
60111
}
61112

113+
async function assureDoucumentation(node: IProblem): Promise<string> {
114+
const leetCodeConfig: vscode.WorkspaceConfiguration = getWorkspaceConfiguration();
115+
const workspaceFolder: string = await selectWorkspaceFolder();
116+
if (!workspaceFolder) {
117+
return "";
118+
}
119+
120+
const fileFolder: string = leetCodeConfig.get<string>(`filePath.doc.folder`, "").trim();
121+
const fileName: string = leetCodeConfig.get<string>(`filePath.doc.filename`, "").trim();
122+
if (fileFolder === "" || fileName === "") {
123+
await promptForOpenOutputChannel("Please configure the file path first.", DialogType.error);
124+
return "";
125+
}
126+
127+
let finalPath: string = path.join(workspaceFolder, fileFolder, fileName);
128+
if (finalPath) {
129+
finalPath = await resolveRelativePath(finalPath, node, "md");
130+
if (!finalPath) {
131+
leetCodeChannel.appendLine("Showing problem canceled by user.");
132+
return "";
133+
}
134+
}
135+
finalPath = wsl.useWsl() ? await wsl.toWinPath(finalPath) : finalPath;
136+
console.log(`Documentation path: ${finalPath}`);
137+
return finalPath;
138+
}
139+
62140
export async function showDocumentation(node?: LeetCodeNode): Promise<void> {
63141
if (!node) {
64142
return;

src/commands/submit.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export async function submitSolution(uri?: vscode.Uri): Promise<void> {
4343

4444
// 匹配通过测试用例数、运行时间和内存使用情况的正则表达式
4545
const regex_cases_passed = /(\d+)\/(\d+) cases passed/;
46-
const regex_runtime_percentage = /Your runtime beats (\d+\.\d+) %/;
47-
const regex_memory_percentage = /Your memory usage beats (\d+\.\d+) %/;
46+
const regex_runtime_percentage = /Your runtime beats (\d+(.\d+)?) %/;
47+
const regex_memory_percentage = /Your memory usage beats (\d+(.\d+)?) %/;
4848
const regex_runtime_ms = /(\d+) ms/;
49-
const regex_memory_usage = /\((\d+\.\d+) MB\)/;
49+
const regex_memory_usage = /\( (\d+(.\d+)?) MB\)/;
5050

5151
// 提取通过测试用例数、运行时间和内存使用情况
5252
const [cases_passed, total_cases] = regex_cases_passed.exec(message)?.slice(1) ?? [];

src/extension.ts

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
5959
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
6060
vscode.commands.registerCommand("leetcode.showDocumentation", (node: LeetCodeNode) => show.showDocumentation(node)),
6161
vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()),
62+
vscode.commands.registerCommand("leetcode.generateDocumentation", () => show.generateDocumentation()),
6263
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
6364
vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)),
6465
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),

src/leetCodeExecutor.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ class LeetCodeExecutor implements Disposable {
156156

157157
public async showDocumentationInternal(problemNode: IProblem, filePath: string): Promise<void> {
158158

159+
if (await fse.pathExists(filePath)) {
160+
await window.showInformationMessage(`File ${filePath} already exists!`);
161+
return;
162+
}
163+
159164
const descString: string = await this.getDescription(problemNode.id, false);
160165
const description = this.parseDescription(descString, problemNode);
161166
const { title, url, category, difficulty, likes, dislikes, body } = description;
@@ -188,13 +193,12 @@ class LeetCodeExecutor implements Disposable {
188193

189194
// console.log("md_content", md_content);
190195

191-
if (!await fse.pathExists(filePath)) {
192-
await fse.createFile(filePath);
193-
await fse.writeFile(filePath, md_content);
194-
workspace.openTextDocument(filePath).then((document) => {
195-
window.showTextDocument(document);
196-
});
197-
}
196+
197+
await fse.createFile(filePath);
198+
await fse.writeFile(filePath, md_content);
199+
workspace.openTextDocument(filePath).then((document) => {
200+
window.showTextDocument(document);
201+
});
198202
}
199203

200204
public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise<void> {

src/utils/workspaceUtils.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ export async function insertSubmitResult(result?: string): Promise<string | unde
7272

7373
// 获取当前光标所在行
7474
const currentLine = editor.selection.active.line;
75-
// 向前遍历找到第一次出现 "// @lc code=start" 的行
75+
// 向前遍历找到第一次出现 "@lc code=start" 的行
7676
let startLine = currentLine;
7777
while (startLine >= 0) {
7878
const lineText = editor.document.lineAt(startLine).text;
79-
if (lineText.includes('// @lc code=start')) {
79+
if (lineText.includes('@lc code=start')) {
8080
break;
8181
}
8282
startLine--;
8383
}
8484
if (startLine < 0) {
85-
vscode.window.showErrorMessage("Please add '// @lc code=start' in your solution file.");
85+
vscode.window.showErrorMessage("Please add '@lc code=start' in your solution file.");
8686
return;
8787
}
8888

@@ -110,21 +110,21 @@ export async function copyCodeBlock(): Promise<string | undefined> {
110110
// 获取当前光标所在行
111111
const currentLine = editor.selection.active.line;
112112

113-
// 向前遍历找到第一次出现 "// @lc code=start" 的行
113+
// 向前遍历找到第一次出现 "@lc code=start" 的行
114114
let startLine = currentLine;
115115
while (startLine >= 0) {
116116
const lineText = editor.document.lineAt(startLine).text;
117-
if (lineText.includes('// @lc code=start')) {
117+
if (lineText.includes('@lc code=start')) {
118118
break;
119119
}
120120
startLine--;
121121
}
122122

123-
// 向后遍历找到第一次出现 "// @lc code=end" 的行
123+
// 向后遍历找到第一次出现 "@lc code=end" 的行
124124
let endLine = currentLine;
125125
while (endLine < editor.document.lineCount) {
126126
const lineText = editor.document.lineAt(endLine).text;
127-
if (lineText.includes('// @lc code=end')) {
127+
if (lineText.includes('@lc code=end')) {
128128
break;
129129
}
130130
endLine++;

0 commit comments

Comments
 (0)