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: adding At feature to bring symbol/files in tabby chatpanel #3598

Closed
wants to merge 6 commits into from
149 changes: 149 additions & 0 deletions clients/tabby-chat-panel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,142 @@ export interface GitRepository {
url: string
}

/**
* A symbol kind from vscode standard
*/
export enum SymbolKind {
/**
* The `File` symbol kind.
*/
File = 0,
/**
* The `Module` symbol kind.
*/
Module = 1,
/**
* The `Namespace` symbol kind.
*/
Namespace = 2,
/**
* The `Package` symbol kind.
*/
Package = 3,
/**
* The `Class` symbol kind.
*/
Class = 4,
/**
* The `Method` symbol kind.
*/
Method = 5,
/**
* The `Property` symbol kind.
*/
Property = 6,
/**
* The `Field` symbol kind.
*/
Field = 7,
/**
* The `Constructor` symbol kind.
*/
Constructor = 8,
/**
* The `Enum` symbol kind.
*/
Enum = 9,
/**
* The `Interface` symbol kind.
*/
Interface = 10,
/**
* The `Function` symbol kind.
*/
Function = 11,
/**
* The `Variable` symbol kind.
*/
Variable = 12,
/**
* The `Constant` symbol kind.
*/
Constant = 13,
/**
* The `String` symbol kind.
*/
String = 14,
/**
* The `Number` symbol kind.
*/
Number = 15,
/**
* The `Boolean` symbol kind.
*/
Boolean = 16,
/**
* The `Array` symbol kind.
*/
Array = 17,
/**
* The `Object` symbol kind.
*/
Object = 18,
/**
* The `Key` symbol kind.
*/
Key = 19,
/**
* The `Null` symbol kind.
*/
Null = 20,
/**
* The `EnumMember` symbol kind.
*/
EnumMember = 21,
/**
* The `Struct` symbol kind.
*/
Struct = 22,
/**
* The `Event` symbol kind.
*/
Event = 23,
/**
* The `Operator` symbol kind.
*/
Operator = 24,
/**
* The `TypeParameter` symbol kind.
*/
TypeParameter = 25,
}

export type AtKind = 'symbol' | 'file'

/**
* Represents as Symbol At Info in the file.
*/
export interface SymbolAtInfo {
// this means trying to query symbol in this at action
// it's different then actual SymbolKind
atKind: 'symbol'
name: string
location: FileLocation
}

/**
* Represents as File At Info in the file.
*/
export interface FileAtInfo {
atKind: 'file'
name: string
filepath: Filepath
}

export interface AtInputOpts { query?: string, limit?: number }

export type AtInfo = SymbolAtInfo | FileAtInfo

