move QualifiedName class from Python to platform

This commit is contained in:
Dmitry Jemerov
2013-10-16 13:21:40 +02:00
parent f2f0e41ff6
commit f7b1beede3
88 changed files with 391 additions and 542 deletions
@@ -11,7 +11,7 @@ import com.intellij.util.ObjectUtils;
import com.intellij.util.PathUtil;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -223,8 +223,8 @@ public class PythonStringUtil {
@Nullable
public static String intersect(String fullName, String elementStringValue) {
PyQualifiedName fullQName = PyQualifiedName.fromDottedString(fullName);
PyQualifiedName stringQName = PyQualifiedName.fromDottedString(elementStringValue);
QualifiedName fullQName = QualifiedName.fromDottedString(fullName);
QualifiedName stringQName = QualifiedName.fromDottedString(elementStringValue);
String[] s1 = stringQName.getComponents().toArray(new String[stringQName.getComponentCount()]);
String[] s2 = fullQName.getComponents().toArray(new String[fullQName.getComponentCount()]);
@@ -2,7 +2,7 @@ package com.jetbrains.python.codeInsight;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.resolve.QualifiedNameResolver;
import org.jetbrains.annotations.Nullable;
@@ -14,10 +14,10 @@ public abstract class PyPsiPath {
public abstract PsiElement resolve(PsiElement module);
public static class ToFile extends PyPsiPath {
private final PyQualifiedName myQualifiedName;
private final QualifiedName myQualifiedName;
public ToFile(String qualifiedName) {
myQualifiedName = PyQualifiedName.fromDottedString(qualifiedName);
myQualifiedName = QualifiedName.fromDottedString(qualifiedName);
}
@Nullable
@@ -30,10 +30,10 @@ public abstract class PyPsiPath {
}
public static class ToClassQName extends PyPsiPath {
private final PyQualifiedName myQualifiedName;
private final QualifiedName myQualifiedName;
public ToClassQName(String qualifiedName) {
myQualifiedName = PyQualifiedName.fromDottedString(qualifiedName);
myQualifiedName = QualifiedName.fromDottedString(qualifiedName);
}
@Nullable
@@ -1,7 +1,7 @@
package com.jetbrains.python.psi;
import com.intellij.psi.StubBasedPsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.stubs.PyDecoratorStub;
import org.jetbrains.annotations.Nullable;
@@ -38,6 +38,6 @@ public interface PyDecorator extends PyCallExpression, StubBasedPsiElement<PyDec
* @return dot-separated qualified name, or just {@link #getName()}'s value if no qualifiers are present.
*/
@Nullable
PyQualifiedName getQualifiedName();
QualifiedName getQualifiedName();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi;
import com.intellij.psi.PsiElement;
import com.intellij.psi.StubBasedPsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.stubs.PyFromImportStatementStub;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -22,7 +22,7 @@ public interface PyFromImportStatement extends PyImportStatementBase, StubBasedP
@Nullable PyReferenceExpression getImportSource();
@Nullable
PyQualifiedName getImportSourceQName();
QualifiedName getImportSourceQName();
/**
* @return the star in "from ... import *"
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi;
import com.intellij.psi.PsiElement;
import com.intellij.psi.StubBasedPsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.stubs.PyImportElementStub;
import org.jetbrains.annotations.Nullable;
@@ -14,7 +14,7 @@ public interface PyImportElement extends PyElement, PyImportedNameDefiner, StubB
PyReferenceExpression getImportReferenceExpression();
@Nullable
PyQualifiedName getImportedQName();
QualifiedName getImportedQName();
@Nullable
PyTargetExpression getAsNameElement();
@@ -4,7 +4,7 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.resolve.QualifiedNameResolver;
import com.jetbrains.python.psi.types.PyClassType;
import com.jetbrains.python.psi.types.PyType;
@@ -22,7 +22,7 @@ public abstract class PyPsiFacade {
}
public abstract QualifiedNameResolver qualifiedNameResolver(String qNameString);
public abstract QualifiedNameResolver qualifiedNameResolver(PyQualifiedName qualifiedName);
public abstract QualifiedNameResolver qualifiedNameResolver(QualifiedName qualifiedName);
@Nullable
public abstract PyClass findClass(String qName);
@@ -1,7 +1,7 @@
package com.jetbrains.python.psi;
import com.intellij.psi.PsiPolyVariantReference;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.resolve.PyResolveContext;
import com.jetbrains.python.psi.resolve.QualifiedResolveResult;
import org.jetbrains.annotations.NotNull;
@@ -26,7 +26,7 @@ public interface PyReferenceExpression extends PyQualifiedExpression, PyReferenc
QualifiedResolveResult followAssignmentsChain(PyResolveContext resolveContext);
@Nullable
PyQualifiedName asQualifiedName();
QualifiedName asQualifiedName();
@NotNull
PsiPolyVariantReference getReference();
@@ -4,7 +4,7 @@ import com.intellij.psi.PsiNameIdentifierOwner;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.StubBasedPsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.stubs.PyTargetExpressionStub;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -26,7 +26,7 @@ public interface PyTargetExpression extends PyQualifiedExpression, PsiNamedEleme
PyExpression findAssignedValue();
@Nullable
PyQualifiedName getAssignedQName();
QualifiedName getAssignedQName();
/**
* If the value assigned to the target expression is a call, returns the (unqualified and unresolved) name of the
@@ -35,7 +35,7 @@ public interface PyTargetExpression extends PyQualifiedExpression, PsiNamedEleme
* @return the name of the callee or null if the assigned value is not a call.
*/
@Nullable
PyQualifiedName getCalleeName();
QualifiedName getCalleeName();
@NotNull
PsiReference getReference();
@@ -2,6 +2,7 @@ package com.jetbrains.python.psi.impl;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.resolve.QualifiedNameResolveContext;
import org.jetbrains.annotations.Nullable;
@@ -12,5 +13,5 @@ public interface PyImportResolver {
ExtensionPointName<PyImportResolver> EP_NAME = ExtensionPointName.create("Pythonid.importResolver");
@Nullable
PsiElement resolveImportReference(PyQualifiedName name, QualifiedNameResolveContext context);
PsiElement resolveImportReference(QualifiedName name, QualifiedNameResolveContext context);
}
@@ -1,200 +0,0 @@
package com.jetbrains.python.psi.impl;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.util.io.StringRef;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyQualifiedExpression;
import com.jetbrains.python.psi.PyReferenceExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author yole
*/
public class PyQualifiedName {
@NotNull private final List<String> myComponents;
private PyQualifiedName(int count) {
myComponents = new ArrayList<String>(count);
}
@Nullable
public static PyQualifiedName fromReferenceChain(List<PyExpression> components) {
PyQualifiedName qName = new PyQualifiedName(components.size());
for (PyExpression component : components) {
final String refName = (component instanceof PyQualifiedExpression) ? ((PyQualifiedExpression)component).getReferencedName() : null;
if (refName == null) {
return null;
}
qName.myComponents.add(refName);
}
return qName;
}
public static PyQualifiedName fromComponents(Collection<String> components) {
PyQualifiedName qName = new PyQualifiedName(components.size());
qName.myComponents.addAll(components);
return qName;
}
public static PyQualifiedName fromComponents(String... components) {
PyQualifiedName result = new PyQualifiedName(components.length);
Collections.addAll(result.myComponents, components);
return result;
}
public PyQualifiedName append(String name) {
PyQualifiedName result = new PyQualifiedName(myComponents.size()+1);
result.myComponents.addAll(myComponents);
result.myComponents.add(name);
return result;
}
public PyQualifiedName append(PyQualifiedName qName) {
PyQualifiedName result = new PyQualifiedName(myComponents.size()+qName.getComponentCount());
result.myComponents.addAll(myComponents);
result.myComponents.addAll(qName.getComponents());
return result;
}
@NotNull
public PyQualifiedName removeLastComponent() {
return removeTail(1);
}
@NotNull
public PyQualifiedName removeTail(int count) {
int size = myComponents.size();
PyQualifiedName result = new PyQualifiedName(size);
result.myComponents.addAll(myComponents);
for (int i = 0; i < count && result.myComponents.size() > 0; i++) {
result.myComponents.remove(result.myComponents.size()-1);
}
return result;
}
@NotNull
public PyQualifiedName removeHead(int count) {
int size = myComponents.size();
PyQualifiedName result = new PyQualifiedName(size);
result.myComponents.addAll(myComponents);
for (int i = 0; i < count && result.myComponents.size() > 0; i++) {
result.myComponents.remove(0);
}
return result;
}
@NotNull
public List<String> getComponents() {
return myComponents;
}
public int getComponentCount() {
return myComponents.size();
}
public boolean matches(String... components) {
if (myComponents.size() != components.length) {
return false;
}
for (int i = 0; i < myComponents.size(); i++) {
if (!myComponents.get(i).equals(components[i])) {
return false;
}
}
return true;
}
public boolean matchesPrefix(PyQualifiedName prefix) {
if (getComponentCount() < prefix.getComponentCount()) {
return false;
}
for (int i = 0; i < prefix.getComponentCount(); i++) {
final String component = getComponents().get(i);
if (component == null || !component.equals(prefix.getComponents().get(i))) {
return false;
}
}
return true;
}
public boolean endsWith(@NotNull String suffix) {
return suffix.equals(getLastComponent());
}
public static void serialize(@Nullable PyQualifiedName qName, StubOutputStream dataStream) throws IOException {
if (qName == null) {
dataStream.writeVarInt(0);
}
else {
dataStream.writeVarInt(qName.getComponentCount());
for (String s : qName.myComponents) {
dataStream.writeName(s);
}
}
}
@Nullable
public static PyQualifiedName deserialize(StubInputStream dataStream) throws IOException {
PyQualifiedName qName;
int size = dataStream.readVarInt();
if (size == 0) {
qName = null;
}
else {
qName = new PyQualifiedName(size);
for (int i = 0; i < size; i++) {
final StringRef name = dataStream.readName();
qName.myComponents.add(name == null ? null : name.getString());
}
}
return qName;
}
@Nullable
public String getLastComponent() {
if (myComponents.size() == 0) {
return null;
}
return myComponents.get(myComponents.size()-1);
}
@Override
public String toString() {
return join(".");
}
public String join(final String separator) {
return StringUtil.join(myComponents, separator);
}
public static PyQualifiedName fromDottedString(@NotNull String refName) {
return fromComponents(refName.split("\\."));
}
@Nullable
public static PyQualifiedName fromExpression(PyExpression expr) {
return expr instanceof PyReferenceExpression ? ((PyReferenceExpression) expr).asQualifiedName() : null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PyQualifiedName that = (PyQualifiedName)o;
return myComponents.equals(that.myComponents);
}
@Override
public int hashCode() {
return myComponents.hashCode();
}
}
@@ -0,0 +1,33 @@
package com.jetbrains.python.psi.impl;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyQualifiedExpression;
import com.jetbrains.python.psi.PyReferenceExpression;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* @author yole
*/
public class PyQualifiedNameFactory {
@Nullable
public static QualifiedName fromReferenceChain(List<PyExpression> components) {
List<String> componentNames = new ArrayList<String>(components.size());
for (PyExpression component : components) {
final String refName = (component instanceof PyQualifiedExpression) ? ((PyQualifiedExpression)component).getReferencedName() : null;
if (refName == null) {
return null;
}
componentNames.add(refName);
}
return QualifiedName.fromComponents(componentNames);
}
@Nullable
public static QualifiedName fromExpression(PyExpression expr) {
return expr instanceof PyReferenceExpression ? ((PyReferenceExpression) expr).asQualifiedName() : null;
}
}
@@ -1,7 +1,7 @@
package com.jetbrains.python.psi.impl.stubs;
import com.intellij.psi.stubs.StubOutputStream;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -16,5 +16,5 @@ public interface CustomTargetExpressionStub {
void serialize(StubOutputStream stream) throws IOException;
@Nullable
PyQualifiedName getCalleeName();
QualifiedName getCalleeName();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi.resolve;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -22,5 +22,5 @@ public interface PyCanonicalPathProvider {
* @return the qualified name to use in the import statement, or null if no replacement is necessary.
*/
@Nullable
PyQualifiedName getCanonicalPath(@NotNull PyQualifiedName qName, @Nullable PsiElement foothold);
QualifiedName getCanonicalPath(@NotNull QualifiedName qName, @Nullable PsiElement foothold);
}
@@ -5,12 +5,12 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.NamedStub;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import java.util.List;
public interface PyClassStub extends NamedStub<PyClass> {
PyQualifiedName[] getSuperClasses();
QualifiedName[] getSuperClasses();
List<String> getSlots();
String getDocString();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.StubElement;
import com.jetbrains.python.psi.PyDecorator;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
/**
* Created by IntelliJ IDEA.
@@ -14,7 +14,7 @@ public interface PyDecoratorStub extends StubElement<PyDecorator> {
/**
* @return name of the decorator after the "@".
*/
PyQualifiedName getQualifiedName();
QualifiedName getQualifiedName();
//PyFunction getTarget();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.StubElement;
import com.jetbrains.python.psi.PyFromImportStatement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import org.jetbrains.annotations.Nullable;
/**
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
*/
public interface PyFromImportStatementStub extends StubElement<PyFromImportStatement> {
@Nullable
PyQualifiedName getImportSourceQName();
QualifiedName getImportSourceQName();
boolean isStarImport();
int getRelativeLevel();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.StubElement;
import com.jetbrains.python.psi.PyImportElement;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import org.jetbrains.annotations.Nullable;
/**
@@ -10,7 +10,7 @@ import org.jetbrains.annotations.Nullable;
*/
public interface PyImportElementStub extends StubElement<PyImportElement> {
@Nullable
PyQualifiedName getImportedQName();
QualifiedName getImportedQName();
String getAsName();
}
@@ -2,7 +2,7 @@ package com.jetbrains.python.psi.stubs;
import com.intellij.psi.stubs.NamedStub;
import com.jetbrains.python.psi.PyTargetExpression;
import com.jetbrains.python.psi.impl.PyQualifiedName;
import com.intellij.psi.util.QualifiedName;
import com.jetbrains.python.psi.impl.stubs.CustomTargetExpressionStub;
import org.jetbrains.annotations.Nullable;
@@ -39,7 +39,7 @@ public interface PyTargetExpressionStub extends NamedStub<PyTargetExpression> {
InitializerType getInitializerType();
@Nullable
PyQualifiedName getInitializer();
QualifiedName getInitializer();
boolean isQualified();