Skip to content

Commit

Permalink
feat: Customize the shortcuts in editor (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Jun 3, 2019
1 parent 41b3d32 commit 2adaa43
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/codelens/CodeLensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
52 changes: 39 additions & 13 deletions src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> = new vscode.EventEmitter<void>();

get onDidChangeCodeLenses(): vscode.Event<void> {
return this.onDidChangeCodeLensesEmitter.event;
}

public refresh(): void {
this.onDidChangeCodeLensesEmitter.fire();
}

public provideCodeLenses(document: vscode.TextDocument): vscode.ProviderResult<vscode.CodeLens[]> {
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;
}
}
4 changes: 4 additions & 0 deletions src/utils/settingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export function getWorkspaceConfiguration(): WorkspaceConfiguration {
export function shouldHideSolvedProblem(): boolean {
return getWorkspaceConfiguration().get<boolean>("hideSolved", false);
}

export function getEditorShortcuts(): string[] {
return getWorkspaceConfiguration().get<string[]>("editor.shortcuts", ["submit", "test"]);
}

0 comments on commit 2adaa43

Please sign in to comment.