Skip to content

Commit 7f9abe1

Browse files
author
Vitaliy
authoredApr 12, 2021
Merge pull request #536 from drpayyne/issue-414
JS and CSS support for Copy Magento Path action
2 parents bd7d510 + 9b2842e commit 7f9abe1

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed
 

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

+39-18
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,47 @@
1515
import com.intellij.psi.PsiFile;
1616
import com.intellij.psi.PsiManager;
1717
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
18+
import java.util.Arrays;
19+
import java.util.List;
1820
import org.jetbrains.annotations.NotNull;
1921
import org.jetbrains.annotations.Nullable;
2022

2123
public class CopyMagentoPath extends CopyPathProvider {
22-
public static final String PHTML = "phtml";
23-
public static final String PHTML_SEPARATOR = "::";
24+
public static final String PHTML_EXTENSION = "phtml";
25+
public static final String JS_EXTENSION = "js";
26+
public static final String CSS_EXTENSION = "css";
27+
private final List<String> acceptedTypes
28+
= Arrays.asList(PHTML_EXTENSION, JS_EXTENSION, CSS_EXTENSION);
29+
public static final String SEPARATOR = "::";
2430
private int index;
31+
2532
private final String[] templatePaths = {
2633
"view/frontend/templates/",
2734
"view/adminhtml/templates/",
2835
"view/base/templates/",
2936
"templates/"
3037
};
3138

39+
private final String[] webPaths = {
40+
"view/frontend/web/",
41+
"view/adminhtml/web/",
42+
"view/base/web/",
43+
"web/"
44+
};
45+
3246
@Override
3347
public void update(@NotNull final AnActionEvent event) {
3448
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
35-
if (virtualFile != null && virtualFile.isDirectory()
36-
|| virtualFile != null && !PHTML.equals(virtualFile.getExtension())) {
49+
if (isNotValidFile(virtualFile)) {
3750
event.getPresentation().setVisible(false);
3851
}
3952
}
4053

54+
private boolean isNotValidFile(final VirtualFile virtualFile) {
55+
return virtualFile != null && virtualFile.isDirectory()
56+
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension());
57+
}
58+
4159
@Nullable
4260
@Override
4361
public String getPathToElement(
@@ -59,27 +77,30 @@ public String getPathToElement(
5977
return null;
6078
}
6179
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());
62-
final StringBuilder magentoPath
63-
= new StringBuilder(moduleName);
64-
String path = fullPath.toString();
6580

66-
if (PHTML.equals(virtualFile.getExtension())) {
67-
index = -1;
68-
final int endIndex = getIndexOf(fullPath, templatePaths[++index]);
69-
final int offset = templatePaths[index].length();
81+
index = -1;
82+
String[] paths;
7083

71-
fullPath.replace(0, endIndex + offset, "");
72-
magentoPath.append(PHTML_SEPARATOR);
73-
magentoPath.append(fullPath);
74-
path = magentoPath.toString();
84+
if (PHTML_EXTENSION.equals(virtualFile.getExtension())) {
85+
paths = templatePaths;
86+
} else if (JS_EXTENSION.equals(virtualFile.getExtension())
87+
|| CSS_EXTENSION.equals(virtualFile.getExtension())) {
88+
paths = webPaths;
89+
} else {
90+
return fullPath.toString();
7591
}
7692

77-
return path;
93+
final int endIndex = getIndexOf(paths, fullPath, paths[++index]);
94+
final int offset = paths[index].length();
95+
96+
fullPath.replace(0, endIndex + offset, "");
97+
98+
return moduleName + SEPARATOR + fullPath;
7899
}
79100

80-
private int getIndexOf(final StringBuilder fullPath, final String path) {
101+
private int getIndexOf(final String[] paths, final StringBuilder fullPath, final String path) {
81102
return fullPath.lastIndexOf(path) == -1
82-
? getIndexOf(fullPath, templatePaths[++index])
103+
? getIndexOf(paths, fullPath, paths[++index])
83104
: fullPath.lastIndexOf(path);
84105
}
85106
}

0 commit comments

Comments
 (0)
Please sign in to comment.