Skip to content

Commit c10a337

Browse files
authored
Merge pull request #42 from aridclown/main
Bugfix: Light files should have PSI only in one project
2 parents eab5074 + 0043e57 commit c10a337

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/main/java/com/tang/intellij/lua/editor/LuaGutterCacheListener.kt

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import com.intellij.openapi.editor.Document
1111
import com.intellij.openapi.editor.EditorFactory
1212
import com.intellij.openapi.editor.event.DocumentEvent
1313
import com.intellij.openapi.editor.event.DocumentListener
14-
import com.intellij.openapi.fileEditor.FileDocumentManager
1514
import com.intellij.openapi.fileEditor.FileEditorManager
1615
import com.intellij.openapi.fileEditor.FileEditorManagerListener
16+
import com.intellij.openapi.fileEditor.FileEditorManagerListener.FILE_EDITOR_MANAGER
1717
import com.intellij.openapi.project.Project
1818
import com.intellij.openapi.startup.ProjectActivity
1919
import com.intellij.openapi.vfs.VirtualFile
2020
import com.intellij.openapi.vfs.VirtualFileManager
2121
import com.intellij.openapi.vfs.newvfs.BulkFileListener
2222
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
2323
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
24+
import com.intellij.psi.PsiDocumentManager
2425
import com.intellij.psi.PsiManager
2526
import com.tang.intellij.lua.lang.LuaFileType
2627
import com.tang.intellij.lua.psi.LuaPsiFile
@@ -38,14 +39,7 @@ object LuaGutterCacheManager {
3839
cacheTimestamps.remove(uri)
3940
}
4041

41-
fun clearAllCache() {
42-
gutterCache.clear()
43-
cacheTimestamps.clear()
44-
}
45-
46-
fun getCache(uri: String): List<GutterInfo>? {
47-
return gutterCache[uri]
48-
}
42+
fun getCache(uri: String): List<GutterInfo>? = gutterCache[uri]
4943

5044
fun setCache(uri: String, infos: List<GutterInfo>) {
5145
gutterCache[uri] = infos
@@ -57,9 +51,7 @@ object LuaGutterCacheManager {
5751
return System.currentTimeMillis() - timestamp
5852
}
5953

60-
fun isCacheStale(uri: String, maxAgeMs: Long = 1000): Boolean {
61-
return getCacheAge(uri) > maxAgeMs
62-
}
54+
fun isCacheStale(uri: String, maxAgeMs: Long = 1000): Boolean = getCacheAge(uri) > maxAgeMs
6355
}
6456

