Skip to content
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

fix(vscode): chat panel uses file as context when no selection. #3739

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
3 changes: 2 additions & 1 deletion clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ export interface ClientApiMethods {
readWorkspaceGitRepositories?: () => Promise<GitRepository[]>

/**
* @returns The active selection of active editor.
* Get the active editor selection as context, or the whole file if no selection.
* @returns The context of the active editor, or null if no active editor is found.
*/
getActiveEditorSelection: () => Promise<EditorFileContext | null>

Expand Down
2 changes: 2 additions & 0 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
"publish-prerelease": "pnpm run vscode:publish-prerelease && pnpm run ovsx:publish-prerelease"
},
"devDependencies": {
"@types/dedent": "^0.7.2",
"@types/deep-equal": "^1.0.4",
"@types/diff": "^5.2.1",
"@types/fs-extra": "^11.0.1",
Expand All @@ -472,6 +473,7 @@
"@vscode/test-web": "^0.0.63",
"@vscode/vsce": "^2.15.0",
"assert": "^2.0.0",
"debounce": "^2.2.0",
"dedent": "^0.7.0",
"deep-equal": "^2.2.1",
"diff": "^5.2.0",
Expand Down
35 changes: 35 additions & 0 deletions clients/vscode/src/chat/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { TextEditor } from "vscode";
import dedent from "dedent";
import type { EditorContext } from "tabby-chat-panel";
import type { GitProvider } from "../git/GitProvider";
import { localUriToChatPanelFilepath, vscodeRangeToChatPanelPositionRange } from "./utils";

// default: use selection if available, otherwise use the whole file
// selection: use selection if available, otherwise return null
// file: use the whole file
export type RangeStrategy = "default" | "selection" | "file";

export async function getEditorContext(
editor: TextEditor,
gitProvider: GitProvider,
rangeStrategy: RangeStrategy = "default",
): Promise<EditorContext | null> {
let range = rangeStrategy !== "file" ? editor.selection : undefined;
let text = !range?.isEmpty ? editor.document.getText(range) : "";
if (!text || text.trim().length < 1) {
if (rangeStrategy === "selection") {
return null;
} else if (range !== undefined) {
range = undefined;
text = editor.document.getText();
}
}
const content = range !== undefined ? dedent(text) : text;

return {
kind: "file",
filepath: localUriToChatPanelFilepath(editor.document.uri, gitProvider),
range: range ? vscodeRangeToChatPanelPositionRange(range) : undefined,
content,
};
}
55 changes: 0 additions & 55 deletions clients/vscode/src/chat/fileContext.ts

This file was deleted.

16 changes: 10 additions & 6 deletions clients/vscode/src/chat/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ import type {
FileRange,
} from "tabby-chat-panel";
import * as semver from "semver";
import debounce from "debounce";
import { v4 as uuid } from "uuid";
import type { StatusInfo, Config } from "tabby-agent";
import type { GitProvider } from "../git/GitProvider";
import type { Client as LspClient } from "../lsp/Client";
import { createClient } from "./createClient";
import { isBrowser } from "../env";
import { getLogger } from "../logger";
import { getFileContextFromSelection } from "./fileContext";
import { getEditorContext } from "./context";
import {
localUriToChatPanelFilepath,
chatPanelFilepathToLocalUri,
Expand Down Expand Up @@ -112,14 +113,14 @@ export class ChatWebview {
this.disposables.push(
window.onDidChangeActiveTextEditor((editor) => {
if (this.chatPanelLoaded) {
this.notifyActiveEditorSelectionChange(editor);
this.debouncedNotifyActiveEditorSelectionChange(editor);
}
}),
);
this.disposables.push(
window.onDidChangeTextEditorSelection((event) => {
if (event.textEditor === window.activeTextEditor && this.chatPanelLoaded) {
this.notifyActiveEditorSelectionChange(event.textEditor);
this.debouncedNotifyActiveEditorSelectionChange(event.textEditor);
}
}),
);
Expand Down Expand Up @@ -474,8 +475,7 @@ export class ChatWebview {
return null;
}

const fileContext = await getFileContextFromSelection(editor, this.gitProvider);
return fileContext;
return await getEditorContext(editor, this.gitProvider);
},

fetchSessionState: async (keys?: string[] | undefined): Promise<Record<string, unknown> | null> => {
Expand Down Expand Up @@ -739,10 +739,14 @@ export class ChatWebview {
return;
}

const fileContext = await getFileContextFromSelection(editor, this.gitProvider);
const fileContext = await getEditorContext(editor, this.gitProvider);
await this.client?.updateActiveSelection(fileContext);
}

private debouncedNotifyActiveEditorSelectionChange = debounce(async (editor: TextEditor | undefined) => {
await this.notifyActiveEditorSelectionChange(editor);
}, 100);

private getColorThemeString() {
switch (window.activeColorTheme.kind) {
case ColorThemeKind.Light:
Expand Down
6 changes: 3 additions & 3 deletions clients/vscode/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ContextVariables } from "../ContextVariables";
import { InlineCompletionProvider } from "../InlineCompletionProvider";
import { ChatSidePanelProvider } from "../chat/sidePanel";
import { createChatPanel } from "../chat/chatPanel";
import { getFileContextFromSelection, getFileContext } from "../chat/fileContext";
import { getEditorContext } from "../chat/context";
import { GitProvider, Repository } from "../git/GitProvider";
import { showOutputPanel } from "../logger";
import { InlineEditController } from "../inline-edit";
Expand Down Expand Up @@ -227,7 +227,7 @@ export class Commands {
"chat.addRelevantContext": async () => {
ensureHasEditorSelection(async (editor) => {
await commands.executeCommand("tabby.chatView.focus");
const fileContext = await getFileContextFromSelection(editor, this.gitProvider);
const fileContext = await getEditorContext(editor, this.gitProvider, "selection");
if (fileContext) {
this.chatSidePanelProvider.addRelevantContext(fileContext);
}
Expand All @@ -237,7 +237,7 @@ export class Commands {
const editor = window.activeTextEditor;
if (editor) {
await commands.executeCommand("tabby.chatView.focus");
const fileContext = await getFileContext(editor, this.gitProvider);
const fileContext = await getEditorContext(editor, this.gitProvider, "file");
if (fileContext) {
this.chatSidePanelProvider.addRelevantContext(fileContext);
}
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading