Skip to content

Commit

Permalink
feat: Support open preview page through Code Lens and context menu (#338
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jdneo authored Jun 1, 2019
1 parent e6cd998 commit cdb46d0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
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;
}

0 comments on commit cdb46d0

Please sign in to comment.