@@ -29,6 +29,8 @@ class ZyLspClient(private val project: Project) : LanguageClient {
2929 private var serverCapabilities: ServerCapabilities ? = null
3030 // 已同步到 LSP 的文档 URI 集合,用于避免重复 didOpen
3131 private val openedDocuments = mutableSetOf<String >()
32+ // 文档版本号,用于跟踪文档变更
33+ private val documentVersions = mutableMapOf<String , Int >()
3234
3335 /* *
3436 * 检查进程状态,确保 LSP 服务器仍在运行
@@ -47,6 +49,7 @@ class ZyLspClient(private val project: Project) : LanguageClient {
4749 private fun cleanupState () {
4850 isInitialized = false
4951 openedDocuments.clear()
52+ documentVersions.clear()
5053 serverCapabilities = null
5154 }
5255
@@ -211,15 +214,51 @@ class ZyLspClient(private val project: Project) : LanguageClient {
211214 }
212215 if (openedDocuments.contains(uri)) return
213216 try {
214- val item = TextDocumentItem (uri, languageId, /* version*/ 1 , text)
217+ val version = documentVersions.getOrPut(uri) { 1 }
218+ val item = TextDocumentItem (uri, languageId, version, text)
215219 languageServer!! .textDocumentService.didOpen(DidOpenTextDocumentParams (item))
216220 openedDocuments.add(uri)
217- LOG .debug(" didOpen sent for uri=$uri , bytes=${text.toByteArray().size} " )
221+ LOG .debug(" didOpen sent for uri=$uri , version= $version , bytes=${text.toByteArray().size} " )
218222 } catch (e: Exception ) {
219223 LOG .debug(" Failed to send didOpen for uri=$uri " , e)
220224 }
221225 }
222226
227+ /* *
228+ * 通知 LSP 服务器文档内容变更
229+ */
230+ fun didChange (uri : String , newText : String ) {
231+ if (! isInitialized || languageServer == null || ! isProcessAlive()) {
232+ if (! isProcessAlive()) {
233+ LOG .warn(" LSP process is dead, cannot send didChange" )
234+ cleanupState()
235+ }
236+ return
237+ }
238+ if (! openedDocuments.contains(uri)) {
239+ LOG .debug(" Document not opened, skipping didChange for uri=$uri " )
240+ return
241+ }
242+
243+ try {
244+ val version = documentVersions.getOrPut(uri) { 1 }
245+ documentVersions[uri] = version + 1
246+
247+ // 使用全量变更(简单实现)
248+ val changes = listOf (TextDocumentContentChangeEvent (newText))
249+
250+ val params = DidChangeTextDocumentParams (
251+ VersionedTextDocumentIdentifier (uri, documentVersions[uri]!! ),
252+ changes
253+ )
254+
255+ languageServer!! .textDocumentService.didChange(params)
256+ LOG .debug(" didChange sent for uri=$uri , version=${documentVersions[uri]} " )
257+ } catch (e: Exception ) {
258+ LOG .debug(" Failed to send didChange for uri=$uri " , e)
259+ }
260+ }
261+
223262 /* *
224263 * 解析 LSP 可执行文件路径
225264 */
0 commit comments