Merge pull request #428 from devchat-ai/get-doc-symbols

Add type definitions and Implement get_document_symbols
This commit is contained in:
boob.yang 2024-02-01 12:17:18 +08:00 committed by GitHub
commit ac395cef2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,59 @@
import * as vscode from "vscode";
import { IDEService } from "../types";
/**
* Get document symbols of a file
*
* @param abspath: absolute path of the file
* @returns an array of IDEService.SymbolNode
*/
export async function getDocumentSymbols(abspath: string) {
const documentSymbols = await vscode.commands.executeCommand<
vscode.DocumentSymbol[] | vscode.SymbolInformation[]
>("vscode.executeDocumentSymbolProvider", vscode.Uri.file(abspath));
const symbols: IDEService.SymbolNode[] = [];
documentSymbols.forEach((symbol) => {
const symbolNode = toSymbolNode(symbol);
symbols.push(symbolNode);
});
return symbols;
}
/**
* Convert vscode.DocumentSymbol or vscode.SymbolInformation to IDEService.SymbolNode recursively
*/
function toSymbolNode(
symbol: vscode.DocumentSymbol | vscode.SymbolInformation
): IDEService.SymbolNode {
const range = "range" in symbol ? symbol.range : symbol.location?.range;
const start = range.start;
const end = range.end;
const symbolNode: IDEService.SymbolNode = {
name: symbol.name,
kind: vscode.SymbolKind[symbol.kind],
range: {
start: {
line: start.line,
character: start.character,
},
end: {
line: end.line,
character: end.character,
},
},
children: [],
};
if ("children" in symbol) {
symbol.children.forEach((child) => {
symbolNode.children.push(toSymbolNode(child));
});
}
return symbolNode;
}

View File

@ -10,6 +10,7 @@ import { updateSlashCommands } from "./endpoints/updateSlashCommands";
import { ideLanguage } from "./endpoints/ideLanguage";
import { LegacyEndpoints } from "./endpoints/legacy";
import { UnofficialEndpoints } from "./endpoints/unofficial";
import { getDocumentSymbols } from "./endpoints/getDocumentSymbols";
const functionRegistry: any = {
/**
@ -43,6 +44,10 @@ const functionRegistry: any = {
keys: ["message"],
handler: logError,
},
"/get_document_symbols": {
keys: ["abspath"],
handler: getDocumentSymbols,
},
/**
* @deprecated

21
src/ide_services/types.ts Normal file
View File

@ -0,0 +1,21 @@
/**
* Type definitions for IDE Service Protocol
*/
export namespace IDEService {
export interface Position {
line: number; // 0-based
character: number; // 0-based
}
export interface Range {
start: Position;
end: Position;
}
export interface SymbolNode {
name: string;
kind: string;
range: Range;
children: SymbolNode[];
}
}