Skip to content

Commit 5472bb6

Browse files
authored
Merge pull request #2486 from vitaliyboykocontributor/2467-Urn-mappings-not-working
2467: Refactor URN map generation and add notifications.
2 parents d2b8e61 + 8056040 commit 5472bb6

File tree

3 files changed

+110
-36
lines changed

3 files changed

+110
-36
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0).
66

77
## 5.4.0
88

9+
### Added
10+
11+
- Clear notifications for the process status of URN generation [#2486](https://github.com/magento/magento2-phpstorm-plugin/pull/2486)
12+
913
### Fixed
1014

1115
- Fixed Upgrade Compatibility Tool [#2482](https://github.com/magento/magento2-phpstorm-plugin/pull/2482)
1216
Replaced hardcoded Magento versions with dynamic fetching via Packagist API.
1317
Fixed UI icon references.
1418
Updated Run command.
1519
- java.lang.Throwable: Assertion failed: Do not use PsiElement for popup model. See PsiTargetNavigator [#2485](https://github.com/magento/magento2-phpstorm-plugin/pull/2485)
20+
- Slow operations are prohibited on EDT [#2486](https://github.com/magento/magento2-phpstorm-plugin/pull/2486)
1621

1722
### Changed
1823

src/main/java/com/magento/idea/magento2plugin/project/RegenerateUrnMapListener.java

+101-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import com.intellij.javaee.ExternalResourceManager;
99
import com.intellij.javaee.ExternalResourceManagerEx;
10+
import com.intellij.notification.NotificationGroupManager;
11+
import com.intellij.notification.NotificationType;
1012
import com.intellij.openapi.application.ApplicationManager;
1113
import com.intellij.openapi.project.Project;
1214
import com.intellij.openapi.vfs.VirtualFile;
@@ -20,15 +22,17 @@
2022
import com.magento.idea.magento2plugin.magento.packages.MagentoModule;
2123
import java.awt.event.MouseAdapter;
2224
import java.awt.event.MouseEvent;
25+
import java.util.ArrayDeque;
2326
import java.util.Collection;
24-
import java.util.Stack;
27+
import java.util.Deque;
2528
import org.jetbrains.annotations.NotNull;
2629
import org.jetbrains.annotations.Nullable;
2730

2831
class 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

src/main/resources/META-INF/plugin.xml

+4
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@
688688
<searchScopesProvider implementation="com.magento.idea.magento2plugin.lang.psi.search.MagentoSearchScopesProvider"/>
689689

690690
<multiHostInjector implementation="com.magento.idea.magento2plugin.lang.injection.UiComponentSyntaxInjector"/>
691+
692+
<notificationGroup
693+
id="Magento Notifications"
694+
displayType="BALLOON" />
691695
</extensions>
692696

693697
<extensions defaultExtensionNs="com.jetbrains.php">

0 commit comments

Comments
 (0)