Skip to content

Commit

Permalink
fix: implement shortcut in client middelware
Browse files Browse the repository at this point in the history
  • Loading branch information
antimonyGu committed Jan 2, 2025
1 parent 66a6dc1 commit bb0aedd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 2 additions & 6 deletions clients/tabby-agent/src/codeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { ClientCapabilities, ServerCapabilities, CodeLens, CodeLensType, ChangesPreviewLineType } from "./protocol";
import { TextDocuments } from "./lsp/textDocuments";
import { TextDocument } from "vscode-languageserver-textdocument";
import { isBrowser } from "./env";

const codeLensType: CodeLensType = "previewChanges";
const changesPreviewLineType = {
Expand Down Expand Up @@ -117,12 +116,10 @@ export class CodeLensProvider implements Feature {
},
});
} else if (!previewBlockMarkers.includes("x")) {
// TODO: read keybinds from LSP client, then send to LSP server to avoid hardcode.
const acceptShortcut = isBrowser ? "" : ` (${process.platform === "darwin" ? "cmd+enter" : "ctrl+enter"})`;
lineCodeLenses.push({
range: codeLensRange,
command: {
title: `$(check)Accept${acceptShortcut}`,
title: `$(check)Accept`,
command: "tabby/chat/edit/resolve",
arguments: [{ location: codeLensLocation, action: "accept" }],
},
Expand All @@ -132,11 +129,10 @@ export class CodeLensProvider implements Feature {
},
});

const discardShortcut = isBrowser ? "" : ` (esc)`;
lineCodeLenses.push({
range: codeLensRange,
command: {
title: `$(remove-close)Discard${discardShortcut}`,
title: `$(remove-close)Discard`,
command: "tabby/chat/edit/resolve",
arguments: [{ location: codeLensLocation, action: "discard" }],
},
Expand Down
24 changes: 22 additions & 2 deletions clients/vscode/src/lsp/CodeLensMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { CodeLensMiddleware as VscodeLspCodeLensMiddleware, ProvideCodeLensesSignature } from "vscode-languageclient";
import { CodeLens as TabbyCodeLens } from "tabby-agent";
import { findTextEditor } from "./vscodeWindowUtils";
import { isBrowser } from "../env";

type CodeLens = VscodeCodeLens & TabbyCodeLens;

Expand Down Expand Up @@ -74,16 +75,22 @@ export class CodeLensMiddleware implements VscodeLspCodeLensMiddleware {
if (!editor) {
return codeLenses;
}

if (!codeLenses) {
return [];
}

this.removeDecorations(editor);
const result =
codeLenses
?.map((codeLens) => this.handleCodeLens(codeLens, editor))
.filter((codeLens): codeLens is CodeLens => codeLens !== null) ?? [];
.map((codeLens) => this.handleCodeLens(codeLens, editor))
.filter((codeLens): codeLens is CodeLens => codeLens !== null);
this.purgeDecorationMap();
return result;
}

private handleCodeLens(codeLens: CodeLens, editor: TextEditor): CodeLens | null {
this.addShortcut(codeLens);
if (!codeLens.data || codeLens.data.type !== "previewChanges") {
return codeLens;
}
Expand All @@ -106,6 +113,19 @@ export class CodeLensMiddleware implements VscodeLspCodeLensMiddleware {
return null;
}

private addShortcut(codeLens: CodeLens) {
if (codeLens.command?.arguments?.[0].action === "accept") {
// TODO: read keybinds from LSP client, then send to LSP server to avoid hardcode.
const acceptShortcut = isBrowser ? '' : ` (${process.platform === 'darwin' ? 'cmd+enter' : 'ctrl+enter'})`;

codeLens.command.title += (acceptShortcut);
} else if (codeLens.command?.arguments?.[0].action === "discard") {
const discardShortcut = isBrowser ? '' : ` (esc)`;

codeLens.command.title += (discardShortcut);
}
}

private addDecorationRange(editor: TextEditor, decorationType: TextEditorDecorationType, range: Range) {
let decorations: Map<TextEditorDecorationType, Range[]> | undefined;
if (this.decorationMap.has(editor)) {
Expand Down

0 comments on commit bb0aedd

Please sign in to comment.