Skip to content

Commit

Permalink
chore(chat, vscode): add api getActiveEditorSelection (#3638)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent 28c4df2 commit fec0ce8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
6 changes: 6 additions & 0 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ export interface ClientApiMethods {

// Provide all repos found in workspace folders.
readWorkspaceGitRepositories?: () => Promise<GitRepository[]>

/**
* @returns The active selection of active editor.
*/
getActiveEditorSelection: () => Promise<EditorFileContext | null>
}

export interface ClientApi extends ClientApiMethods {
Expand All @@ -297,6 +302,7 @@ export function createClient(target: HTMLIFrameElement, api: ClientApiMethods):
openInEditor: api.openInEditor,
openExternal: api.openExternal,
readWorkspaceGitRepositories: api.readWorkspaceGitRepositories,
getActiveEditorSelection: api.getActiveEditorSelection,
},
})
}
Expand Down
1 change: 1 addition & 0 deletions clients/vscode/src/chat/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createClient(webview: Webview, api: ClientApiMethods): ServerApi
openInEditor: api.openInEditor,
openExternal: api.openExternal,
readWorkspaceGitRepositories: api.readWorkspaceGitRepositories,
getActiveEditorSelection: api.getActiveEditorSelection,
},
});
}
26 changes: 17 additions & 9 deletions clients/vscode/src/chat/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}),
);
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -448,6 +446,16 @@ export class ChatWebview {
}
return infoList;
},

getActiveEditorSelection: async (): Promise<EditorFileContext | null> => {
const editor = window.activeTextEditor;
if (!editor || !isValidForSyncActiveEditorSelection(editor)) {
return null;
}

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

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
},
readWorkspaceGitRepositories: async () => {
return readWorkspaceGitRepositories.current?.()
}
},
getActiveEditorSelection: async() => {
// FIXME(@jueliang) implement
return null
},
})

const getCommand = ({ action }: QuickActionEventPayload) => {
Expand Down

0 comments on commit fec0ce8

Please sign in to comment.