Merge pull request #428 from devchat-ai/get-doc-symbols
Add type definitions and Implement get_document_symbols
This commit is contained in:
commit
ac395cef2b
59
src/ide_services/endpoints/getDocumentSymbols.ts
Normal file
59
src/ide_services/endpoints/getDocumentSymbols.ts
Normal 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;
|
||||||
|
}
|
@ -10,6 +10,7 @@ import { updateSlashCommands } from "./endpoints/updateSlashCommands";
|
|||||||
import { ideLanguage } from "./endpoints/ideLanguage";
|
import { ideLanguage } from "./endpoints/ideLanguage";
|
||||||
import { LegacyEndpoints } from "./endpoints/legacy";
|
import { LegacyEndpoints } from "./endpoints/legacy";
|
||||||
import { UnofficialEndpoints } from "./endpoints/unofficial";
|
import { UnofficialEndpoints } from "./endpoints/unofficial";
|
||||||
|
import { getDocumentSymbols } from "./endpoints/getDocumentSymbols";
|
||||||
|
|
||||||
const functionRegistry: any = {
|
const functionRegistry: any = {
|
||||||
/**
|
/**
|
||||||
@ -43,6 +44,10 @@ const functionRegistry: any = {
|
|||||||
keys: ["message"],
|
keys: ["message"],
|
||||||
handler: logError,
|
handler: logError,
|
||||||
},
|
},
|
||||||
|
"/get_document_symbols": {
|
||||||
|
keys: ["abspath"],
|
||||||
|
handler: getDocumentSymbols,
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated
|
* @deprecated
|
||||||
|
21
src/ide_services/types.ts
Normal file
21
src/ide_services/types.ts
Normal 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[];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user