From 395126da5b5a8497fbf29ddd850c875851ce5292 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Mon, 20 Jul 2020 00:09:56 +0800
Subject: [PATCH 1/3] add: star command in shortcuts
---
package.json | 3 ++-
src/codelens/CodeLensController.ts | 4 ++--
src/codelens/CustomCodeLensProvider.ts | 19 ++++++++++++++++++-
src/commands/star.ts | 7 +++++--
4 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/package.json b/package.json
index b6164f17..f58159f6 100644
--- a/package.json
+++ b/package.json
@@ -621,7 +621,8 @@
"type": "array",
"default": [
"submit",
- "test"
+ "test",
+ "star"
],
"scope": "application",
"items": {
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..a710ffa9 100644
--- a/src/commands/star.ts
+++ b/src/commands/star.ts
@@ -2,6 +2,7 @@
// 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";
@@ -10,7 +11,8 @@ import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
export async function addFavorite(node: LeetCodeNode): Promise {
try {
await leetCodeExecutor.toggleFavorite(node, true);
- leetCodeTreeDataProvider.refresh();
+ await leetCodeTreeDataProvider.refresh();
+ customCodeLensProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error);
}
@@ -19,7 +21,8 @@ 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();
+ customCodeLensProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error);
}
From 2cfbddf7c445b056cdc3d3529ec8279f2233b950 Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Mon, 20 Jul 2020 15:16:04 +0800
Subject: [PATCH 2/3] document, change: default to config
---
README.md | 5 +++--
docs/README_zh-CN.md | 5 +++--
package.json | 4 ++--
3 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 490db51e..53044c50 100644
--- a/README.md
+++ b/README.md
@@ -90,9 +90,10 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
-- 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 @@
-- 插件会在编辑区域内支持四种不同的快捷方式(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 f58159f6..38db450b 100644
--- a/package.json
+++ b/package.json
@@ -621,8 +621,7 @@
"type": "array",
"default": [
"submit",
- "test",
- "star"
+ "test"
],
"scope": "application",
"items": {
@@ -630,6 +629,7 @@
"enum": [
"submit",
"test",
+ "star",
"solution",
"description"
]
From 19e35b769e92f7bc8c1a56c6156cb3066161c86b Mon Sep 17 00:00:00 2001
From: yihong0618
Date: Mon, 20 Jul 2020 18:26:36 +0800
Subject: [PATCH 3/3] fix: typo, optimize star command logic, add: enum desc
---
README.md | 2 +-
package.json | 7 +++++++
src/commands/star.ts | 9 +++++++--
src/utils/settingUtils.ts | 5 +++++
4 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 53044c50..3396d0c7 100644
--- a/README.md
+++ b/README.md
@@ -93,7 +93,7 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
- 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.
+ - `Star/Unstar`: Star or unstar the current problem.
- `Solution`: Show the top voted solution for the current problem.
- `Description`: Show the problem description page.
diff --git a/package.json b/package.json
index 38db450b..81a8536b 100644
--- a/package.json
+++ b/package.json
@@ -632,6 +632,13 @@
"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/commands/star.ts b/src/commands/star.ts
index a710ffa9..36611499 100644
--- a/src/commands/star.ts
+++ b/src/commands/star.ts
@@ -6,13 +6,16 @@ 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);
await leetCodeTreeDataProvider.refresh();
- customCodeLensProvider.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);
}
@@ -22,7 +25,9 @@ export async function removeFavorite(node: LeetCodeNode): Promise {
try {
await leetCodeExecutor.toggleFavorite(node, false);
await leetCodeTreeDataProvider.refresh();
- customCodeLensProvider.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 = {