forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateAPluginAction.java
147 lines (123 loc) · 4.96 KB
/
CreateAPluginAction.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
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.ActionUpdateThreadAware;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.magento.idea.magento2plugin.MagentoIcons;
import com.magento.idea.magento2plugin.actions.generation.dialog.CreateAPluginDialog;
import com.magento.idea.magento2plugin.inspections.php.util.PhpClassImplementsNoninterceptableInterfaceUtil;
import com.magento.idea.magento2plugin.project.Settings;
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
import com.magento.idea.magento2plugin.util.magento.plugin.IsPluginAllowedForMethodUtil;
import org.jetbrains.annotations.NotNull;
public class CreateAPluginAction extends DumbAwareAction implements ActionUpdateThreadAware {
public static final String ACTION_NAME = "Create a new Plugin";
public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Plugin";
private final GetFirstClassOfFile getFirstClassOfFile;
private Method targetMethod;
private PhpClass targetClass;
/**
* Constructor.
*/
public CreateAPluginAction() {
super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE);
this.getFirstClassOfFile = GetFirstClassOfFile.getInstance();
}
/**
* Updates the state of action.
*/
@Override
public void update(final AnActionEvent event) {
targetClass = null;// NOPMD
targetMethod = null;// NOPMD
final Project project = event.getData(PlatformDataKeys.PROJECT);
if (project != null && Settings.isEnabled(project)) {
final Pair<PsiFile, PhpClass> pair = this.findPhpClass(event);
final PsiFile psiFile = pair.getFirst();
final PhpClass phpClass = pair.getSecond();
if (phpClass == null
|| !(psiFile instanceof PhpFile)
|| phpClass.isFinal()
|| PhpClassImplementsNoninterceptableInterfaceUtil.execute(phpClass)
) {
this.setStatus(event, false);
return;
}
targetClass = phpClass;
this.setStatus(event, true);
return;
}
this.setStatus(event, false);
}
private void setStatus(final AnActionEvent event, final boolean status) {
event.getPresentation().setVisible(status);
event.getPresentation().setEnabled(status);
}
@Override
public void actionPerformed(final @NotNull AnActionEvent event) {
final Project project = event.getProject();
if (project == null) {
return;
}
CreateAPluginDialog.open(project, this.targetMethod, this.targetClass);
}
@Override
public boolean isDumbAware() {
return false;
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
private Pair<PsiFile, PhpClass> findPhpClass(final @NotNull AnActionEvent event) {
final PsiFile psiFile = event.getData(PlatformDataKeys.PSI_FILE);
PhpClass phpClass = null;
if (psiFile instanceof PhpFile) {
phpClass = getFirstClassOfFile.execute((PhpFile) psiFile);
fetchTargetMethod(event, psiFile, phpClass);
}
return Pair.create(psiFile, phpClass);
}
@SuppressWarnings({"PMD.CyclomaticComplexity"})
private void fetchTargetMethod(
final @NotNull AnActionEvent event,
final PsiFile psiFile,
final PhpClass phpClass
) {
final Caret caret = event.getData(PlatformDataKeys.CARET);
if (caret == null) {
return;
}
final int offset = caret.getOffset();
final PsiElement element = psiFile.findElementAt(offset);
if (element == null) {
return;
}
if (element instanceof Method && element.getParent().equals(phpClass)
&& IsPluginAllowedForMethodUtil.check((Method) element)) {
this.targetMethod = (Method) element;
return;
}
if (element instanceof PhpClass) {
return;
}
final PsiElement parent = element.getParent();
if (parent instanceof Method && parent.getParent().equals(phpClass)
&& IsPluginAllowedForMethodUtil.check((Method) parent)) {
this.targetMethod = (Method) parent;
}
}
}