2525 * @author Daniel Espendiller <daniel@espendiller.net>
2626 */
2727public class TranslationStubIndex extends FileBasedIndexExtension <String , Set <String >> {
28-
2928 public static final ID <String , Set <String >> KEY = ID .create ("fr.adrienbrault.idea.symfony2plugin.translations" );
29+ private static final StringSetDataExternalizer DATA_EXTERNALIZER = new StringSetDataExternalizer ();
3030 private final KeyDescriptor <String > myKeyDescriptor = new EnumeratorStringDescriptor ();
3131
3232 @ NotNull
@@ -43,9 +43,7 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
4343
4444 String extension = inputData .getFile ().getExtension ();
4545 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 );
4947 }
5048
5149 PsiFile psiFile = inputData .getPsiFile ();
@@ -80,48 +78,51 @@ public Map<String, Set<String>> map(@NotNull FileContent inputData) {
8078 return map ;
8179 }
8280
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"
8685 String relativePath = VfsUtil .getRelativePath (inputData .getFile (), psiFile .getProject ().getBaseDir (), '/' );
8786 if (relativePath != null ) {
88- return relativePath .contains ("/translations" );
87+ return relativePath .contains ("/translations" ) || relativePath . startsWith ( "translations/" ) ;
8988 }
9089
9190 // Resources/translations/messages.de.yml
9291 // @TODO: Resources/translations/de/messages.yml
9392 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 ());
9994 }
10095
101- private void getXlfStringMap ( FileContent inputData , Map < String , Set < String >> map ) {
102-
96+ @ NotNull
97+ private Map < String , Set < String >> getXlfStringMap ( @ NotNull FileContent inputData ) {
10398 // testing files are not that nice
10499 String relativePath = VfsUtil .getRelativePath (inputData .getFile (), inputData .getProject ().getBaseDir (), '/' );
105100 if (relativePath != null && (relativePath .contains ("/Test/" ) || relativePath .contains ("/Tests/" ) || relativePath .contains ("/Fixture/" ) || relativePath .contains ("/Fixtures/" ))) {
106- return ;
101+ return Collections . emptyMap () ;
107102 }
108103
109- String domainName = this .getDomainName (inputData .getFileName ());
104+ // extract domain name
105+ String domainName = getDomainName (inputData .getFileName ());
110106 if (domainName == null ) {
111- return ;
107+ return Collections . emptyMap () ;
112108 }
113109
114110 InputStream inputStream ;
115111 try {
116112 inputStream = inputData .getFile ().getInputStream ();
117113 } catch (IOException e ) {
118- return ;
114+ return Collections . emptyMap () ;
119115 }
120116
121117 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 ( );
124120 }
121+
122+ // wrap with domain
123+ Map <String , Set <String >> map = new THashMap <>();
124+ map .put (domainName , set );
125+ return map ;
125126 }
126127
127128 @ Nullable
@@ -149,7 +150,6 @@ public ID<String, Set<String>> getName() {
149150 return KEY ;
150151 }
151152
152-
153153 @ NotNull
154154 @ Override
155155 public KeyDescriptor <String > getKeyDescriptor () {
@@ -158,7 +158,7 @@ public KeyDescriptor<String> getKeyDescriptor() {
158158
159159 @ NotNull
160160 public DataExternalizer <Set <String >> getValueExternalizer () {
161- return new StringSetDataExternalizer () ;
161+ return DATA_EXTERNALIZER ;
162162 }
163163
164164 @ NotNull
@@ -177,5 +177,4 @@ public boolean dependsOnFileContent() {
177177 public int getVersion () {
178178 return 6 ;
179179 }
180-
181180}
0 commit comments