Skip to content

Commit ade48fb

Browse files
authored
add: star command in shortcuts (#601)
1 parent 7bc6d6c commit ade48fb

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
9090
<img src="https://raw.githubusercontent.com/LeetCode-OpenSource/vscode-leetcode/master/docs/imgs/shortcuts.png" alt="Editor Shortcuts" />
9191
</p>
9292

93-
- The extension supports 4 editor shortcuts (aka Code Lens):
93+
- The extension supports 5 editor shortcuts (aka Code Lens):
9494
- `Submit`: Submit your answer to LeetCode.
9595
- `Test`: Test your answer with customized test cases.
96+
- `Star/Unstar`: Star or unstar the current problem.
9697
- `Solution`: Show the top voted solution for the current problem.
9798
- `Description`: Show the problem description page.
9899

@@ -128,7 +129,7 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
128129
| `leetcode.workspaceFolder` | Specify the path of the workspace folder to store the problem files. | `""` |
129130
| `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). | |
130131
| `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` |
131-
| `leetcode.editor.shortcuts` | Specify the customized shorcuts in editors. Supported values are: `submit`, `test`, `solution` and `description`. | `["submit, test"]` |
132+
| `leetcode.editor.shortcuts` | Specify the customized shorcuts in editors. Supported values are: `submit`, `test`, `star`, `solution` and `description`. | `["submit, test"]` |
132133
| `leetcode.enableSideMode` | Specify whether `preview`, `solution` and `submission` tab should be grouped into the second editor column when solving a problem. | `true` |
133134
| `leetcode.nodePath` | Specify the `Node.js` executable path. for example, C:\Program Files\nodejs\node.exe | `node` |
134135
| `leetcode.showCommentDescription` | Specify whether to include the problem description in the comments | `false` |

docs/README_zh-CN.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@
9191
<img src="https://raw.githubusercontent.com/LeetCode-OpenSource/vscode-leetcode/master/docs/imgs/shortcuts.png" alt="Editor Shortcuts" />
9292
</p>
9393

94-
- 插件会在编辑区域内支持四种不同的快捷方式(Code Lens):
94+
- 插件会在编辑区域内支持五种不同的快捷方式(Code Lens):
9595
- `Submit`: 提交你的答案至 LeetCode;
9696
- `Test`: 用给定的测试用例测试你的答案;
97+
- `Star`: 收藏或取消收藏该问题;
9798
- `Solution`: 显示该问题的高票解答;
9899
- `Description`: 显示该问题的题目描述。
99100

@@ -129,7 +130,7 @@
129130
| `leetcode.workspaceFolder` | 指定保存文件的工作区目录 | `""` |
130131
| `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)| |
131132
| `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` |
132-
| `leetcode.editor.shortcuts` | 指定在编辑器内所自定义的快捷方式。可用的快捷方式有: `submit`, `test`, `solution`, `description`| `["submit, test"]` |
133+
| `leetcode.editor.shortcuts` | 指定在编辑器内所自定义的快捷方式。可用的快捷方式有: `submit`, `test`, `star`, `solution`, `description`| `["submit, test"]` |
133134
| `leetcode.enableSideMode` | 指定在解决一道题时,是否将`问题预览``高票答案``提交结果`窗口集中在编辑器的第二栏。 | `true` |
134135
| `leetcode.nodePath` | 指定 `Node.js` 可执行文件的路径。如:C:\Program Files\nodejs\node.exe | `node` |
135136
| `leetcode.showCommentDescription` | 指定是否要在注释中显示题干。 | `false` |

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,16 @@
629629
"enum": [
630630
"submit",
631631
"test",
632+
"star",
632633
"solution",
633634
"description"
635+
],
636+
"enumDescriptions": [
637+
"Submit your answer to LeetCode.",
638+
"Test your answer with customized test cases.",
639+
"Star or unstar the current problem.",
640+
"Show the top voted solution for the current problem.",
641+
"Show the problem description page."
634642
]
635643
},
636644
"description": "Customize the shorcuts in editors."

src/codelens/CodeLensController.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Licensed under the MIT license.
33

44
import { ConfigurationChangeEvent, Disposable, languages, workspace } from "vscode";
5-
import { CustomCodeLensProvider } from "./CustomCodeLensProvider";
5+
import { customCodeLensProvider, CustomCodeLensProvider } from "./CustomCodeLensProvider";
66

