Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add reset the default code of LeetCode#834

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jackluson wants to merge2 commits intoLeetCode-OpenSource:master
base:master
Choose a base branch
Loading
fromjackluson:master
Open
Show file tree
Hide file tree
Changes fromall 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 deletionspackage.json
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 DownExpand 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 deletionssrc/codelens/CustomCodeLensProvider.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/commands/reset.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/extension.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionssrc/leetCodeExecutor.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionssrc/utils/settingUtils.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp