66package com .magento .idea .magento2plugin .actions ;
77
88import com .intellij .ide .actions .CopyPathProvider ;
9- import com .intellij .openapi .actionSystem .AnActionEvent ;
10- import com .intellij .openapi .actionSystem .PlatformDataKeys ;
119import com .intellij .openapi .editor .Editor ;
1210import com .intellij .openapi .project .Project ;
1311import com .intellij .openapi .vfs .VirtualFile ;
@@ -27,8 +25,7 @@ public class CopyMagentoPath extends CopyPathProvider {
2725 public static final String PHTML_EXTENSION = "phtml" ;
2826 public static final String JS_EXTENSION = "js" ;
2927 public static final String CSS_EXTENSION = "css" ;
30- private final List <String > acceptedTypes
31- = Arrays .asList (PHTML_EXTENSION , JS_EXTENSION , CSS_EXTENSION );
28+ public static final String HTML_EXTENSION = "html" ;
3229 private static final List <String > SUPPORTED_IMAGE_EXTENSIONS
3330 = new ArrayList <>(Arrays .asList (ImageIO .getReaderFormatNames ()));
3431 public static final String SEPARATOR = "::" ;
@@ -57,20 +54,6 @@ public CopyMagentoPath() {
5754 SUPPORTED_IMAGE_EXTENSIONS .add ("svg" );
5855 }
5956
60- @ Override
61- public void update (@ NotNull final AnActionEvent event ) {
62- final VirtualFile virtualFile = event .getData (PlatformDataKeys .VIRTUAL_FILE );
63- if (isNotValidFile (virtualFile )) {
64- event .getPresentation ().setVisible (false );
65- }
66- }
67-
68- private boolean isNotValidFile (final VirtualFile virtualFile ) {
69- return virtualFile != null && virtualFile .isDirectory ()
70- || virtualFile != null && !acceptedTypes .contains (virtualFile .getExtension ())
71- && !SUPPORTED_IMAGE_EXTENSIONS .contains (virtualFile .getExtension ());
72- }
73-
7457 @ Override
7558 public @ Nullable String getPathToElement (
7659 final @ NotNull Project project ,
@@ -94,30 +77,61 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
9477 final StringBuilder fullPath = new StringBuilder (virtualFile .getPath ());
9578
9679 index = -1 ;
97- String [] paths ;
80+ final String [] paths ;
9881
9982 if (PHTML_EXTENSION .equals (virtualFile .getExtension ())) {
10083 paths = templatePaths ;
101- } else if (JS_EXTENSION .equals (virtualFile .getExtension ())
102- || CSS_EXTENSION .equals (virtualFile .getExtension ())
103- || SUPPORTED_IMAGE_EXTENSIONS .contains (virtualFile .getExtension ())) {
84+ } else if (isMagentoFile (virtualFile )) {
10485 paths = webPaths ;
10586 } else {
106- return fullPath . toString () ;
87+ return "" ;
10788 }
10889
10990 try {
110- final int endIndex = getIndexOf (paths , fullPath , paths [++index ]);
111- final int offset = paths [index ].length ();
112-
113- fullPath .replace (0 , endIndex + offset , "" );
114-
115- return moduleName + SEPARATOR + fullPath ;
91+ return getResultPath (virtualFile , paths , fullPath , moduleName );
11692 } catch (ArrayIndexOutOfBoundsException exception ) {
117- return fullPath . toString () ;
93+ return "" ;
11894 }
11995 }
12096
97+ /**
98+ * Determines if the provided file is supported by Magento Path.
99+ *
100+ * @param virtualFile the virtual file to be checked
101+ * @return bool
102+ */
103+ private static boolean isMagentoFile (@ NotNull final VirtualFile virtualFile ) {
104+ return JS_EXTENSION .equals (virtualFile .getExtension ())
105+ || CSS_EXTENSION .equals (virtualFile .getExtension ())
106+ || HTML_EXTENSION .equals (virtualFile .getExtension ())
107+ || SUPPORTED_IMAGE_EXTENSIONS .contains (virtualFile .getExtension ());
108+ }
109+
110+ /**
111+ * Constructs a result.
112+ *
113+ * @param virtualFile the virtual file being processed
114+ * @param paths an array of potential path segments to be checked
115+ * @param fullPath the full path of the virtual file as a mutable string builder
116+ * @param moduleName the name of the module associated with the file
117+ * @return the constructed result path
118+ */
119+ private @ NotNull String getResultPath (
120+ @ NotNull final VirtualFile virtualFile ,
121+ final String [] paths ,
122+ final StringBuilder fullPath ,
123+ final String moduleName
124+ ) {
125+ final int endIndex = getIndexOf (paths , fullPath , paths [++index ]);
126+ final int offset = paths [index ].length ();
127+
128+ fullPath .replace (0 , endIndex + offset , "" );
129+
130+ return PHTML_EXTENSION .equals (virtualFile .getExtension ())
131+ ? moduleName + SEPARATOR + fullPath
132+ : moduleName + "/" + fullPath .substring (0 , fullPath .lastIndexOf ("." ));
133+ }
134+
121135 /**
122136 * Get index where web|template path starts in the fullPath.
123137 *
0 commit comments