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(vscode-leetcode): add batch operation to get problem by ids or names #683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@
"onCommand:leetcode.showProblem",
"onCommand:leetcode.previewProblem",
"onCommand:leetcode.searchProblem",
"onCommand:leetcode.getProblemByIds",
"onCommand:leetcode.testSolution",
"onCommand:leetcode.submitSolution",
"onCommand:leetcode.switchDefaultLanguage",
@@ -97,6 +98,12 @@
"category": "LeetCode",
"icon": "$(search)"
},
{
"command": "leetcode.getProblemByIds",
"title": "Get Problem By Ids",
"category": "LeetCode",
"icon": "$(add)"
},
{
"command": "leetcode.showSolution",
"title": "Show Top Voted Solution",
@@ -175,6 +182,11 @@
"when": "view == leetCodeExplorer",
"group": "navigation@3"
},
{
"command": "leetcode.getProblemByIds",
"when": "view == leetCodeExplorer",
"group": "navigation@4"
},
{
"command": "leetcode.pickOne",
"when": "view == leetCodeExplorer",
41 changes: 40 additions & 1 deletion src/commands/show.ts
Original file line number Diff line number Diff line change
@@ -77,6 +77,41 @@ export async function searchProblem(): Promise<void> {
await showProblemInternal(choice.value);
}

export async function getProblemByIds(): Promise<void> {
if (!leetCodeManager.getUser()) {
promptForSignIn();
return;
}
const ids: string | undefined = await vscode.window.showInputBox({
prompt: "Input problem ids ,such as [1,2,3]",
ignoreFocusOut: true,
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "The input must not be empty",
});
try {
let res: any = JSON.parse( ids || "" );
if (!Array.isArray(res)) {
vscode.window.showErrorMessage("The problem ids should be array");
return;
}
if (res.length === 0) {
vscode.window.showErrorMessage("The problem ids must not be empty");
return;
}
res = res.map((v: any) => String(v));
const allProblems: any = await list.listProblems();
const problems: any = allProblems.filter((v: IProblem) => res.includes(v.id) || res.includes(v.name));
if (!problems.length) {
return;
}
while (problems.length !== 0) {
const cur: IProblem = problems.shift();
await showProblemInternal(cur, true);
}
} catch (error) {
vscode.window.showErrorMessage(error);
}
}

export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<void> {
let problemInput: string | undefined;
if (input instanceof LeetCodeNode) { // Triggerred from explorer
@@ -131,7 +166,7 @@ async function fetchProblemLanguage(): Promise<string | undefined> {
return language;
}

async function showProblemInternal(node: IProblem): Promise<void> {
async function showProblemInternal(node: IProblem, muteFlag: boolean = false ): Promise<void> {
try {
const language: string | undefined = await fetchProblemLanguage();
if (!language) {
@@ -168,6 +203,10 @@ async function showProblemInternal(node: IProblem): Promise<void> {

const descriptionConfig: IDescriptionConfiguration = settingUtils.getDescriptionConfiguration();
await leetCodeExecutor.showProblem(node, language, finalPath, descriptionConfig.showInComment);
if (muteFlag) {
vscode.window.showInformationMessage(`Added ${node.id}.${node.name} !`);
return;
}
const promises: any[] = [
vscode.window.showTextDocument(vscode.Uri.file(finalPath), { preview: false, viewColumn: vscode.ViewColumn.One }),
promptHintMessage(
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
vscode.commands.registerCommand("leetcode.pickOne", () => show.pickOne()),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
vscode.commands.registerCommand("leetcode.getProblemByIds", () => show.getProblemByIds()),
vscode.commands.registerCommand("leetcode.showSolution", (input: LeetCodeNode | vscode.Uri) => show.showSolution(input)),
vscode.commands.registerCommand("leetcode.refreshExplorer", () => leetCodeTreeDataProvider.refresh()),
vscode.commands.registerCommand("leetcode.testSolution", (uri?: vscode.Uri) => test.testSolution(uri)),