From 3f53f0c5af8df29aedcf1ed2d586aef8d0057de4 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Sat, 1 Jun 2019 16:38:10 +0800 Subject: [PATCH 1/2] feat: Support open preview page through Code Lens and context menu --- package.json | 4 ++++ src/codelens/CustomCodeLensProvider.ts | 5 +++++ src/commands/show.ts | 31 +++++++++++++++++++++++--- src/leetCodeExecutor.ts | 4 ++-- src/utils/problemUtils.ts | 19 ++++++++++++++++ 5 files changed, 58 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5413bc2b..7dae6443 100644 --- a/package.json +++ b/package.json @@ -225,6 +225,10 @@ { "command": "leetcode.showSolution", "group": "leetcode@3" + }, + { + "command": "leetcode.previewProblem", + "group": "leetcode@4" } ] }, diff --git a/src/codelens/CustomCodeLensProvider.ts b/src/codelens/CustomCodeLensProvider.ts index 3ee7270d..1290e057 100644 --- a/src/codelens/CustomCodeLensProvider.ts +++ b/src/codelens/CustomCodeLensProvider.ts @@ -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], + }), ]; } } diff --git a/src/commands/show.ts b/src/commands/show.ts index a0b23f64..f3bd0750 100644 --- a/src/commands/show.ts +++ b/src/commands/show.ts @@ -5,11 +5,13 @@ 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"; @@ -17,8 +19,31 @@ 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 { - const descString: string = await leetCodeExecutor.getDescription(node); +export async function previewProblem(input: IProblem | vscode.Uri, isSideMode: boolean = false): Promise { + 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); } @@ -54,7 +79,7 @@ export async function showSolution(input: LeetCodeNode | vscode.Uri): Promise { - return await this.executeCommandWithProgressEx("Fetching problem description...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNode.id, "-x"]); + public async getDescription(problemNodeId: string): Promise { + return await this.executeCommandWithProgressEx("Fetching problem description...", this.nodeExecutable, [await this.getLeetCodeBinaryPath(), "show", problemNodeId, "-x"]); } public async listSessions(): Promise { diff --git a/src/utils/problemUtils.ts b/src/utils/problemUtils.ts index 10df523b..32288ef6 100644 --- a/src/utils/problemUtils.ts +++ b/src/utils/problemUtils.ts @@ -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 { @@ -17,3 +19,20 @@ 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 { + const fileContent: string = await fse.readFile(fsPath, "utf8"); + const line: string = fileContent.split("\n") + .find((l: string) => l.indexOf(" @lc ") >= 0) || ""; + let id: string = ""; + const matchResults: RegExpMatchArray | null = line.match(/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; +} From 857c89d48250b8982fcdf49c09e248e1a4a53c35 Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Sat, 1 Jun 2019 20:31:38 +0800 Subject: [PATCH 2/2] Simplify the code --- src/utils/problemUtils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/problemUtils.ts b/src/utils/problemUtils.ts index 32288ef6..d2dcc3ed 100644 --- a/src/utils/problemUtils.ts +++ b/src/utils/problemUtils.ts @@ -22,10 +22,8 @@ export function genFileName(node: IProblem, language: string): string { export async function getNodeIdFromFile(fsPath: string): Promise { const fileContent: string = await fse.readFile(fsPath, "utf8"); - const line: string = fileContent.split("\n") - .find((l: string) => l.indexOf(" @lc ") >= 0) || ""; let id: string = ""; - const matchResults: RegExpMatchArray | null = line.match(/id=(.+?) /); + const matchResults: RegExpMatchArray | null = fileContent.match(/@lc.+id=(.+?) /); if (matchResults && matchResults.length === 2) { id = matchResults[1]; }