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 ) {
@@ -43,58 +47,119 @@ public RegenerateUrnMapListener(final @NotNull Project project) {
4347 */
4448 @ Override
4549 public void mouseClicked (final MouseEvent event ) {
46- final ExternalResourceManager externalResourceManager =
50+ final ExternalResourceManager manager =
4751 ExternalResourceManager .getInstance ();
4852 final PsiManager psiManager = PsiManager .getInstance (project );
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 () {
59-
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 ) {
69- continue ;
70- }
71-
72- final String urnKey = buildUrnKeyForFile (psiFile , xsdOwner );
73- if (urnKey == null ) {
60+ final Collection <VirtualFile > xsdFiles
61+ = FilenameIndex .getAllFilesByExt (project , "xsd" );
62+ final Collection <MagentoComponent > components
63+ = componentManager .getAllComponents ();
64+ int processedFileCount = 0 ;
65+
66+ for (final VirtualFile file : xsdFiles ) {
67+ if (handleXsdFile (file , components , psiManager , manager )) {
7468 continue ;
7569 }
7670
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- }
71+ processedFileCount ++;
9072 }
73+
74+ showNotification (processedFileCount );
9175 }
9276 }
9377 );
9478
9579 super .mouseClicked (event );
9680 }
9781
82+ /**
83+ * Handles an XSD file by associating it with a resource in the ExternalResourceManager
84+ * and resolves its context with relevant components.
85+ *
86+ * @param virtualFile The virtual file representing the XSD file to be handled.
87+ * @param components A collection of MagentoComponent objects used to determine the
88+ * component context for the XSD file.
89+ * @param psiManager The PsiManager used to resolve the virtual file into a PsiFile.
90+ * @param externalResourceManager The manager used to add or map external resources.
91+ * @return {@code true} if the XSD file was processed successfully or required no actions;
92+ * {@code false} if the file was successfully associated with a URN resource.
93+ */
94+ private boolean handleXsdFile (
95+ final VirtualFile virtualFile ,
96+ final Collection <MagentoComponent > components ,
97+ final PsiManager psiManager ,
98+ final ExternalResourceManager externalResourceManager
99+ ) {
100+ final PsiFile psiFile = psiManager .findFile (virtualFile );
101+ if (psiFile == null ) {
102+ return true ;
103+ }
104+
105+ final MagentoComponent xsdOwner =
106+ findComponentForXsd (psiFile , components );
107+ if (xsdOwner == null ) {
108+ return true ;
109+ }
110+
111+ final String urnKey = buildUrnKeyForFile (psiFile , xsdOwner );
112+ if (urnKey == null ) {
113+ return true ;
114+ }
115+
116+ // we need to attach resource to a project scope
117+ // but with ExternalResourceManager itself it's not
118+ // possible unfortunately
119+ if (externalResourceManager instanceof ExternalResourceManagerEx ) {
120+ ((ExternalResourceManagerEx ) externalResourceManager ).addResource (
121+ urnKey , virtualFile .getCanonicalPath (), project
122+ );
123+ } else {
124+ externalResourceManager .addResource (
125+ urnKey ,
126+ virtualFile .getCanonicalPath ()
127+ );
128+ }
129+ return false ;
130+ }
131+
132+ /**
133+ * Displays a notification based on the number of processed files for URN mapping generation.
134+ * If the {@code processedFileCount} is greater than zero, an information notification is shown
135+ * indicating the successful completion of URN map generation. Otherwise, a warning notification
136+ * is displayed indicating the failure of URN map generation.
137+ *
138+ * @param processedFileCount The number of files successfully processed for URN map generation.
139+ */
140+ @ SuppressWarnings ("PMD.UseNotifyAllInsteadOfNotify" )
141+ private void showNotification (final int processedFileCount ) {
142+ if (processedFileCount > 0 ) {
143+ NotificationGroupManager .getInstance ()
144+ .getNotificationGroup ("Magento Notifications" )
145+ .createNotification (
146+ "URN map generation completed" ,
147+ "Processed " + processedFileCount + " URN mappings." ,
148+ NotificationType .INFORMATION
149+ )
150+ .notify (project );
151+ } else {
152+ NotificationGroupManager .getInstance ()
153+ .getNotificationGroup ("Magento Notifications" )
154+ .createNotification (
155+ "URN map generation failed" ,
156+ "No URN mappings were generated. Check your configuration." ,
157+ NotificationType .WARNING
158+ )
159+ .notify (project );
160+ }
161+ }
162+
98163 @ Nullable
99164 protected MagentoComponent findComponentForXsd (
100165 final @ NotNull PsiFile psiFile ,
@@ -120,7 +185,7 @@ protected String buildUrnKeyForFile(
120185 prefix = MODULE + ((MagentoModule )magentoComponent ).getMagentoName () + ":" ;
121186 } else {
122187 final ComposerPackageModel composerPackageModel = magentoComponent .getComposerModel ();
123- if ("magento2-library" .equals (composerPackageModel .getType ())) {
188+ if (COMPOSER_MODEL .equals (composerPackageModel .getType ())) {
124189 prefix = FRAMEWORK ;
125190 }
126191 }
@@ -129,7 +194,7 @@ protected String buildUrnKeyForFile(
129194 return null ;
130195 }
131196
132- final Stack <String > relativePath = new Stack <>();
197+ final Deque <String > relativePath = new ArrayDeque <>();
133198 relativePath .push (psiFile .getName ());
134199
135200 final PsiManager psiManager = magentoComponent .getDirectory ().getManager ();
@@ -144,7 +209,7 @@ protected String buildUrnKeyForFile(
144209 }
145210
146211 final StringBuilder stringBuilder = new StringBuilder (prefix );
147- while (!relativePath .empty ()) {
212+ while (!relativePath .isEmpty ()) {
148213 stringBuilder .append (relativePath .pop ());
149214 }
150215
0 commit comments