From c5c77c912a061d2946e1bd6303818bf4406c088e Mon Sep 17 00:00:00 2001 From: "sheche@microsoft.com" Date: Tue, 16 Apr 2019 20:40:31 +0800 Subject: [PATCH] Can config whether to show or hide the codelens --- README.md | 7 ++- docs/README_zh-CN.md | 7 ++- package.json | 6 +++ src/codelens/CodeLensController.ts | 46 +++++++++++++++++++ .../CustomCodeLensProvider.ts} | 20 ++++---- src/extension.ts | 4 +- 6 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 src/codelens/CodeLensController.ts rename src/{codeLensProvider.ts => codelens/CustomCodeLensProvider.ts} (62%) diff --git a/README.md b/README.md index f818911c..37c7aae8 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,9 @@ Submit the Answer

-- 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. +- 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`. + +> If you want to hide the shortcuts showing in the editor, just simply set the setting `leetcode.enableShortcuts` to false. --- @@ -102,7 +104,7 @@ Test the Answer

-- To **test** your answer, right click in the editor and select `Test in LeetCode`. +- 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`. - There are 3 ways to test the answer: - **Test with the default cases** @@ -138,6 +140,7 @@ | `leetcode.endpoint` | Specify the active endpoint. Supported endpoints are: `leetcode`, `leetcode-cn` | `leetcode` | | `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: | N/A | | `leetcode.enableStatusBar` | Specify whether the LeetCode status bar will be shown or not. | `true` | +| `leetcode.enableShortcuts` | Specify whether the submit and test shortcuts in editor or not. | `true` | | `leetcode.nodePath` | Specify the `Node.js` executable path. | `node` | ## Troubleshooting diff --git a/docs/README_zh-CN.md b/docs/README_zh-CN.md index 1673343f..04514c4d 100644 --- a/docs/README_zh-CN.md +++ b/docs/README_zh-CN.md @@ -93,7 +93,9 @@ 提交答案

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

-- 在编辑区内右键并选择 `Test in LeetCode`,可对**当前**答案进行测试。 +- 通过点击文件最下方的 `Test` 可测试答案。你也可以在编辑区内右键并选择 `Test in LeetCode`,对**当前**文件进行测试。 - 有下列三种测试集来源: - **默认测试集**:Test with the default cases @@ -138,6 +140,7 @@ | `leetcode.endpoint` | 指定使用的终端,可用终端有:`leetcode`, `leetcode-cn` | `leetcode` | | `leetcode.outputFolder` | 指定保存文件时所用的相对文件夹路径。除了用户自定义路径外,也可以使用保留项,包括: | N/A | | `leetcode.enableStatusBar` | 指定是否在 VS Code 下方显示插件状态栏。 | `true` | +| `leetcode.enableShortcuts` | 指定是否在 VS Code 编辑文件下方显示提交和测试的快捷按钮。 | `true` | | `leetcode.nodePath` | 指定 `Node.js` 可执行文件的路径。 | `node` | ## 疑难解答 diff --git a/package.json b/package.json index 41e73303..8f8a4c04 100644 --- a/package.json +++ b/package.json @@ -295,6 +295,12 @@ "scope": "application", "description": "Show the LeetCode status bar or not." }, + "leetcode.enableShortcuts": { + "type": "boolean", + "default": true, + "scope": "application", + "description": "Show the submit and test shortcuts in editor or not." + }, "leetcode.nodePath": { "type": "string", "default": "node", diff --git a/src/codelens/CodeLensController.ts b/src/codelens/CodeLensController.ts new file mode 100644 index 00000000..a7fa1ef7 --- /dev/null +++ b/src/codelens/CodeLensController.ts @@ -0,0 +1,46 @@ +// Copyright (c) jdneo. All rights reserved. +// Licensed under the MIT license. + +import { ConfigurationChangeEvent, Disposable, languages, workspace, WorkspaceConfiguration } from "vscode"; +import { CustomCodeLensProvider } from "./CustomCodeLensProvider"; + +class CodeLensController implements Disposable { + private internalProvider: CustomCodeLensProvider; + private registeredProvider: Disposable | undefined; + private configurationChangeListener: Disposable; + + constructor() { + this.internalProvider = new CustomCodeLensProvider(); + + this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => { + if (event.affectsConfiguration("leetcode.enableShortcuts")) { + this.setCodeLensVisibility(); + } + }, this); + + this.setCodeLensVisibility(); + } + + public dispose(): void { + if (this.registeredProvider) { + this.registeredProvider.dispose(); + } + this.configurationChangeListener.dispose(); + } + + private setCodeLensVisibility(): void { + if (this.isShortcutsEnabled() && !this.registeredProvider) { + this.registeredProvider = languages.registerCodeLensProvider({ scheme: "file" }, this.internalProvider); + } else if (!this.isShortcutsEnabled() && this.registeredProvider) { + this.registeredProvider.dispose(); + this.registeredProvider = undefined; + } + } + + private isShortcutsEnabled(): boolean { + const configuration: WorkspaceConfiguration = workspace.getConfiguration(); + return configuration.get("leetcode.enableShortcuts", true); + } +} + +export const codeLensController: CodeLensController = new CodeLensController(); diff --git a/src/codeLensProvider.ts b/src/codelens/CustomCodeLensProvider.ts similarity index 62% rename from src/codeLensProvider.ts rename to src/codelens/CustomCodeLensProvider.ts index ac19cd40..001ccc0b 100644 --- a/src/codeLensProvider.ts +++ b/src/codelens/CustomCodeLensProvider.ts @@ -3,7 +3,7 @@ import * as vscode from "vscode"; -class CodeLensProvider implements vscode.CodeLensProvider { +export class CustomCodeLensProvider implements vscode.CodeLensProvider { private validFileNamePattern: RegExp = /\d+\..*\.(.+)/; @@ -16,13 +16,15 @@ class CodeLensProvider implements vscode.CodeLensProvider { const range: vscode.Range = new vscode.Range(document.lineCount - 1, 0, document.lineCount - 1, 0); - const lens: vscode.CodeLens = new vscode.CodeLens(range, { - title: "🙏 Submit to LeetCode", - command: "leetcode.submitSolution", - }); - - return [lens]; + return [ + new vscode.CodeLens(range, { + title: "Submit", + command: "leetcode.submitSolution", + }), + new vscode.CodeLens(range, { + title: "Test", + command: "leetcode.testSolution", + }), + ]; } } - -export const codeLensProvider: CodeLensProvider = new CodeLensProvider(); diff --git a/src/extension.ts b/src/extension.ts index 6cf38966..60138085 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,7 @@ // Licensed under the MIT license. import * as vscode from "vscode"; -import { codeLensProvider } from "./codeLensProvider"; +import { codeLensController } from "./codelens/CodeLensController"; import * as cache from "./commands/cache"; import { switchDefaultLanguage } from "./commands/language"; import * as plugin from "./commands/plugin"; @@ -43,8 +43,8 @@ export async function activate(context: vscode.ExtensionContext): Promise leetCodeSolutionProvider, leetCodeExecutor, markdownEngine, + codeLensController, vscode.window.createTreeView("leetCodeExplorer", { treeDataProvider: leetCodeTreeDataProvider, showCollapseAll: true }), - vscode.languages.registerCodeLensProvider({ scheme: "file" }, codeLensProvider), vscode.commands.registerCommand("leetcode.deleteCache", () => cache.deleteCache()), vscode.commands.registerCommand("leetcode.toggleLeetCodeCn", () => plugin.switchEndpoint()), vscode.commands.registerCommand("leetcode.signin", () => leetCodeManager.signIn()),