77
class CodeLensController implements Disposable {
88
private internalProvider: CustomCodeLensProvider;
99
private registeredProvider: Disposable | undefined;
1010
private configurationChangeListener: Disposable;
1111

1212
constructor() {
13-
this.internalProvider = new CustomCodeLensProvider();
13+
this.internalProvider = customCodeLensProvider;
1414

1515
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
1616
if (event.affectsConfiguration("leetcode.editor.shortcuts")) {

src/codelens/CustomCodeLensProvider.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { explorerNodeManager } from "../explorer/explorerNodeManager";
6+
import { LeetCodeNode } from "../explorer/LeetCodeNode";
57
import { getEditorShortcuts } from "../utils/settingUtils";
68

79
export class CustomCodeLensProvider implements vscode.CodeLensProvider {
@@ -23,10 +25,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
2325
}
2426

2527
const content: string = document.getText();
26-
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=.* lang=.*/);
28+
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=.*/);
2729
if (!matchResult) {
2830
return undefined;
2931
}
32+
const nodeId: string | undefined = matchResult[1];
33+
let node: LeetCodeNode | undefined;
34+
if (nodeId) {
35+
node = explorerNodeManager.getNodeById(nodeId);
36+
}
3037

3138
let codeLensLine: number = document.lineCount - 1;
3239
for (let i: number = document.lineCount - 1; i >= 0; i--) {
@@ -56,6 +63,14 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
5663
}));
5764
}
5865

66+
if (shortcuts.indexOf("star") >= 0 && node) {
67+
codeLens.push(new vscode.CodeLens(range, {
68+
title: node.isFavorite ? "Unstar" : "Star",
69+
command: node.isFavorite ? "leetcode.removeFavorite" : "leetcode.addFavorite",
70+
arguments: [node],
71+
}));
72+
}
73+
5974
if (shortcuts.indexOf("solution") >= 0) {
6075
codeLens.push(new vscode.CodeLens(range, {
6176
title: "Solution",
@@ -75,3 +90,5 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
7590
return codeLens;
7691
}
7792
}
93+
94+
export const customCodeLensProvider: CustomCodeLensProvider = new CustomCodeLensProvider();

src/commands/star.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
// Copyright (c) jdneo. All rights reserved.
33
// Licensed under the MIT license.
44

5+
import { customCodeLensProvider } from "../codelens/CustomCodeLensProvider";
56
import { LeetCodeNode } from "../explorer/LeetCodeNode";
67
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
78
import { leetCodeExecutor } from "../leetCodeExecutor";
9+
import { hasStarShortcut } from "../utils/settingUtils";
810
import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
911

1012
export async function addFavorite(node: LeetCodeNode): Promise<void> {
1113
try {
1214
await leetCodeExecutor.toggleFavorite(node, true);
13-
leetCodeTreeDataProvider.refresh();
15+
await leetCodeTreeDataProvider.refresh();
16+
if (hasStarShortcut()) {
17+
customCodeLensProvider.refresh();
18+
}
1419
} catch (error) {
1520
await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error);
1621
}
@@ -19,7 +24,10 @@ export async function addFavorite(node: LeetCodeNode): Promise<void> {
1924
export async function removeFavorite(node: LeetCodeNode): Promise<void> {
2025
try {
2126
await leetCodeExecutor.toggleFavorite(node, false);
22-
leetCodeTreeDataProvider.refresh();
27+
await leetCodeTreeDataProvider.refresh();
28+
if (hasStarShortcut()) {
29+
customCodeLensProvider.refresh();
30+
}
2331
} catch (error) {
2432
await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error);
2533
}

src/utils/settingUtils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function getEditorShortcuts(): string[] {
2020
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
2121
}
2222

23+
export function hasStarShortcut(): boolean {
24+
const shortcuts: string[] = getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
25+
return shortcuts.indexOf("star") >= 0;
26+
}
27+
2328
export function getDescriptionConfiguration(): IDescriptionConfiguration {
2429
const setting: string = getWorkspaceConfiguration().get<string>("showDescription", DescriptionConfiguration.InWebView);
2530
const config: IDescriptionConfiguration = {

0 commit comments

Comments
 (0)