export interface ServerApi {
init: (request: InitRequest) => void
sendMessage: (message: ChatMessage) => void
Expand Down Expand Up @@ -234,6 +370,19 @@ export interface ClientApiMethods {

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

/**
* Return a SymbolAtInfo List with kind of file
* @param kind passing what kind of At info client want to get
* @returns SymbolAtInfo array
*/
provideSymbolAtInfo?: (opts?: AtInputOpts) => Promise<SymbolAtInfo[] | null>

getSymbolAtInfoContent?: (info: SymbolAtInfo) => Promise<string | null>

provideFileAtInfo?: (opts?: AtInputOpts) => Promise<FileAtInfo[] | null>

getFileAtInfoContent?: (info: FileAtInfo) => Promise<string | null>
}

export interface ClientApi extends ClientApiMethods {
Expand Down
102 changes: 102 additions & 0 deletions clients/vscode/src/chat/WebviewHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
Location,
LocationLink,
workspace,
SymbolKind,
DocumentSymbol,
SymbolInformation,
} from "vscode";
import type {
ServerApi,
Expand All @@ -25,6 +28,9 @@ import type {
SymbolInfo,
FileLocation,
GitRepository,
AtInputOpts,
SymbolAtInfo,
FileAtInfo,
} from "tabby-chat-panel";
import { TABBY_CHAT_PANEL_API_VERSION } from "tabby-chat-panel";
import hashObject from "object-hash";
Expand All @@ -42,6 +48,10 @@ import {
vscodePositionToChatPanelPosition,
vscodeRangeToChatPanelPositionRange,
chatPanelLocationToVSCodeRange,
getAllowedSymbolKinds,
vscodeSymbolToAtInfo as vscodeSymbolToSymbolAtInfo,
isDocumentSymbol,
uriToFileAtInfo,
} from "./utils";

export class WebviewHelper {
Expand Down Expand Up @@ -71,6 +81,7 @@ export class WebviewHelper {

public setWebview(webview: Webview) {
this.webview = webview;
SymbolKind;
}

public setClient(client: ServerApi) {
Expand Down Expand Up @@ -728,6 +739,97 @@ export class WebviewHelper {
}
return infoList;
},
provideSymbolAtInfo: async (opts?: AtInputOpts): Promise<SymbolAtInfo[] | null> => {
const maxResults = opts?.limit || 50;
const query = opts?.query?.toLowerCase();

const editor = window.activeTextEditor;
if (!editor) return null;
const document = editor.document;

// Try document symbols first
const documentSymbols = await commands.executeCommand<DocumentSymbol[] | SymbolInformation[]>(
"vscode.executeDocumentSymbolProvider",
document.uri,
);

let results: SymbolAtInfo[] = [];

if (documentSymbols && documentSymbols.length > 0) {
const processSymbol = (symbol: DocumentSymbol | SymbolInformation) => {
if (results.length >= maxResults) return;

const symbolName = symbol.name.toLowerCase();
if (query && !symbolName.includes(query)) return;

if (getAllowedSymbolKinds().includes(symbol.kind)) {
results.push(vscodeSymbolToSymbolAtInfo(symbol, document.uri, this.gitProvider));
}
if (isDocumentSymbol(symbol)) {
symbol.children.forEach(processSymbol);
}
};
documentSymbols.forEach(processSymbol);
}

// Try workspace symbols if no document symbols found
if (results.length === 0 && query) {
const workspaceSymbols = await commands.executeCommand<SymbolInformation[]>(
"vscode.executeWorkspaceSymbolProvider",
query,
);

if (workspaceSymbols) {
results = workspaceSymbols
.filter((symbol) => getAllowedSymbolKinds().includes(symbol.kind))
.slice(0, maxResults)
.map((symbol) => vscodeSymbolToSymbolAtInfo(symbol, symbol.location.uri, this.gitProvider));
}
}

return results.length > 0 ? results : null;
},

provideFileAtInfo: async (opts?: AtInputOpts): Promise<FileAtInfo[] | null> => {
const maxResults = opts?.limit || 50;
const query = opts?.query?.toLowerCase();

const globPattern = query ? `**/${query}*` : "**/*";
try {
const files = await workspace.findFiles(globPattern, null, maxResults);
return files.map((uri) => uriToFileAtInfo(uri, this.gitProvider));
} catch (error) {
this.logger.error("Failed to find files:", error);
return null;
}
},
getSymbolAtInfoContent: async (info: SymbolAtInfo): Promise<string | null> => {
try {
const uri = chatPanelFilepathToLocalUri(info.location.filepath, this.gitProvider);
if (!uri) return null;

const document = await workspace.openTextDocument(uri);
const range = chatPanelLocationToVSCodeRange(info.location.location);
if (!range) return null;

return document.getText(range);
} catch (error) {
this.logger.error("Failed to get symbol content:", error);
return null;
}
},
getFileAtInfoContent: async (info: FileAtInfo): Promise<string | null> => {
try {
const uri = chatPanelFilepathToLocalUri(info.filepath, this.gitProvider);
if (!uri) return null;

const document = await workspace.openTextDocument(uri);
return document.getText();
} catch (error) {
this.logger.error("Failed to get file content:", error);
return null;
}
},
});
}
}
4 changes: 4 additions & 0 deletions clients/vscode/src/chat/chatPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export function createClient(webview: Webview, api: ClientApiMethods): ServerApi
lookupSymbol: api.lookupSymbol,
openInEditor: api.openInEditor,
readWorkspaceGitRepositories: api.readWorkspaceGitRepositories,
provideSymbolAtInfo: api.provideSymbolAtInfo,
getSymbolAtInfoContent: api.getSymbolAtInfoContent,
provideFileAtInfo: api.provideFileAtInfo,
getFileAtInfoContent: api.getFileAtInfoContent,
},
});
}
69 changes: 67 additions & 2 deletions clients/vscode/src/chat/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import path from "path";
import { Position as VSCodePosition, Range as VSCodeRange, Uri, workspace } from "vscode";
import type { Filepath, Position as ChatPanelPosition, LineRange, PositionRange, Location } from "tabby-chat-panel";
import {
Position as VSCodePosition,
Range as VSCodeRange,
Uri,
workspace,
DocumentSymbol,
SymbolInformation,
SymbolKind,
} from "vscode";
import type {
Filepath,
Position as ChatPanelPosition,
LineRange,
PositionRange,
Location,
SymbolAtInfo,
FileAtInfo,
} from "tabby-chat-panel";
import type { GitProvider } from "../git/GitProvider";
import { getLogger } from "../logger";

Expand Down Expand Up @@ -100,3 +116,52 @@ export function chatPanelLocationToVSCodeRange(location: Location): VSCodeRange
logger.warn(`Invalid location params.`, location);
return null;
}

export function isDocumentSymbol(symbol: DocumentSymbol | SymbolInformation): symbol is DocumentSymbol {
return "children" in symbol;
}

// FIXME: All allow symbol kinds, could be change later
export function getAllowedSymbolKinds(): SymbolKind[] {
return [
SymbolKind.Class,
SymbolKind.Function,
SymbolKind.Method,
SymbolKind.Interface,
SymbolKind.Enum,
SymbolKind.Struct,
];
}

export function vscodeSymbolToAtInfo(
symbol: DocumentSymbol | SymbolInformation,
documentUri: Uri,
gitProvider: GitProvider,
): SymbolAtInfo {
if (isDocumentSymbol(symbol)) {
return {
atKind: "symbol",
name: symbol.name,
location: {
filepath: localUriToChatPanelFilepath(documentUri, gitProvider),
location: vscodeRangeToChatPanelPositionRange(symbol.range),
},
};
}
return {
atKind: "symbol",
name: symbol.name,
location: {
filepath: localUriToChatPanelFilepath(documentUri, gitProvider),
location: vscodeRangeToChatPanelPositionRange(symbol.location.range),
},
};
}

export function uriToFileAtInfo(uri: Uri, gitProvider: GitProvider): FileAtInfo {
return {
atKind: "file",
name: path.basename(uri.fsPath),
filepath: localUriToChatPanelFilepath(uri, gitProvider),
};
}
Loading