我想始终解析用户更改的文件,因此我从 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/