|
2 | 2 | * Copyright © Magento, Inc. All rights reserved.
|
3 | 3 | * See COPYING.txt for license details.
|
4 | 4 | */
|
| 5 | + |
5 | 6 | package com.magento.idea.magento2plugin.completion.provider;
|
6 | 7 |
|
7 | 8 | import com.intellij.codeInsight.completion.CompletionParameters;
|
|
18 | 19 | import com.jetbrains.php.lang.psi.elements.PhpNamespace;
|
19 | 20 | import com.magento.idea.magento2plugin.util.RegExUtil;
|
20 | 21 | import gnu.trove.THashSet;
|
21 |
| -import org.jetbrains.annotations.NotNull; |
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Collection;
|
| 24 | +import java.util.Locale; |
24 | 25 | import java.util.regex.Matcher;
|
25 | 26 | import java.util.regex.Pattern;
|
26 | 27 | import java.util.stream.Collectors;
|
| 28 | +import org.jetbrains.annotations.NotNull; |
27 | 29 |
|
28 | 30 | public class PhpClassCompletionProvider extends CompletionProvider<CompletionParameters> {
|
29 | 31 |
|
30 |
| - final private static String PHP_CLASS_COMPLETION_REGEX |
| 32 | + private static final String PHP_CLASS_COMPLETION_REGEX |
31 | 33 | = "\\\\?" + RegExUtil.PhpRegex.FQN + "\\\\?";
|
32 | 34 |
|
| 35 | + @SuppressWarnings({ |
| 36 | + "PMD.CyclomaticComplexity", |
| 37 | + "PMD.NPathComplexity" |
| 38 | + }) |
33 | 39 | @Override
|
34 |
| - protected void addCompletions(@NotNull CompletionParameters parameters, |
35 |
| - ProcessingContext context, |
36 |
| - @NotNull CompletionResultSet result) { |
37 |
| - PsiElement position = parameters.getPosition().getOriginalElement(); |
| 40 | + protected void addCompletions( |
| 41 | + final @NotNull CompletionParameters parameters, |
| 42 | + final ProcessingContext context, |
| 43 | + final @NotNull CompletionResultSet result |
| 44 | + ) { |
| 45 | + final PsiElement position = parameters.getPosition().getOriginalElement(); |
38 | 46 | if (position == null) {
|
39 | 47 | return;
|
40 | 48 | }
|
41 |
| - String prefix = result.getPrefixMatcher().getPrefix(); |
42 |
| - Matcher matcher = Pattern.compile(PHP_CLASS_COMPLETION_REGEX).matcher(prefix); |
| 49 | + final String prefix = result.getPrefixMatcher().getPrefix(); |
| 50 | + final Matcher matcher = Pattern.compile(PHP_CLASS_COMPLETION_REGEX) |
| 51 | + .matcher(prefix); |
43 | 52 | if (!matcher.matches()) {
|
44 | 53 | return;
|
45 | 54 | }
|
46 | 55 |
|
47 |
| - String className = prefix.lastIndexOf(92) < 0 ? prefix : prefix.substring(prefix.lastIndexOf(92) + 1); |
48 |
| - String namespace = prefix.lastIndexOf(92) < 0 ? "" : prefix.substring(0, prefix.lastIndexOf(92)); |
| 56 | + final String className = prefix.lastIndexOf(92) < 0 ? prefix : prefix |
| 57 | + .substring(prefix.lastIndexOf(92) + 1); |
| 58 | + final String namespace = prefix.lastIndexOf(92) < 0 ? "" : prefix |
| 59 | + .substring(0, prefix.lastIndexOf(92)); |
49 | 60 |
|
50 |
| - PhpIndex phpIndex = PhpIndex.getInstance(parameters.getPosition().getProject()); |
| 61 | + final PhpIndex phpIndex = PhpIndex.getInstance( |
| 62 | + parameters.getPosition().getProject() |
| 63 | + ); |
51 | 64 |
|
52 | 65 | final Collection<PhpClass> phpClasses = new THashSet<>();
|
53 | 66 | Collection<String> namespaceNames = new ArrayList<>();
|
54 | 67 |
|
55 |
| - if (!className.isEmpty()) { |
56 |
| - // case for input: "SomeClassOrNamespace" |
57 |
| - |
58 |
| - // add classes |
59 |
| - Collection<String> classNames = phpIndex.getAllClassNames(new CamelHumpMatcher(className)); |
60 |
| - for (String cName: classNames) { |
61 |
| - phpClasses.addAll(phpIndex.getClassesByName(cName)); |
| 68 | + if (className.isEmpty()) { |
| 69 | + // add namespaces |
| 70 | + final Collection<PhpNamespace> namespaces |
| 71 | + = phpIndex.getNamespacesByName(("\\" + namespace) |
| 72 | + .toLowerCase(Locale.ROOT)); |
| 73 | + for (final PhpNamespace nsp: namespaces) { |
| 74 | + phpClasses.addAll(PsiTreeUtil.getChildrenOfTypeAsList( |
| 75 | + nsp.getStatements(), |
| 76 | + PhpClass.class |
| 77 | + ) |
| 78 | + ); |
62 | 79 | }
|
| 80 | + |
| 81 | + // add namespaces and classes (string representation) |
| 82 | + namespaceNames |
| 83 | + = phpIndex.getChildNamespacesByParentName("\\".concat(namespace) |
| 84 | + .concat("\\").toLowerCase(Locale.ROOT)); |
| 85 | + namespaceNames |
| 86 | + = namespaceNames.stream().map(n -> namespace.concat("\\") |
| 87 | + .concat(n)).collect(Collectors.toList()); |
| 88 | + } else { |
63 | 89 | // add interfaces
|
64 |
| - Collection<String> interfaceNames = phpIndex.getAllInterfaceNames(); |
65 |
| - interfaceNames.removeIf(i -> !i.contains(className)); |
66 |
| - for (String iName: interfaceNames) { |
| 90 | + final Collection<String> interfaceNames = phpIndex.getAllInterfaceNames(); |
| 91 | + interfaceNames.removeIf(i -> !i.contains(className.toLowerCase(Locale.ROOT))); |
| 92 | + for (final String iName: interfaceNames) { |
67 | 93 | phpClasses.addAll(phpIndex.getInterfacesByName(iName));
|
68 | 94 | }
|
69 |
| - if (!namespace.isEmpty()) { |
70 |
| - phpClasses.removeIf(c -> !c.getPresentableFQN().startsWith(namespace)); |
71 |
| - } else { |
| 95 | + // add classes |
| 96 | + final Collection<String> classNames = phpIndex.getAllClassNames( |
| 97 | + new CamelHumpMatcher(className) |
| 98 | + ); |
| 99 | + for (final String cName: classNames) { |
| 100 | + phpClasses.addAll(phpIndex.getClassesByName(cName)); |
| 101 | + } |
| 102 | + if (namespace.isEmpty()) { |
72 | 103 | namespaceNames = phpIndex.getChildNamespacesByParentName("\\");
|
73 | 104 | namespaceNames.removeIf(n -> !n.contains(prefix));
|
| 105 | + } else { |
| 106 | + phpClasses.removeIf(c -> !c.getPresentableFQN().startsWith(namespace)); |
74 | 107 | }
|
75 |
| - } else { |
76 |
| - // case for input: "Some\Namespace\ + ^+<Space>" |
77 |
| - |
78 |
| - // add namespaces |
79 |
| - Collection<PhpNamespace> namespaces = phpIndex.getNamespacesByName(("\\" + namespace).toLowerCase()); |
80 |
| - for (PhpNamespace nsp: namespaces) { |
81 |
| - phpClasses.addAll(PsiTreeUtil.getChildrenOfTypeAsList(nsp.getStatements(), PhpClass.class)); |
82 |
| - } |
83 |
| - |
84 |
| - // add namespaces and classes (string representation) |
85 |
| - namespaceNames |
86 |
| - = phpIndex.getChildNamespacesByParentName("\\".concat(namespace).concat("\\").toLowerCase()); |
87 |
| - namespaceNames |
88 |
| - = namespaceNames.stream().map(n -> namespace.concat("\\").concat(n)).collect(Collectors.toList()); |
89 | 108 | }
|
90 | 109 |
|
91 | 110 | // add all above founded items to lookup builder
|
92 |
| - // order is important (items with the same name override each other), add classes first |
93 |
| - for (PhpClass phpClass : phpClasses) { |
| 111 | + // order is important (items with the same name override each other), |
| 112 | + // add classes first |
| 113 | + for (final PhpClass phpClass : phpClasses) { |
94 | 114 | result.addElement(
|
95 | 115 | LookupElementBuilder
|
96 | 116 | .create(phpClass.getPresentableFQN())
|
97 | 117 | .withIcon(phpClass.getIcon())
|
98 | 118 | );
|
99 | 119 | }
|
100 | 120 |
|
101 |
| - for (String nsName : namespaceNames) { |
| 121 | + for (final String nsName : namespaceNames) { |
102 | 122 | result.addElement(
|
103 | 123 | LookupElementBuilder
|
104 | 124 | .create(nsName)
|
|
0 commit comments