如何将VS Code的大纲与编辑器的当前位置同步

3

我正在使用一种通常具有非常大且难以导航的文件的语言,因此我希望能够随时看到自己所在的函数,因为这通常是最大的困扰(向上翻页约20次以找到当前函数名称,然后向下翻页)。

我知道可以编写扩展程序通过注册文档符号提供程序来列出所有函数。但在深入研究之前,我想知道是否有一种自动且持续地在轮廓视图中显示哪个节点代表代码编辑器中的当前位置的某种指示方式。 如果没有,我可能需要创建自己的树视图(这会有这种功能吗?)。


你可以自己创建一棵树,但它会更加有限(例如目前无法添加“筛选器”文本字段:https://github.com/Microsoft/vscode/issues/50062)。 - Gama11
2个回答

1
是的,这是可能的。您的树提供程序需要一种将符号与树项匹配的方式,然后调用 TreeView.reveal()。这里是我使用的代码link1,可以在当前源编辑器中根据插入符号位置选择操作列表中的条目。
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);
    }
});

这是一个可以与简单的registerDocumentSymbolProvider解决方案一起使用的解决方案吗,还是我需要自己的registerTreeDataProvider - BlueMonkMN
你需要一个树形数据提供程序。标准符号提供程序没有树形结构,只在下拉菜单或编辑器中显示符号。 - Mike Lischke
我曾经认为从 https://marketplace.visualstudio.com/items?itemName=hitode909.perl-outline 获取符号可以让它们显示在大纲树中。虽然我可能无法控制该树,但我认为它至少会在那里显示出来。 - BlueMonkMN
结果表明我所需要的只是大纲和面包屑的结合。 - BlueMonkMN

1
如果你有一个相当功能齐全的大纲视图,也就是说,它通过为每个符号提供范围对象来按层次排列符号,而不仅仅是位置,那么你可以在视图菜单中切换“面包屑”项,以便始终查看大纲层次结构中的位置。这正是我想要的。
为了帮助实现这一点,我将一些数据存储在一个名为 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;
    }
}

在编辑器面板上方,您可以看到面包屑导航反映了当前位置的完整上下文。它基于用于构建大纲的相同信息。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接