Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS and CSS support for Copy Magento Path action #536

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Changes from all 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
57 changes: 39 additions & 18 deletions src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,47 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
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 PHTML_SEPARATOR = "::";
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 SEPARATOR = "::";
private int index;

private final String[] templatePaths = {
"view/frontend/templates/",
"view/adminhtml/templates/",
"view/base/templates/",
"templates/"
};

private final String[] webPaths = {
"view/frontend/web/",
"view/adminhtml/web/",
"view/base/web/",
"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(final VirtualFile virtualFile) {
return virtualFile != null && virtualFile.isDirectory()
|| virtualFile != null && !acceptedTypes.contains(virtualFile.getExtension());
}

@Nullable
@Override
public String getPathToElement(
Expand All @@ -59,27 +77,30 @@ public String getPathToElement(
return null;
}
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());
final StringBuilder magentoPath
= new StringBuilder(moduleName);
String path = fullPath.toString();

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

fullPath.replace(0, endIndex + offset, "");
magentoPath.append(PHTML_SEPARATOR);
magentoPath.append(fullPath);
path = magentoPath.toString();
if (PHTML_EXTENSION.equals(virtualFile.getExtension())) {
paths = templatePaths;
} else if (JS_EXTENSION.equals(virtualFile.getExtension())
|| CSS_EXTENSION.equals(virtualFile.getExtension())) {
paths = webPaths;
} else {
return fullPath.toString();
}

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

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

return moduleName + SEPARATOR + fullPath;
}

private int getIndexOf(final StringBuilder fullPath, final String path) {
private int getIndexOf(final String[] paths, final StringBuilder fullPath, final String path) {
return fullPath.lastIndexOf(path) == -1
? getIndexOf(fullPath, templatePaths[++index])
? getIndexOf(paths, fullPath, paths[++index])
: fullPath.lastIndexOf(path);
}
}