Skip to content

Commit 7f39c15

Browse files
author
kzheart
committed
fix: 修复语言键使用状态不实时更新的问题
1 parent 14f9b88 commit 7f39c15

File tree

2 files changed

+82
-5
lines changed

2 files changed

+82
-5
lines changed

src/main/kotlin/org/tabooproject/development/inlay/EditorDocumentListener.kt

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class EditorDocumentListener(private val project: Project) : Disposable {
3838
val document = event.document
3939
val file = FileDocumentManager.getInstance().getFile(document)
4040

41-
if (file != null && isLanguageFile(file)) {
41+
if (file != null && (isLanguageFile(file) || isKotlinFile(file))) {
4242
// 防抖:避免频繁刷新
4343
val currentTime = System.currentTimeMillis()
4444
if (currentTime - lastRefreshTime < refreshDebounceMs) {
@@ -49,7 +49,11 @@ class EditorDocumentListener(private val project: Project) : Disposable {
4949
// 延迟刷新以合并连续的修改
5050
ApplicationManager.getApplication().invokeLater({
5151
if (!project.isDisposed) {
52-
refreshEditorsForFile(file)
52+
if (isLanguageFile(file)) {
53+
refreshEditorsForFile(file)
54+
} else if (isKotlinFile(file)) {
55+
refreshUsageCache()
56+
}
5357
}
5458
}, project.disposed)
5559
}
@@ -77,7 +81,7 @@ class EditorDocumentListener(private val project: Project) : Disposable {
7781
val openFiles = fileEditorManager.openFiles
7882

7983
for (file in openFiles) {
80-
if (isLanguageFile(file)) {
84+
if (isLanguageFile(file) || isKotlinFile(file)) {
8185
val document = FileDocumentManager.getInstance().getDocument(file)
8286
if (document != null) {
8387
registerDocument(document)
@@ -188,6 +192,40 @@ class EditorDocumentListener(private val project: Project) : Disposable {
188192
return LangFiles.isLangFile(file)
189193
}
190194

195+
/**
196+
* 检查文件是否为Kotlin文件
197+
*
198+
* @param file 要检查的文件
199+
* @return 如果是Kotlin文件返回 true
200+
*/
201+
private fun isKotlinFile(file: VirtualFile): Boolean {
202+
return file.name.endsWith(".kt")
203+
}
204+
205+
/**
206+
* 刷新使用缓存并更新相关编辑器
207+
*/
208+
private fun refreshUsageCache() {
209+
// 清除语言使用分析缓存
210+
LangUsageAnalyzer.clearCache(project)
211+
212+
// 更新编辑器通知
213+
com.intellij.ui.EditorNotifications.updateAll()
214+
215+
// 重新分析所有语言文件以更新未使用标记
216+
val fileEditorManager = FileEditorManager.getInstance(project)
217+
val psiManager = PsiManager.getInstance(project)
218+
val daemonCodeAnalyzer = com.intellij.codeInsight.daemon.DaemonCodeAnalyzer.getInstance(project)
219+
220+
// 查找所有语言文件并触发重新分析
221+
LangIndex.findProjectLangFiles(project).forEach { langFile ->
222+
val psiFile = psiManager.findFile(langFile)
223+
if (psiFile != null) {
224+
daemonCodeAnalyzer.restart(psiFile)
225+
}
226+
}
227+
}
228+
191229
override fun dispose() {
192230
// 清理所有监听器
193231
registeredDocuments.forEach { document ->

src/main/kotlin/org/tabooproject/development/inlay/LangUsageAnalyzer.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ object LangUsageAnalyzer {
2424

2525
// 缓存使用情况分析结果
2626
private val usageCache = ConcurrentHashMap<String, LangUsageResult>()
27+
// 缓存文件最后修改时间,用于检测缓存是否过期
28+
private val fileModificationCache = ConcurrentHashMap<String, Long>()
2729

2830
/**
2931
* 语言使用结果
@@ -49,8 +51,10 @@ object LangUsageAnalyzer {
4951
fun analyzeLangUsage(project: Project): LangUsageResult {
5052
val projectPath = project.basePath ?: return LangUsageResult(emptySet(), emptyMap())
5153

52-
// 检查缓存
53-
usageCache[projectPath]?.let { return it }
54+
// 检查缓存是否过期
55+
if (isCacheValid(project, projectPath)) {
56+
usageCache[projectPath]?.let { return it }
57+
}
5458

5559
val usedKeys = mutableSetOf<String>()
5660
val keyUsages = mutableMapOf<String, MutableList<LangUsageLocation>>()
@@ -86,6 +90,9 @@ object LangUsageAnalyzer {
8690
val result = LangUsageResult(usedKeys, keyUsages)
8791
usageCache[projectPath] = result
8892

93+
// 更新文件修改时间缓存
94+
updateFileModificationCache(project, projectPath)
95+
8996
return result
9097
}
9198

@@ -122,11 +129,43 @@ object LangUsageAnalyzer {
122129
val projectPath = project.basePath
123130
if (projectPath != null) {
124131
usageCache.remove(projectPath)
132+
fileModificationCache.remove(projectPath)
125133
}
126134
} else {
127135
usageCache.clear()
136+
fileModificationCache.clear()
128137
}
129138
}
139+
140+
/**
141+
* 检查缓存是否有效
142+
*/
143+
private fun isCacheValid(project: Project, projectPath: String): Boolean {
144+
if (!usageCache.containsKey(projectPath)) {
145+
return false
146+
}
147+
148+
val cachedTime = fileModificationCache[projectPath] ?: return false
149+
val currentMaxModificationTime = getCurrentMaxModificationTime(project)
150+
151+
return cachedTime >= currentMaxModificationTime
152+
}
153+
154+
/**
155+
* 获取项目中所有Kotlin文件的最大修改时间
156+
*/
157+
private fun getCurrentMaxModificationTime(project: Project): Long {
158+
val kotlinFiles = findKotlinFiles(project)
159+
return kotlinFiles.maxOfOrNull { it.timeStamp } ?: 0L
160+
}
161+
162+
/**
163+
* 更新文件修改时间缓存
164+
*/
165+
private fun updateFileModificationCache(project: Project, projectPath: String) {
166+
val maxModificationTime = getCurrentMaxModificationTime(project)
167+
fileModificationCache[projectPath] = maxModificationTime
168+
}
130169

131170
/**
132171
* 查找项目中的所有Kotlin文件

0 commit comments

Comments
 (0)