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

feat: Support open preview page through Code Lens and context menu #338

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

Merged
merged 2 commits into from
Jun 1, 2019
Merged
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@
{
"command": "leetcode.showSolution",
"group": "leetcode@3"
},
{
"command": "leetcode.previewProblem",
"group": "leetcode@4"
}
]
},
Expand Down
5 changes: 5 additions & 0 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
command: "leetcode.showSolution",
arguments: [document.uri],
}),
new vscode.CodeLens(range, {
title: "Preview",
command: "leetcode.previewProblem",
arguments: [document.uri],
}),
];
}
}
31 changes: 28 additions & 3 deletions src/commands/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,45 @@ import * as fse from "fs-extra";
import * as path from "path";
import * as unescapeJS from "unescape-js";
import * as vscode from "vscode";
import { explorerNodeManager } from "../explorer/explorerNodeManager";
import { LeetCodeNode } from "../explorer/LeetCodeNode";
import { leetCodeChannel } from "../leetCodeChannel";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { leetCodeManager } from "../leetCodeManager";
import { IProblem, IQuickItemEx, languages, ProblemState } from "../shared";
import { getNodeIdFromFile } from "../utils/problemUtils";
import { DialogOptions, DialogType, openSettingsEditor, promptForOpenOutputChannel, promptForSignIn, promptHintMessage } from "../utils/uiUtils";
import { selectWorkspaceFolder } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";
import { leetCodePreviewProvider } from "../webview/leetCodePreviewProvider";
import { leetCodeSolutionProvider } from "../webview/leetCodeSolutionProvider";
import * as list from "./list";

export async function previewProblem(node: IProblem, isSideMode: boolean = false): Promise<void> {
const descString: string = await leetCodeExecutor.getDescription(node);
export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: boolean = false): Promise<void> {
let node: LeetCodeNode;
if (input instanceof LeetCodeNode) {
node = input;
} else if (input instanceof vscode.Uri) {
const activeFilePath: string = input.fsPath;
const id: string = await getNodeIdFromFile(activeFilePath);
if (!id) {
vscode.window.showErrorMessage(`Failed to resolve the problem id from file: ${activeFilePath}.`);
return;
}
const cachedNode: LeetCodeNode | undefined = explorerNodeManager.getNodeById(id);
if (!cachedNode) {
vscode.window.showErrorMessage(`Failed to resolve the problem with id: ${id}.`);
return;
}
node = cachedNode;
// Move the preview page aside if it's triggered from Code Lens
isSideMode = true;
} else {
vscode.window.showErrorMessage("Invalid input to fetch the preview data.");
return;
}

const descString: string = await leetCodeExecutor.getDescription(node.id);
leetCodePreviewProvider.show(descString, node, isSideMode);
}

Expand Down Expand Up @@ -54,7 +79,7 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise<vo
} else if (input instanceof vscode.Uri) {
problemInput = `"${input.fsPath}"`;
} else {
vscode.window.showErrorMessage("Invalid input to fetch the solution data");
vscode.window.showErrorMessage("Invalid input to fetch the solution data.");
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class LeetCodeExecutor implements Disposable {
return solution;
}

public async getDescription(problemNode: IProblem): Promise<string> {
return await this.executeCommandWithProgressEx("Fetching problem description...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "-x"]);
public async getDescription(problemNodeId: string): Promise<string> {
return await this.executeCommandWithProgressEx("Fetching problem description...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNodeId, "-x"]);
}

public async listSessions(): Promise<string> {
Expand Down
17 changes: 17 additions & 0 deletions src/utils/problemUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as _ from "lodash";
import * as path from "path";
import { IProblem, langExt } from "../shared";

export function genFileExt(language: string): string {
Expand All @@ -17,3 +19,18 @@ export function genFileName(node: IProblem, language: string): string {
const ext: string = genFileExt(language);
return `${node.id}.${slug}.${ext}`;
}

export async function getNodeIdFromFile(fsPath: string): Promise<string> {
const fileContent: string = await fse.readFile(fsPath, "utf8");
let id: string = "";
const matchResults: RegExpMatchArray | null = fileContent.match(/@lc.+id=(.+?) /);
if (matchResults && matchResults.length === 2) {
id = matchResults[1];
}
// Try to get id from file name if getting from comments failed
if (!id) {
id = path.basename(fsPath).split(".")[0];
}

return id;
}