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