Skip to content

Commit 4e9659c

Browse files
committed
跳转
1 parent 82ca917 commit 4e9659c

7 files changed

Lines changed: 119 additions & 2 deletions

File tree

src/main/kotlin/com/company/plugin/completion/ZyCompletionContributor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class ZyCompletionContributor : CompletionContributor() {
6262
val text = document.text
6363
lspService.ensureDidOpen(uri, "zy", text)
6464

65+
// 通知 LSP 文档变更(确保同步最新内容)
66+
lspService.notifyDocumentChange(uri, text)
67+
6568
LOG.debug("Requesting completion at ${file.name}:${lineNumber}:${character}")
6669

6770
val completionFuture = lspService.getCompletion(uri, lineNumber, character)

src/main/kotlin/com/company/plugin/lsp/ZyDocumentListener.kt

Whitespace-only changes.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.company.plugin.lsp
2+
3+
import com.intellij.openapi.diagnostic.Logger
4+
import com.intellij.openapi.editor.Document
5+
import com.intellij.openapi.editor.event.DocumentEvent
6+
import com.intellij.openapi.editor.event.DocumentListener
7+
import com.intellij.openapi.fileEditor.FileDocumentManager
8+
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.startup.ProjectActivity
10+
11+
/**
12+
* ZY 文档同步组件
13+
* 监听文档变更并通知 LSP 服务器
14+
*/
15+
class ZyDocumentSyncComponent : ProjectActivity {
16+
17+
companion object {
18+
private val LOG = Logger.getInstance(ZyDocumentSyncComponent::class.java)
19+
}
20+
21+
override suspend fun execute(project: Project) {
22+
// 添加全局文档监听器
23+
val documentListener = object : DocumentListener {
24+
override fun documentChanged(event: DocumentEvent) {
25+
handleDocumentChange(project, event)
26+
}
27+
}
28+
29+
// 使用 EditorFactory 添加全局监听器
30+
com.intellij.openapi.editor.EditorFactory.getInstance()
31+
.eventMulticaster.addDocumentListener(documentListener, project)
32+
33+
LOG.info("ZY document sync component initialized for project: ${project.name}")
34+
}
35+
36+
private fun handleDocumentChange(project: Project, event: DocumentEvent) {
37+
try {
38+
val document = event.document
39+
val file = FileDocumentManager.getInstance().getFile(document)
40+
41+
// 只处理 .zy 文件
42+
if (file == null || !file.name.endsWith(".zy")) {
43+
return
44+
}
45+
46+
val lspService = project.getService(ZyLspService::class.java)
47+
if (lspService == null || !lspService.isStarted()) {
48+
return
49+
}
50+
51+
// 通知 LSP 服务器文档变更
52+
val uri = file.url
53+
val newText = document.text
54+
lspService.notifyDocumentChange(uri, newText)
55+
56+
LOG.debug("Document change notified to LSP: ${file.name}")
57+
58+
} catch (e: Exception) {
59+
LOG.debug("Error handling document change", e)
60+
}
61+
}
62+
}

src/main/kotlin/com/company/plugin/lsp/ZyLspClient.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

src/main/kotlin/com/company/plugin/lsp/ZyLspService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ class ZyLspService(private val project: Project) {
9191
lspClient?.ensureDidOpen(uri, languageId, text)
9292
}
9393

94+
/**
95+
* 通知文档内容变更
96+
*/
97+
fun notifyDocumentChange(uri: String, newText: String) {
98+
lspClient?.didChange(uri, newText)
99+
}
100+
94101
/**
95102
* 检查 LSP 服务是否已启动
96103
*/

src/main/kotlin/com/company/plugin/navigation/ZyGotoDeclarationHandler.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class ZyGotoDeclarationHandler : GotoDeclarationHandler {
6161
// 确保文档已同步到LSP
6262
val text = document.text
6363
lspService.ensureDidOpen(uri, "zy", text)
64+
65+
// 通知 LSP 文档变更(确保同步最新内容)
66+
lspService.notifyDocumentChange(uri, text)
6467

6568
LOG.debug("Requesting definition for uri=$uri, line=$lineNumber, char=$character")
6669
val future = lspService.getDefinition(uri, lineNumber, character)

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747

4848
<!-- Project startup activity -->
4949
<postStartupActivity implementation="com.company.plugin.lsp.ZyProjectStartupActivity" />
50+
51+
<!-- Document sync activity -->
52+
<postStartupActivity implementation="com.company.plugin.lsp.ZyDocumentSyncComponent" />
5053

5154
<!-- Enter handler: 保持作用域内缩进 -->
5255
<enterHandlerDelegate implementation="com.company.plugin.language.ZyEnterHandler" order="first"/>

0 commit comments

Comments
 (0)