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 ) {
@@ -49,52 +53,110 @@ public void mouseClicked(final MouseEvent event) {
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 () {
60
+ final Collection <VirtualFile > xsdFiles = FilenameIndex .getAllFilesByExt (project , "xsd" );
61
+ final Collection <MagentoComponent > components = componentManager .getAllComponents ();
62
+ int processedFileCount = 0 ;
59
63
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 )) {
69
66
continue ;
70
- }
67
+ };
71
68
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 ++;
90
70
}
71
+
72
+ showNotification (processedFileCount );
91
73
}
92
74
}
93
75
);
94
76
95
77
super .mouseClicked (event );
96
78
}
97
79
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
+
98
160
@ Nullable
99
161
protected MagentoComponent findComponentForXsd (
100
162
final @ NotNull PsiFile psiFile ,
@@ -120,7 +182,7 @@ protected String buildUrnKeyForFile(
120
182
prefix = MODULE + ((MagentoModule )magentoComponent ).getMagentoName () + ":" ;
121
183
} else {
122
184
final ComposerPackageModel composerPackageModel = magentoComponent .getComposerModel ();
123
- if ("magento2-library" .equals (composerPackageModel .getType ())) {
185
+ if (COMPOSER_MODEL .equals (composerPackageModel .getType ())) {
124
186
prefix = FRAMEWORK ;
125
187
}
126
188
}
@@ -129,7 +191,7 @@ protected String buildUrnKeyForFile(
129
191
return null ;
130
192
}
131
193
132
- final Stack <String > relativePath = new Stack <>();
194
+ final Deque <String > relativePath = new ArrayDeque <>();
133
195
relativePath .push (psiFile .getName ());
134
196
135
197
final PsiManager psiManager = magentoComponent .getDirectory ().getManager ();
@@ -144,7 +206,7 @@ protected String buildUrnKeyForFile(
144
206
}
145
207
146
208
final StringBuilder stringBuilder = new StringBuilder (prefix );
147
- while (!relativePath .empty ()) {
209
+ while (!relativePath .isEmpty ()) {
148
210
stringBuilder .append (relativePath .pop ());
149
211
}
150
212
0 commit comments