Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0).

### Fixed

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

## 2025.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
package com.magento.idea.magento2plugin.actions;

import com.intellij.ide.actions.CopyPathProvider;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
Expand All @@ -27,8 +25,7 @@ public class CopyMagentoPath extends CopyPathProvider {
public static final String PHTML_EXTENSION = "phtml";
public static final String JS_EXTENSION = "js";
public static final String CSS_EXTENSION = "css";
private final List<String> acceptedTypes
= Arrays.asList(PHTML_EXTENSION, JS_EXTENSION, CSS_EXTENSION);
public static final String HTML_EXTENSION = "html";
private static final List<String> SUPPORTED_IMAGE_EXTENSIONS
= new ArrayList<>(Arrays.asList(ImageIO.getReaderFormatNames()));
public static final String SEPARATOR = "::";
Expand Down Expand Up @@ -57,20 +54,6 @@ public CopyMagentoPath() {
SUPPORTED_IMAGE_EXTENSIONS.add("svg");
}

@Override
public void update(@NotNull final AnActionEvent event) {
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
if (isNotValidFile(virtualFile)) {
event.getPresentation().setVisible(false);
}
}

private boolean isNotValidFile(final VirtualFile virtualFile) {
return virtualFile != null && virtualFile.isDirectory()
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension())
&& !SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension());
}

@Override
public @Nullable String getPathToElement(
final @NotNull Project project,
Expand All @@ -94,30 +77,61 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());

index = -1;
String[] paths;
final String[] paths;

if (PHTML_EXTENSION.equals(virtualFile.getExtension())) {
paths = templatePaths;
} else if (JS_EXTENSION.equals(virtualFile.getExtension())
|| CSS_EXTENSION.equals(virtualFile.getExtension())
|| SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension())) {
} else if (isMagentoFile(virtualFile)) {
paths = webPaths;
} else {
return fullPath.toString();
}

try {
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
final int offset = paths[index].length();

fullPath.replace(0, endIndex + offset, "");

return moduleName + SEPARATOR + fullPath;
return getResultPath(virtualFile, paths, fullPath, moduleName);
} catch (ArrayIndexOutOfBoundsException exception) {
return fullPath.toString();
}
}

/**
* Determines if the provided file is supported by Magento Path.
*
* @param virtualFile the virtual file to be checked
* @return bool
*/
private static boolean isMagentoFile(@NotNull final VirtualFile virtualFile) {
return JS_EXTENSION.equals(virtualFile.getExtension())
|| CSS_EXTENSION.equals(virtualFile.getExtension())
|| HTML_EXTENSION.equals(virtualFile.getExtension())
|| SUPPORTED_IMAGE_EXTENSIONS.contains(virtualFile.getExtension());
}

/**
* Constructs a result.
*
* @param virtualFile the virtual file being processed
* @param paths an array of potential path segments to be checked
* @param fullPath the full path of the virtual file as a mutable string builder
* @param moduleName the name of the module associated with the file
* @return the constructed result path
*/
private @NotNull String getResultPath(
@NotNull final VirtualFile virtualFile,
final String[] paths,
final StringBuilder fullPath,
final String moduleName
) {
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
final int offset = paths[index].length();

fullPath.replace(0, endIndex + offset, "");

return PHTML_EXTENSION.equals(virtualFile.getExtension())
? moduleName + SEPARATOR + fullPath
: moduleName + "/" + fullPath.substring(0, fullPath.lastIndexOf("."));
}

/**
* Get index where web|template path starts in the fullPath.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@

<action id="CopyMagentoPath"
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
text="Magento Path"
description="Copies Magento's path of file depending on file type">
text="Magento Asset"
description="Copies Magento-formatted file asset path depending on file type">
<add-to-group group-id="CopyFileReference" anchor="last"/>
</action>
<group id="UctReindexMenu">
Expand Down
Loading