From 2adaa4351f0b5429ef5303f7e7bc2a6b6c283cd0 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 3 Jun 2019 14:35:49 +0800 Subject: [PATCH] feat: Customize the shortcuts in editor (#340) --- package.json | 20 +++++++++- src/codelens/CodeLensController.ts | 2 + src/codelens/CustomCodeLensProvider.ts | 52 +++++++++++++++++++------- src/utils/settingUtils.ts | 4 ++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 7dae6443..d8187565 100644 --- a/package.json +++ b/package.json @@ -331,7 +331,25 @@ "type": "boolean", "default": true, "scope": "application", - "description": "Show the submit and test shortcuts in editor or not." + "description": "[Deprecated] Show the submit and test shortcuts in editor or not." + }, + "leetcode.editor.shortcuts": { + "type": "array", + "default": [ + "submit", + "test" + ], + "scope": "application", + "items": { + "type": "string", + "enum": [ + "submit", + "test", + "solution", + "description" + ] + }, + "description": "Customize the shorcuts in editor." }, "leetcode.enableSideMode": { "type": "boolean", diff --git a/src/codelens/CodeLensController.ts b/src/codelens/CodeLensController.ts index a7fa1ef7..9373df27 100644 --- a/src/codelens/CodeLensController.ts +++ b/src/codelens/CodeLensController.ts @@ -15,6 +15,8 @@ class CodeLensController implements Disposable { this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => { if (event.affectsConfiguration("leetcode.enableShortcuts")) { this.setCodeLensVisibility(); + } else if (event.affectsConfiguration("leetcode.editor.shortcuts")) { + this.internalProvider.refresh(); } }, this); diff --git a/src/codelens/CustomCodeLensProvider.ts b/src/codelens/CustomCodeLensProvider.ts index 1290e057..f201dbd7 100644 --- a/src/codelens/CustomCodeLensProvider.ts +++ b/src/codelens/CustomCodeLensProvider.ts @@ -2,41 +2,67 @@ // Licensed under the MIT license. import * as vscode from "vscode"; +import { getEditorShortcuts } from "../utils/settingUtils"; export class CustomCodeLensProvider implements vscode.CodeLensProvider { - private validFileNamePattern: RegExp = /\d+\..*\.(.+)/; + private onDidChangeCodeLensesEmitter: vscode.EventEmitter = new vscode.EventEmitter(); + + get onDidChangeCodeLenses(): vscode.Event { + return this.onDidChangeCodeLensesEmitter.event; + } + + public refresh(): void { + this.onDidChangeCodeLensesEmitter.fire(); + } public provideCodeLenses(document: vscode.TextDocument): vscode.ProviderResult { + const shortcuts: string[] = getEditorShortcuts(); + if (!shortcuts) { + return; + } + const fileName: string = document.fileName.trim(); - const matchResult: RegExpMatchArray | null = fileName.match(this.validFileNamePattern); + const matchResult: RegExpMatchArray | null = fileName.match(/\d+\..*\.(.+)/); if (!matchResult) { return undefined; } const range: vscode.Range = new vscode.Range(document.lineCount - 1, 0, document.lineCount - 1, 0); + const codeLens: vscode.CodeLens[] = []; - return [ - new vscode.CodeLens(range, { + if (shortcuts.indexOf("submit") >= 0) { + codeLens.push(new vscode.CodeLens(range, { title: "Submit", command: "leetcode.submitSolution", arguments: [document.uri], - }), - new vscode.CodeLens(range, { + })); + } + + if (shortcuts.indexOf("test") >= 0) { + codeLens.push(new vscode.CodeLens(range, { title: "Test", command: "leetcode.testSolution", arguments: [document.uri], - }), - new vscode.CodeLens(range, { + })); + } + + if (shortcuts.indexOf("solution") >= 0) { + codeLens.push(new vscode.CodeLens(range, { title: "Solution", command: "leetcode.showSolution", arguments: [document.uri], - }), - new vscode.CodeLens(range, { - title: "Preview", + })); + } + + if (shortcuts.indexOf("description") >= 0) { + codeLens.push(new vscode.CodeLens(range, { + title: "Description", command: "leetcode.previewProblem", arguments: [document.uri], - }), - ]; + })); + } + + return codeLens; } } diff --git a/src/utils/settingUtils.ts b/src/utils/settingUtils.ts index c6c643c4..3cc48ea7 100644 --- a/src/utils/settingUtils.ts +++ b/src/utils/settingUtils.ts @@ -10,3 +10,7 @@ export function getWorkspaceConfiguration(): WorkspaceConfiguration { export function shouldHideSolvedProblem(): boolean { return getWorkspaceConfiguration().get("hideSolved", false); } + +export function getEditorShortcuts(): string[] { + return getWorkspaceConfiguration().get("editor.shortcuts", ["submit", "test"]); +}