Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1e4e704

Browse files
author
luxuemin
committed
feat: add reset the default code of LeetCode
1 parent d713ad5 commit 1e4e704

File tree

6 files changed

+98
-5
lines changed

6 files changed

+98
-5
lines changed

package.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"onCommand:leetcode.searchProblem",
3838
"onCommand:leetcode.testSolution",
3939
"onCommand:leetcode.submitSolution",
40+
"onCommand:leetcode.resetSolution",
4041
"onCommand:leetcode.switchDefaultLanguage",
4142
"onCommand:leetcode.problems.sort",
4243
"onView:leetCodeExplorer"
@@ -113,6 +114,11 @@
113114
"title": "Submit to LeetCode",
114115
"category": "LeetCode"
115116
},
117+
{
118+
"command": "leetcode.resetSolution",
119+
"title": "Reset the default code of LeetCode",
120+
"category": "LeetCode"
121+
},
116122
{
117123
"command": "leetcode.addFavorite",
118124
"title": "Add to Favorite List",
@@ -252,6 +258,11 @@
252258
"command": "leetcode.submitSolution",
253259
"when": "explorerResourceIsFolder == false",
254260
"group": "leetcode@2"
261+
},
262+
{
263+
"command": "leetcode.resetSolution",
264+
"when": "explorerResourceIsFolder == false",
265+
"group": "leetcode@3"
255266
}
256267
],
257268
"editor/context": [
@@ -269,12 +280,16 @@
269280
"group": "leetcode@2"
270281
},
271282
{
272-
"command": "leetcode.showSolution",
283+
"command": "leetcode.resetSolution",
273284
"group": "leetcode@3"
274285
},
275286
{
276-
"command": "leetcode.previewProblem",
287+
"command": "leetcode.showSolution",
277288
"group": "leetcode@4"
289+
},
290+
{
291+
"command": "leetcode.previewProblem",
292+
"group": "leetcode@5"
278293
}
279294
]
280295
},
@@ -650,21 +665,24 @@
650665
"type": "array",
651666
"default": [
652667
"submit",
653-
"test"
668+
"test",
669+
"reset"
654670
],
655671
"scope": "application",
656672
"items": {
657673
"type": "string",
658674
"enum": [
659675
"submit",
660676
"test",
677+
"reset",
661678
"star",
662679
"solution",
663680
"description"
664681
],
665682
"enumDescriptions": [
666683
"Submit your answer to LeetCode.",
667684
"Test your answer with customized test cases.",
685+
"Reset the default code of template.",
668686
"Star or unstar the current problem.",
669687
"Show the top voted solution for the current problem.",
670688
"Show the problem description page."

src/codelens/CustomCodeLensProvider.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
6363
}));
6464
}
6565

66+
if (shortcuts.indexOf("reset") >= 0) {
67+
codeLens.push(new vscode.CodeLens(range, {
68+
title: "Reset",
69+
command: "leetcode.resetSolution",
70+
arguments: [document.uri],
71+
}));
72+
}
73+
74+
6675
if (shortcuts.indexOf("star") >= 0 && node) {
6776
codeLens.push(new vscode.CodeLens(range, {
6877
title: node.isFavorite ? "Unstar" : "Star",

src/commands/reset.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as vscode from "vscode";
2+
import { getActiveFilePath } from "../utils/workspaceUtils";
3+
import * as settingUtils from "../utils/settingUtils";
4+
import { IDescriptionConfiguration } from "../utils/settingUtils";
5+
import { langExt } from '../shared'
6+
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
7+
import { leetCodeExecutor } from "../leetCodeExecutor";
8+
9+
const resetBtn = 'Reset';
10+
11+
export async function resetSolution(uri?: vscode.Uri): Promise<void> {
12+
try {
13+
const selection = await vscode.window.showInformationMessage("Are you sure to reset the default code", {
14+
'detail': 'If reset, your current code will be lost',
15+
modal: true
16+
} as vscode.MessageOptions, resetBtn)
17+
const filePath: string | undefined = await getActiveFilePath(uri);
18+
if (selection === resetBtn && filePath) {
19+
resetProblemFile(filePath)
20+
}
21+
} catch (error) {
22+
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
23+
24+
}
25+
}
26+
27+
28+
async function resetProblemFile(finalPath): Promise<void> {
29+
try {
30+
const reg = /(\d+)\.\S+\.(\S+)/;
31+
const problemId = finalPath.match(reg) && finalPath.match(reg)[1]
32+
const fileExt = finalPath.match(reg) && finalPath.match(reg)[2]
33+
let language;
34+
for (let item of langExt) {
35+
if (item[1] === fileExt) {
36+
language = item[0]
37+
break;
38+
}
39+
}
40+
const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration();
41+
const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation();
42+
await leetCodeExecutor.resetProblem(problemId, language, finalPath, descriptionConfig.showInComment, needTranslation);
43+
const column = vscode.window.activeTextEditor
44+
? vscode.window.activeTextEditor.viewColumn
45+
: vscode.ViewColumn.One;
46+
await vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: column });
47+
} catch (error) {
48+
await promptForOpenOutputChannel(`${error} Please open the output channel for details.`, DialogType.error);
49+
}
50+
}
51+
52+

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as show from "./commands/show";
1111
import * as star from "./commands/star";
1212
import * as submit from "./commands/submit";
1313
import * as test from "./commands/test";
14+
import * as reset from './commands/reset';
1415
import { explorerNodeManager } from "./explorer/explorerNodeManager";
1516
import { LeetCodeNode } from "./explorer/LeetCodeNode";
1617
import { leetCodeTreeDataProvider } from "./explorer/LeetCodeTreeDataProvider";
@@ -63,6 +64,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
6364
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
6465
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
6566
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
67+
vscode.commands.registerCommand("leetcode.resetSolution", (uri?: vscode.Uri) => reset.resetSolution(uri)),
6668
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
6769
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
6870
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),

src/leetCodeExecutor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ class LeetCodeExecutor implements Disposable {
115115
}
116116
}
117117

118+
public async resetProblem(problemNodeId: IProblem['id'], language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise<void> {
119+
const templateType: string = showDescriptionInComment ? "-cx" : "-c";
120+
const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNodeId, templateType, "-l", language];
121+
122+
if (!needTranslation) {
123+
cmd.push("-T"); // use -T to force English version
124+
}
125+
126+
const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd);
127+
await fse.writeFile(filePath, codeTemplate);
128+
}
129+
118130
/**
119131
* This function returns solution of a problem identified by input
120132
*

src/utils/settingUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export function getWorkspaceFolder(): string {
1717
}
1818

1919
export function getEditorShortcuts(): string[] {
20-
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
20+
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test", "reset"]);
2121
}
2222

2323
export function hasStarShortcut(): boolean {
24-
const shortcuts: string[] = getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
24+
const shortcuts: string[] = getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test", "reset"]);
2525
return shortcuts.indexOf("star") >= 0;
2626
}
2727

0 commit comments

Comments
 (0)