Skip to content

Commit 63357e4

Browse files
committed
Simply translation identification code and make interpolation inlining less aggressive
1 parent 099e295 commit 63357e4

14 files changed

+277
-480
lines changed

src/main/kotlin/translations/folding.kt

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import com.intellij.openapi.editor.FoldingGroup
3535
import com.intellij.openapi.options.BeanConfigurable
3636
import com.intellij.psi.PsiElement
3737
import org.jetbrains.uast.UCallExpression
38-
import org.jetbrains.uast.UElement
38+
import org.jetbrains.uast.UExpression
3939
import org.jetbrains.uast.textRange
4040
import org.jetbrains.uast.toUElement
4141
import org.jetbrains.uast.visitor.AbstractUastVisitor
@@ -45,7 +45,7 @@ class TranslationCodeFoldingOptionsProvider :
4545
init {
4646
title = "Minecraft"
4747
checkBox(
48-
"Translation Strings",
48+
"Translation strings",
4949
TranslationFoldingSettings.instance::shouldFoldTranslations,
5050
) {
5151
TranslationFoldingSettings.instance.shouldFoldTranslations = it
@@ -86,50 +86,51 @@ class TranslationFoldingSettings : PersistentStateComponent<TranslationFoldingSe
8686
class TranslationFoldingBuilder : FoldingBuilderEx() {
8787
override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
8888
if (ApplicationManager.getApplication().isDispatchThread) {
89-
return emptyArray()
89+
return FoldingDescriptor.EMPTY_ARRAY
9090
}
9191

9292
val descriptors = mutableListOf<FoldingDescriptor>()
93-
for (identifier in TranslationIdentifier.INSTANCES) {
94-
val uElement = root.toUElement() ?: continue
95-
val children = mutableListOf<UElement>()
96-
uElement.accept(object : AbstractUastVisitor() {
97-
override fun visitElement(node: UElement): Boolean {
98-
if (identifier.elementClass().isAssignableFrom(node.javaClass)) {
99-
children.add(node)
100-
}
101-
102-
return super.visitElement(node)
93+
val uElement = root.toUElement() ?: return FoldingDescriptor.EMPTY_ARRAY
94+
val translations = mutableListOf<TranslationInstance>()
95+
uElement.accept(object : AbstractUastVisitor() {
96+
override fun visitExpression(node: UExpression): Boolean {
97+
val translation = TranslationIdentifier.identify(node)
98+
if (translation != null) {
99+
translations += translation
103100
}
104-
})
105-
for (element in children) {
106-
val translation = identifier.identifyUnsafe(element)
107-
val foldingElement = translation?.foldingElement ?: continue
108-
val range =
109-
if (foldingElement is UCallExpression && translation.foldStart != 0) {
110-
val args = foldingElement.valueArguments.drop(translation.foldStart)
111-
val startRange = args.first().textRange ?: continue
112-
val endRange = args.last().textRange ?: continue
113-
startRange.union(endRange)
114-
} else {
115-
foldingElement.textRange ?: continue
116-
}
117-
if (!translation.required && translation.formattingError != null) {
118-
continue
101+
102+
return super.visitElement(node)
103+
}
104+
})
105+
for (translation in translations) {
106+
if (!translation.shouldFold) {
107+
continue
108+
}
109+
val foldingElement = translation.foldingElement ?: continue
110+
val range =
111+
if (foldingElement is UCallExpression && translation.foldStart != 0) {
112+
val args = foldingElement.valueArguments.drop(translation.foldStart)
113+
val startRange = args.first().textRange ?: continue
114+
val endRange = args.last().textRange ?: continue
115+
startRange.union(endRange)
116+
} else {
117+
foldingElement.textRange ?: continue
119118
}
120-
descriptors.add(
121-
FoldingDescriptor(
122-
translation.foldingElement.sourcePsi?.node!!,
123-
range,
124-
FoldingGroup.newGroup("mc.translation." + translation.key),
125-
if (translation.formattingError == TranslationInstance.Companion.FormattingError.MISSING) {
126-
"\"Insufficient parameters for formatting '${translation.text}'\""
127-
} else {
128-
"\"${translation.text}\""
129-
},
130-
),
131-
)
119+
if (!translation.required && translation.formattingError != null) {
120+
continue
132121
}
122+
descriptors.add(
123+
FoldingDescriptor(
124+
translation.foldingElement.sourcePsi?.node!!,
125+
range,
126+
FoldingGroup.newGroup("mc.translation." + translation.key),
127+
if (translation.formattingError == TranslationInstance.FormattingError.MISSING) {
128+
"\"Insufficient parameters for formatting '${translation.text}'\""
129+
} else {
130+
"\"${translation.text}\""
131+
},
132+
),
133+
)
133134
}
134135
return descriptors.toTypedArray()
135136
}

src/main/kotlin/translations/identification/LiteralTranslationIdentifier.kt

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/main/kotlin/translations/identification/ReferenceTranslationIdentifier.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)