From f1be2c6d3327661a2fc6f6ab6771069ca3092f6c Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 12 Dec 2017 02:25:50 +0300 Subject: [PATCH] PY-2821: Methods moved to extension --- .../python/nameResolver/FQNamesProvider.java | 66 +------------------ .../extensions/FQNamesProviderExt.kt | 38 +++++++++++ 2 files changed, 39 insertions(+), 65 deletions(-) create mode 100644 python/src/com/jetbrains/extensions/FQNamesProviderExt.kt diff --git a/python/psi-api/src/com/jetbrains/python/nameResolver/FQNamesProvider.java b/python/psi-api/src/com/jetbrains/python/nameResolver/FQNamesProvider.java index 7d47d9000d08..fed6091a53a2 100644 --- a/python/psi-api/src/com/jetbrains/python/nameResolver/FQNamesProvider.java +++ b/python/psi-api/src/com/jetbrains/python/nameResolver/FQNamesProvider.java @@ -15,17 +15,9 @@ */ package com.jetbrains.python.nameResolver; -import com.intellij.navigation.NavigationItem; -import com.intellij.psi.util.QualifiedName; -import com.intellij.util.ArrayUtil; import com.jetbrains.python.psi.PyQualifiedNameOwner; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - /** * Some enum value that represents one or more fully qualified names for some function * @@ -38,32 +30,6 @@ public interface FQNamesProvider { @NotNull String[] getNames(); - @NotNull - default QualifiedName[] getQualifiedNames() { - return Arrays.stream(getNames()).map(QualifiedName::fromDottedString).toArray(QualifiedName[]::new); - } - - - @NotNull - default String getFirstName() { - return getNames()[0]; - } - - default boolean isShortNameMatches(@NotNull final NavigationItem item) { - final String name = item.getName(); - if (name == null) { - return false; - } - return getShortNames().contains(name); - } - - /** - * @return all names in unqualified ("after last dot") format - */ - @NotNull - default List getShortNames() { - return Arrays.stream(getQualifiedNames()).map(QualifiedName::getLastComponent).filter(o -> o != null).collect(Collectors.toList()); - } /** * @return is name of class (true) or function (false) @@ -71,40 +37,10 @@ public interface FQNamesProvider { boolean isClass(); /** - * @return if element should be checked by full name conformity by {@link #isNameMatches(PyQualifiedNameOwner)} + * @return if element should be checked by full name conformity by {@link PyQualifiedNameOwner#getQualifiedName()} * or only name and package should be checked - * @see #isNameMatches(PyQualifiedNameOwner) */ default boolean alwaysCheckQualifiedName() { return true; } - - /** - * Checks if element name matches. {@link #alwaysCheckQualifiedName()} controls if full name should be checked, or only last and first - * parts (name and package) are enough. It may be used for cases when physical FQN is not documented. - */ - default boolean isNameMatches(@NotNull final PyQualifiedNameOwner qualifiedNameOwner) { - final String qualifiedName = qualifiedNameOwner.getQualifiedName(); - if (qualifiedName == null) { - return false; - } - - // Only check qualified name - if (alwaysCheckQualifiedName()) { - return ArrayUtil.contains(qualifiedName, getNames()); - } - - // Relaxed check: package and name - final QualifiedName elementQualifiedName = QualifiedName.fromDottedString(qualifiedName); - final Stream nameStream = Arrays.stream(getQualifiedNames()); - return nameStream.anyMatch((name) -> { - final String first = name.getFirstComponent(); - final String last = name.getLastComponent(); - return first != null - && last != null - && first.equals(elementQualifiedName.getFirstComponent()) - && last.equals(elementQualifiedName.getLastComponent()); - } - ); - } } diff --git a/python/src/com/jetbrains/extensions/FQNamesProviderExt.kt b/python/src/com/jetbrains/extensions/FQNamesProviderExt.kt new file mode 100644 index 000000000000..c5031e68c788 --- /dev/null +++ b/python/src/com/jetbrains/extensions/FQNamesProviderExt.kt @@ -0,0 +1,38 @@ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.jetbrains.extensions + +import com.intellij.navigation.NavigationItem +import com.intellij.psi.util.QualifiedName +import com.jetbrains.python.nameResolver.FQNamesProvider +import com.jetbrains.python.psi.PyQualifiedNameOwner + +fun FQNamesProvider.getQualifiedNames() = names.map(QualifiedName::fromDottedString).toTypedArray() + +fun FQNamesProvider.getFirstName() = names[0]!! + +fun FQNamesProvider.shortNameMatches(item: NavigationItem) = item.name.run { this in getShortNames() } + +/** + * @return all names in unqualified ("after last dot") format + */ +fun FQNamesProvider.getShortNames() = getQualifiedNames().mapNotNull(QualifiedName::getLastComponent).toList() + +/** + * Checks if element name matches. [.alwaysCheckQualifiedName] controls if full name should be checked, or only last and first + * parts (name and package) are enough. It may be used for cases when physical FQN is not documented. + */ +fun FQNamesProvider.isNameMatches(qualifiedNameOwner: PyQualifiedNameOwner): Boolean { + val qualifiedName = qualifiedNameOwner.qualifiedName ?: return false + + // Only check qualified name + if (alwaysCheckQualifiedName()) { + return qualifiedName in names + } + + // Relaxed check: package and name + val elementQualifiedName = QualifiedName.fromDottedString(qualifiedName) + return getQualifiedNames().any { + return it.firstComponent == elementQualifiedName.firstComponent && + it.lastComponent == elementQualifiedName.lastComponent + } +} \ No newline at end of file