Skip to content

feat: Customize the shortcuts in editor #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]);
}