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

Support showing description from text document #294

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert solution part
  • Loading branch information
Vigilans committed Apr 27, 2019
commit 23e5247ad8f337faff8f7dba38696fbfb87f72e5
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,13 @@
"command": "leetcode.previewProblem",
"group": "leetcode@1"
},
{
"command": "leetcode.showSolution",
"group": "leetcode@2"
},
{
"command": "leetcode.testSolution",
"group": "leetcode@3"
"group": "leetcode@2"
},
{
"command": "leetcode.submitSolution",
"group": "leetcode@4"
"group": "leetcode@3"
}
]
},
Expand Down
4 changes: 0 additions & 4 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
title: "Test",
command: "leetcode.testSolution",
}),
new vscode.CodeLens(range, {
title: "Solution",
command: "leetcode.showSolution",
}),
new vscode.CodeLens(range, {
title: "Description",
command: "leetcode.previewProblem",
Expand Down
38 changes: 18 additions & 20 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,6 @@ export async function previewProblem(source: IProblem | vscode.Uri | undefined,
}
}

export async function showSolution(source: IProblem | vscode.Uri | undefined): Promise<void> {
try {
let problem: IProblem;
let solutionString: string;
if (source && "difficulty" in source) {
problem = source;
solutionString = await leetCodeExecutor.showSolution(problem.id, await fetchProblemLanguage());
} else {
const filename: string = (await getActiveFilePath(source))!;
const [meta] = splitMetaOutput(await leetCodeExecutor.getDescription(filename));
problem = leetCodeTreeDataProvider.getProblem(meta.id)!;
solutionString = await leetCodeExecutor.showSolution(meta.id, meta.lang);
}
leetCodeSolutionProvider.show(unescapeJS(solutionString), problem);
} catch (error) {
leetCodeChannel.appendLine(error.toString());
await promptForOpenOutputChannel("Failed to fetch the top voted solution. Please open the output channel for details.", DialogType.error);
}
}

export async function showProblem(node?: LeetCodeNode): Promise<void> {
if (!node) {
return;
Expand All @@ -84,6 +64,24 @@ export async function searchProblem(): Promise<void> {
await showProblemInternal(choice.value);
}

export async function showSolution(node?: LeetCodeNode): Promise<void> {
if (!node) {
return;
}
const language: string | undefined = await fetchProblemLanguage();
if (!language) {
return;
}
try {
let solution: string = await leetCodeExecutor.showSolution(node, language);
// remove backslash in espaced \'...\'(generated by leetcode's database)
solution = solution.replace(/\\'/g, "'");
await leetCodeSolutionProvider.show(solution, node);
} catch (error) {
await promptForOpenOutputChannel("Failed to fetch the top voted solution. Please open the output channel for details.", DialogType.error);
}
}

// SUGGESTION: group config retriving into one file
async function fetchProblemLanguage(): Promise<string | undefined> {
const leetCodeConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("leetcode");
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand("leetcode.previewProblem", (source: IProblem | vscode.Uri) => show.previewProblem(source)),
vscode.commands.registerCommand("leetcode.showProblem", (node: LeetCodeNode) => show.showProblem(node)),
vscode.commands.registerCommand("leetcode.searchProblem", () => show.searchProblem()),
vscode.commands.registerCommand("leetcode.showSolution", (source: IProblem | vscode.Uri) => show.showSolution(source)),
vscode.commands.registerCommand("leetcode.showSolution", (node: IProblem) => show.showSolution(node)),
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)),
Expand Down
8 changes: 2 additions & 6 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ class LeetCodeExecutor implements Disposable {
return filePath;
}

public async showSolution(keyword: string, language?: string): Promise<string> {
const args: string[] = [await this.getLeetCodeBinaryPath(), "show", keyword, "--solution"];
if (language) {
args.push("-l", language);
}
const solution: string = await this.executeCommandWithProgressEx("Fetching top voted solution from discussions...", this.nodeExecutable, args);
public async showSolution(problemNode: IProblem, language: string): Promise<string> {
const solution: string = await this.executeCommandWithProgressEx("Fetching top voted solution from discussions...", "node", [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "--solution", "-l", language]);
return solution;
}

Expand Down