我正在使用一种通常具有非常大且难以导航的文件的语言,因此我希望随时都能看到我所处的功能,因为这通常是最大的烦恼(寻呼大约20次才能找到当前函数名称,然后向下翻页)。
我看到如何通过注册文档符号提供程序来编写扩展来列出所有函数。然而,在我走得太远之前,我想知道是否会有一些方法可以在大纲视图中自动且不断地指出哪个节点代表代码编辑器中的当前位置。如果没有,我可能只需要创建自己的树视图(具有此功能吗?)。
是的,这是可能的。树提供者需要一种方法将符号与树项匹配,然后调用TreeView.reveal()
。这是code I use在动作列表中选择一个条目,具体取决于当前源编辑器中插入符的位置:
public update(editor: TextEditor) {
let position = editor.selection.active;
let action = Utils.findInListFromPosition(this.actions, position.character, position.line + 1);
if (action) {
this.actionTree.reveal(action, { select: true });
return;
}
let predicate = Utils.findInListFromPosition(this.predicates, position.character, position.line + 1);
if (predicate) {
this.actionTree.reveal(predicate, { select: true });
return;
}
}
从主扩展文件中注册的选择更改事件调用此方法:
window.onDidChangeTextEditorSelection((event: TextEditorSelectionChangeEvent) => {
if (event.textEditor.document.languageId === "antlr" && event.textEditor.document.uri.scheme === "file") {
...
actionsProvider.update(event.textEditor);
}
});
如果你有一个功能合理的大纲视图,也就是说,通过为每个符号的整个范围提供范围对象而不仅仅是一个位置来分层排列符号,那么你可以在视图中切换“Breadcrumbs”项菜单可以持续查看轮廓层次结构中的位置。这正是我追求的。
为了解决这个问题,我将一些数据存储在一个名为currentBlock
的变量中,包括我遇到第一行时创建的symbolInformation
,例如,一个方法(取自正则表达式返回的匹配对象):
currentBlock.symbolInformation = new vscode.SymbolInformation(
match[1],
vscode.SymbolKind.Method,
className,
new vscode.Location(document.uri,
new vscode.Position(lineNum, line.firstNonWhitespaceCharacterIndex)));
然后,当我到达块的末尾时,我将包含先前存储的数据的剩余信息,并将其推送到SymbolInformation[]
结果。
private popBlock(document: vscode.TextDocument, lineNum: number, currentBlock: IndentInfo): vscode.SymbolInformation | undefined {
if (currentBlock.symbolInformation !== undefined) {
currentBlock.symbolInformation = new vscode.SymbolInformation(
currentBlock.symbolInformation.name,
currentBlock.symbolInformation.kind,
currentBlock.symbolInformation.containerName,
new vscode.Location(
currentBlock.symbolInformation.location.uri,
new vscode.Range(
currentBlock.symbolInformation.location.range.start,
new vscode.Position(lineNum-1, document.lineAt(lineNum-1).text.length)
)
)
);
return currentBlock.symbolInformation;
}
}
在这里,您可以看到面包屑报告编辑器窗格上方当前位置的完整上下文。它基于用于构建大纲的相同信息。