PY-2821: Methods moved to extension

This commit is contained in:
Ilya.Kazakevich
2017-12-18 23:15:26 +03:00
parent e401531a4a
commit f1be2c6d33
2 changed files with 39 additions and 65 deletions
@@ -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<String> 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<QualifiedName> 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());
}
);
}
}
@@ -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
}
}