簡體   English   中英

如何在 vscode 語言服務器實現上的 onDidChangeTextDocument 中獲取整個文檔文本?

[英]How to get the entire document text in onDidChangeTextDocument on vscode Language Server implementation?

我想始終解析用戶更改的文件,因此我從connection實現了onDidChangeTextDocument方法。

但是這個事件只是給了我 URI 和內容的變化。 我怎樣才能得到完整的文件?

onDidChangeContent 我也嘗試從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 在文檔中所寫的內容

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

所以文檔(以及文本)應該在事件中可用。

觀察:我也嘗試從文檔中實現 onDidChangeContent,但從未調用過。

我想知道的是,為什么在使用以下任何一個時onDidChangeContent停止被調用: onDidChangeTextDocumentonDidOpenTextDocumentonDidCloseTextDocument 似乎我們只能使用一種方式或另一種方式。

所以,你要找的是這個:

documents.onDidChangeContent(change => {
  connection.console.log(change.document.getText())
})

所有change.document成員: uri,languageId,version,getText,update,getLineOffsets,positionAt,offsetAt,lineCount

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM