Skip to content

Commit

Permalink
fix(vscode): fix handle inline edit cancellation. (#3748)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored Jan 22, 2025
1 parent 5a7558e commit 96b4b42
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 168 deletions.
5 changes: 2 additions & 3 deletions clients/tabby-agent/src/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ export async function showDocument(params: ShowDocumentParams, lspConnection: Co
// [+] inserted
// [-] deleted
// [>] footer
// [x] stopped
// footer line
// >>>>>>> End of changes
export function generateChangesPreview(edit: Edit): string[] {
Expand Down Expand Up @@ -301,7 +300,7 @@ export function generateChangesPreview(edit: Edit): string[] {
}
if (inProgressChunk && lastDiff) {
if (edit.state === "stopped") {
pushDiffValue(lastDiff.value, "x");
pushDiffValue(lastDiff.value, "+");
} else {
pushDiffValue(lastDiff.value, "|");
}
Expand All @@ -312,7 +311,7 @@ export function generateChangesPreview(edit: Edit): string[] {
break;
}
if (edit.state === "stopped") {
pushDiffValue(diff.value, "x");
pushDiffValue(diff.value, "=");
} else {
pushDiffValue(diff.value, ".");
}
Expand Down
4 changes: 2 additions & 2 deletions clients/tabby-agent/src/codeLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class CodeLensProvider implements Feature {
if (match && editId) {
lineInPreviewBlock = -1;

if (previewBlockMarkers.includes(".")) {
if (previewBlockMarkers.includes(".") || previewBlockMarkers.includes("|")) {
lineCodeLenses.push({
range: codeLensRange,
command: {
Expand All @@ -115,7 +115,7 @@ export class CodeLensProvider implements Feature {
line: changesPreviewLineType.header,
},
});
} else if (!previewBlockMarkers.includes("x")) {
} else {
lineCodeLenses.push({
range: codeLensRange,
command: {
Expand Down
4 changes: 2 additions & 2 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,12 @@
"command": "tabby.chat.edit.accept",
"key": "ctrl+enter",
"mac": "cmd+enter",
"when": "tabby.chatEditResolving && editorTextFocus && !editorReadonly"
"when": "tabby.chatEditResolving && !tabby.chatEditInProgress && editorTextFocus && !editorReadonly"
},
{
"command": "tabby.chat.edit.discard",
"key": "escape",
"when": "tabby.chatEditResolving && editorTextFocus && !editorReadonly"
"when": "tabby.chatEditResolving && !tabby.chatEditInProgress && editorTextFocus && !editorReadonly"
},
{
"command": "tabby.chat.addRelevantContext",
Expand Down
2 changes: 1 addition & 1 deletion clients/vscode/src/code-action/QuickFix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class QuickFixCodeActionProvider implements CodeActionProviderInterface {
quickFixEditing.command = {
command: "tabby.chat.edit.start",
title: "Fix using Tabby",
arguments: [quickFixCmd, mergedRange],
arguments: [undefined, mergedRange, quickFixCmd],
};

const explainErrorCmd = `\nHere is some error information that occurred in the selection:
Expand Down
53 changes: 28 additions & 25 deletions clients/vscode/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,48 +266,51 @@ export class Commands {
"chat.createPanel": async () => {
await createChatPanel(this.context, this.client, this.gitProvider);
},
"chat.edit.start": async (userCommand?: string, range?: Range) => {
const editor = window.activeTextEditor;
if (!editor) {
"chat.edit.start": async (
fileUri?: string | undefined,
range?: Range | undefined,
userCommand?: string | undefined,
) => {
if (this.contextVariables.chatEditInProgress) {
window.setStatusBarMessage("Edit is already in progress.", 3000);
return;
}

const editRange = range || editor.selection;

const editLocation = {
uri: editor.document.uri.toString(),
range: {
start: { line: editRange.start.line, character: 0 },
end: {
line: editRange.end.character === 0 ? editRange.end.line : editRange.end.line + 1,
character: 0,
},
},
};

if (userCommand) {
let editor: TextEditor | undefined;
if (fileUri) {
try {
// when invoke from editor context menu, the first param `userCommand` is the current file path, we reset userCommand to undefined.
// uri parse will throw error when no scheme can be parsed.
Uri.parse(userCommand, true);
userCommand = undefined;
const uri = Uri.parse(fileUri, true);
editor = window.visibleTextEditors.find((editor) => editor.document.uri.toString() === uri.toString());
} catch {
//
// ignore
}
}
if (!editor) {
editor = window.activeTextEditor;
}
if (!editor) {
return;
}

const editRange = range ?? editor.selection;

const inlineEditController = new InlineEditController(
this.client,
this.config,
this.contextVariables,
editor,
editLocation,
userCommand,
editRange,
);
inlineEditController.start();
const cancellationTokenSource = new CancellationTokenSource();
this.chatEditCancellationTokenSource = cancellationTokenSource;
await inlineEditController.start(userCommand, cancellationTokenSource.token);
cancellationTokenSource.dispose();
this.chatEditCancellationTokenSource = null;
},
"chat.edit.stop": async () => {
this.chatEditCancellationTokenSource?.cancel();
this.chatEditCancellationTokenSource?.dispose();
this.chatEditCancellationTokenSource = null;
},
"chat.edit.accept": async () => {
const editor = window.activeTextEditor;
Expand Down
Loading

0 comments on commit 96b4b42

Please sign in to comment.