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

feat(api): adding list symbols in vscode #3770

Closed
Closed
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
10 changes: 10 additions & 0 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,13 @@ export interface ListFileItem {
filepath: Filepath
}

// Draft list active symbol item
export interface ListActiveSymbolItem {
filepath: Filepath
range: LineRange
label: string
}

export interface ServerApi {
init: (request: InitRequest) => void

Expand Down Expand Up @@ -344,13 +351,16 @@ export interface ClientApiMethods {
*/
listFileInWorkspace?: (params: ListFilesInWorkspaceParams) => Promise<ListFileItem[]>

listActiveSymbols?: () => Promise<ListActiveSymbolItem[]>

/**
* Returns the content of a file within the specified range.
* If `range` is not provided, the entire file content is returned.
* @param info A {@link FileRange} object that includes the file path and optionally a 1-based line range.
* @returns The content of the file as a string, or `null` if the file or range cannot be accessed.
*/
readFileContent?: (info: FileRange) => Promise<string | null>

}

export interface ClientApi extends ClientApiMethods {
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 @@ -40,6 +40,7 @@ export function createClient(webview: Webview, api: ClientApiMethods): ServerApi
storeSessionState: api.storeSessionState,
listFileInWorkspace: api.listFileInWorkspace,
readFileContent: api.readFileContent,
listActiveSymbols: api.listActiveSymbols,
},
});
}
7 changes: 7 additions & 0 deletions clients/vscode/src/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export function chatPanelLineRangeToVSCodeRange(lineRange: LineRange): VSCodeRan
return new VSCodeRange(Math.max(0, lineRange.start - 1), 0, lineRange.end, 0);
}

export function vscodeRangeToChatPanelLineRange(range: VSCodeRange): LineRange {
return {
start: range.start.line + 1,
end: range.end.line + 1,
};
}

export function chatPanelLocationToVSCodeRange(location: Location | undefined): VSCodeRange | null {
if (!location) {
return null;
Expand Down
66 changes: 66 additions & 0 deletions clients/vscode/src/chat/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
Location,
LocationLink,
TabInputText,
SymbolInformation,
DocumentSymbol,
SymbolKind,
} from "vscode";
import { TABBY_CHAT_PANEL_API_VERSION } from "tabby-chat-panel";
import type {
Expand All @@ -30,6 +33,7 @@ import type {
ListFilesInWorkspaceParams,
ListFileItem,
FileRange,
ListActiveSymbolItem,
} from "tabby-chat-panel";
import * as semver from "semver";
import debounce from "debounce";
Expand All @@ -50,6 +54,7 @@ import {
isValidForSyncActiveEditorSelection,
localUriToListFileItem,
escapeGlobPattern,
vscodeRangeToChatPanelLineRange,
} from "./utils";
import { findFiles } from "../findFiles";
import mainHtml from "./html/main.html";
Expand Down Expand Up @@ -550,6 +555,67 @@ export class ChatWebview {
const document = await workspace.openTextDocument(uri);
return document.getText(chatPanelLocationToVSCodeRange(info.range) ?? undefined);
},
listActiveSymbols: async (): Promise<ListActiveSymbolItem[]> => {
const editor = window.activeTextEditor;
if (!editor) {
return [];
}

try {
const symbols =
((await commands.executeCommand("vscode.executeDocumentSymbolProvider", editor.document.uri)) as (
| SymbolInformation
| DocumentSymbol
)[]) || [];

// FIXME: remove this
this.logger.info(`Fetched symbols: ${symbols?.length}`);
this.logger.info("Symbols:", symbols);
// TODO: put this into utils
const includesSymbolList = [
SymbolKind.Function,
SymbolKind.Struct,
SymbolKind.Interface,
SymbolKind.Class,
SymbolKind.Method,
SymbolKind.Module,
];

const collectFunctions = (symbols: (SymbolInformation | DocumentSymbol)[]): SymbolInformation[] => {
const result: SymbolInformation[] = [];

for (const symbol of symbols) {
if (symbol instanceof DocumentSymbol) {
if (includesSymbolList.includes(symbol.kind)) {
result.push(
new SymbolInformation(
symbol.name,
symbol.kind,
symbol.detail,
new Location(editor.document.uri, symbol.range),
),
);
}
if (symbol.children?.length) {
result.push(...collectFunctions(symbol.children));
}
} else if (includesSymbolList.includes(symbol.kind)) {
result.push(symbol);
}
}
return result;
};
const filepath = localUriToChatPanelFilepath(editor.document.uri, this.gitProvider);
return collectFunctions(symbols || []).map((symbol) => ({
filepath,
range: vscodeRangeToChatPanelLineRange(symbol.location.range),
label: symbol.name,
}));
} catch (error) {
this.logger.error(`Failed to fetch symbols: ${error}`);
return [];
}
},
});
}

Expand Down