77
88import com .intellij .javaee .ExternalResourceManager ;
99import com .intellij .javaee .ExternalResourceManagerEx ;
10+ import com .intellij .notification .NotificationGroupManager ;
11+ import com .intellij .notification .NotificationType ;
1012import com .intellij .openapi .application .ApplicationManager ;
1113import com .intellij .openapi .project .Project ;
1214import com .intellij .openapi .vfs .VirtualFile ;
2022import com .magento .idea .magento2plugin .magento .packages .MagentoModule ;
2123import java .awt .event .MouseAdapter ;
2224import java .awt .event .MouseEvent ;
25+ import java .util .ArrayDeque ;
2326import java .util .Collection ;
24- import java .util .Stack ;
27+ import java .util .Deque ;
2528import org .jetbrains .annotations .NotNull ;
2629import org .jetbrains .annotations .Nullable ;
2730
2831class RegenerateUrnMapListener extends MouseAdapter {
2932 protected final Project project ;
3033 private static final String FRAMEWORK = "urn:magento:framework:" ;
3134 private static final String MODULE = "urn:magento:module:" ;
35+ private static final String COMPOSER_MODEL = "magento2-library" ;
3236
3337
3438 public RegenerateUrnMapListener (final @ NotNull Project project ) {
@@ -49,52 +53,110 @@ public void mouseClicked(final MouseEvent event) {
4953 final MagentoComponentManager componentManager =
5054 MagentoComponentManager .getInstance (project );
5155
52- final Collection <VirtualFile > xsdFiles = FilenameIndex .getAllFilesByExt (project , "xsd" );
53- final Collection <MagentoComponent > components = componentManager .getAllComponents ();
54-
5556 ApplicationManager .getApplication ().runWriteAction (
5657 new Runnable () {
5758 @ Override
5859 public void run () {
60+ final Collection <VirtualFile > xsdFiles = FilenameIndex .getAllFilesByExt (project , "xsd" );
61+ final Collection <MagentoComponent > components = componentManager .getAllComponents ();
62+ int processedFileCount = 0 ;
5963
60- for (final VirtualFile virtualFile : xsdFiles ) {
61- final PsiFile psiFile = psiManager .findFile (virtualFile );
62- if (psiFile == null ) {
63- continue ;
64- }
65-
66- final MagentoComponent xsdOwner =
67- findComponentForXsd (psiFile , components );
68- if (xsdOwner == null ) {
64+ for (final VirtualFile virtualFile : xsdFiles ) {
65+ if (handleXsdFile (virtualFile , components , psiManager , externalResourceManager )) {
6966 continue ;
70- }
67+ };
7168
72- final String urnKey = buildUrnKeyForFile (psiFile , xsdOwner );
73- if (urnKey == null ) {
74- continue ;
75- }
76-
77- // we need to attach resource to a project scope
78- // but with ExternalResourceManager itself it's not
79- // possible unfortunately
80- if (externalResourceManager instanceof ExternalResourceManagerEx ) {
81- ((ExternalResourceManagerEx )externalResourceManager ).addResource (
82- urnKey , virtualFile .getCanonicalPath (), project
83- );
84- } else {
85- externalResourceManager .addResource (
86- urnKey ,
87- virtualFile .getCanonicalPath ()
88- );
89- }
69+ processedFileCount ++;
9070 }
71+
72+ showNotification (processedFileCount );
9173 }
9274 }
9375 );
9476
9577 super .mouseClicked (event );
9678 }
9779
80+ /**
81+ * Handles an XSD file by associating it with a resource in the ExternalResourceManager
82+ * and resolves its context with relevant components.
83+ *
84+ * @param virtualFile The virtual file representing the XSD file to be handled.
85+ * @param components A collection of MagentoComponent objects used to determine the
86+ * component context for the XSD file.
87+ * @param psiManager The PsiManager used to resolve the virtual file into a PsiFile.
88+ * @param externalResourceManager The manager used to add or map external resources.
89+ * @return {@code true} if the XSD file was processed successfully or required no actions;
90+ * {@code false} if the file was successfully associated with a URN resource.
91+ */
92+ private boolean handleXsdFile (
93+ final VirtualFile virtualFile ,
94+ final Collection <MagentoComponent > components ,
95+ final PsiManager psiManager ,
96+ final ExternalResourceManager externalResourceManager
97+ ) {
98+ final PsiFile psiFile = psiManager .findFile (virtualFile );
99+ if (psiFile == null ) {
100+ return true ;
101+ }
102+
103+ final MagentoComponent xsdOwner =
104+ findComponentForXsd (psiFile , components );
105+ if (xsdOwner == null ) {
106+ return true ;
107+ }
108+
109+ final String urnKey = buildUrnKeyForFile (psiFile , xsdOwner );
110+ if (urnKey == null ) {
111+ return true ;
112+ }
113+
114+ // we need to attach resource to a project scope
115+ // but with ExternalResourceManager itself it's not
116+ // possible unfortunately
117+ if (externalResourceManager instanceof ExternalResourceManagerEx ) {
118+ ((ExternalResourceManagerEx ) externalResourceManager ).addResource (
119+ urnKey , virtualFile .getCanonicalPath (), project
120+ );
121+ } else {
122+ externalResourceManager .addResource (
123+ urnKey ,
124+ virtualFile .getCanonicalPath ()
125+ );
126+ }
127+ return false ;
128+ }
129+
130+ /**
131+ * Displays a notification based on the number of processed files for URN mapping generation.
132+ * If the {@code processedFileCount} is greater than zero, an information notification is shown
133+ * indicating the successful completion of URN map generation. Otherwise, a warning notification
134+ * is displayed indicating the failure of URN map generation.
135+ *
136+ * @param processedFileCount The number of files successfully processed for URN mapping generation.
137+ */
138+ private void showNotification (final int processedFileCount ) {
139+ if (processedFileCount > 0 ) {
140+ NotificationGroupManager .getInstance ()
141+ .getNotificationGroup ("Magento Notifications" )
142+ .createNotification (
143+ "URN map generation completed" ,
144+ "Processed " + processedFileCount + " URN mappings." ,
145+ NotificationType .INFORMATION
146+ )
147+ .notify (project );
148+ } else {
149+ NotificationGroupManager .getInstance ()
150+ .getNotificationGroup ("Magento Notifications" )
151+ .createNotification (
152+ "URN map generation failed" ,
153+ "No URN mappings were generated. Check your configuration." ,
154+ NotificationType .WARNING
155+ )
156+ .notify (project );
157+ }
158+ }
159+
98160 @ Nullable
99161 protected MagentoComponent findComponentForXsd (
100162 final @ NotNull PsiFile psiFile ,
@@ -120,7 +182,7 @@ protected String buildUrnKeyForFile(
120182 prefix = MODULE + ((MagentoModule )magentoComponent ).getMagentoName () + ":" ;
121183 } else {
122184 final ComposerPackageModel composerPackageModel = magentoComponent .getComposerModel ();
123- if ("magento2-library" .equals (composerPackageModel .getType ())) {
185+ if (COMPOSER_MODEL .equals (composerPackageModel .getType ())) {
124186 prefix = FRAMEWORK ;
125187 }
126188 }
@@ -129,7 +191,7 @@ protected String buildUrnKeyForFile(
129191 return null ;
130192 }
131193
132- final Stack <String > relativePath = new Stack <>();
194+ final Deque <String > relativePath = new ArrayDeque <>();
133195 relativePath .push (psiFile .getName ());
134196
135197 final PsiManager psiManager = magentoComponent .getDirectory ().getManager ();
@@ -144,7 +206,7 @@ protected String buildUrnKeyForFile(
144206 }
145207
146208 final StringBuilder stringBuilder = new StringBuilder (prefix );
147- while (!relativePath .empty ()) {
209+ while (!relativePath .isEmpty ()) {
148210 stringBuilder .append (relativePath .pop ());
149211 }
150212
0 commit comments