9
9
import com .intellij .psi .PsiManager ;
10
10
import com .intellij .psi .search .GlobalSearchScope ;
11
11
import com .intellij .psi .util .PsiTreeUtil ;
12
- import com .intellij .util .Processor ;
12
+ import com .intellij .psi .xml .XmlFile ;
13
+ import com .intellij .psi .xml .XmlTag ;
14
+ import com .intellij .util .Consumer ;
13
15
import com .intellij .util .indexing .FileBasedIndexImpl ;
14
16
import com .jetbrains .php .PhpIndex ;
15
17
import fr .adrienbrault .idea .symfony2plugin .stubs .SymfonyProcessors ;
43
45
import java .io .IOException ;
44
46
import java .io .InputStream ;
45
47
import java .util .*;
48
+ import java .util .stream .Collectors ;
46
49
47
50
public class TranslationUtil {
48
51
@@ -108,7 +111,7 @@ public static PsiElement[] getTranslationPsiElements(final Project project, fina
108
111
return true ;
109
112
};
110
113
111
- FileBasedIndexImpl .getInstance ().getFilesWithKey (YamlTranslationStubIndex .KEY , new HashSet <>(Arrays . asList (domain )), virtualFile -> {
114
+ FileBasedIndexImpl .getInstance ().getFilesWithKey (YamlTranslationStubIndex .KEY , new HashSet <>(Collections . singletonList (domain )), virtualFile -> {
112
115
// prevent duplicate targets and dont walk same file twice
113
116
if (virtualFilesFound .contains (virtualFile )) {
114
117
return true ;
@@ -117,23 +120,71 @@ public static PsiElement[] getTranslationPsiElements(final Project project, fina
117
120
PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
118
121
if (psiFile instanceof YAMLFile ) {
119
122
YamlTranslationVistor .collectFileTranslations ((YAMLFile ) psiFile , translationCollector );
120
- } else if ("xlf" .equalsIgnoreCase (virtualFile .getExtension ()) && psiFile != null ) {
123
+ } else if (("xlf" .equalsIgnoreCase (virtualFile .getExtension ()) || "xliff" .equalsIgnoreCase (virtualFile .getExtension ())) && psiFile instanceof XmlFile ) {
124
+ // fine: xlf registered as XML file. try to find source value
125
+ psiFoundElements .addAll (getTargetForXlfAsXmlFile ((XmlFile ) psiFile , translationKey ));
126
+ } else if (("xlf" .equalsIgnoreCase (virtualFile .getExtension ()) || "xliff" .equalsIgnoreCase (virtualFile .getExtension ()) && psiFile != null )) {
121
127
// xlf are plain text because not supported by jetbrains
122
128
// for now we can only set file target
123
- for (Set <String > string : FileBasedIndexImpl .getInstance ().getValues (YamlTranslationStubIndex .KEY , domain , GlobalSearchScope .filesScope (project , Arrays .asList (virtualFile )))) {
124
- if (string .contains (translationKey )) {
125
- psiFoundElements .add (psiFile );
126
- }
127
- }
129
+ psiFoundElements .addAll (
130
+ FileBasedIndexImpl .getInstance ().getValues (YamlTranslationStubIndex .KEY , domain , GlobalSearchScope .filesScope (project , Collections .singletonList (virtualFile )))
131
+ .stream ().filter (string -> string .contains (translationKey )).map (string -> psiFile ).collect (Collectors .toList ())
132
+ );
128
133
}
129
134
130
135
return true ;
131
136
}, GlobalSearchScope .allScope (project ));
132
137
133
-
134
138
return psiFoundElements .toArray (new PsiElement [psiFoundElements .size ()]);
135
139
}
136
140
141
+ /**
142
+ * Find targets for xlf files if registered as XML
143
+
144
+ * 1.2 xliff -> file -> body -> trans-unit -> source
145
+ * 2.0 xliff -> file -> group -> unit -> segment -> source
146
+ */
147
+ @ NotNull
148
+ public static Collection <PsiElement > getTargetForXlfAsXmlFile (@ NotNull XmlFile xmlFile , @ NotNull String key ) {
149
+ XmlTag rootTag = xmlFile .getRootTag ();
150
+ if (rootTag == null ) {
151
+ return Collections .emptyList ();
152
+ }
153
+
154
+ Collection <PsiElement > psiElements = new ArrayList <>();
155
+
156
+ // find source key
157
+ Consumer <XmlTag > consumer = xmlTag -> {
158
+ XmlTag source = xmlTag .findFirstSubTag ("source" );
159
+ if (source != null ) {
160
+ String text = source .getValue ().getText ();
161
+ if (key .equalsIgnoreCase (text )) {
162
+ psiElements .add (source );
163
+ }
164
+ }
165
+ };
166
+
167
+ for (XmlTag file : rootTag .findSubTags ("file" )) {
168
+ // version="1.2"
169
+ for (XmlTag body : file .findSubTags ("body" )) {
170
+ for (XmlTag transUnit : body .findSubTags ("trans-unit" )) {
171
+ consumer .consume (transUnit );
172
+ }
173
+ }
174
+
175
+ // version="2.0"
176
+ for (XmlTag group : file .findSubTags ("group" )) {
177
+ for (XmlTag unit : group .findSubTags ("unit" )) {
178
+ for (XmlTag segment : unit .findSubTags ("segment" )) {
179
+ consumer .consume (segment );
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ return psiElements ;
186
+ }
187
+
137
188
public static boolean hasDomain (Project project , String domainName ) {
138
189
return TranslationIndex .getInstance (project ).getTranslationMap ().getDomainList ().contains (domainName ) ||
139
190
FileBasedIndexImpl .getInstance ().getValues (
0 commit comments