|
| 1 | +package com.tang.intellij.lua.editor |
| 2 | + |
| 3 | +import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer |
| 4 | +import com.intellij.openapi.application.ApplicationManager |
| 5 | +import com.intellij.openapi.editor.Document |
| 6 | +import com.intellij.openapi.editor.event.DocumentEvent |
| 7 | +import com.intellij.openapi.editor.event.DocumentListener |
| 8 | +import com.intellij.openapi.fileEditor.FileDocumentManager |
| 9 | +import com.intellij.openapi.fileEditor.FileEditorManager |
| 10 | +import com.intellij.openapi.fileEditor.FileEditorManagerListener |
| 11 | +import com.intellij.openapi.project.Project |
| 12 | +import com.intellij.openapi.startup.ProjectActivity |
| 13 | +import com.intellij.openapi.vfs.VirtualFile |
| 14 | +import com.intellij.psi.PsiManager |
| 15 | +import com.tang.intellij.lua.psi.LuaPsiFile |
| 16 | +import java.util.concurrent.ConcurrentHashMap |
| 17 | + |
| 18 | +/** |
| 19 | + * Manager to handle gutter cache and trigger updates |
| 20 | + */ |
| 21 | +object LuaGutterCacheManager { |
| 22 | + private val gutterCache = ConcurrentHashMap<String, List<com.cppcxy.ide.lsp.GutterInfo>>() |
| 23 | + |
| 24 | + fun clearCache(uri: String) { |
| 25 | + gutterCache.remove(uri) |
| 26 | + } |
| 27 | + |
| 28 | + fun clearAllCache() { |
| 29 | + gutterCache.clear() |
| 30 | + } |
| 31 | + |
| 32 | + fun getCache(uri: String): List<com.cppcxy.ide.lsp.GutterInfo>? { |
| 33 | + return gutterCache[uri] |
| 34 | + } |
| 35 | + |
| 36 | + fun setCache(uri: String, infos: List<com.cppcxy.ide.lsp.GutterInfo>) { |
| 37 | + gutterCache[uri] = infos |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Document listener to clear cache and restart analysis on document changes |
| 43 | + */ |
| 44 | +class LuaDocumentListener(private val project: Project) : DocumentListener { |
| 45 | + private val updateScheduler = mutableMapOf<Document, Long>() |
| 46 | + |
| 47 | + override fun documentChanged(event: DocumentEvent) { |
| 48 | + val document = event.document |
| 49 | + val file = FileDocumentManager.getInstance().getFile(document) ?: return |
| 50 | + |
| 51 | + if (file.extension != "lua") return |
| 52 | + |
| 53 | + // Clear cache immediately |
| 54 | + LuaGutterCacheManager.clearCache(file.url) |
| 55 | + |
| 56 | + // Schedule restart of code analysis (debounced to avoid too frequent updates) |
| 57 | + val now = System.currentTimeMillis() |
| 58 | + val lastUpdate = updateScheduler[document] ?: 0 |
| 59 | + |
| 60 | + // Only update if 500ms has passed since last update |
| 61 | + if (now - lastUpdate > 500) { |
| 62 | + updateScheduler[document] = now |
| 63 | + |
| 64 | + ApplicationManager.getApplication().invokeLater { |
| 65 | + val psiFile = PsiManager.getInstance(project).findFile(file) |
| 66 | + if (psiFile is LuaPsiFile) { |
| 67 | + DaemonCodeAnalyzer.getInstance(project).restart(psiFile) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * File editor listener to trigger gutter update when files are opened |
| 76 | + */ |
| 77 | +class LuaFileEditorListener(private val project: Project) : FileEditorManagerListener { |
| 78 | + override fun fileOpened(source: FileEditorManager, file: VirtualFile) { |
| 79 | + if (file.extension == "lua") { |
| 80 | + // Clear cache for newly opened file to ensure fresh data |
| 81 | + LuaGutterCacheManager.clearCache(file.url) |
| 82 | + |
| 83 | + // Trigger code analysis |
| 84 | + ApplicationManager.getApplication().invokeLater { |
| 85 | + val psiFile = PsiManager.getInstance(project).findFile(file) |
| 86 | + if (psiFile is LuaPsiFile) { |
| 87 | + DaemonCodeAnalyzer.getInstance(project).restart(psiFile) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Startup activity to register listeners |
| 96 | + */ |
| 97 | +class LuaGutterCacheStartupActivity : ProjectActivity { |
| 98 | + override suspend fun execute(project: Project) { |
| 99 | + // Register document listener |
| 100 | + val documentListener = LuaDocumentListener(project) |
| 101 | + val connection = ApplicationManager.getApplication().messageBus.connect(project) |
| 102 | + |
| 103 | + // Listen to editor creation events to attach document listener |
| 104 | + val editorFactory = com.intellij.openapi.editor.EditorFactory.getInstance() |
| 105 | + editorFactory.addEditorFactoryListener( |
| 106 | + object : com.intellij.openapi.editor.event.EditorFactoryListener { |
| 107 | + override fun editorCreated(event: com.intellij.openapi.editor.event.EditorFactoryEvent) { |
| 108 | + event.editor.document.addDocumentListener(documentListener, project) |
| 109 | + } |
| 110 | + }, |
| 111 | + project |
| 112 | + ) |
| 113 | + |
| 114 | + // Register file editor listener |
| 115 | + project.messageBus |
| 116 | + .connect() |
| 117 | + .subscribe( |
| 118 | + FileEditorManagerListener.FILE_EDITOR_MANAGER, |
| 119 | + LuaFileEditorListener(project) |
| 120 | + ) |
| 121 | + } |
| 122 | +} |
0 commit comments