From b4c7ea9a4822debca7c956521af96207c2867321 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 10 Mar 2021 15:06:01 +0200 Subject: [PATCH 1/2] 502: Class completion doesn't display interfaces. --- .../provider/PhpClassCompletionProvider.java | 102 +++++++++++------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/completion/provider/PhpClassCompletionProvider.java b/src/com/magento/idea/magento2plugin/completion/provider/PhpClassCompletionProvider.java index b86dde94d..30be46ece 100644 --- a/src/com/magento/idea/magento2plugin/completion/provider/PhpClassCompletionProvider.java +++ b/src/com/magento/idea/magento2plugin/completion/provider/PhpClassCompletionProvider.java @@ -2,6 +2,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + package com.magento.idea.magento2plugin.completion.provider; import com.intellij.codeInsight.completion.CompletionParameters; @@ -18,79 +19,98 @@ import com.jetbrains.php.lang.psi.elements.PhpNamespace; import com.magento.idea.magento2plugin.util.RegExUtil; import gnu.trove.THashSet; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collection; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import org.jetbrains.annotations.NotNull; public class PhpClassCompletionProvider extends CompletionProvider { - final private static String PHP_CLASS_COMPLETION_REGEX + private static final String PHP_CLASS_COMPLETION_REGEX = "\\\\?" + RegExUtil.PhpRegex.FQN + "\\\\?"; + @SuppressWarnings({ + "PMD.CyclomaticComplexity", + "PMD.NPathComplexity" + }) @Override - protected void addCompletions(@NotNull CompletionParameters parameters, - ProcessingContext context, - @NotNull CompletionResultSet result) { - PsiElement position = parameters.getPosition().getOriginalElement(); + protected void addCompletions( + final @NotNull CompletionParameters parameters, + final ProcessingContext context, + final @NotNull CompletionResultSet result + ) { + final PsiElement position = parameters.getPosition().getOriginalElement(); if (position == null) { return; } - String prefix = result.getPrefixMatcher().getPrefix(); - Matcher matcher = Pattern.compile(PHP_CLASS_COMPLETION_REGEX).matcher(prefix); + final String prefix = result.getPrefixMatcher().getPrefix(); + final Matcher matcher = Pattern.compile(PHP_CLASS_COMPLETION_REGEX) + .matcher(prefix); if (!matcher.matches()) { return; } - String className = prefix.lastIndexOf(92) < 0 ? prefix : prefix.substring(prefix.lastIndexOf(92) + 1); - String namespace = prefix.lastIndexOf(92) < 0 ? "" : prefix.substring(0, prefix.lastIndexOf(92)); + final String className = prefix.lastIndexOf(92) < 0 ? prefix : prefix + .substring(prefix.lastIndexOf(92) + 1); + final String namespace = prefix.lastIndexOf(92) < 0 ? "" : prefix + .substring(0, prefix.lastIndexOf(92)); - PhpIndex phpIndex = PhpIndex.getInstance(parameters.getPosition().getProject()); + final PhpIndex phpIndex = PhpIndex.getInstance( + parameters.getPosition().getProject() + ); final Collection phpClasses = new THashSet<>(); Collection namespaceNames = new ArrayList<>(); - if (!className.isEmpty()) { - // case for input: "SomeClassOrNamespace" - - // add classes - Collection classNames = phpIndex.getAllClassNames(new CamelHumpMatcher(className)); - for (String cName: classNames) { - phpClasses.addAll(phpIndex.getClassesByName(cName)); + if (className.isEmpty()) { + // add namespaces + final Collection namespaces + = phpIndex.getNamespacesByName(("\\" + namespace) + .toLowerCase(Locale.ROOT)); + for (final PhpNamespace nsp: namespaces) { + phpClasses.addAll(PsiTreeUtil.getChildrenOfTypeAsList( + nsp.getStatements(), + PhpClass.class + ) + ); } + + // add namespaces and classes (string representation) + namespaceNames + = phpIndex.getChildNamespacesByParentName("\\".concat(namespace) + .concat("\\").toLowerCase(Locale.ROOT)); + namespaceNames + = namespaceNames.stream().map(n -> namespace.concat("\\") + .concat(n)).collect(Collectors.toList()); + } else { // add interfaces - Collection interfaceNames = phpIndex.getAllInterfaceNames(); - interfaceNames.removeIf(i -> !i.contains(className)); - for (String iName: interfaceNames) { + final Collection interfaceNames = phpIndex.getAllInterfaceNames(); + interfaceNames.removeIf(i -> !i.contains(className.toLowerCase(Locale.ROOT))); + for (final String iName: interfaceNames) { phpClasses.addAll(phpIndex.getInterfacesByName(iName)); } - if (!namespace.isEmpty()) { - phpClasses.removeIf(c -> !c.getPresentableFQN().startsWith(namespace)); - } else { + // add classes + final Collection classNames = phpIndex.getAllClassNames( + new CamelHumpMatcher(className) + ); + for (final String cName: classNames) { + phpClasses.addAll(phpIndex.getClassesByName(cName)); + } + if (namespace.isEmpty()) { namespaceNames = phpIndex.getChildNamespacesByParentName("\\"); namespaceNames.removeIf(n -> !n.contains(prefix)); + } else { + phpClasses.removeIf(c -> !c.getPresentableFQN().startsWith(namespace)); } - } else { - // case for input: "Some\Namespace\ + ^+" - - // add namespaces - Collection namespaces = phpIndex.getNamespacesByName(("\\" + namespace).toLowerCase()); - for (PhpNamespace nsp: namespaces) { - phpClasses.addAll(PsiTreeUtil.getChildrenOfTypeAsList(nsp.getStatements(), PhpClass.class)); - } - - // add namespaces and classes (string representation) - namespaceNames - = phpIndex.getChildNamespacesByParentName("\\".concat(namespace).concat("\\").toLowerCase()); - namespaceNames - = namespaceNames.stream().map(n -> namespace.concat("\\").concat(n)).collect(Collectors.toList()); } // add all above founded items to lookup builder - // order is important (items with the same name override each other), add classes first - for (PhpClass phpClass : phpClasses) { + // order is important (items with the same name override each other), + // add classes first + for (final PhpClass phpClass : phpClasses) { result.addElement( LookupElementBuilder .create(phpClass.getPresentableFQN()) @@ -98,7 +118,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, ); } - for (String nsName : namespaceNames) { + for (final String nsName : namespaceNames) { result.addElement( LookupElementBuilder .create(nsName) From 9c918192b9890c36dde60fe87c20698d6c195c9b Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 10 Mar 2021 15:10:41 +0200 Subject: [PATCH 2/2] 502: Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c0aa245..c45bce42a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0). - Require restart on plugin update due to using native libraries +### Fixed + +- Class completion doesn't display interfaces + ## 3.1.2 ### Fixed