Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@
<add-to-group group-id="ProjectViewPopupMenu"/>
</action>

<action id="CopyMagentoPath"
class="com.magento.idea.magento2plugin.actions.CopyMagentoPath"
text="Magento Path"
description="Copies Magento's path of file depending on file type">
<add-to-group group-id="CopyFileReference" anchor="last"/>
</action>
</actions>

<extensions defaultExtensionNs="com.intellij">
Expand Down
71 changes: 71 additions & 0 deletions src/com/magento/idea/magento2plugin/actions/CopyMagentoPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions;

import com.intellij.ide.actions.CopyPathProvider;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
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 = "::";
private int index;
private final String[] templatePaths = {
"view/frontend/templates/",
"view/adminhtml/templates/",
"view/base/templates/"
};

@Override
public void update(@NotNull final AnActionEvent event) {
final VirtualFile virtualFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
if (virtualFile != null && virtualFile.isDirectory()) {
event.getPresentation().setVisible(false);
}
}

@Nullable
@Override
public String getPathToElement(
@NotNull final Project project,
@Nullable final VirtualFile virtualFile,
@Nullable final Editor editor
) {
final PsiDirectory directory
= PsiManager.getInstance(project).findFile(virtualFile).getContainingDirectory();
final StringBuilder fullPath = new StringBuilder(virtualFile.getPath());
final StringBuilder magentoPath
= new StringBuilder(GetModuleNameByDirectoryUtil.execute(directory, project));
String path = fullPath.toString();

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

fullPath.replace(0, endIndex + offset, "");
magentoPath.append(PHTML_SEPARATOR);
magentoPath.append(fullPath);
path = magentoPath.toString();
}

return path;
}

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