25
25
* @author Daniel Espendiller <daniel@espendiller.net>
26
26
*/
27
27
public class TranslationStubIndex extends FileBasedIndexExtension <String , Set <String >> {
28
-
29
28
public static final ID <String , Set <String >> KEY = ID .create ("fr.adrienbrault.idea.symfony2plugin.translations" );
29
+ private static final StringSetDataExternalizer DATA_EXTERNALIZER = new StringSetDataExternalizer ();
30
30
private final KeyDescriptor <String > myKeyDescriptor = new EnumeratorStringDescriptor ();
31
31
32
32
@ NotNull
@@ -43,9 +43,7 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
43
43
44
44
String extension = inputData .getFile ().getExtension ();
45
45
if ("xlf" .equalsIgnoreCase (extension ) || "xliff" .equalsIgnoreCase (extension )) {
46
- Map <String , Set <String >> map = new THashMap <>();
47
- getXlfStringMap (inputData , map );
48
- return map ;
46
+ return getXlfStringMap (inputData );
49
47
}
50
48
51
49
PsiFile psiFile = inputData .getPsiFile ();
@@ -80,48 +78,51 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
80
78
return map ;
81
79
}
82
80
83
- private boolean isValidTranslationFile (FileContent inputData , PsiFile psiFile ) {
84
-
85
- // dont index all yaml files; "Resources/translations" should be good for now
81
+ private boolean isValidTranslationFile (@ NotNull FileContent inputData , @ NotNull PsiFile psiFile ) {
82
+ // dont index all yaml files; valid:
83
+ // - "Resources/translations"
84
+ // - "translations/[.../]foo.de.yml"
86
85
String relativePath = VfsUtil .getRelativePath (inputData .getFile (), psiFile .getProject ().getBaseDir (), '/' );
87
86
if (relativePath != null ) {
88
- return relativePath .contains ("/translations" );
87
+ return relativePath .contains ("/translations" ) || relativePath . startsWith ( "translations/" ) ;
89
88
}
90
89
91
90
// Resources/translations/messages.de.yml
92
91
// @TODO: Resources/translations/de/messages.yml
93
92
String path = inputData .getFile ().getPath ();
94
- if (path .endsWith ("/translations/" + inputData .getFileName ())) {
95
- return true ;
96
- }
97
-
98
- return false ;
93
+ return path .endsWith ("/translations/" + inputData .getFileName ());
99
94
}
100
95
101
- private void getXlfStringMap ( FileContent inputData , Map < String , Set < String >> map ) {
102
-
96
+ @ NotNull
97
+ private Map < String , Set < String >> getXlfStringMap ( @ NotNull FileContent inputData ) {
103
98
// testing files are not that nice
104
99
String relativePath = VfsUtil .getRelativePath (inputData .getFile (), inputData .getProject ().getBaseDir (), '/' );
105
100
if (relativePath != null && (relativePath .contains ("/Test/" ) || relativePath .contains ("/Tests/" ) || relativePath .contains ("/Fixture/" ) || relativePath .contains ("/Fixtures/" ))) {
106
- return ;
101
+ return Collections . emptyMap () ;
107
102
}
108
103
109
- String domainName = this .getDomainName (inputData .getFileName ());
104
+ // extract domain name
105
+ String domainName = getDomainName (inputData .getFileName ());
110
106
if (domainName == null ) {
111
- return ;
107
+ return Collections . emptyMap () ;
112
108
}
113
109
114
110
InputStream inputStream ;
115
111
try {
116
112
inputStream = inputData .getFile ().getInputStream ();
117
113
} catch (IOException e ) {
118
- return ;
114
+ return Collections . emptyMap () ;
119
115
}
120
116
121
117
Set <String > set = TranslationUtil .getXliffTranslations (inputStream );
122
- if (set .size () > 0 ) {
123
- map . put ( domainName , set );
118
+ if (set .size () == 0 ) {
119
+ return Collections . emptyMap ( );
124
120
}
121
+
122
+ // wrap with domain
123
+ Map <String , Set <String >> map = new THashMap <>();
124
+ map .put (domainName , set );
125
+ return map ;
125
126
}
126
127
127
128
@ Nullable
@@ -149,7 +150,6 @@ public ID<String, Set<String>> getName() {
149
150
return KEY ;
150
151
}
151
152
152
-
153
153
@ NotNull
154
154
@ Override
155
155
public KeyDescriptor <String > getKeyDescriptor () {
@@ -158,7 +158,7 @@ public KeyDescriptor<String> getKeyDescriptor() {
158
158
159
159
@ NotNull
160
160
public DataExternalizer <Set <String >> getValueExternalizer () {
161
- return new StringSetDataExternalizer () ;
161
+ return DATA_EXTERNALIZER ;
162
162
}
163
163
164
164
@ NotNull
@@ -177,5 +177,4 @@ public boolean dependsOnFileContent() {
177
177
public int getVersion () {
178
178
return 6 ;
179
179
}
180
-
181
180
}
0 commit comments