Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add reset the default code of LeetCode #834

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"onCommand:leetcode.searchProblem",
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.resetSolution",
"onCommand:leetcode.switchDefaultLanguage",
"onCommand:leetcode.problems.sort",
"onView:leetCodeExplorer"
Expand Down Expand Up @@ -113,6 +114,11 @@
"title": "Submit to LeetCode",
"category": "LeetCode"
},
{
"command": "leetcode.resetSolution",
"title": "Reset to default code definition",
"category": "LeetCode"
},
{
"command": "leetcode.addFavorite",
"title": "Add to Favorite List",
Expand Down Expand Up @@ -252,6 +258,11 @@
"command": "leetcode.submitSolution",
"when": "explorerResourceIsFolder == false",
"group": "leetcode@2"
},
{
"command": "leetcode.resetSolution",
"when": "explorerResourceIsFolder == false",
"group": "leetcode@3"
}
],
"editor/context": [
Expand All @@ -269,12 +280,16 @@
"group": "leetcode@2"
},
{
"command": "leetcode.showSolution",
"command": "leetcode.resetSolution",
"group": "leetcode@3"
},
{
"command": "leetcode.previewProblem",
"command": "leetcode.showSolution",
"group": "leetcode@4"
},
{
"command": "leetcode.previewProblem",
"group": "leetcode@5"
}
]
},
Expand Down Expand Up @@ -650,21 +665,24 @@
"type": "array",
"default": [
"submit",
"test"
"test",
"reset"
],
"scope": "application",
"items": {
"type": "string",
"enum": [
"submit",
"test",
"reset",
"star",
"solution",
"description"
],
"enumDescriptions": [
"Submit your answer to LeetCode.",
"Test your answer with customized test cases.",
"Reset to default code definition.",
"Star or unstar the current problem.",
"Show the top voted solution for the current problem.",
"Show the problem description page."
Expand Down
9 changes: 9 additions & 0 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
}));
}

if (shortcuts.indexOf("reset") >= 0) {
codeLens.push(new vscode.CodeLens(range, {
title: "Reset",
command: "leetcode.resetSolution",
arguments: [document.uri],
}));
}


if (shortcuts.indexOf("star") >= 0 && node) {
codeLens.push(new vscode.CodeLens(range, {
title: node.isFavorite ? "Unstar" : "Star",
Expand Down
52 changes: 52 additions & 0 deletions src/commands/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as vscode from "vscode";
import { getActiveFilePath } from "../utils/workspaceUtils";
import * as settingUtils from "../utils/settingUtils";
import { IDescriptionConfiguration } from "../utils/settingUtils";
import { langExt } from '../shared'
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
import { leetCodeExecutor } from "../leetCodeExecutor";

const resetBtn = 'Reset';

export async function resetSolution(uri?: vscode.Uri): Promise<void> {
try {
const selection = await vscode.window.showInformationMessage("Are you sure to reset to default code definition", {
'detail': 'If reset, your current code will be lost',
modal: true
} as vscode.MessageOptions, resetBtn)
const filePath: string | undefined = await getActiveFilePath(uri);
if (selection === resetBtn && filePath) {
resetProblemFile(filePath)
}
} catch (error) {
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);

}
}


async function resetProblemFile(finalPath): Promise<void> {
try {
const reg = /(\d+)\.\S+\.(\S+)/;
const problemId = finalPath.match(reg) && finalPath.match(reg)[1]
const fileExt = finalPath.match(reg) && finalPath.match(reg)[2]
let language;
for (let item of langExt) {
if (item[1] === fileExt) {
language = item[0]
break;
}
}
const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration();
const needTranslation: boolean = settingUtils.shouldUseEndpointTranslation();
await leetCodeExecutor.resetProblem(problemId, language, finalPath, descriptionConfig.showInComment, needTranslation);
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: vscode.ViewColumn.One;
await vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: column });
} catch (error) {
await promptForOpenOutputChannel(`${error} Please open the output channel for details.`, DialogType.error);
}
}


2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as show from "./commands/show";
import * as star from "./commands/star";
import * as submit from "./commands/submit";
import * as test from "./commands/test";
import * as reset from './commands/reset';
import { explorerNodeManager } from "./explorer/explorerNodeManager";
import { LeetCodeNode } from "./explorer/LeetCodeNode";
import { leetCodeTreeDataProvider } from "./explorer/LeetCodeTreeDataProvider";
Expand Down Expand Up @@ -63,6 +64,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),
vscode.commands.registerCommand("leetcode.submitSolution", (uri?: vscode.Uri) => submit.submitSolution(uri)),
vscode.commands.registerCommand("leetcode.resetSolution", (uri?: vscode.Uri) => reset.resetSolution(uri)),
vscode.commands.registerCommand("leetcode.switchDefaultLanguage", () => switchDefaultLanguage()),
vscode.commands.registerCommand("leetcode.addFavorite", (node: LeetCodeNode) => star.addFavorite(node)),
vscode.commands.registerCommand("leetcode.removeFavorite", (node: LeetCodeNode) => star.removeFavorite(node)),
Expand Down
12 changes: 12 additions & 0 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ class LeetCodeExecutor implements Disposable {
}
}

public async resetProblem(problemNodeId: IProblem['id'], language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise<void> {
const templateType: string = showDescriptionInComment ? "-cx" : "-c";
const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNodeId, templateType, "-l", language];

if (!needTranslation) {
cmd.push("-T"); // use -T to force English version
}

const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd);
await fse.writeFile(filePath, codeTemplate);
}

/**
* This function returns solution of a problem identified by input
*
Expand Down
4 changes: 2 additions & 2 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export function getWorkspaceFolder(): string {
}

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

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

Expand Down