6557
/**
@@ -71,19 +63,21 @@ class LuaDocumentListener(private val project: Project) : DocumentListener {
7163

7264
override fun documentChanged(event: DocumentEvent) {
7365
val document = event.document
74-
val file = FileDocumentManager.getInstance().getFile(document) ?: return
66+
val psiDocumentManager = PsiDocumentManager.getInstance(project)
67+
val psiFile = psiDocumentManager.getCachedPsiFile(document) as? LuaPsiFile ?: return
68+
val virtualFile = psiFile.virtualFile ?: return
7569

76-
if (file.fileType !== LuaFileType.INSTANCE) return
70+
if (!virtualFile.isValid || virtualFile.fileType !== LuaFileType.INSTANCE) return
7771

78-
// Clear cache immediately for instant refresh
79-
LuaGutterCacheManager.clearCache(file.url)
72+
// Clear the cache immediately for instant refresh
73+
LuaGutterCacheManager.clearCache(virtualFile.url)
8074

8175
// Cancel any pending update
8276
pendingUpdates[document]?.let {
8377
// The runnable will be replaced
8478
}
8579

86-
// Schedule restart of code analysis with shorter debounce (200ms)
80+
// Schedule restart of code analysis with shorter debouncing (200ms)
8781
val now = System.currentTimeMillis()
8882
val lastUpdate = updateScheduler[document] ?: 0
8983

@@ -92,9 +86,10 @@ class LuaDocumentListener(private val project: Project) : DocumentListener {
9286

9387
val updateRunnable = Runnable {
9488
ApplicationManager.getApplication().invokeLater {
95-
val psiFile = PsiManager.getInstance(project).findFile(file)
96-
if (psiFile is LuaPsiFile && psiFile.isValid) {
97-
DaemonCodeAnalyzer.getInstance(project).restart(psiFile)
89+
if (project.isDisposed) return@invokeLater
90+
val latestPsi = psiDocumentManager.getPsiFile(document)
91+
if (latestPsi is LuaPsiFile && latestPsi.isValid) {
92+
DaemonCodeAnalyzer.getInstance(project).restart(latestPsi)
9893
}
9994
}
10095
}
@@ -127,7 +122,7 @@ class LuaDocumentListener(private val project: Project) : DocumentListener {
127122
class LuaFileEditorListener(private val project: Project) : FileEditorManagerListener {
128123
override fun fileOpened(source: FileEditorManager, file: VirtualFile) {
129124
if (file.fileType === LuaFileType.INSTANCE) {
130-
// Clear cache for newly opened file to ensure fresh data
125+
// Clear cache for the newly opened file to ensure fresh data
131126
LuaGutterCacheManager.clearCache(file.url)
132127

133128
// Trigger code analysis
@@ -147,28 +142,25 @@ class LuaFileEditorListener(private val project: Project) : FileEditorManagerLis
147142
class LuaGutterCacheStartupActivity : ProjectActivity {
148143
override suspend fun execute(project: Project) {
149144
val parentDisposable = project.service<LuaGutterCacheListenerDisposable>()
150-
145+
151146
// Prevent duplicate registration
152147
if (parentDisposable.isInitialized) {
153148
return
154149
}
155150
parentDisposable.isInitialized = true
156-
151+
157152
// Register document listener
158153
val documentListener = LuaDocumentListener(project)
159154
val appConnection = ApplicationManager.getApplication().messageBus.connect(parentDisposable)
160155
val projectConnection = project.messageBus.connect(parentDisposable)
161156

162-
// Listen to editor creation events to attach document listener
157+
// Listen to editor creation events to attach the document listener
163158
EditorFactory.getInstance()
164159
.eventMulticaster
165160
.addDocumentListener(documentListener, parentDisposable)
166161

167162
// Register file editor listener
168-
projectConnection.subscribe(
169-
FileEditorManagerListener.FILE_EDITOR_MANAGER,
170-
LuaFileEditorListener(project)
171-
)
163+
projectConnection.subscribe(FILE_EDITOR_MANAGER, LuaFileEditorListener(project))
172164

173165
// Register bulk file listener to detect external changes
174166
appConnection.subscribe(
@@ -178,7 +170,7 @@ class LuaGutterCacheStartupActivity : ProjectActivity {
178170
for (event in events) {
179171
val file = event.file
180172
if (file != null && file.isValid && file.fileType === LuaFileType.INSTANCE) {
181-
// Clear cache when file changes externally
173+
// Clear cache when the file changes externally
182174
LuaGutterCacheManager.clearCache(file.url)
183175

184176
// Skip delete events - no need to restart analysis on deleted files
@@ -188,7 +180,7 @@ class LuaGutterCacheStartupActivity : ProjectActivity {
188180

189181
// Trigger update
190182
ApplicationManager.getApplication().invokeLater {
191-
// Double-check file is still valid when callback executes
183+
// Double-check file is still valid when the callback executes
192184
if (file.isValid) {
193185
val psiFile = PsiManager.getInstance(project).findFile(file)
194186
if (psiFile is LuaPsiFile && psiFile.isValid) {
@@ -208,7 +200,7 @@ class LuaGutterCacheStartupActivity : ProjectActivity {
208200
class LuaGutterCacheListenerDisposable : Disposable {
209201
@Volatile
210202
var isInitialized: Boolean = false
211-
203+
212204
override fun dispose() {
213205
isInitialized = false
214206
}

0 commit comments

Comments
 (0)