visual-studio-code - 如何在 vscode 语言服务器实现的 onDidChangeTextDocument 中获取整个文档文本?

标签 visual-studio-code vscode-extensions

我想始终解析用户更改的文件,因此我从 connection 实现了 onDidChangeTextDocument 方法。

但是这个事件只是给我 URI 和内容的变化。我怎样才能得到完整的文档?

Obs.: 我还尝试从 documents 实现 onDidChangeContent,但它从未被调用。

最佳答案

文档在传递给 onDidChangeTextDocument 的事件中。我是这样处理的:

var changeTimeout;
vscode.workspace.onDidChangeTextDocument(function (event) {
    if (changeTimeout != null)
        clearTimeout(changeTimeout);
    changeTimeout = setInterval(function () {
        clearTimeout(changeTimeout);
        changeTimeout = null;
        backend.reparse(event.document.fileName, event.document.getText());
        processDiagnostic(event.document);
    }, 500);
});

这就是 MS writes in the documentation :

// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent((change) => {
    let diagnostics: Diagnostic[] = [];
    let lines = change.document.getText().split(/\r?\n/g);
    lines.forEach((line, i) => {
        let index = line.indexOf('typescript');
        if (index >= 0) {
            diagnostics.push({
                severity: DiagnosticSeverity.Warning,
                range: {
                    start: { line: i, character: index},
                    end: { line: i, character: index + 10 }
                },
                message: `${line.substr(index, 10)} should be spelled TypeScript`,
                source: 'ex'
            });
        }
    })
    // Send the computed diagnostics to VS Code.
    connection.sendDiagnostics({ uri: change.document.uri, diagnostics });
});

因此文档(以及文本)应该在事件中可用。

关于visual-studio-code - 如何在 vscode 语言服务器实现的 onDidChangeTextDocument 中获取整个文档文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543937/

相关文章:

asp.net - 在 OSX 上开发 asp.net 会面临哪些限制(如果有)? (2015/2016年度)

visual-studio-code - VS Code 中更改的行突出显示

visual-studio-code - 在 Visual Studio Code 中自定义链接/URL 语法突出显示颜色?

visual-studio-code - 如何使用终端 API 监听 vscode 中的所有终端输出?

azure - 将python web部署到azure应用程序服务时如何选择区域?

visual-studio-code - 如何在VS Code中关闭TextDocument?

visual-studio-code - 如何找到我在 VS Code 中使用的主题的文件夹

javascript - Visual Studio Code - 跨 JS 模块的智能感知

visual-studio-code - 扩展可以在 VS Code 中贡献自定义任务吗?

visual-studio-code - 如何在 VS Code 中以编程方式获取带有光标的行?