我正在使用的语言通常有非常大且难以导航的文件,所以我想随时查看我所在的函数,因为这通常是最麻烦的(向上翻页约20次以找到当前的函数名,然后向下翻页)。
我知道如何通过注册一个文档符号提供程序来编写一个扩展来列出所有函数。但是,在我走得太远之前,我想知道是否有某种方法可以在大纲视图中自动不断地显示哪个节点表示代码编辑器中的当前位置。如果没有,我可能只需要创建自己的树视图(这会有这个功能吗?)。
发布于 2019-02-23 07:37:21
如果您有一个功能合理的大纲视图,也就是说,通过为每个符号的整个范围而不仅仅是一个位置提供range对象来按层次结构排列符号,那么您可以切换视图菜单中的"Breadcrumbs“项,以获得您在大纲层次结构中所处位置的恒定视图。这正是我要找的。
为了帮助实现这一点,我将一些数据存储在一个名为currentBlock的变量中,包括当我遇到第一行方法(取自正则表达式返回的match对象)时创建的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;
}
}在这里,您可以在编辑器窗格上方看到报告当前位置的完整上下文的面包屑。它基于用于构建大纲的相同信息。

发布于 2019-02-21 16:17:33
是的,这是可能的。您的树提供程序需要一种将符号与树项目匹配的方法,然后调用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);
}
});https://stackoverflow.com/questions/54796636
复制相似问题