55
66package com .magento .idea .magento2plugin .actions .comparator ;
77
8- import com .intellij .diff .DiffContentFactory ;
98import com .intellij .diff .DiffDialogHints ;
109import 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 ;
1410import com .intellij .diff .chains .DiffRequestChain ;
15- import com .intellij .diff .contents .DiffContent ;
16- import com .intellij .diff .contents .DocumentContent ;
1711import com .intellij .openapi .actionSystem .AnAction ;
1812import com .intellij .openapi .actionSystem .AnActionEvent ;
1913import com .intellij .openapi .actionSystem .PlatformDataKeys ;
2317import com .intellij .psi .PsiDirectory ;
2418import com .intellij .psi .PsiFile ;
2519import com .magento .idea .magento2plugin .MagentoIcons ;
20+ import com .magento .idea .magento2plugin .actions .comparator .util .DiffRequestChainUtil ;
2621import com .magento .idea .magento2plugin .indexes .ModuleIndex ;
22+ import com .magento .idea .magento2plugin .magento .packages .Areas ;
2723import com .magento .idea .magento2plugin .project .Settings ;
28- import com .magento .idea .magento2plugin .util .RegExUtil ;
2924import com .magento .idea .magento2plugin .util .magento .GetModuleNameByDirectoryUtil ;
25+ import com .magento .idea .magento2plugin .util .magento .area .AreaResolverUtil ;
3026import java .nio .file .Path ;
31- import java .util .regex .Matcher ;
32- import java .util .regex .Pattern ;
3327import org .apache .commons .lang3 .StringUtils ;
3428import org .jetbrains .annotations .NotNull ;
3529import org .jetbrains .annotations .Nullable ;
3630
3731public class CompareTemplateAction extends AnAction {
3832
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 " ;
4135
4236 private static final String PHTML_EXTENSION = "phtml" ;
4337 protected VirtualFile selectedFile ;
4438 protected VirtualFile originalFile ;
4539
4640 /**
47- * Inject constructor argument action constructor.
41+ * Compare template action constructor.
4842 */
4943 public CompareTemplateAction () {
5044 super (ACTION_NAME , ACTION_DESCRIPTION , MagentoIcons .MODULE );
5145 }
5246
5347 /**
5448 * Updates the state of action.
49+ *
50+ * @param event AnActionEvent
5551 */
52+ @ SuppressWarnings ("PMD.NPathComplexity" )
5653 @ Override
5754 public void update (final @ NotNull AnActionEvent event ) {
55+ setStatus (event , false );
5856 final Project project = event .getData (PlatformDataKeys .PROJECT );
57+
5958 if (project == null ) {
6059 return ;
6160 }
6261
6362 if (!Settings .isEnabled (project )) {
64- this .setStatus (event , false );
6563 return ;
6664 }
6765 final PsiFile psiFile = event .getData (PlatformDataKeys .PSI_FILE );
68- selectedFile = psiFile != null ? psiFile .getVirtualFile () : null ;//NOPMD
6966
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 ())) {
7477 return ;
7578 }
79+ final Areas area = AreaResolverUtil .getForFileInCustomTheme (targetFileCandidate );
7680
77- final String fullPath = selectedFile .getPath ();
78- final String area = getArea (fullPath );
81+ if (area == null ) {
82+ return ;
83+ }
7984 final String originalModuleName = getOriginalModuleName (project , psiFile );
8085 final PsiDirectory originalModuleDirectory =
8186 new ModuleIndex (project ).getModuleDirectoryByModuleName (originalModuleName );
8287
83- if (originalModuleDirectory == null
84- || area == null
85- ) {
86- this .setStatus (event , false );
88+ if (originalModuleDirectory == null ) {
8789 return ;
8890 }
89-
9091 final String originalFilePath = originalModuleDirectory .getVirtualFile ().getPath ()
9192 + "/view/"
9293 + area
93- + StringUtils .substringAfter (fullPath , originalModuleName );
94+ + StringUtils .substringAfter (targetFileCandidate . getPath () , originalModuleName );
9495
95- originalFile = VfsUtil .findFile (Path .of (originalFilePath ), false );
96+ final VirtualFile origFileCandidate = VfsUtil .findFile (Path .of (originalFilePath ), false );
9697
97- if (originalFile != null ) {
98- this .setStatus (event , true );
98+ if (origFileCandidate == null ) {
9999 return ;
100100 }
101-
102- this .setStatus (event , false );
101+ selectedFile = targetFileCandidate ;
102+ originalFile = origFileCandidate ;
103+ this .setStatus (event , true );
103104 }
104105
105106 @ Override
106107 public void actionPerformed (final @ NotNull AnActionEvent event ) {
107108 final Project project = event .getProject ();
108- final DiffRequestChain chain =
109- createMutableChainFromFiles (project , selectedFile , originalFile );
110-
111- DiffManager .getInstance ().showDiff (project , chain , DiffDialogHints .DEFAULT );
112- }
113109
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 ;
121112 }
113+ final DiffRequestChain chain = DiffRequestChainUtil .createMutableChain (
114+ project ,
115+ selectedFile ,
116+ originalFile
117+ );
122118
123- return areaName ;
119+ if (chain == null ) {
120+ return ;
121+ }
122+ DiffManager .getInstance ().showDiff (
123+ project ,
124+ chain ,
125+ DiffDialogHints .DEFAULT
126+ );
124127 }
125128
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+ ) {
127133 final PsiDirectory directory = psiFile .getContainingDirectory ();
128134
129135 return GetModuleNameByDirectoryUtil .execute (directory , project );
130136 }
131137
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-
154138 private void setStatus (final AnActionEvent event , final boolean status ) {
155139 event .getPresentation ().setVisible (status );
156140 event .getPresentation ().setEnabled (status );
157141 }
158- }
142+ }
0 commit comments