Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何将VS代码的大纲同步到编辑器中的当前位置_Typescript_Visual Studio Code_Vscode Extensions - Fatal编程技术网

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

Typescript 如何将VS代码的大纲同步到编辑器中的当前位置,typescript,visual-studio-code,vscode-extensions,Typescript,Visual Studio Code,Vscode Extensions,我使用的语言通常有非常大且难以导航的文件,因此我希望随时查看我使用的函数,因为这通常是最大的麻烦,向上翻页20次以查找当前函数名,然后向下翻页 我了解了如何通过注册文档符号提供程序来编写扩展以列出所有函数。不过,在我走得太远之前,我想知道是否有某种方法可以在大纲视图中自动并持续地显示哪些节点表示代码编辑器中的当前位置。如果没有,我可能只需要创建自己的树状视图,而不是具有此功能的树状视图?是的,这是可能的。您的树提供程序需要一种将符号与树项目匹配的方法,然后调用TreeView.reveal。根据

我使用的语言通常有非常大且难以导航的文件,因此我希望随时查看我使用的函数,因为这通常是最大的麻烦,向上翻页20次以查找当前函数名,然后向下翻页


我了解了如何通过注册文档符号提供程序来编写扩展以列出所有函数。不过,在我走得太远之前,我想知道是否有某种方法可以在大纲视图中自动并持续地显示哪些节点表示代码编辑器中的当前位置。如果没有,我可能只需要创建自己的树状视图,而不是具有此功能的树状视图?

是的,这是可能的。您的树提供程序需要一种将符号与树项目匹配的方法,然后调用TreeView.reveal。根据插入符号在当前源代码编辑器中的位置,可以在操作列表中选择一个条目:

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);
    }
});

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

为了帮助实现这一点,我将一些数据存储在一个名为currentBlock的变量中,包括在遇到第一行时创建的符号信息,例如,从正则表达式返回的匹配对象中获取的方法:

currentBlock.symbolInformation = new vscode.SymbolInformation(
    match[1],
    vscode.SymbolKind.Method,
    className,
    new vscode.Location(document.uri,
        new vscode.Position(lineNum, line.firstNonWhitespaceCharacterIndex)));
然后,当我到达块的末尾时,我会将包含先前存储的数据的剩余信息打包,并将其推送到SymbolInformation[]结果上

在这里,您可以在编辑器窗格上方看到报告当前位置完整上下文的面包屑。它基于用于构建大纲的相同信息


您可以创建自己的树,但它将受到更大的限制,无法添加筛选器文本字段atm例如:。这是一个我可以与简单registerDocumentSymbolProvider解决方案一起使用的解决方案,还是我需要自己的registerTreeDataProvider?您需要一个树数据提供程序。标准符号提供程序没有树,只是在下拉列表或编辑器中显示符号。我的印象是,提供符号确实允许它们显示在大纲树中。我可能无法控制那个棵树,但我想它至少会出现在那个里,结果我所需要的只是轮廓和面包屑的组合。
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;
    }
}