77import com .intellij .openapi .vfs .VirtualFile ;
88import com .intellij .psi .PsiElement ;
99import com .intellij .psi .PsiFile ;
10+ import com .intellij .psi .search .GlobalSearchScope ;
1011import com .intellij .psi .xml .XmlFile ;
1112import com .intellij .psi .xml .XmlTag ;
1213import com .intellij .util .Consumer ;
14+ import com .intellij .util .indexing .FileBasedIndex ;
1315import fr .adrienbrault .idea .symfony2plugin .extension .TranslatorProvider ;
1416import fr .adrienbrault .idea .symfony2plugin .extension .TranslatorProviderDict ;
17+ import fr .adrienbrault .idea .symfony2plugin .stubs .indexes .TranslationStubIndex ;
1518import fr .adrienbrault .idea .symfony2plugin .translation .TranslatorLookupElement ;
19+ import fr .adrienbrault .idea .symfony2plugin .translation .collector .YamlTranslationVisitor ;
1620import fr .adrienbrault .idea .symfony2plugin .translation .parser .DomainMappings ;
1721import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
1822import fr .adrienbrault .idea .symfony2plugin .util .PsiElementUtils ;
1923import fr .adrienbrault .idea .symfony2plugin .util .service .ServiceXmlParserFactory ;
2024import org .apache .commons .lang .StringUtils ;
2125import org .jetbrains .annotations .NotNull ;
26+ import org .jetbrains .yaml .psi .YAMLFile ;
2227import org .jetbrains .yaml .psi .YAMLScalar ;
2328import org .w3c .dom .*;
2429import org .xml .sax .SAXException ;
@@ -57,10 +62,9 @@ public class TranslationUtil {
5762 "//xliff/file/unit/segment/source"
5863 };
5964
60- static public VirtualFile [] getDomainFilePsiElements (Project project , String domainName ) {
61-
65+ public static Collection <VirtualFile > getDomainFilesFromCompiledContainer (@ NotNull Project project , @ NotNull String domainName ) {
6266 DomainMappings domainMappings = ServiceXmlParserFactory .getInstance (project , DomainMappings .class );
63- List <VirtualFile > virtualFiles = new ArrayList <>();
67+ Collection <VirtualFile > virtualFiles = new ArrayList <>();
6468
6569 for (DomainFileMap domain : domainMappings .getDomainFileMaps ()) {
6670 if (domain .getDomain ().equals (domainName )) {
@@ -71,7 +75,78 @@ static public VirtualFile[] getDomainFilePsiElements(Project project, String dom
7175 }
7276 }
7377
74- return virtualFiles .toArray (new VirtualFile [virtualFiles .size ()]);
78+ return virtualFiles ;
79+ }
80+
81+ /**
82+ * Get targets for translation based on the domain path inside the compiled container
83+ *
84+ * TODO: completely remove this? support translation paths from service compiler
85+ */
86+ public static Collection <PsiElement > getTranslationKeyFromCompiledContainerDomain (@ NotNull Project project , @ NotNull String domain , @ NotNull String translationKey ) {
87+ Collection <PsiElement > psiFoundElements = new ArrayList <>();
88+
89+ // search for available domain files
90+ for (PsiFile psiFile : PsiElementUtils .convertVirtualFilesToPsiFiles (project , TranslationUtil .getDomainFilesFromCompiledContainer (project , domain ))) {
91+ psiFoundElements .addAll (getTranslationKeyTargetInsideFile (psiFile , domain , translationKey ));
92+ }
93+
94+ return psiFoundElements ;
95+ }
96+
97+ public static boolean hasDomainInsideCompiledContainer (@ NotNull Project project , @ NotNull String domainName ) {
98+ DomainMappings domainMappings = ServiceXmlParserFactory .getInstance (project , DomainMappings .class );
99+ for (DomainFileMap domain : domainMappings .getDomainFileMaps ()) {
100+ if (domain .getDomain ().equals (domainName )) {
101+ return true ;
102+ }
103+ }
104+
105+ return false ;
106+ }
107+
108+ static public Collection <String > getDomainsFromContainer (@ NotNull Project project ) {
109+ DomainMappings domainMappings = ServiceXmlParserFactory .getInstance (project , DomainMappings .class );
110+
111+ return domainMappings .getDomainFileMaps ().stream ()
112+ .map (DomainFileMap ::getDomain )
113+ .collect (Collectors .toSet ());
114+ }
115+
116+ /**
117+ * Find a target translation key based on all supported formats
118+ */
119+ public static Collection <PsiElement > getTranslationKeyTargetInsideFile (@ NotNull PsiFile psiFile , @ NotNull String domain , @ NotNull String translationKey ) {
120+ Set <PsiElement > elements = new HashSet <>();
121+
122+ if (psiFile instanceof YAMLFile ) {
123+ // collect on yaml keys
124+ YamlTranslationVisitor .collectFileTranslations ((YAMLFile ) psiFile , (keyName , yamlKeyValue ) -> {
125+ if (keyName .equals (translationKey )) {
126+ // multiline "line values" are not resolve properly on psiElements use key as fallback target
127+ PsiElement valuePsiElement = yamlKeyValue .getValue ();
128+ elements .add (valuePsiElement != null ? valuePsiElement : yamlKeyValue );
129+
130+ return false ;
131+ }
132+
133+ return true ;
134+ });
135+ } else if (TranslationUtil .isSupportedXlfFile (psiFile )) {
136+ // fine: xlf registered as XML file. try to find source value
137+ elements .addAll (TranslationUtil .getTargetForXlfAsXmlFile ((XmlFile ) psiFile , translationKey ));
138+ } else if (("xlf" .equalsIgnoreCase (psiFile .getVirtualFile ().getExtension ()) || "xliff" .equalsIgnoreCase (psiFile .getVirtualFile ().getExtension ()))) {
139+ // xlf are plain text because not supported by jetbrains
140+ // for now we can only set file target
141+ Project project = psiFile .getProject ();
142+ elements .addAll (FileBasedIndex .getInstance ()
143+ .getValues (TranslationStubIndex .KEY , domain , GlobalSearchScope .filesScope (project , Collections .singletonList (psiFile .getVirtualFile ()))).stream ()
144+ .filter (string -> string .contains (translationKey )).map (string -> psiFile )
145+ .collect (Collectors .toList ())
146+ );
147+ }
148+
149+ return elements ;
75150 }
76151
77152 public static PsiElement [] getTranslationPsiElements (@ NotNull Project project , @ NotNull String translationKey , @ NotNull String domain ) {
0 commit comments