Skip to content

Commit d120b97

Browse files
committed
Adjust color priority
1 parent 4a66f49 commit d120b97

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

src/main/java/com/tang/intellij/lua/annotator/LuaLspExternalAnnotator.kt

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ class LuaLspExternalAnnotator : ExternalAnnotator<LuaLspExternalAnnotator.Collec
3232
val document: Document
3333
)
3434

35+
companion object {
36+
val priority = HighlightSeverity.INFORMATION.myVal + 1000 // 确保最高优先级
37+
val highlight = HighlightSeverity("LuaLspHighlight", priority)
38+
}
39+
3540
override fun collectInformation(file: PsiFile): CollectedInfo? {
3641
if (file !is LuaPsiFile) return null
3742

3843
val virtualFile = file.virtualFile ?: return null
3944
val document = file.viewProvider.document ?: return null
4045

46+
// 即使存在语法错误也要继续处理
47+
// LSP 服务器通常能够处理语法错误的文件并提供高亮信息
4148
return CollectedInfo(
4249
psiFile = file,
4350
document = document,
@@ -55,26 +62,37 @@ class LuaLspExternalAnnotator : ExternalAnnotator<LuaLspExternalAnnotator.Collec
5562
.getLanguageServer("EmmyLua")
5663
.thenAccept { languageServerItem: LanguageServerItem? ->
5764
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 {
65+
try {
66+
val ls = languageServerItem.server as EmmyLuaCustomApi
67+
val annotatorInfos = ls.getAnnotator(AnnotatorParams(collectedInfo.uri))
68+
annotatorInfos.thenAccept { annotators ->
69+
if (annotators != null && annotators.isNotEmpty()) {
70+
future.complete(AnnotationResult(annotators, collectedInfo.document))
71+
} else {
72+
future.complete(null)
73+
}
74+
}.exceptionally { _ ->
75+
// 记录异常但不完全失败,允许继续处理
76+
// 语法错误不应该阻止高亮显示
6477
future.complete(null)
78+
null
6579
}
66-
}.exceptionally {
80+
} catch (_: Exception) {
81+
// 即使转换失败也不要完全放弃
6782
future.complete(null)
68-
null
6983
}
7084
} else {
7185
future.complete(null)
7286
}
87+
}.exceptionally { _ ->
88+
future.complete(null)
89+
null
7390
}
7491

75-
// 等待结果,但不要阻塞太久
76-
future.get(2, TimeUnit.SECONDS)
77-
} catch (e: Exception) {
92+
// 增加超时时间,给 LSP 服务器更多时间处理有语法错误的文件
93+
future.get(5, TimeUnit.SECONDS)
94+
} catch (_: Exception) {
95+
// 即使超时或出错,也返回 null 而不是抛出异常
7896
null
7997
}
8098
}
@@ -86,20 +104,23 @@ class LuaLspExternalAnnotator : ExternalAnnotator<LuaLspExternalAnnotator.Collec
86104
) {
87105
if (annotationResult == null) return
88106

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-
// 使用WEAK_WARNING级别,并强制使用textAttributes确保显示优先级
96-
holder.newSilentAnnotation(HighlightSeverity.WEAK_WARNING)
97-
.range(textRange)
98-
.textAttributes(textAttributesKey)
99-
.needsUpdateOnTyping(false)
100-
.create()
107+
try {
108+
for (annotator in annotationResult.annotators) {
109+
for (range in annotator.ranges) {
110+
val textRange = lspRangeToTextRange(range, annotationResult.document)
111+
if (textRange != null && textRange.startOffset >= 0 && textRange.endOffset <= annotationResult.document.textLength) {
112+
val textAttributesKey = LuaHighlightingData.getLspHighlightKey(annotator.type)
113+
holder.newSilentAnnotation(highlight)
114+
.range(textRange)
115+
.textAttributes(textAttributesKey)
116+
.needsUpdateOnTyping(false)
117+
.create()
118+
}
101119
}
102120
}
121+
} catch (_: Exception) {
122+
// 防止在应用注解时出现异常导致整个注解过程失败
123+
// 语法错误情况下文档可能不稳定,需要更加谨慎
103124
}
104125
}
105126

@@ -118,7 +139,7 @@ class LuaLspExternalAnnotator : ExternalAnnotator<LuaLspExternalAnnotator.Collec
118139
} else {
119140
null
120141
}
121-
} catch (e: Exception) {
142+
} catch (_: Exception) {
122143
null
123144
}
124145
}

0 commit comments

Comments
 (0)