Skip to content

Commit b77d76a

Browse files
authored
Can config whether to show or hide the codelens (LeetCode-OpenSource#273)
1 parent 781feb2 commit b77d76a

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/submit.png" alt="Submit the Answer" />
9494
</p>
9595

96-
- You can submit the answer by clicking `🙏 Submit to LeetCode` at the bottom of the file. Or you can trigger the command: **LeetCode: Submit to LeetCode** to submit the **active** file as the answer.
96+
- You can submit the answer by clicking `Submit` at the bottom of the file. Or you can right click in the editor and select `Submit to LeetCode`.
97+
98+
> If you want to hide the shortcuts showing in the editor, just simply set the setting `leetcode.enableShortcuts` to false.
9799
98100
---
99101

@@ -102,7 +104,7 @@
102104
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/test.png" alt="Test the Answer" />
103105
</p>
104106

105-
- To **test** your answer, right click in the editor and select `Test in LeetCode`.
107+
- You can test the answer by clicking `Test` at the bottom of the file. Or you can right click in the editor and select `Test in LeetCode`.
106108

107109
- There are 3 ways to test the answer:
108110
- **Test with the default cases**
@@ -138,6 +140,7 @@
138140
| `leetcode.endpoint` | Specify the active endpoint. Supported endpoints are: `leetcode`, `leetcode-cn` | `leetcode` |
139141
| `leetcode.outputFolder`| Specify the relative path to save the problem files. Besides using customized path, there are also several reserved words which can be used here: <ul><li>`${tag}`: Categorize the problem according to their tags.<li>`${language}`: Categorize the problem according to their language.</li><li>`${difficulty}`: Categorize the problem according to their difficulty.</li></ul> | N/A |
140142
| `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` |
143+
| `leetcode.enableShortcuts` | Specify whether the submit and test shortcuts in editor or not. | `true` |
141144
| `leetcode.nodePath` | Specify the `Node.js` executable path. | `node` |
142145

143146
## Troubleshooting

docs/README_zh-CN.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@
9393
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/submit.png" alt="提交答案" />
9494
</p>
9595

96-
- 通过点击文件最下方的 `🙏 Submit to LeetCode` 可提交答案。 你也可以触发 **LeetCode: Submit to LeetCode** 命令将**当前**文件作为答案进行提交。
96+
- 通过点击文件最下方的 `Submit` 可提交答案。 你也可以在编辑区内右键并选择 `Submit to LeetCode`,将**当前**文件提交。
97+
98+
> 如果你不希望在编辑器中显示**测试****提交**的快捷方式,可以将配置项 `leetcode.enableShortcuts` 设置为 `false`
9799
98100
---
99101

@@ -102,7 +104,7 @@
102104
<img src="https://raw.githubusercontent.com/jdneo/vscode-leetcode/master/docs/imgs/test.png" alt="测试答案" />
103105
</p>
104106

105-
- 在编辑区内右键并选择 `Test in LeetCode`可对**当前**答案进行测试
107+
- 通过点击文件最下方的 `Test` 可测试答案。你也可以在编辑区内右键并选择 `Test in LeetCode`**当前**文件进行测试
106108

107109
- 有下列三种测试集来源:
108110
- **默认测试集**:Test with the default cases
@@ -138,6 +140,7 @@
138140
| `leetcode.endpoint` | 指定使用的终端,可用终端有:`leetcode`, `leetcode-cn` | `leetcode` |
139141
| `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括:<ul><li>`${tag}`: 根据题目的类别进行分类。<li>`${language}`: 根据题目的语言进行分类。</li><li>`${difficulty}`: 根据题目的难度进行分类。</li></ul> | N/A |
140142
| `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` |
143+
| `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` |
141144
| `leetcode.nodePath` | 指定 `Node.js` 可执行文件的路径。 | `node` |
142145

143146
## 疑难解答

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@
295295
"scope": "application",
296296
"description": "Show the LeetCode status bar or not."
297297
},
298+
"leetcode.enableShortcuts": {
299+
"type": "boolean",
300+
"default": true,
301+
"scope": "application",
302+
"description": "Show the submit and test shortcuts in editor or not."
303+
},
298304
"leetcode.nodePath": {
299305
"type": "string",
300306
"default": "node",

src/codelens/CodeLensController.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) jdneo. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { ConfigurationChangeEvent, Disposable, languages, workspace, WorkspaceConfiguration } from "vscode";
5+
import { CustomCodeLensProvider } from "./CustomCodeLensProvider";
6+
7+
class CodeLensController implements Disposable {
8+
private internalProvider: CustomCodeLensProvider;
9+
private registeredProvider: Disposable | undefined;
10+
private configurationChangeListener: Disposable;
11+
12+
constructor() {
13+
this.internalProvider = new CustomCodeLensProvider();
14+
15+
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
16+
if (event.affectsConfiguration("leetcode.enableShortcuts")) {
17+
this.setCodeLensVisibility();
18+
}
19+
}, this);
20+
21+
this.setCodeLensVisibility();
22+
}
23+
24+
public dispose(): void {
25+
if (this.registeredProvider) {
26+
this.registeredProvider.dispose();
27+
}
28+
this.configurationChangeListener.dispose();
29+
}
30+
31+
private setCodeLensVisibility(): void {
32+
if (this.isShortcutsEnabled() && !this.registeredProvider) {
33+
this.registeredProvider = languages.registerCodeLensProvider({ scheme: "file" }, this.internalProvider);
34+
} else if (!this.isShortcutsEnabled() && this.registeredProvider) {
35+
this.registeredProvider.dispose();
36+
this.registeredProvider = undefined;
37+
}
38+
}
39+
40+
private isShortcutsEnabled(): boolean {
41+
const configuration: WorkspaceConfiguration = workspace.getConfiguration();
42+
return configuration.get<boolean>("leetcode.enableShortcuts", true);
43+
}
44+
}
45+
46+
export const codeLensController: CodeLensController = new CodeLensController();

src/codeLensProvider.ts renamed to src/codelens/CustomCodeLensProvider.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as vscode from "vscode";
55

6-
class CodeLensProvider implements vscode.CodeLensProvider {
6+
export class CustomCodeLensProvider implements vscode.CodeLensProvider {
77

88
private validFileNamePattern: RegExp = /\d+\..*\.(.+)/;
99

@@ -16,13 +16,15 @@ class CodeLensProvider implements vscode.CodeLensProvider {
1616

1717
const range: vscode.Range = new vscode.Range(document.lineCount - 1, 0, document.lineCount - 1, 0);
1818

19-
const lens: vscode.CodeLens = new vscode.CodeLens(range, {
20-
title: "🙏 Submit to LeetCode",
21-
command: "leetcode.submitSolution",
22-
});
23-
24-
return [lens];
19+
return [
20+
new vscode.CodeLens(range, {
21+
title: "Submit",
22+
command: "leetcode.submitSolution",
23+
}),
24+
new vscode.CodeLens(range, {
25+
title: "Test",
26+
command: "leetcode.testSolution",
27+
}),
28+
];
2529
}
2630
}
27-
28-
export const codeLensProvider: CodeLensProvider = new CodeLensProvider();

src/extension.ts

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

44
import * as vscode from "vscode";
5-
import { codeLensProvider } from "./codeLensProvider";
5+
import { codeLensController } from "./codelens/CodeLensController";
66
import * as cache from "./commands/cache";
77
import { switchDefaultLanguage } from "./commands/language";
88
import * as plugin from "./commands/plugin";
@@ -43,8 +43,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
4343
leetCodeSolutionProvider,
4444
leetCodeExecutor,
4545
markdownEngine,
46+
codeLensController,
4647
vscode.window.createTreeView("leetCodeExplorer", { treeDataProvider: leetCodeTreeDataProvider, showCollapseAll: true }),
47-
vscode.languages.registerCodeLensProvider({ scheme: "file" }, codeLensProvider),
4848
vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()),
4949
vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()),
5050
vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),

0 commit comments

Comments
 (0)