forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluginGenerateMethodHandlerBase.java
392 lines (358 loc) · 15.6 KB
/
PluginGenerateMethodHandlerBase.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.ide.util.MemberChooser;
import com.intellij.lang.LanguageCodeInsightActionHandler;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
import com.jetbrains.php.lang.actions.PhpNamedElementNode;
import com.jetbrains.php.lang.lexer.PhpTokenTypes;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.parser.PhpStubElementTypes;
import com.jetbrains.php.lang.psi.PhpCodeEditUtil;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.PhpPsiUtil;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.magento.idea.magento2plugin.actions.generation.ImportReferences.PhpClassReferenceResolver;
import com.magento.idea.magento2plugin.actions.generation.data.code.PluginMethodData;
import com.magento.idea.magento2plugin.actions.generation.generator.code.PluginMethodsGenerator;
import com.magento.idea.magento2plugin.actions.generation.util.CodeStyleSettings;
import com.magento.idea.magento2plugin.actions.generation.util.CollectInsertedMethods;
import com.magento.idea.magento2plugin.actions.generation.util.FillTextBufferWithPluginMethods;
import com.magento.idea.magento2plugin.bundles.CommonBundle;
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
import com.magento.idea.magento2plugin.magento.files.Plugin;
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
import com.magento.idea.magento2plugin.util.magento.plugin.IsPluginAllowedForMethodUtil;
import gnu.trove.THashSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeMap;
import javax.swing.JOptionPane;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings({
"PMD.GodClass",
"PMD.ExcessiveImports",
"PMD.CyclomaticComplexity"
})
public abstract class PluginGenerateMethodHandlerBase implements LanguageCodeInsightActionHandler {
private final CollectInsertedMethods collectInsertedMethods;
private final ValidatorBundle validatorBundle;
private final CommonBundle commonBundle;
public String type;
public FillTextBufferWithPluginMethods fillTextBuffer;
/**
* Constructor.
*
* @param type Plugin.PluginType
*/
public PluginGenerateMethodHandlerBase(final Plugin.PluginType type) {
this.type = type.toString();
this.fillTextBuffer = FillTextBufferWithPluginMethods.getInstance();
this.collectInsertedMethods = CollectInsertedMethods.getInstance();
this.validatorBundle = new ValidatorBundle();
this.commonBundle = new CommonBundle();
}
@Override
public boolean isValidFor(final Editor editor, final PsiFile file) {
if (!(file instanceof PhpFile)) {
return false;
}
final PhpClass phpClass = PhpCodeEditUtil.findClassAtCaret(editor, file);
if (phpClass == null) {
return false;
}
final GetTargetClassNamesByPluginClassName targetClassesService
= GetTargetClassNamesByPluginClassName.getInstance(editor.getProject());
final String currentClass = phpClass.getFQN().substring(1);
final ArrayList<String> targetClassNames = targetClassesService.execute(currentClass);
return !targetClassNames.isEmpty();
}
@Override
public void invoke(
final @NotNull Project project,
final @NotNull Editor editor,
final @NotNull PsiFile pluginFile
) {
final PhpFile pluginPhpFile = (PhpFile)pluginFile;
final PhpClass pluginClass = PhpCodeEditUtil.findClassAtCaret(editor, pluginPhpFile);
if (pluginClass == null) {
return;
}
final Key<Object> targetClassKey = Key.create(PluginMethodsGenerator.originalTargetKey);
final PhpNamedElementNode[] fieldsToShow = this.targetMethods(pluginClass, targetClassKey);
if (fieldsToShow.length == 0) {
if (ApplicationManager.getApplication().isHeadlessEnvironment()) {
return;
}
HintManager.getInstance().showErrorHint(editor, this.getErrorMessage());
return;
}
final PhpNamedElementNode[] members = this.chooseMembers(
fieldsToShow,
true,
pluginFile.getProject()
);
if (members == null || members.length == 0) {
return;
}
final int insertPos = getSuitableEditorPosition(editor, pluginPhpFile);
final CodeStyleSettings codeStyleSettings = new CodeStyleSettings(pluginPhpFile);
codeStyleSettings.adjustBeforeWrite();
ApplicationManager.getApplication().runWriteAction(() -> {
final Set<CharSequence> insertedMethodsNames = new THashSet();
final PhpClassReferenceResolver resolver = new PhpClassReferenceResolver();
final StringBuffer textBuf = new StringBuffer();
final PhpPsiElement scope = PhpCodeInsightUtil.findScopeForUseOperator(pluginClass);
for (final PhpNamedElementNode member : members) {
final PsiElement method = member.getPsiElement();
final PluginMethodData[] pluginMethods = this.createPluginMethods(
pluginClass,
(Method) method,
targetClassKey
);
fillTextBuffer.execute(
targetClassKey,
insertedMethodsNames,
resolver,
textBuf,
pluginMethods
);
}
insertPluginMethodsToFile(
project,
editor,
pluginFile,
pluginClass,
insertPos,
insertedMethodsNames,
resolver,
textBuf,
scope
);
});
codeStyleSettings.restore();
}
private void insertPluginMethodsToFile(
final @NotNull Project project,
final @NotNull Editor editor,
final @NotNull PsiFile pluginFile,
final PhpClass pluginClass,
final int insertPos,
final Set<CharSequence> insertedMethodsNames,
final PhpClassReferenceResolver resolver,
final StringBuffer textBuf,
final PhpPsiElement scope
) {
if (textBuf.length() > 0 && insertPos >= 0) {
editor.getDocument().insertString(insertPos, textBuf);
final int endPos = insertPos + textBuf.length();
CodeStyleManager.getInstance(project).reformatText(pluginFile, insertPos, endPos);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
}
if (!insertedMethodsNames.isEmpty()) {
final List<PsiElement> insertedMethods = collectInsertedMethods.execute(
pluginFile,
pluginClass.getNameCS(),
insertedMethodsNames
);
if (scope != null && insertedMethods != null) {
resolver.importReferences(scope, insertedMethods);
}
}
}
protected abstract PluginMethodData[] createPluginMethods(
PhpClass currentClass,
Method method,
Key<Object> targetClassKey
);
protected String getErrorMessage() {
return "No methods to generate";
}
@Override
public boolean startInWriteAction() {
return false;
}
@Nullable
protected PhpNamedElementNode[] chooseMembers(
final PhpNamedElementNode[] members,
final boolean allowEmptySelection,
final Project project
) {
final PhpNamedElementNode[] nodes = fixOrderToBeAsOriginalFiles(members).toArray(
new PhpNamedElementNode[members.length]
);
if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
final PluginGenerateMethodHandlerBase.MyMemberChooser chooser
= new PluginGenerateMethodHandlerBase.MyMemberChooser(
nodes,
allowEmptySelection,
project
);
chooser.setTitle("Choose Methods");
chooser.setCopyJavadocVisible(false);
chooser.show();
final List<PhpNamedElementNode> list = chooser.getSelectedElements();
return list == null ? null : list.toArray(new PhpNamedElementNode[0]);
}
return nodes;
}
@NotNull
protected PhpNamedElementNode[] targetMethods(
final @NotNull PhpClass phpClass,
final Key<Object> targetClassKey
) {
final TreeMap<String, PhpNamedElementNode> nodes = new TreeMap();
final GetTargetClassNamesByPluginClassName targetClassesService =
GetTargetClassNamesByPluginClassName.getInstance(phpClass.getProject());
final String currentClass = phpClass.getFQN().substring(1);
final ArrayList<String> targetClassNames = targetClassesService.execute(currentClass);
for (final String targetClassName : targetClassNames) {
final PhpClass targetClass = GetPhpClassByFQN.getInstance(
phpClass.getProject()
).execute(targetClassName);
if (targetClass == null) {
final String errorMessage = validatorBundle.message(
"validator.class.targetClassNotFound"
);
JOptionPane.showMessageDialog(
null,
errorMessage,
commonBundle.message("common.error"),
JOptionPane.ERROR_MESSAGE
);
continue;
}
if (targetClass.isFinal()) {
continue;
}
final Collection<Method> methods = targetClass.getMethods();
final Iterator methodIterator = methods.iterator();
while (methodIterator.hasNext()) {
final Method method = (Method) methodIterator.next();
if (IsPluginAllowedForMethodUtil.check(method)
&& !pluginAlreadyHasMethod(phpClass, method)) {
method.putUserData(targetClassKey, targetClass);
nodes.put(method.getName(), new PhpNamedElementNode(method));//NOPMD
}
}
}
return nodes.values().toArray(new PhpNamedElementNode[0]);
}
/**
* Plugin has a method check.
*
* @param currentClass PhpClass
* @param method Method
* @return boolean
*/
protected boolean pluginAlreadyHasMethod(
final @NotNull PhpClass currentClass,
final @NotNull Method method
) {
final Collection<Method> currentMethods = currentClass.getMethods();
final String methodName = method.getName();
final String methodPrefix = type;
final String methodSuffix = methodName.substring(0, 1).toUpperCase(Locale.getDefault())
+ methodName.substring(1);
final String pluginMethodName = methodPrefix.concat(methodSuffix);
for (final Method currentMethod: currentMethods) {
if (currentMethod.getName().equals(pluginMethodName)) {
return true;
}
}
return false;
}
/**
* Sort Order fix.
*
* @param selected PhpNamedElementNode
* @return Collection
*/
public static Collection<PhpNamedElementNode> fixOrderToBeAsOriginalFiles(
final PhpNamedElementNode... selected
) {
final List<PhpNamedElementNode> newSelected = ContainerUtil.newArrayList(selected);
Collections.sort(newSelected, (o1, o2) -> {
final PsiElement psiElement = o1.getPsiElement();
final PsiElement psiElement2 = o2.getPsiElement();
final PsiFile containingFile = psiElement.getContainingFile();
final PsiFile containingFile2 = psiElement2.getContainingFile();
return containingFile.equals(containingFile2)
? psiElement.getTextOffset() - psiElement2.getTextOffset()
: containingFile.getName().compareTo(containingFile2.getName());
});
return newSelected;
}
private static int getSuitableEditorPosition(final Editor editor, final PhpFile phpFile) {
final PsiElement currElement = phpFile.findElementAt(editor.getCaretModel().getOffset());
if (currElement != null) {
PsiElement parent = currElement.getParent();
for (PsiElement prevParent = currElement;
parent != null && !(parent instanceof PhpFile); parent = parent.getParent()) {
if (isClassMember(parent)) {
return getNextPos(parent);
}
if (parent instanceof PhpClass) {
while (prevParent != null) {
if (isClassMember(prevParent) || PhpPsiUtil.isOfType(//NOPMD
prevParent, PhpTokenTypes.chLBRACE)) {
return getNextPos(prevParent);
}
prevParent = prevParent.getPrevSibling();//NOPMD
}
for (PsiElement classChild = parent.getFirstChild();
classChild != null; classChild = classChild.getNextSibling()) {
if (PhpPsiUtil.isOfType(classChild, PhpTokenTypes.chLBRACE)) { //NOPMD
return getNextPos(classChild);
}
}
}
prevParent = parent;//NOPMD
}
}
return -1;
}
private static boolean isClassMember(final PsiElement element) {
if (element == null) {
return false;
}
final IElementType elementType = element.getNode().getElementType();
return elementType == PhpElementTypes.CLASS_FIELDS
|| elementType == PhpElementTypes.CLASS_CONSTANTS
|| elementType == PhpStubElementTypes.CLASS_METHOD;
}
private static int getNextPos(final PsiElement element) {
final PsiElement next = element.getNextSibling();
return next != null ? next.getTextOffset() : -1;//NOPMD
}
private static class MyMemberChooser extends MemberChooser<PhpNamedElementNode> {
protected MyMemberChooser(
final @NotNull PhpNamedElementNode[] nodes,
final boolean allowEmptySelection,
final @NotNull Project project) {
super(nodes, allowEmptySelection, true, project, false);
}
}
}