【问题标题】:Parse through a txt document using vscode extension api使用 vscode 扩展 api 解析一个 txt 文档
【发布时间】:2019-09-23 17:27:33
【问题描述】:

我正在尝试编写一个 vscode 扩展,我可以在其中打开一个 txt 文件,并逐行解析该 txt 文件的内容,并能够从每一行中搜索特定的关键词。我一直在查看 vscode api 中的 TextDocument,但我对实现它有点困惑。

如何逐行解析txt文档?

谢谢!

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:
    const { activeTextEditor } = vscode.window;
    if (!activeTextEditor) return;
    const lines = activeTextEditor.document.getText().split('\n');
    for (let i = 0; i < lines.length; i++) {
        const line = lines[i];
        console.log(i, line);
    }
    

    【讨论】:

    • 通常最好使用document.lineAt,因为它可以正确处理不同的行尾样式。例如:for (let i = 0; i &lt; activeTextEditor.document.lineCount; ++i) { const line = activeTextEditor.document.lineAt(i); ... }
    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    相关资源
    最近更新 更多