Skip to content

add: star command in shortcuts #601

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 3 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@
"type": "array",
"default": [
"submit",
"test"
"test",
"star"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add this shortcut by default. But please add it in the items, and also documents in readme

],
"scope": "application",
"items": {
Expand Down
4 changes: 2 additions & 2 deletions src/codelens/CodeLensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Licensed under the MIT license.

import { ConfigurationChangeEvent, Disposable, languages, workspace } from "vscode";
import { CustomCodeLensProvider } from "./CustomCodeLensProvider";
import { customCodeLensProvider, CustomCodeLensProvider } from "./CustomCodeLensProvider";

class CodeLensController implements Disposable {
private internalProvider: CustomCodeLensProvider;
private registeredProvider: Disposable | undefined;
private configurationChangeListener: Disposable;

constructor() {
this.internalProvider = new CustomCodeLensProvider();
this.internalProvider = customCodeLensProvider;

this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("leetcode.editor.shortcuts")) {
Expand Down
19 changes: 18 additions & 1 deletion src/codelens/CustomCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license.

import * as vscode from "vscode";
import { explorerNodeManager } from "../explorer/explorerNodeManager";
import { LeetCodeNode } from "../explorer/LeetCodeNode";
import { getEditorShortcuts } from "../utils/settingUtils";

export class CustomCodeLensProvider implements vscode.CodeLensProvider {
Expand All @@ -23,10 +25,15 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
}

const content: string = document.getText();
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=.* lang=.*/);
const matchResult: RegExpMatchArray | null = content.match(/@lc app=.* id=(.*) lang=.*/);
if (!matchResult) {
return undefined;
}
const nodeId: string | undefined = matchResult[1];
let node: LeetCodeNode | undefined;
if (nodeId) {
node = explorerNodeManager.getNodeById(nodeId);
}

let codeLensLine: number = document.lineCount - 1;
for (let i: number = document.lineCount - 1; i >= 0; i--) {
Expand Down Expand Up @@ -56,6 +63,14 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
}));
}

if (shortcuts.indexOf("star") >= 0 && node) {
codeLens.push(new vscode.CodeLens(range, {
title: node.isFavorite ? "Unstar" : "Star",
command: node.isFavorite ? "leetcode.removeFavorite" : "leetcode.addFavorite",
arguments: [node],
}));
}

if (shortcuts.indexOf("solution") >= 0) {
codeLens.push(new vscode.CodeLens(range, {
title: "Solution",
Expand All @@ -75,3 +90,5 @@ export class CustomCodeLensProvider implements vscode.CodeLensProvider {
return codeLens;
}
}

export const customCodeLensProvider: CustomCodeLensProvider = new CustomCodeLensProvider();
7 changes: 5 additions & 2 deletions src/commands/star.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

import { customCodeLensProvider } from "../codelens/CustomCodeLensProvider";
import { LeetCodeNode } from "../explorer/LeetCodeNode";
import { leetCodeTreeDataProvider } from "../explorer/LeetCodeTreeDataProvider";
import { leetCodeExecutor } from "../leetCodeExecutor";
Expand All @@ -10,7 +11,8 @@ import { DialogType, promptForOpenOutputChannel } from "../utils/uiUtils";
export async function addFavorite(node: LeetCodeNode): Promise<void> {
try {
await leetCodeExecutor.toggleFavorite(node, true);
leetCodeTreeDataProvider.refresh();
await leetCodeTreeDataProvider.refresh();
customCodeLensProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to add the problem to favorite. Please open the output channel for details.", DialogType.error);
}
Expand All @@ -19,7 +21,8 @@ export async function addFavorite(node: LeetCodeNode): Promise<void> {
export async function removeFavorite(node: LeetCodeNode): Promise<void> {
try {
await leetCodeExecutor.toggleFavorite(node, false);
leetCodeTreeDataProvider.refresh();
await leetCodeTreeDataProvider.refresh();
customCodeLensProvider.refresh();
} catch (error) {
await promptForOpenOutputChannel("Failed to remove the problem from favorite. Please open the output channel for details.", DialogType.error);
}
Expand Down