7
7
8
8
import com .intellij .javaee .ExternalResourceManager ;
9
9
import com .intellij .javaee .ExternalResourceManagerEx ;
10
+ import com .intellij .notification .NotificationGroupManager ;
11
+ import com .intellij .notification .NotificationType ;
10
12
import com .intellij .openapi .application .ApplicationManager ;
11
13
import com .intellij .openapi .project .Project ;
12
14
import com .intellij .openapi .vfs .VirtualFile ;
20
22
import com .magento .idea .magento2plugin .magento .packages .MagentoModule ;
21
23
import java .awt .event .MouseAdapter ;
22
24
import java .awt .event .MouseEvent ;
25
+ import java .util .ArrayDeque ;
23
26
import java .util .Collection ;
24
- import java .util .Stack ;
27
+ import java .util .Deque ;
25
28
import org .jetbrains .annotations .NotNull ;
26
29
import org .jetbrains .annotations .Nullable ;
27
30
28
31
class RegenerateUrnMapListener extends MouseAdapter {
29
32
protected final Project project ;
30
33
private static final String FRAMEWORK = "urn:magento:framework:" ;
31
34
private static final String MODULE = "urn:magento:module:" ;
35
+ private static final String COMPOSER_MODEL = "magento2-library" ;
32
36
33
37
34
38
public RegenerateUrnMapListener (final @ NotNull Project project ) {
@@ -43,58 +47,119 @@ public RegenerateUrnMapListener(final @NotNull Project project) {
43
47
*/
44
48
@ Override
45
49
public void mouseClicked (final MouseEvent event ) {
46
- final ExternalResourceManager externalResourceManager =
50
+ final ExternalResourceManager manager =
47
51
ExternalResourceManager .getInstance ();
48
52
final PsiManager psiManager = PsiManager .getInstance (project );
49
53
final MagentoComponentManager componentManager =
50
54
MagentoComponentManager .getInstance (project );
51
55
52
- final Collection <VirtualFile > xsdFiles = FilenameIndex .getAllFilesByExt (project , "xsd" );
53
- final Collection <MagentoComponent > components = componentManager .getAllComponents ();
54
-
55
56
ApplicationManager .getApplication ().runWriteAction (
56
57
new Runnable () {
57
58
@ Override
58
59
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 )) {
74
68
continue ;
75
69
}
76
70
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 ++;
90
72
}
73
+
74
+ showNotification (processedFileCount );
91
75
}
92
76
}
93
77
);
94
78
95
79
super .mouseClicked (event );
96
80
}
97
81
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
+
98
163
@ Nullable
99
164
protected MagentoComponent findComponentForXsd (
100
165
final @ NotNull PsiFile psiFile ,
@@ -120,7 +185,7 @@ protected String buildUrnKeyForFile(
120
185
prefix = MODULE + ((MagentoModule )magentoComponent ).getMagentoName () + ":" ;
121
186
} else {
122
187
final ComposerPackageModel composerPackageModel = magentoComponent .getComposerModel ();
123
- if ("magento2-library" .equals (composerPackageModel .getType ())) {
188
+ if (COMPOSER_MODEL .equals (composerPackageModel .getType ())) {
124
189
prefix = FRAMEWORK ;
125
190
}
126
191
}
@@ -129,7 +194,7 @@ protected String buildUrnKeyForFile(
129
194
return null ;
130
195
}
131
196
132
- final Stack <String > relativePath = new Stack <>();
197
+ final Deque <String > relativePath = new ArrayDeque <>();
133
198
relativePath .push (psiFile .getName ());
134
199
135
200
final PsiManager psiManager = magentoComponent .getDirectory ().getManager ();
@@ -144,7 +209,7 @@ protected String buildUrnKeyForFile(
144
209
}
145
210
146
211
final StringBuilder stringBuilder = new StringBuilder (prefix );
147
- while (!relativePath .empty ()) {
212
+ while (!relativePath .isEmpty ()) {
148
213
stringBuilder .append (relativePath .pop ());
149
214
}
150
215
0 commit comments