|
| 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 AnActionEvent e) { |
| 32 | + final VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE); |
| 33 | + if (virtualFile != null && virtualFile.isDirectory()) { |
| 34 | + e.getPresentation().setVisible(false); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Nullable |
| 39 | + @Override |
| 40 | + public String getPathToElement(@NotNull Project project, @Nullable VirtualFile virtualFile, @Nullable Editor editor) { |
| 41 | + final PsiDirectory directory = PsiManager.getInstance(project).findFile(virtualFile).getContainingDirectory(); |
| 42 | + final StringBuilder fullPath = new StringBuilder(virtualFile.getPath()); |
| 43 | + final StringBuilder magentoPath = new StringBuilder(GetModuleNameByDirectoryUtil.execute(directory, project)); |
| 44 | + String path = fullPath.toString(); |
| 45 | + |
| 46 | + if (PHTML.equals(virtualFile.getExtension())) { |
| 47 | + index = -1; |
| 48 | + final int endIndex = getIndexOf(fullPath, templatePaths[++index]); |
| 49 | + final int offset = templatePaths[index].length(); |
| 50 | + |
| 51 | + fullPath.replace(0, endIndex + offset, ""); |
| 52 | + magentoPath.append(PHTML_SEPARATOR); |
| 53 | + magentoPath.append(fullPath); |
| 54 | + path = magentoPath.toString(); |
| 55 | + } |
| 56 | + |
| 57 | + return path; |
| 58 | + } |
| 59 | + |
| 60 | + private int getIndexOf(StringBuilder fullPath, String path) { |
| 61 | + return fullPath.lastIndexOf(path) != -1 |
| 62 | + ? fullPath.lastIndexOf(path) |
| 63 | + : getIndexOf(fullPath, templatePaths[++index]); |
| 64 | + } |
| 65 | +} |
0 commit comments