|
| 1 | +package com.tang.intellij.lua.annotator |
| 2 | + |
| 3 | +import com.cppcxy.ide.lsp.AnnotatorParams |
| 4 | +import com.cppcxy.ide.lsp.AnnotatorType.* |
| 5 | +import com.cppcxy.ide.lsp.EmmyLuaCustomApi |
| 6 | +import com.intellij.lang.annotation.AnnotationHolder |
| 7 | +import com.intellij.lang.annotation.ExternalAnnotator |
| 8 | +import com.intellij.lang.annotation.HighlightSeverity |
| 9 | +import com.intellij.openapi.editor.Document |
| 10 | +import com.intellij.openapi.util.TextRange |
| 11 | +import com.intellij.psi.PsiFile |
| 12 | +import com.redhat.devtools.lsp4ij.LanguageServerItem |
| 13 | +import com.redhat.devtools.lsp4ij.LanguageServerManager |
| 14 | +import com.tang.intellij.lua.highlighting.LuaHighlightingData |
| 15 | +import com.tang.intellij.lua.psi.LuaPsiFile |
| 16 | +import java.util.concurrent.CompletableFuture |
| 17 | +import java.util.concurrent.TimeUnit |
| 18 | + |
| 19 | +/** |
| 20 | + * LSP 外部注解器,优先级比普通 Annotator 更高 |
| 21 | + */ |
| 22 | +class LuaLspExternalAnnotator : ExternalAnnotator<LuaLspExternalAnnotator.CollectedInfo, LuaLspExternalAnnotator.AnnotationResult>() { |
| 23 | + |
| 24 | + data class CollectedInfo( |
| 25 | + val psiFile: PsiFile, |
| 26 | + val document: Document, |
| 27 | + val uri: String |
| 28 | + ) |
| 29 | + |
| 30 | + data class AnnotationResult( |
| 31 | + val annotators: List<com.cppcxy.ide.lsp.Annotator>, |
| 32 | + val document: Document |
| 33 | + ) |
| 34 | + |
| 35 | + override fun collectInformation(file: PsiFile): CollectedInfo? { |
| 36 | + if (file !is LuaPsiFile) return null |
| 37 | + |
| 38 | + val virtualFile = file.virtualFile ?: return null |
| 39 | + val document = file.viewProvider.document ?: return null |
| 40 | + |
| 41 | + return CollectedInfo( |
| 42 | + psiFile = file, |
| 43 | + document = document, |
| 44 | + uri = virtualFile.url |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + override fun doAnnotate(collectedInfo: CollectedInfo): AnnotationResult? { |
| 49 | + val project = collectedInfo.psiFile.project |
| 50 | + |
| 51 | + return try { |
| 52 | + val future = CompletableFuture<AnnotationResult?>() |
| 53 | + |
| 54 | + LanguageServerManager.getInstance(project) |
| 55 | + .getLanguageServer("EmmyLua") |
| 56 | + .thenAccept { languageServerItem: LanguageServerItem? -> |
| 57 | + if (languageServerItem != null) { |
| 58 | + val ls = languageServerItem.server as EmmyLuaCustomApi |
| 59 | + val annotatorInfos = ls.getAnnotator(AnnotatorParams(collectedInfo.uri)) |
| 60 | + annotatorInfos.thenAccept { annotators -> |
| 61 | + if (annotators != null) { |
| 62 | + future.complete(AnnotationResult(annotators, collectedInfo.document)) |
| 63 | + } else { |
| 64 | + future.complete(null) |
| 65 | + } |
| 66 | + }.exceptionally { |
| 67 | + future.complete(null) |
| 68 | + null |
| 69 | + } |
| 70 | + } else { |
| 71 | + future.complete(null) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // 等待结果,但不要阻塞太久 |
| 76 | + future.get(2, TimeUnit.SECONDS) |
| 77 | + } catch (e: Exception) { |
| 78 | + null |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + override fun apply( |
| 83 | + file: PsiFile, |
| 84 | + annotationResult: AnnotationResult?, |
| 85 | + holder: AnnotationHolder |
| 86 | + ) { |
| 87 | + if (annotationResult == null) return |
| 88 | + |
| 89 | + for (annotator in annotationResult.annotators) { |
| 90 | + for (range in annotator.ranges) { |
| 91 | + val textRange = lspRangeToTextRange(range, annotationResult.document) |
| 92 | + if (textRange != null) { |
| 93 | + val textAttributesKey = LuaHighlightingData.getLspHighlightKey(annotator.type) |
| 94 | + |
| 95 | + holder.newSilentAnnotation(HighlightSeverity.INFORMATION) |
| 96 | + .range(textRange) |
| 97 | + .textAttributes(textAttributesKey) |
| 98 | + .create() |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private fun lspRangeToTextRange(range: org.eclipse.lsp4j.Range, document: Document): TextRange? { |
| 105 | + return try { |
| 106 | + val startLine = range.start.line |
| 107 | + val startChar = range.start.character |
| 108 | + val endLine = range.end.line |
| 109 | + val endChar = range.end.character |
| 110 | + |
| 111 | + val startOffset = document.getLineStartOffset(startLine) + startChar |
| 112 | + val endOffset = document.getLineStartOffset(endLine) + endChar |
| 113 | + |
| 114 | + if (startOffset >= 0 && endOffset >= startOffset && endOffset <= document.textLength) { |
| 115 | + TextRange(startOffset, endOffset) |
| 116 | + } else { |
| 117 | + null |
| 118 | + } |
| 119 | + } catch (e: Exception) { |
| 120 | + null |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments