From 64ee79fada91d05da61af2569d7fc157f9a0828f Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sat, 22 Feb 2025 19:53:48 +0200 Subject: [PATCH 1/3] 2496: Refactor file path logic and update Magento asset handling Simplified file handling by introducing utility methods and reducing redundant checks. Added support for HTML files, clarified logic for determining Magento paths, and updated action text and descriptions to better reflect functionality. Enhanced maintainability and adherence to code standards with these changes. --- .../actions/CopyMagentoPath.java | 70 +++++++++++-------- src/main/resources/META-INF/plugin.xml | 4 +- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java b/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java index 1d381b0fd..ce1e0da7c 100644 --- a/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java +++ b/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java @@ -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; @@ -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 acceptedTypes - = Arrays.asList(PHTML_EXTENSION, JS_EXTENSION, CSS_EXTENSION); + public static final String HTML_EXTENSION = "html"; private static final List SUPPORTED_IMAGE_EXTENSIONS = new ArrayList<>(Arrays.asList(ImageIO.getReaderFormatNames())); public static final String SEPARATOR = "::"; @@ -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, @@ -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. * diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index f802f43ac..52c63530e 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -151,8 +151,8 @@ + text="Magento Asset" + description="Copies Magento-formatted file asset path depending on file type"> From 03c6be99416bdb80123add27a3593fffb65e2ef0 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sat, 22 Feb 2025 19:59:25 +0200 Subject: [PATCH 2/3] 2496: Update CHANGELOG with latest fixes Added details for the "Copy Path/Reference" issue fix alongside the existing compatibility update for PhpStorm/IntelliJ 2025.*. This keeps the changelog accurate and up-to-date. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a1a26c0b..bd0d34a67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From d878f2d9b3192bbfdcd4fab3e03eaba4e3dfb0b2 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Sat, 22 Feb 2025 20:24:20 +0200 Subject: [PATCH 3/3] 2496: Fix return value for non-Magento files in path copying Updated the method to return an empty string instead of the full path for non-Magento files, ensuring consistent behavior. This also handles exceptions more cleanly by aligning the return values. --- .../magento/idea/magento2plugin/actions/CopyMagentoPath.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java b/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java index ce1e0da7c..e975df2e2 100644 --- a/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java +++ b/src/main/java/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java @@ -84,13 +84,13 @@ public CopyMagentoPath() { } else if (isMagentoFile(virtualFile)) { paths = webPaths; } else { - return fullPath.toString(); + return ""; } try { return getResultPath(virtualFile, paths, fullPath, moduleName); } catch (ArrayIndexOutOfBoundsException exception) { - return fullPath.toString(); + return ""; } }