Skip to content

Commit f380d0f

Browse files
authored
Merge branch '3.1.0-develop' into issues-444
2 parents 790bd6d + ddadbe7 commit f380d0f

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

resources/META-INF/plugin.xml

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
<add-to-group group-id="ProjectViewPopupMenu"/>
9595
</action>
9696

97+
<action id="CopyMagentoPath"
98+
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
99+
text="Magento Path"
100+
description="Copies Magento's path of file depending on file type">
101+
<add-to-group group-id="CopyFileReference" anchor="last"/>
102+
</action>
97103
</actions>
98104

99105
<extensions defaultExtensionNs="com.intellij">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions;
7+
8+
import com.intellij.ide.actions.CopyPathProvider;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.actionSystem.PlatformDataKeys;
11+
import com.intellij.openapi.editor.Editor;
12+
import com.intellij.openapi.project.Project;
13+
import com.intellij.openapi.vfs.VirtualFile;
14+
import com.intellij.psi.PsiDirectory;
15+
import com.intellij.psi.PsiManager;
16+
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
17+
import org.jetbrains.annotations.NotNull;
18+
import org.jetbrains.annotations.Nullable;
19+
20+
public class CopyMagentoPath extends CopyPathProvider {
21+
public static final String PHTML = "phtml";
22+
public static final String PHTML_SEPARATOR = "::";
23+
private int index;
24+
private final String[] templatePaths = {
25+
"view/frontend/templates/",
26+
"view/adminhtml/templates/",
27+
"view/base/templates/"
28+
};
29+
30+
@Override
31+
public void update(@NotNull final AnActionEvent event) {
32+
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
33+
if (virtualFile != null && virtualFile.isDirectory()) {
34+
event.getPresentation().setVisible(false);
35+
}
36+
}
37+
38+
@Nullable
39+
@Override
40+
public String getPathToElement(
41+
@NotNull final Project project,
42+
@Nullable final VirtualFile virtualFile,
43+
@Nullable final Editor editor
44+
) {
45+
final PsiDirectory directory
46+
= PsiManager.getInstance(project).findFile(virtualFile).getContainingDirectory();
47+
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());
48+
final StringBuilder magentoPath
49+
= new StringBuilder(GetModuleNameByDirectoryUtil.execute(directory, project));
50+
String path = fullPath.toString();
51+
52+
if (PHTML.equals(virtualFile.getExtension())) {
53+
index = -1;
54+
final int endIndex = getIndexOf(fullPath, templatePaths[++index]);
55+
final int offset = templatePaths[index].length();
56+
57+
fullPath.replace(0, endIndex + offset, "");
58+
magentoPath.append(PHTML_SEPARATOR);
59+
magentoPath.append(fullPath);
60+
path = magentoPath.toString();
61+
}
62+
63+
return path;
64+
}
65+
66+
private int getIndexOf(final StringBuilder fullPath, final String path) {
67+
return fullPath.lastIndexOf(path) == -1
68+
? getIndexOf(fullPath, templatePaths[++index])
69+
: fullPath.lastIndexOf(path);
70+
}
71+
}

0 commit comments

Comments
 (0)