From fec0ce89560860cdf3a87916610951c1ee3f1f7d Mon Sep 17 00:00:00 2001 From: aliang Date: Thu, 2 Jan 2025 17:23:14 +0800 Subject: [PATCH] chore(chat, vscode): add api getActiveEditorSelection (#3638) * refactor(chat): change the implementation of synchronizing the active editor selection * update * [autofix.ci] apply automated fixes * Update clients/vscode/src/chat/webview.ts Co-authored-by: Zhiming Ma * update: required * revert * update * update * update --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Zhiming Ma --- clients/tabby-chat-panel/src/index.ts | 6 +++++ clients/vscode/src/chat/createClient.ts | 1 + clients/vscode/src/chat/webview.ts | 26 ++++++++++++------- .../app/files/components/chat-side-bar.tsx | 6 ++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/clients/tabby-chat-panel/src/index.ts b/clients/tabby-chat-panel/src/index.ts index 43e44e350522..857d83a42362 100644 --- a/clients/tabby-chat-panel/src/index.ts +++ b/clients/tabby-chat-panel/src/index.ts @@ -273,6 +273,11 @@ export interface ClientApiMethods { // Provide all repos found in workspace folders. readWorkspaceGitRepositories?: () => Promise + + /** + * @returns The active selection of active editor. + */ + getActiveEditorSelection: () => Promise } export interface ClientApi extends ClientApiMethods { @@ -297,6 +302,7 @@ export function createClient(target: HTMLIFrameElement, api: ClientApiMethods): openInEditor: api.openInEditor, openExternal: api.openExternal, readWorkspaceGitRepositories: api.readWorkspaceGitRepositories, + getActiveEditorSelection: api.getActiveEditorSelection, }, }) } diff --git a/clients/vscode/src/chat/createClient.ts b/clients/vscode/src/chat/createClient.ts index a0c65b93b98d..924ee0f546d0 100644 --- a/clients/vscode/src/chat/createClient.ts +++ b/clients/vscode/src/chat/createClient.ts @@ -35,6 +35,7 @@ export function createClient(webview: Webview, api: ClientApiMethods): ServerApi openInEditor: api.openInEditor, openExternal: api.openExternal, readWorkspaceGitRepositories: api.readWorkspaceGitRepositories, + getActiveEditorSelection: api.getActiveEditorSelection, }, }); } diff --git a/clients/vscode/src/chat/webview.ts b/clients/vscode/src/chat/webview.ts index e51747620210..9cef59828d50 100644 --- a/clients/vscode/src/chat/webview.ts +++ b/clients/vscode/src/chat/webview.ts @@ -25,6 +25,7 @@ import type { SymbolInfo, FileLocation, GitRepository, + EditorFileContext, } from "tabby-chat-panel"; import * as semver from "semver"; import type { StatusInfo, Config } from "tabby-agent"; @@ -101,14 +102,14 @@ export class ChatWebview { this.disposables.push( window.onDidChangeActiveTextEditor((editor) => { if (this.chatPanelLoaded) { - this.syncActiveEditorSelection(editor); + this.notifyActiveEditorSelectionChange(editor); } }), ); this.disposables.push( window.onDidChangeTextEditorSelection((event) => { if (event.textEditor === window.activeTextEditor && this.chatPanelLoaded) { - this.syncActiveEditorSelection(event.textEditor); + this.notifyActiveEditorSelectionChange(event.textEditor); } }), ); @@ -237,12 +238,9 @@ export class ChatWebview { this.chatPanelLoaded = true; - // 1. Sync the active editor selection - // 2. Send pending actions - // 3. Call the client's init method - // 4. Show the chat panel (call syncStyle underlay) - await this.syncActiveEditorSelection(window.activeTextEditor); - + // 1. Send pending actions + // 2. Call the client's init method + // 3. Show the chat panel (call syncStyle underlay) this.pendingActions.forEach(async (fn) => { await fn(); }); @@ -448,6 +446,16 @@ export class ChatWebview { } return infoList; }, + + getActiveEditorSelection: async (): Promise => { + const editor = window.activeTextEditor; + if (!editor || !isValidForSyncActiveEditorSelection(editor)) { + return null; + } + + const fileContext = await getFileContextFromSelection(editor, this.gitProvider); + return fileContext; + }, }); } @@ -635,7 +643,7 @@ export class ChatWebview { ); } - private async syncActiveEditorSelection(editor: TextEditor | undefined) { + private async notifyActiveEditorSelectionChange(editor: TextEditor | undefined) { if (!editor || !isValidForSyncActiveEditorSelection(editor)) { await this.client?.updateActiveSelection(null); return; diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 52d1a1156586..134de1ce0486 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -95,7 +95,11 @@ export const ChatSideBar: React.FC = ({ }, readWorkspaceGitRepositories: async () => { return readWorkspaceGitRepositories.current?.() - } + }, + getActiveEditorSelection: async() => { + // FIXME(@jueliang) implement + return null + }, }) const getCommand = ({ action }: QuickActionEventPayload) => {