Skip to content

Commit

Permalink
add: star command in shortcuts (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong0618 committed Jul 21, 2020
1 parent 7bc6d6c commit ade48fb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ Thanks for [@yihong0618](https://github.com/yihong0618) provided a workaround wh
<img src="https://raw.githubusercontent.com/LeetCode-OpenSource/vscode-leetcode/master/docs/imgs/shortcuts.png" alt="Editor Shortcuts" />
</p>

- 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.

Expand Down Expand Up @@ -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` |
Expand Down
5 changes: 3 additions & 2 deletions docs/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@
<img src="https://raw.githubusercontent.com/LeetCode-OpenSource/vscode-leetcode/master/docs/imgs/shortcuts.png" alt="Editor Shortcuts" />
</p>

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

Expand Down Expand Up @@ -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` |
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
4 changes: 2 additions & 2 deletions src/codelens/CodeLensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// 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;
private registeredProvider: Disposable | undefined;
private configurationChangeListener: Disposable;

constructor() {
this.internalProvider = new CustomCodeLensProvider();
this.internalProvider = customCodeLensProvider;

this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("leetcode.editor.shortcuts")) {
Expand Down
19 changes: 18 additions & 1 deletion src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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--) {
Expand Down Expand Up @@ -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",
Expand All @@ -75,3 +90,5 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
return codeLens;
}
}

export const customCodeLensProvider: CustomCodeLensProvider = new CustomCodeLensProvider();
12 changes: 10 additions & 2 deletions src/commands/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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);
}
Expand All @@ -19,7 +24,10 @@ export async function addFavorite(node: LeetCodeNode): Promise<void> {
export async function removeFavorite(node: LeetCodeNode): Promise<void> {
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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export function getEditorShortcuts(): string[] {
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
}

export function hasStarShortcut(): boolean {
const shortcuts: string[] = getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
return shortcuts.indexOf("star") >= 0;
}

export function getDescriptionConfiguration(): IDescriptionConfiguration {
const setting: string = getWorkspaceConfiguration().get<string>("showDescription", DescriptionConfiguration.InWebView);
const config: IDescriptionConfiguration = {
Expand Down

0 comments on commit ade48fb

Please sign in to comment.