5
5
6
6
package com .magento .idea .magento2plugin .actions .comparator ;
7
7
8
- import com .intellij .diff .DiffContentFactory ;
9
8
import com .intellij .diff .DiffDialogHints ;
10
9
import com .intellij .diff .DiffManager ;
11
- import com .intellij .diff .DiffRequestFactory ;
12
- import com .intellij .diff .actions .BlankDiffWindowUtil ;
13
- import com .intellij .diff .actions .impl .MutableDiffRequestChain ;
14
10
import com .intellij .diff .chains .DiffRequestChain ;
15
- import com .intellij .diff .contents .DiffContent ;
16
- import com .intellij .diff .contents .DocumentContent ;
17
11
import com .intellij .openapi .actionSystem .AnAction ;
18
12
import com .intellij .openapi .actionSystem .AnActionEvent ;
19
13
import com .intellij .openapi .actionSystem .PlatformDataKeys ;
23
17
import com .intellij .psi .PsiDirectory ;
24
18
import com .intellij .psi .PsiFile ;
25
19
import com .magento .idea .magento2plugin .MagentoIcons ;
20
+ import com .magento .idea .magento2plugin .actions .comparator .util .DiffRequestChainUtil ;
26
21
import com .magento .idea .magento2plugin .indexes .ModuleIndex ;
22
+ import com .magento .idea .magento2plugin .magento .packages .Areas ;
27
23
import com .magento .idea .magento2plugin .project .Settings ;
28
- import com .magento .idea .magento2plugin .util .RegExUtil ;
29
24
import com .magento .idea .magento2plugin .util .magento .GetModuleNameByDirectoryUtil ;
25
+ import com .magento .idea .magento2plugin .util .magento .area .AreaResolverUtil ;
30
26
import java .nio .file .Path ;
31
- import java .util .regex .Matcher ;
32
- import java .util .regex .Pattern ;
33
27
import org .apache .commons .lang3 .StringUtils ;
34
28
import org .jetbrains .annotations .NotNull ;
35
29
import org .jetbrains .annotations .Nullable ;
36
30
37
31
public class CompareTemplateAction extends AnAction {
38
32
39
- public static final String ACTION_NAME = "Compare Template with Original " ;
40
- public static final String ACTION_DESCRIPTION = "Compare Template with Original " ;
33
+ public static final String ACTION_NAME = "Compare overridden template with the original one " ;
34
+ public static final String ACTION_DESCRIPTION = "The Magento 2 overridden template comparing " ;
41
35
42
36
private static final String PHTML_EXTENSION = "phtml" ;
43
37
protected VirtualFile selectedFile ;
44
38
protected VirtualFile originalFile ;
45
39
46
40
/**
47
- * Inject constructor argument action constructor.
41
+ * Compare template action constructor.
48
42
*/
49
43
public CompareTemplateAction () {
50
44
super (ACTION_NAME , ACTION_DESCRIPTION , MagentoIcons .MODULE );
51
45
}
52
46
53
47
/**
54
48
* Updates the state of action.
49
+ *
50
+ * @param event AnActionEvent
55
51
*/
52
+ @ SuppressWarnings ("PMD.NPathComplexity" )
56
53
@ Override
57
54
public void update (final @ NotNull AnActionEvent event ) {
55
+ setStatus (event , false );
58
56
final Project project = event .getData (PlatformDataKeys .PROJECT );
57
+
59
58
if (project == null ) {
60
59
return ;
61
60
}
62
61
63
62
if (!Settings .isEnabled (project )) {
64
- this .setStatus (event , false );
65
63
return ;
66
64
}
67
65
final PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
68
- selectedFile = psiFile != null ? psiFile .getVirtualFile () : null ;//NOPMD
69
66
70
- if (selectedFile != null
71
- && !PHTML_EXTENSION .equals (selectedFile .getExtension ())
72
- ) {
73
- this .setStatus (event , false );
67
+ if (psiFile == null ) {
68
+ return ;
69
+ }
70
+ final VirtualFile targetFileCandidate = psiFile .getVirtualFile ();
71
+
72
+ if (targetFileCandidate == null ) {
73
+ return ;
74
+ }
75
+
76
+ if (!PHTML_EXTENSION .equals (targetFileCandidate .getExtension ())) {
74
77
return ;
75
78
}
79
+ final Areas area = AreaResolverUtil .getForFileInCustomTheme (targetFileCandidate );
76
80
77
- final String fullPath = selectedFile .getPath ();
78
- final String area = getArea (fullPath );
81
+ if (area == null ) {
82
+ return ;
83
+ }
79
84
final String originalModuleName = getOriginalModuleName (project , psiFile );
80
85
final PsiDirectory originalModuleDirectory =
81
86
new ModuleIndex (project ).getModuleDirectoryByModuleName (originalModuleName );
82
87
83
- if (originalModuleDirectory == null
84
- || area == null
85
- ) {
86
- this .setStatus (event , false );
88
+ if (originalModuleDirectory == null ) {
87
89
return ;
88
90
}
89
-
90
91
final String originalFilePath = originalModuleDirectory .getVirtualFile ().getPath ()
91
92
+ "/view/"
92
93
+ area
93
- + StringUtils .substringAfter (fullPath , originalModuleName );
94
+ + StringUtils .substringAfter (targetFileCandidate . getPath () , originalModuleName );
94
95
95
- originalFile = VfsUtil .findFile (Path .of (originalFilePath ), false );
96
+ final VirtualFile origFileCandidate = VfsUtil .findFile (Path .of (originalFilePath ), false );
96
97
97
- if (originalFile != null ) {
98
- this .setStatus (event , true );
98
+ if (origFileCandidate == null ) {
99
99
return ;
100
100
}
101
-
102
- this .setStatus (event , false );
101
+ selectedFile = targetFileCandidate ;
102
+ originalFile = origFileCandidate ;
103
+ this .setStatus (event , true );
103
104
}
104
105
105
106
@ Override
106
107
public void actionPerformed (final @ NotNull AnActionEvent event ) {
107
108
final Project project = event .getProject ();
108
- final DiffRequestChain chain =
109
- createMutableChainFromFiles (project , selectedFile , originalFile );
110
-
111
- DiffManager .getInstance ().showDiff (project , chain , DiffDialogHints .DEFAULT );
112
- }
113
109
114
- @ Nullable
115
- private String getArea (final String fullPath ) {
116
- final Pattern pattern = Pattern .compile (RegExUtil .ViewArea .AREA );
117
- final Matcher matcher = pattern .matcher (fullPath );
118
- String areaName = null ;
119
- if (matcher .find ()) {
120
- areaName = matcher .group (1 );
110
+ if (project == null || selectedFile == null || originalFile == null ) {
111
+ return ;
121
112
}
113
+ final DiffRequestChain chain = DiffRequestChainUtil .createMutableChain (
114
+ project ,
115
+ selectedFile ,
116
+ originalFile
117
+ );
122
118
123
- return areaName ;
119
+ if (chain == null ) {
120
+ return ;
121
+ }
122
+ DiffManager .getInstance ().showDiff (
123
+ project ,
124
+ chain ,
125
+ DiffDialogHints .DEFAULT
126
+ );
124
127
}
125
128
126
- private String getOriginalModuleName (final Project project , final PsiFile psiFile ) {
129
+ private @ Nullable String getOriginalModuleName (
130
+ final @ NotNull Project project ,
131
+ final @ NotNull PsiFile psiFile
132
+ ) {
127
133
final PsiDirectory directory = psiFile .getContainingDirectory ();
128
134
129
135
return GetModuleNameByDirectoryUtil .execute (directory , project );
130
136
}
131
137
132
- @ NotNull
133
- private MutableDiffRequestChain createMutableChainFromFiles (
134
- final @ Nullable Project project ,
135
- final @ NotNull VirtualFile file1 ,
136
- final @ NotNull VirtualFile file2
137
- ) {
138
- final DiffContentFactory contentFactory = DiffContentFactory .getInstance ();
139
- final DiffRequestFactory requestFactory = DiffRequestFactory .getInstance ();
140
-
141
- final DiffContent content1 = contentFactory .create (project , file1 );
142
- final DiffContent content2 = contentFactory .create (project , file2 );
143
-
144
- final MutableDiffRequestChain chain = BlankDiffWindowUtil .createBlankDiffRequestChain (
145
- (DocumentContent )content1 ,
146
- (DocumentContent )content2 ,
147
- null
148
- );
149
- chain .setWindowTitle (requestFactory .getTitle (file1 , file2 ));
150
-
151
- return chain ;
152
- }
153
-
154
138
private void setStatus (final AnActionEvent event , final boolean status ) {
155
139
event .getPresentation ().setVisible (status );
156
140
event .getPresentation ().setEnabled (status );
157
141
}
158
- }
142
+ }
0 commit comments