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

chore(chat, vscode): add api getActiveEditorSelection #3638

Merged
merged 9 commits into from
Jan 2, 2025
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
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
Loading