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