6
6
package com .magento .idea .magento2plugin .actions ;
7
7
8
8
import com .intellij .ide .actions .CopyPathProvider ;
9
- import com .intellij .openapi .actionSystem .AnActionEvent ;
10
- import com .intellij .openapi .actionSystem .PlatformDataKeys ;
11
9
import com .intellij .openapi .editor .Editor ;
12
10
import com .intellij .openapi .project .Project ;
13
11
import com .intellij .openapi .vfs .VirtualFile ;
@@ -27,8 +25,7 @@ public class CopyMagentoPath extends CopyPathProvider {
27
25
public static final String PHTML_EXTENSION = "phtml" ;
28
26
public static final String JS_EXTENSION = "js" ;
29
27
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" ;
32
29
private static final List <String > SUPPORTED_IMAGE_EXTENSIONS
33
30
= new ArrayList <>(Arrays .asList (ImageIO .getReaderFormatNames ()));
34
31
public static final String SEPARATOR = "::" ;
@@ -57,20 +54,6 @@ public CopyMagentoPath() {
57
54
SUPPORTED_IMAGE_EXTENSIONS .add ("svg" );
58
55
}
59
56
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
-
74
57
@ Override
75
58
public @ Nullable String getPathToElement (
76
59
final @ NotNull Project project ,
@@ -94,30 +77,61 @@ private boolean isNotValidFile(final VirtualFile virtualFile) {
94
77
final StringBuilder fullPath = new StringBuilder (virtualFile .getPath ());
95
78
96
79
index = -1 ;
97
- String [] paths ;
80
+ final String [] paths ;
98
81
99
82
if (PHTML_EXTENSION .equals (virtualFile .getExtension ())) {
100
83
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 )) {
104
85
paths = webPaths ;
105
86
} else {
106
- return fullPath . toString () ;
87
+ return "" ;
107
88
}
108
89
109
90
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 );
116
92
} catch (ArrayIndexOutOfBoundsException exception ) {
117
- return fullPath . toString () ;
93
+ return "" ;
118
94
}
119
95
}
120
96
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
+
121
135
/**
122
136
* Get index where web|template path starts in the fullPath.
123
137
*
0 commit comments