From ade48fbbea9fd55bff628fe95e948c4d694871e1 Mon Sep 17 00:00:00 2001 From: yihong Date: Tue, 21 Jul 2020 11:29:12 +0800 Subject: [PATCH] add: star command in shortcuts (#601) --- README.md | 5 +++-- docs/README_zh-CN.md | 5 +++-- package.json | 8 ++++++++ src/codelens/CodeLensController.ts | 4 ++-- src/codelens/CustomCodeLensProvider.ts | 19 ++++++++++++++++++- src/commands/star.ts | 12 ++++++++++-- src/utils/settingUtils.ts | 5 +++++ 7 files changed, 49 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 490db51e..3396d0c7 100644 --- a/README.md +++ b/README.md @@ -90,9 +90,10 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh Editor Shortcuts

-- The extension supports 4 editor shortcuts (aka Code Lens): +- The extension supports 5 editor shortcuts (aka Code Lens): - `Submit`: Submit your answer to LeetCode. - `Test`: Test your answer with customized test cases. + - `Star/Unstar`: Star or unstar the current problem. - `Solution`: Show the top voted solution for the current problem. - `Description`: Show the problem description page. @@ -128,7 +129,7 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh | `leetcode.workspaceFolder` | Specify the path of the workspace folder to store the problem files. | `""` | | `leetcode.filePath` | Specify the relative path under the workspace and the file name to save the problem files. More details can be found [here](https://github.com/LeetCode-OpenSource/vscode-leetcode/wiki/Customize-the-Relative-Folder-and-the-File-Name-of-the-Problem-File). | | | `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` | -| `leetcode.editor.shortcuts` | Specify the customized shorcuts in editors. Supported values are: `submit`, `test`, `solution` and `description`. | `["submit, test"]` | +| `leetcode.editor.shortcuts` | Specify the customized shorcuts in editors. Supported values are: `submit`, `test`, `star`, `solution` and `description`. | `["submit, test"]` | | `leetcode.enableSideMode` | Specify whether `preview`, `solution` and `submission` tab should be grouped into the second editor column when solving a problem. | `true` | | `leetcode.nodePath` | Specify the `Node.js` executable path. for example, C:\Program Files\nodejs\node.exe | `node` | | `leetcode.showCommentDescription` | Specify whether to include the problem description in the comments | `false` | diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 6972902d..608c6226 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -91,9 +91,10 @@ Editor Shortcuts

-- 插件会在编辑区域内支持四种不同的快捷方式(Code Lens): +- 插件会在编辑区域内支持五种不同的快捷方式(Code Lens): - `Submit`: 提交你的答案至 LeetCode; - `Test`: 用给定的测试用例测试你的答案; + - `Star`: 收藏或取消收藏该问题; - `Solution`: 显示该问题的高票解答; - `Description`: 显示该问题的题目描述。 @@ -129,7 +130,7 @@ | `leetcode.workspaceFolder` | 指定保存文件的工作区目录 | `""` | | `leetcode.filePath` | 指定生成题目文件的相对文件夹路径名和文件名。点击查看[更多详细用法](https://github.com/LeetCode-OpenSource/vscode-leetcode/wiki/%E8%87%AA%E5%AE%9A%E4%B9%89%E9%A2%98%E7%9B%AE%E6%96%87%E4%BB%B6%E7%9A%84%E7%9B%B8%E5%AF%B9%E6%96%87%E4%BB%B6%E5%A4%B9%E8%B7%AF%E5%BE%84%E5%92%8C%E6%96%87%E4%BB%B6%E5%90%8D)。 | | | `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` | -| `leetcode.editor.shortcuts` | 指定在编辑器内所自定义的快捷方式。可用的快捷方式有: `submit`, `test`, `solution`, `description`。 | `["submit, test"]` | +| `leetcode.editor.shortcuts` | 指定在编辑器内所自定义的快捷方式。可用的快捷方式有: `submit`, `test`, `star`, `solution`, `description`。 | `["submit, test"]` | | `leetcode.enableSideMode` | 指定在解决一道题时,是否将`问题预览`、`高票答案`与`提交结果`窗口集中在编辑器的第二栏。 | `true` | | `leetcode.nodePath` | 指定 `Node.js` 可执行文件的路径。如:C:\Program Files\nodejs\node.exe | `node` | | `leetcode.showCommentDescription` | 指定是否要在注释中显示题干。 | `false` | diff --git a/package.json b/package.json index b6164f17..81a8536b 100644 --- a/package.json +++ b/package.json @@ -629,8 +629,16 @@ "enum": [ "submit", "test", + "star", "solution", "description" + ], + "enumDescriptions": [ + "Submit your answer to LeetCode.", + "Test your answer with customized test cases.", + "Star or unstar the current problem.", + "Show the top voted solution for the current problem.", + "Show the problem description page." ] }, "description": "Customize the shorcuts in editors." diff --git a/src/codelens/CodeLensController.ts b/src/codelens/CodeLensController.ts index 8e2fa3c9..b41f28f0 100644 --- a/src/codelens/CodeLensController.ts +++ b/src/codelens/CodeLensController.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import { ConfigurationChangeEvent, Disposable, languages, workspace } from "vscode"; -import { CustomCodeLensProvider } from "./CustomCodeLensProvider"; +import { customCodeLensProvider, CustomCodeLensProvider } from "./CustomCodeLensProvider"; class CodeLensController implements Disposable { private internalProvider: CustomCodeLensProvider; @@ -10,7 +10,7 @@ class CodeLensController implements Disposable { private configurationChangeListener: Disposable; constructor() { - this.internalProvider = new CustomCodeLensProvider(); + this.internalProvider = customCodeLensProvider; this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => { if (event.affectsConfiguration("leetcode.editor.shortcuts")) { diff --git a/src/codelens/CustomCodeLensProvider.ts b/src/codelens/CustomCodeLensProvider.ts index 4764e999..4b9b6491 100644 --- a/src/codelens/CustomCodeLensProvider.ts +++ b/src/codelens/CustomCodeLensProvider.ts @@ -2,6 +2,8 @@ // Licensed under the MIT license. import * as vscode from "vscode"; +import { explorerNodeManager } from "../explorer/explorerNodeManager"; +import { LeetCodeNode } from "../explorer/LeetCodeNode"; import { getEditorShortcuts } from "../utils/settingUtils"; export class CustomCodeLensProvider implements vscode.CodeLensProvider { @@ -23,10 +25,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider { } const content: string = document.getText(); - const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=.* lang=.*/); + const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=.*/); if (!matchResult) { return undefined; } + const nodeId: string | undefined = matchResult[1]; + let node: LeetCodeNode | undefined; + if (nodeId) { + node = explorerNodeManager.getNodeById(nodeId); + } let codeLensLine: number = document.lineCount - 1; for (let i: number = document.lineCount - 1; i >= 0; i--) { @@ -56,6 +63,14 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider { })); } + if (shortcuts.indexOf("star") >= 0 && node) { + codeLens.push(new vscode.CodeLens(range, { + title: node.isFavorite ? "Unstar" : "Star", + command: node.isFavorite ? "leetcode.removeFavorite" : "leetcode.addFavorite", + arguments: [node], + })); + } + if (shortcuts.indexOf("solution") >= 0) { codeLens.push(new vscode.CodeLens(range, { title: "Solution", @@ -75,3 +90,5 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider { return codeLens; } } + +export const customCodeLensProvider: CustomCodeLensProvider = new CustomCodeLensProvider(); diff --git a/src/commands/star.ts b/src/commands/star.ts index baae0b07..36611499 100644 --- a/src/commands/star.ts +++ b/src/commands/star.ts @@ -2,15 +2,20 @@ // Copyright (c) jdneo. All rights reserved. // Licensed under the MIT license. +import { customCodeLensProvider } from "../codelens/CustomCodeLensProvider"; import { LeetCodeNode } from "../explorer/LeetCodeNode"; import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider"; import { leetCodeExecutor } from "../leetCodeExecutor"; +import { hasStarShortcut } from "../utils/settingUtils"; import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils"; export async function addFavorite(node: LeetCodeNode): Promise { try { await leetCodeExecutor.toggleFavorite(node, true); - leetCodeTreeDataProvider.refresh(); + await leetCodeTreeDataProvider.refresh(); + if (hasStarShortcut()) { + customCodeLensProvider.refresh(); + } } catch (error) { await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error); } @@ -19,7 +24,10 @@ export async function addFavorite(node: LeetCodeNode): Promise { export async function removeFavorite(node: LeetCodeNode): Promise { try { await leetCodeExecutor.toggleFavorite(node, false); - leetCodeTreeDataProvider.refresh(); + await leetCodeTreeDataProvider.refresh(); + if (hasStarShortcut()) { + customCodeLensProvider.refresh(); + } } catch (error) { await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error); } diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index 8c94a95f..207604fa 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -20,6 +20,11 @@ export function getEditorShortcuts(): string[] { return getWorkspaceConfiguration().get("editor.shortcuts", ["submit", "test"]); } +export function hasStarShortcut(): boolean { + const shortcuts: string[] = getWorkspaceConfiguration().get("editor.shortcuts", ["submit", "test"]); + return shortcuts.indexOf("star") >= 0; +} + export function getDescriptionConfiguration(): IDescriptionConfiguration { const setting: string = getWorkspaceConfiguration().get("showDescription", DescriptionConfiguration.InWebView); const config: IDescriptionConfiguration = {