Skip to content

Commit eb878c3

Browse files
committed
refactor: 统一 sendLang/asLangText 调用判断逻辑
将多处重复的 sendLang/asLangText 方法调用判断逻辑提取为工具函数,简化代码并保持判断一致。
1 parent 0ca1dee commit eb878c3

File tree

5 files changed

+22
-73
lines changed

5 files changed

+22
-73
lines changed

src/main/kotlin/org/tabooproject/development/Utils.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ import java.nio.file.Path
1818
import java.nio.file.Paths
1919
import java.util.concurrent.TimeUnit
2020

21+
fun isSendLangCall(callExpression: KtCallExpression): Boolean {
22+
val calleeText = callExpression.calleeExpression?.text ?: return false
23+
24+
// 直接调用sendLang
25+
if (calleeText == "sendLang" || calleeText == "asLangText") {
26+
return true
27+
}
28+
29+
// 处理 player.sendLang 这种模式
30+
if (calleeText.endsWith(".sendLang") || calleeText.endsWith(".asLangText")) {
31+
return true
32+
}
33+
34+
// 处理其它可能的sendLang调用方式
35+
val dotIndex = calleeText.lastIndexOf('.')
36+
return dotIndex > 0 && (calleeText.substring(dotIndex + 1) == "sendLang" || calleeText.substring(dotIndex + 1) == "asLangText")
37+
}
38+
2139
fun createFileWithDirectories(baseDir: String, relativePath: String): Path? {
2240
val fullPath = Paths.get(baseDir, relativePath)
2341
val parentDir = fullPath.parent ?: return null

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class EditorDocumentListener(private val project: Project) : Disposable {
141141
*/
142142
private fun containsSendLangCalls(psiFile: com.intellij.psi.PsiFile): Boolean {
143143
return try {
144-
psiFile.text.contains("sendLang")
144+
psiFile.text.contains("sendLang") || psiFile.text.contains("asLangText")
145145
} catch (e: Exception) {
146146
true // 发生异常时默认刷新
147147
}

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.intellij.psi.PsiElement
1111
import com.intellij.psi.PsiRecursiveElementVisitor
1212
import org.jetbrains.kotlin.psi.KtCallExpression
1313
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
14+
import org.tabooproject.development.isSendLangCall
1415

1516
/**
1617
* TabooLib 语言文件代码折叠构建器
@@ -117,28 +118,6 @@ class LangFoldingBuilder : FoldingBuilderEx(), DumbAware {
117118
return "\"$finalText\""
118119
}
119120

120-
121-
/**
122-
* 判断是否是sendLang方法调用
123-
*/
124-
private fun isSendLangCall(callExpression: KtCallExpression): Boolean {
125-
val calleeText = callExpression.calleeExpression?.text ?: return false
126-
127-
// 直接调用sendLang
128-
if (calleeText == "sendLang" || calleeText == "asLangText") {
129-
return true
130-
}
131-
132-
// 处理 player.sendLang 这种模式
133-
if (calleeText.endsWith(".sendLang") || calleeText.endsWith(".asLangText")) {
134-
return true
135-
}
136-
137-
// 处理其它可能的sendLang调用方式
138-
val dotIndex = calleeText.lastIndexOf('.')
139-
return dotIndex > 0 && (calleeText.substring(dotIndex + 1) == "sendLang" || calleeText.substring(dotIndex + 1) == "asLangText")
140-
}
141-
142121
/**
143122
* 提取字符串字面量
144123
*/

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.psi.PsiElement
88
import com.intellij.psi.PsiManager
99
import org.jetbrains.kotlin.psi.KtCallExpression
1010
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
11+
import org.tabooproject.development.isSendLangCall
1112
import javax.swing.Icon
1213

1314
/**
@@ -62,31 +63,6 @@ class LangLineMarkerProvider : RelatedItemLineMarkerProvider() {
6263
result.add(builder.createLineMarkerInfo(targetElement))
6364
}
6465

65-
/**
66-
* 判断是否是sendLang方法调用
67-
*/
68-
private fun isSendLangCall(callExpression: KtCallExpression): Boolean {
69-
val calleeText = callExpression.calleeExpression?.text ?: return false
70-
71-
// 直接调用sendLang
72-
if (calleeText == "sendLang") {
73-
return true
74-
}
75-
76-
// 处理 player.sendLang 这种模式
77-
if (calleeText.endsWith(".sendLang")) {
78-
return true
79-
}
80-
81-
// 处理其它可能的sendLang调用方式
82-
val dotIndex = calleeText.lastIndexOf('.')
83-
if (dotIndex > 0 && calleeText.substring(dotIndex + 1) == "sendLang") {
84-
return true
85-
}
86-
87-
return false
88-
}
89-
9066
/**
9167
* 提取字符串字面量
9268
*/

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

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project
77
import com.intellij.psi.PsiElement
88
import org.jetbrains.kotlin.psi.KtCallExpression
99
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
10+
import org.tabooproject.development.isSendLangCall
1011
import java.awt.*
1112
import java.awt.image.BufferedImage
1213
import javax.swing.Icon
@@ -206,31 +207,6 @@ class MinecraftLangGutterProvider : LineMarkerProvider {
206207
return "翻译:${stripColorCodes(translation)}\n颜色:$colorInfo"
207208
}
208209

209-
/**
210-
* 判断是否是sendLang方法调用
211-
*/
212-
private fun isSendLangCall(callExpression: KtCallExpression): Boolean {
213-
val calleeText = callExpression.calleeExpression?.text ?: return false
214-
215-
// 直接调用sendLang
216-
if (calleeText == "sendLang") {
217-
return true
218-
}
219-
220-
// 处理 player.sendLang 这种模式
221-
if (calleeText.endsWith(".sendLang")) {
222-
return true
223-
}
224-
225-
// 处理其它可能的sendLang调用方式
226-
val dotIndex = calleeText.lastIndexOf('.')
227-
if (dotIndex > 0 && calleeText.substring(dotIndex + 1) == "sendLang") {
228-
return true
229-
}
230-
231-
return false
232-
}
233-
234210
/**
235211
* 提取字符串字面量
236212
*/

0 commit comments

Comments
 (0)