Skip to content

Commit 2adaa43

Browse files
authored
feat: Customize the shortcuts in editor (#340)
1 parent 41b3d32 commit 2adaa43

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

package.json

+19-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,25 @@
331331
"type": "boolean",
332332
"default": true,
333333
"scope": "application",
334-
"description": "Show the submit and test shortcuts in editor or not."
334+
"description": "[Deprecated] Show the submit and test shortcuts in editor or not."
335+
},
336+
"leetcode.editor.shortcuts": {
337+
"type": "array",
338+
"default": [
339+
"submit",
340+
"test"
341+
],
342+
"scope": "application",
343+
"items": {
344+
"type": "string",
345+
"enum": [
346+
"submit",
347+
"test",
348+
"solution",
349+
"description"
350+
]
351+
},
352+
"description": "Customize the shorcuts in editor."
335353
},
336354
"leetcode.enableSideMode": {
337355
"type": "boolean",

src/codelens/CodeLensController.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class CodeLensController implements Disposable {
1515
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
1616
if (event.affectsConfiguration("leetcode.enableShortcuts")) {
1717
this.setCodeLensVisibility();
18+
} else if (event.affectsConfiguration("leetcode.editor.shortcuts")) {
19+
this.internalProvider.refresh();
1820
}
1921
}, this);
2022

src/codelens/CustomCodeLensProvider.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,67 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { getEditorShortcuts } from "../utils/settingUtils";
56

67
export class CustomCodeLensProvider implements vscode.CodeLensProvider {
78

8-
private validFileNamePattern: RegExp = /\d+\..*\.(.+)/;
9+
private onDidChangeCodeLensesEmitter: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
10+
11+
get onDidChangeCodeLenses(): vscode.Event<void> {
12+
return this.onDidChangeCodeLensesEmitter.event;
13+
}
14+
15+
public refresh(): void {
16+
this.onDidChangeCodeLensesEmitter.fire();
17+
}
918

1019
public provideCodeLenses(document: vscode.TextDocument): vscode.ProviderResult<vscode.CodeLens[]> {
20+
const shortcuts: string[] = getEditorShortcuts();
21+
if (!shortcuts) {
22+
return;
23+
}
24+
1125
const fileName: string = document.fileName.trim();
12-
const matchResult: RegExpMatchArray | null = fileName.match(this.validFileNamePattern);
26+
const matchResult: RegExpMatchArray | null = fileName.match(/\d+\..*\.(.+)/);
1327
if (!matchResult) {
1428
return undefined;
1529
}
1630

1731
const range: vscode.Range = new vscode.Range(document.lineCount - 1, 0, document.lineCount - 1, 0);
32+
const codeLens: vscode.CodeLens[] = [];
1833

19-
return [
20-
new vscode.CodeLens(range, {
34+
if (shortcuts.indexOf("submit") >= 0) {
35+
codeLens.push(new vscode.CodeLens(range, {
2136
title: "Submit",
2237
command: "leetcode.submitSolution",
2338
arguments: [document.uri],
24-
}),
25-
new vscode.CodeLens(range, {
39+
}));
40+
}
41+
42+
if (shortcuts.indexOf("test") >= 0) {
43+
codeLens.push(new vscode.CodeLens(range, {
2644
title: "Test",
2745
command: "leetcode.testSolution",
2846
arguments: [document.uri],
29-
}),
30-
new vscode.CodeLens(range, {
47+
}));
48+
}
49+
50+
if (shortcuts.indexOf("solution") >= 0) {
51+
codeLens.push(new vscode.CodeLens(range, {
3152
title: "Solution",
3253
command: "leetcode.showSolution",
3354
arguments: [document.uri],
34-
}),
35-
new vscode.CodeLens(range, {
36-
title: "Preview",
55+
}));
56+
}
57+
58+
if (shortcuts.indexOf("description") >= 0) {
59+
codeLens.push(new vscode.CodeLens(range, {
60+
title: "Description",
3761
command: "leetcode.previewProblem",
3862
arguments: [document.uri],
39-
}),
40-
];
63+
}));
64+
}
65+
66+
return codeLens;
4167
}
4268
}

src/utils/settingUtils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export function getWorkspaceConfiguration(): WorkspaceConfiguration {
1010
export function shouldHideSolvedProblem(): boolean {
1111
return getWorkspaceConfiguration().get<boolean>("hideSolved", false);
1212
}
13+
14+
export function getEditorShortcuts(): string[] {
15+
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
16+
}

0 commit comments

Comments
 (0)