代码之家  ›  专栏  ›  技术社区  ›  BlueMonkMN

如何将VS代码的大纲同步到编辑器中的当前位置

  •  0
  • BlueMonkMN  · 技术社区  · 7 年前

    我通常很难在一个大的分页函数中找到它。

    我看到了如何通过注册文档符号提供程序来编写一个扩展来列出所有函数。不过,在我走得太远之前,我想知道是否有某种方法可以在outline视图中自动地、持续地显示代码编辑器中哪个节点代表当前位置。如果没有,我可能只需要创建自己的树视图(它会有这个功能吗?)。

    0 回复  |  直到 7 年前
        1
  •  2
  •   Mike Lischke    7 年前

    是的,这是可能的。树提供程序需要一种将符号与树项匹配的方法,然后调用 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);
        }
    });
    
        2
  •  0
  •   BlueMonkMN    7 年前

    如果你有一个功能合理的大纲视图,也就是说,一个通过为每个符号的整个范围提供范围对象而不仅仅是一个位置,将符号按层次排列的视图,那么你可以切换视图菜单中的“面包屑”项,以获得在大纲层次结构中的恒定视图。这正是我所追求的。

    为了帮助解决这个问题,我将一些数据存储在一个名为 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