Skip to content

POC: Code Lenses #243

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
42 changes: 42 additions & 0 deletions src/contributes/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,48 @@ function registerAskForCodeCommand(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('devchat.askForCode_chinese', callback));
}

export function registerCodeLenses(context: vscode.ExtensionContext) {
// This is the list of commands that will be shown as code lenses
// whenever selection is non empty.
// TODO: Add more commands
const lensCommands = [
{
title: '</?>',
tooltip: 'Ask code',
command: 'devchat.askForCode',
},
];

const codeLensProvider = new class implements vscode.CodeLensProvider {
private _onDidChangeCodeLenses: vscode.EventEmitter<void> = new vscode.EventEmitter<void>();
public readonly onDidChangeCodeLenses: vscode.Event<void> = this._onDidChangeCodeLenses.event;

constructor() {
// Update code lenses each time selection changes
vscode.window.onDidChangeTextEditorSelection((_) => {
this._onDidChangeCodeLenses.fire();
});
}

async provideCodeLenses(_: vscode.TextDocument): Promise<vscode.CodeLens[]> {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return [];
}

const selection = editor.selection;
if (selection.isEmpty) {
return [];
}

return lensCommands.map(command => new vscode.CodeLens(selection, command));
}
};

vscode.languages.registerCodeLensProvider("*", codeLensProvider);
}


function registerAskForFileCommand(context: vscode.ExtensionContext) {
const callback = async () => {
const editor = vscode.window.activeTextEditor;
Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
registerAddContextCommand,
registerAskForCodeCommand,
registerAskForFileCommand,
registerCodeLenses,
registerOpenAiApiKeySettingCommand,
registerDevChatApiKeySettingCommand,
regTopicDeleteCommand,
Expand Down Expand Up @@ -46,6 +47,8 @@ function activate(context: vscode.ExtensionContext) {
registerAskForFileCommand(context);
registerStatusBarItemClickCommand(context);

registerCodeLenses(context);

createStatusBarItem(context);

regTopicDeleteCommand(context);
Expand Down