@@ -35,7 +35,7 @@ import com.intellij.openapi.editor.FoldingGroup
35
35
import com.intellij.openapi.options.BeanConfigurable
36
36
import com.intellij.psi.PsiElement
37
37
import org.jetbrains.uast.UCallExpression
38
- import org.jetbrains.uast.UElement
38
+ import org.jetbrains.uast.UExpression
39
39
import org.jetbrains.uast.textRange
40
40
import org.jetbrains.uast.toUElement
41
41
import org.jetbrains.uast.visitor.AbstractUastVisitor
@@ -45,7 +45,7 @@ class TranslationCodeFoldingOptionsProvider :
45
45
init {
46
46
title = " Minecraft"
47
47
checkBox(
48
- " Translation Strings " ,
48
+ " Translation strings " ,
49
49
TranslationFoldingSettings .instance::shouldFoldTranslations,
50
50
) {
51
51
TranslationFoldingSettings .instance.shouldFoldTranslations = it
@@ -86,50 +86,51 @@ class TranslationFoldingSettings : PersistentStateComponent<TranslationFoldingSe
86
86
class TranslationFoldingBuilder : FoldingBuilderEx () {
87
87
override fun buildFoldRegions (root : PsiElement , document : Document , quick : Boolean ): Array <FoldingDescriptor > {
88
88
if (ApplicationManager .getApplication().isDispatchThread) {
89
- return emptyArray()
89
+ return FoldingDescriptor . EMPTY_ARRAY
90
90
}
91
91
92
92
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
103
100
}
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
119
118
}
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
132
121
}
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
+ )
133
134
}
134
135
return descriptors.toTypedArray()
135
136
}
0 commit comments