如何在 vscode Language Server 实现的 onDidChangeTextDocument 中获取整个文档文本?

thu*_*hur 3 visual-studio-code vscode-extensions

我想解析用户始终更改的文件,因此我实现onDidChangeTextDocumentconnection.

但这个事件只给我 URI 和内容更改。我怎样才能获得完整的文档?

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

Mik*_*hke 5

该文档在事件中传递给 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);
});
Run Code Online (Sandbox Code Playgroud)

这是 MS在文档中写的

// 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 });
});
Run Code Online (Sandbox Code Playgroud)

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