forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareTemplateAction.java
152 lines (128 loc) · 4.99 KB
/
CompareTemplateAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.comparator;
import com.intellij.diff.DiffDialogHints;
import com.intellij.diff.DiffManager;
import com.intellij.diff.chains.DiffRequestChain;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.comparator.util.DiffRequestChainUtil;
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import com.magento.idea.magento2plugin.util.magento.area.AreaResolverUtil;
import java.nio.file.Path;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CompareTemplateAction extends AnAction {
public static final String ACTION_NAME = "Compare overridden template with the original one";
public static final String ACTION_DESCRIPTION = "The Magento 2 overridden template comparing";
private static final String PHTML_EXTENSION = "phtml";
protected VirtualFile selectedFile;
protected VirtualFile originalFile;
/**
* Compare template action constructor.
*/
public CompareTemplateAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
}
/**
* Updates the state of action.
*
* @param event AnActionEvent
*/
@Override
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"})
public void update(final @NotNull AnActionEvent event) {
setStatus(event, false);
final Project project = event.getData(PlatformDataKeys.PROJECT);
if (project == null) {
return;
}
if (!Settings.isEnabled(project)) {
return;
}
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
if (psiFile == null) {
return;
}
final VirtualFile targetFileCandidate = psiFile.getVirtualFile();
if (targetFileCandidate == null) {
return;
}
if (!PHTML_EXTENSION.equals(targetFileCandidate.getExtension())) {
return;
}
final Areas area = AreaResolverUtil.getForFileInCustomTheme(targetFileCandidate);
if (area == null) {
return;
}
final String originalModuleName = getOriginalModuleName(project, psiFile);
if (originalModuleName == null) {
return;
}
final PsiDirectory originalModuleDirectory =
new ModuleIndex(project).getModuleDirectoryByModuleName(originalModuleName);
if (originalModuleDirectory == null) {
return;
}
final String originalFilePath = originalModuleDirectory.getVirtualFile().getPath()
+ "/view/"
+ area
+ StringUtils.substringAfter(targetFileCandidate.getPath(), originalModuleName);
final VirtualFile origFileCandidate = VfsUtil.findFile(Path.of(originalFilePath), false);
if (origFileCandidate == null) {
return;
}
selectedFile = targetFileCandidate;
originalFile = origFileCandidate;
this.setStatus(event, true);
}
@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final Project project = event.getProject();
if (project == null || selectedFile == null || originalFile == null) {
return;
}
final DiffRequestChain chain = DiffRequestChainUtil.createMutableChain(
project,
selectedFile,
originalFile
);
if (chain == null) {
return;
}
DiffManager.getInstance().showDiff(
project,
chain,
DiffDialogHints.DEFAULT
);
}
private @Nullable String getOriginalModuleName(
final @NotNull Project project,
final @NotNull PsiFile psiFile
) {
final PsiDirectory directory = psiFile.getContainingDirectory();
return GetModuleNameByDirectoryUtil.execute(directory, project);
}
private void setStatus(final AnActionEvent event, final boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}