Skip to content

Commit cb6e815

Browse files
authored
Merge pull request #2497 from vitaliyboykocontributor/2496-Copy-Path/Reference-does-not-show-the-preview-value
2496: Refactor file path logic and update Magento asset handling
2 parents b0a87e7 + d878f2d commit cb6e815

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0).
88

99
### Fixed
1010

11-
- Fixed compatibility with PhpStorm/IntelliJ 2025.* [#2495](https://github.com/magento/magento2-phpstorm-plugin/pull/2495)
11+
- Compatibility with PhpStorm/IntelliJ 2025.* [#2495](https://github.com/magento/magento2-phpstorm-plugin/pull/2495)
12+
- "Copy Path/Reference" does not show the preview value [#2497](https://github.com/magento/magento2-phpstorm-plugin/pull/2497)
1213

1314
## 2025.0.0
1415

src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java

+44-30
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package com.magento.idea.magento2plugin.actions;
77

88
import com.intellij.ide.actions.CopyPathProvider;
9-
import com.intellij.openapi.actionSystem.AnActionEvent;
10-
import com.intellij.openapi.actionSystem.PlatformDataKeys;
119
import com.intellij.openapi.editor.Editor;
1210
import com.intellij.openapi.project.Project;
1311
import com.intellij.openapi.vfs.VirtualFile;
@@ -27,8 +25,7 @@ public class CopyMagentoPath extends CopyPathProvider {
2725
public static final String PHTML_EXTENSION = "phtml";
2826
public static final String JS_EXTENSION = "js";
2927
public static final String CSS_EXTENSION = "css";
30-
private final List<String> acceptedTypes
31-
= Arrays.asList(PHTML_EXTENSION, JS_EXTENSION, CSS_EXTENSION);
28+
public static final String HTML_EXTENSION = "html";
3229
private static final List<String> SUPPORTED_IMAGE_EXTENSIONS
3330
= new ArrayList<>(Arrays.asList(ImageIO.getReaderFormatNames()));
3431
public static final String SEPARATOR = "::";
@@ -57,20 +54,6 @@ public CopyMagentoPath() {
5754
SUPPORTED_IMAGE_EXTENSIONS.add("svg");
5855
}
5956

60-
@Override
61-
public void update(@NotNull final AnActionEvent event) {
62-
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
63-
if (isNotValidFile(virtualFile)) {
64-
event.getPresentation().setVisible(false);
65-
}
66-
}
67-
68-
private boolean isNotValidFile(final VirtualFile virtualFile) {
69-
return virtualFile != null && virtualFile.isDirectory()
70-
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension())
71-
&& !SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension());
72-
}
73-
7457
@Override
7558
public @Nullable String getPathToElement(
7659
final @NotNull Project project,
@@ -94,30 +77,61 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
9477
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());
9578

9679
index = -1;
97-
String[] paths;
80+
final String[] paths;
9881

9982
if (PHTML_EXTENSION.equals(virtualFile.getExtension())) {
10083
paths = templatePaths;
101-
} else if (JS_EXTENSION.equals(virtualFile.getExtension())
102-
|| CSS_EXTENSION.equals(virtualFile.getExtension())
103-
|| SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension())) {
84+
} else if (isMagentoFile(virtualFile)) {
10485
paths = webPaths;
10586
} else {
106-
return fullPath.toString();
87+
return "";
10788
}
10889

10990
try {
110-
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
111-
final int offset = paths[index].length();
112-
113-
fullPath.replace(0, endIndex + offset, "");
114-
115-
return moduleName + SEPARATOR + fullPath;
91+
return getResultPath(virtualFile, paths, fullPath, moduleName);
11692
} catch (ArrayIndexOutOfBoundsException exception) {
117-
return fullPath.toString();
93+
return "";
11894
}
11995
}
12096

97+
/**
98+
* Determines if the provided file is supported by Magento Path.
99+
*
100+
* @param virtualFile the virtual file to be checked
101+
* @return bool
102+
*/
103+
private static boolean isMagentoFile(@NotNull final VirtualFile virtualFile) {
104+
return JS_EXTENSION.equals(virtualFile.getExtension())
105+
|| CSS_EXTENSION.equals(virtualFile.getExtension())
106+
|| HTML_EXTENSION.equals(virtualFile.getExtension())
107+
|| SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension());
108+
}
109+
110+
/**
111+
* Constructs a result.
112+
*
113+
* @param virtualFile the virtual file being processed
114+
* @param paths an array of potential path segments to be checked
115+
* @param fullPath the full path of the virtual file as a mutable string builder
116+
* @param moduleName the name of the module associated with the file
117+
* @return the constructed result path
118+
*/
119+
private @NotNull String getResultPath(
120+
@NotNull final VirtualFile virtualFile,
121+
final String[] paths,
122+
final StringBuilder fullPath,
123+
final String moduleName
124+
) {
125+
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
126+
final int offset = paths[index].length();
127+
128+
fullPath.replace(0, endIndex + offset, "");
129+
130+
return PHTML_EXTENSION.equals(virtualFile.getExtension())
131+
? moduleName + SEPARATOR + fullPath
132+
: moduleName + "/" + fullPath.substring(0, fullPath.lastIndexOf("."));
133+
}
134+
121135
/**
122136
* Get index where web|template path starts in the fullPath.
123137
*

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@
151151

152152
<action id="CopyMagentoPath"
153153
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
154-
text="Magento Path"
155-
description="Copies Magento's path of file depending on file type">
154+
text="Magento Asset"
155+
description="Copies Magento-formatted file asset path depending on file type">
156156
<add-to-group group-id="CopyFileReference" anchor="last"/>
157157
</action>
158158
<group id="UctReindexMenu">

0 commit comments

Comments
 (0)