From 906a192d56f6f9e637831622d12fd44e68ce34ec Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Sun, 11 Apr 2021 17:04:18 +0530 Subject: [PATCH 1/3] Added JS support for Copy Magento Path --- .../actions/CopyMagentoPath.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java index 1e88d84f4..a8cbbb8f7 100644 --- a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java +++ b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java @@ -18,10 +18,16 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.List; + public class CopyMagentoPath extends CopyPathProvider { public static final String PHTML = "phtml"; - public static final String PHTML_SEPARATOR = "::"; + public static final String JS = "js"; + private final List acceptedTypes = Arrays.asList(PHTML, JS); + public static final String SEPARATOR = "::"; private int index; + private final String[] templatePaths = { "view/frontend/templates/", "view/adminhtml/templates/", @@ -29,15 +35,25 @@ public class CopyMagentoPath extends CopyPathProvider { "templates/" }; + private final String[] jsPaths = { + "view/frontend/web/", + "view/adminhtml/web/", + "view/base/web/" + }; + @Override public void update(@NotNull final AnActionEvent event) { final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE); - if (virtualFile != null && virtualFile.isDirectory() - || virtualFile != null && !PHTML.equals(virtualFile.getExtension())) { + if (isNotValidFile(virtualFile)) { event.getPresentation().setVisible(false); } } + private boolean isNotValidFile(VirtualFile virtualFile) { + return virtualFile != null && virtualFile.isDirectory() + || virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension()); + } + @Nullable @Override public String getPathToElement( @@ -65,11 +81,20 @@ public String getPathToElement( if (PHTML.equals(virtualFile.getExtension())) { index = -1; - final int endIndex = getIndexOf(fullPath, templatePaths[++index]); + final int endIndex = getIndexOf(fullPath, templatePaths[++index], templatePaths); final int offset = templatePaths[index].length(); fullPath.replace(0, endIndex + offset, ""); - magentoPath.append(PHTML_SEPARATOR); + magentoPath.append(SEPARATOR); + magentoPath.append(fullPath); + path = magentoPath.toString(); + } else if (JS.equals(virtualFile.getExtension())) { + index = -1; + final int endIndex = getIndexOf(fullPath, jsPaths[++index], jsPaths); + final int offset = jsPaths[index].length(); + + fullPath.replace(0, endIndex + offset, ""); + magentoPath.append(SEPARATOR); magentoPath.append(fullPath); path = magentoPath.toString(); } @@ -77,9 +102,9 @@ public String getPathToElement( return path; } - private int getIndexOf(final StringBuilder fullPath, final String path) { + private int getIndexOf(final StringBuilder fullPath, final String path, final String[] paths) { return fullPath.lastIndexOf(path) == -1 - ? getIndexOf(fullPath, templatePaths[++index]) + ? getIndexOf(fullPath, paths[++index], paths) : fullPath.lastIndexOf(path); } } From c053c608e158e5816000573a69c640b7b399ddfe Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Sun, 11 Apr 2021 17:32:38 +0530 Subject: [PATCH 2/3] Added CSS support for Copy Magento Path --- .../actions/CopyMagentoPath.java | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java index a8cbbb8f7..212543c33 100644 --- a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java +++ b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java @@ -24,7 +24,8 @@ public class CopyMagentoPath extends CopyPathProvider { public static final String PHTML = "phtml"; public static final String JS = "js"; - private final List acceptedTypes = Arrays.asList(PHTML, JS); + public static final String CSS = "css"; + private final List acceptedTypes = Arrays.asList(PHTML, JS, CSS); public static final String SEPARATOR = "::"; private int index; @@ -35,10 +36,11 @@ public class CopyMagentoPath extends CopyPathProvider { "templates/" }; - private final String[] jsPaths = { + private final String[] webPaths = { "view/frontend/web/", "view/adminhtml/web/", - "view/base/web/" + "view/base/web/", + "web/" }; @Override @@ -77,29 +79,24 @@ public String getPathToElement( final StringBuilder fullPath = new StringBuilder(virtualFile.getPath()); final StringBuilder magentoPath = new StringBuilder(moduleName); - String path = fullPath.toString(); + + index = -1; + String[] paths; if (PHTML.equals(virtualFile.getExtension())) { - index = -1; - final int endIndex = getIndexOf(fullPath, templatePaths[++index], templatePaths); - final int offset = templatePaths[index].length(); + paths = templatePaths; + } else if (JS.equals(virtualFile.getExtension()) || CSS.equals(virtualFile.getExtension())) { + paths = webPaths; + } else { + return fullPath.toString(); + } - fullPath.replace(0, endIndex + offset, ""); - magentoPath.append(SEPARATOR); - magentoPath.append(fullPath); - path = magentoPath.toString(); - } else if (JS.equals(virtualFile.getExtension())) { - index = -1; - final int endIndex = getIndexOf(fullPath, jsPaths[++index], jsPaths); - final int offset = jsPaths[index].length(); + final int endIndex = getIndexOf(fullPath, paths[++index], paths); + final int offset = paths[index].length(); - fullPath.replace(0, endIndex + offset, ""); - magentoPath.append(SEPARATOR); - magentoPath.append(fullPath); - path = magentoPath.toString(); - } + fullPath.replace(0, endIndex + offset, ""); - return path; + return magentoPath.append(SEPARATOR).append(fullPath).toString(); } private int getIndexOf(final StringBuilder fullPath, final String path, final String[] paths) { From 9b2842e2380ac6e09eaff822b6a6708e83b5c11f Mon Sep 17 00:00:00 2001 From: Adarsh Manickam Date: Sun, 11 Apr 2021 18:31:03 +0530 Subject: [PATCH 3/3] Fixed CheckStyle and PMD issues --- .../actions/CopyMagentoPath.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java index 212543c33..ac79aebbe 100644 --- a/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java +++ b/src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java @@ -15,17 +15,17 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.PsiManager; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - import java.util.Arrays; import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class CopyMagentoPath extends CopyPathProvider { - public static final String PHTML = "phtml"; - public static final String JS = "js"; - public static final String CSS = "css"; - private final List acceptedTypes = Arrays.asList(PHTML, JS, CSS); + 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 SEPARATOR = "::"; private int index; @@ -51,7 +51,7 @@ public void update(@NotNull final AnActionEvent event) { } } - private boolean isNotValidFile(VirtualFile virtualFile) { + private boolean isNotValidFile(final VirtualFile virtualFile) { return virtualFile != null && virtualFile.isDirectory() || virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension()); } @@ -77,31 +77,30 @@ public String getPathToElement( return null; } final StringBuilder fullPath = new StringBuilder(virtualFile.getPath()); - final StringBuilder magentoPath - = new StringBuilder(moduleName); index = -1; String[] paths; - if (PHTML.equals(virtualFile.getExtension())) { + if (PHTML_EXTENSION.equals(virtualFile.getExtension())) { paths = templatePaths; - } else if (JS.equals(virtualFile.getExtension()) || CSS.equals(virtualFile.getExtension())) { + } else if (JS_EXTENSION.equals(virtualFile.getExtension()) + || CSS_EXTENSION.equals(virtualFile.getExtension())) { paths = webPaths; } else { return fullPath.toString(); } - final int endIndex = getIndexOf(fullPath, paths[++index], paths); + final int endIndex = getIndexOf(paths, fullPath, paths[++index]); final int offset = paths[index].length(); fullPath.replace(0, endIndex + offset, ""); - return magentoPath.append(SEPARATOR).append(fullPath).toString(); + return moduleName + SEPARATOR + fullPath; } - private int getIndexOf(final StringBuilder fullPath, final String path, final String[] paths) { + private int getIndexOf(final String[] paths, final StringBuilder fullPath, final String path) { return fullPath.lastIndexOf(path) == -1 - ? getIndexOf(fullPath, paths[++index], paths) + ? getIndexOf(paths, fullPath, paths[++index]) : fullPath.lastIndexOf(path); } }