use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyTargetExpressionElementType method deserialize.
@NotNull
public PyTargetExpressionStub deserialize(@NotNull final StubInputStream stream, final StubElement parentStub) throws IOException {
String name = StringRef.toString(stream.readName());
String docString = stream.readUTFFast();
if (docString.isEmpty()) {
docString = null;
}
PyTargetExpressionStub.InitializerType initializerType = PyTargetExpressionStub.InitializerType.fromIndex(stream.readVarInt());
final StringRef typeCommentRef = stream.readName();
final String typeComment = typeCommentRef == null ? null : typeCommentRef.getString();
if (initializerType == PyTargetExpressionStub.InitializerType.Custom) {
final String typeName = stream.readName().getString();
for (CustomTargetExpressionStubType type : getCustomStubTypes()) {
if (type.getClass().getCanonicalName().equals(typeName)) {
CustomTargetExpressionStub stub = type.deserializeStub(stream);
return new PyTargetExpressionStubImpl(name, docString, typeComment, stub, parentStub);
}
}
throw new IOException("Unknown custom stub type " + typeName);
}
QualifiedName initializer = QualifiedName.deserialize(stream);
boolean isQualified = stream.readBoolean();
return new PyTargetExpressionStubImpl(name, docString, initializerType, initializer, isQualified, typeComment, parentStub);
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyiUtil method getOriginalFile.
@Nullable
private static PyFile getOriginalFile(@NotNull PyiFile file) {
final QualifiedName name = QualifiedNameFinder.findCanonicalImportPath(file, file);
if (name == null) {
return null;
}
final PyQualifiedNameResolveContext context = PyResolveImportUtil.fromFoothold(file).copyWithoutStubs();
return PyUtil.as(PyResolveImportUtil.resolveQualifiedName(name, context).stream().findFirst().orElse(null), PyFile.class);
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class QualifiedNameFinder method doFindCanonicalImportPath.
@Nullable
private static QualifiedName doFindCanonicalImportPath(@NotNull Couple<PsiElement> param) {
final PsiElement symbol = param.getFirst();
final PsiElement foothold = param.getSecond();
final PsiFileSystemItem srcfile = PyPsiUtils.getFileSystemItem(symbol);
if (srcfile == null) {
return null;
}
VirtualFile virtualFile = srcfile.getVirtualFile();
if (virtualFile == null) {
return null;
}
if (srcfile instanceof PsiFile && symbol instanceof PsiNamedElement && !(symbol instanceof PsiFileSystemItem)) {
PsiElement toplevel = symbol;
if (symbol instanceof PyFunction) {
final PyClass containingClass = ((PyFunction) symbol).getContainingClass();
if (containingClass != null) {
toplevel = containingClass;
}
}
PsiDirectory dir = ((PsiFile) srcfile).getContainingDirectory();
while (dir != null) {
PsiFile initPy = dir.findFile(PyNames.INIT_DOT_PY);
if (initPy == null) {
break;
}
if (initPy instanceof PyFile) {
//noinspection ConstantConditions
final List<RatedResolveResult> resolved = ((PyFile) initPy).multiResolveName(((PsiNamedElement) toplevel).getName());
final PsiElement finalTopLevel = toplevel;
if (resolved.stream().anyMatch(r -> r.getElement() == finalTopLevel)) {
virtualFile = dir.getVirtualFile();
}
}
dir = dir.getParentDirectory();
}
}
final QualifiedName qname = findShortestImportableQName(foothold != null ? foothold : symbol, virtualFile);
if (qname != null) {
for (PyCanonicalPathProvider provider : Extensions.getExtensions(PyCanonicalPathProvider.EP_NAME)) {
final QualifiedName restored = provider.getCanonicalPath(qname, foothold);
if (restored != null) {
return restored;
}
}
}
return qname;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyClassRefactoringUtil method insertImport.
public static boolean insertImport(@NotNull PsiElement anchor, @NotNull PsiNamedElement element, @Nullable String asName, boolean preferFromImport) {
if (PyBuiltinCache.getInstance(element).isBuiltin(element))
return false;
final PsiFileSystemItem elementSource = element instanceof PsiDirectory ? (PsiFileSystemItem) element : element.getContainingFile();
final PsiFile file = anchor.getContainingFile();
if (elementSource == file)
return false;
final QualifiedName qname = QualifiedNameFinder.findCanonicalImportPath(element, anchor);
if (qname == null || !isValidQualifiedName(qname)) {
return false;
}
final QualifiedName containingQName;
final String importedName;
final boolean importingModuleOrPackage = element instanceof PyFile || element instanceof PsiDirectory;
if (importingModuleOrPackage) {
containingQName = qname.removeLastComponent();
importedName = qname.getLastComponent();
} else {
containingQName = qname;
importedName = getOriginalName(element);
}
final AddImportHelper.ImportPriority priority = AddImportHelper.getImportPriority(anchor, elementSource);
if (preferFromImport && !containingQName.getComponents().isEmpty() || !importingModuleOrPackage) {
return AddImportHelper.addOrUpdateFromImportStatement(file, containingQName.toString(), importedName, asName, priority, anchor);
} else {
return AddImportHelper.addImportStatement(file, containingQName.append(importedName).toString(), asName, priority, anchor);
}
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyClassElementType method deserialize.
@NotNull
public PyClassStub deserialize(@NotNull final StubInputStream dataStream, final StubElement parentStub) throws IOException {
final String name = StringRef.toString(dataStream.readName());
final int superClassCount = dataStream.readByte();
final Map<QualifiedName, QualifiedName> superClasses = new LinkedHashMap<>();
for (int i = 0; i < superClassCount; i++) {
superClasses.put(QualifiedName.deserialize(dataStream), QualifiedName.deserialize(dataStream));
}
final QualifiedName metaClass = QualifiedName.deserialize(dataStream);
final List<String> slots = PyFileElementType.readNullableList(dataStream);
final String docStringInStub = dataStream.readUTFFast();
final String docString = docStringInStub.length() > 0 ? docStringInStub : null;
return new PyClassStubImpl(name, parentStub, superClasses, metaClass, slots, docString, getStubElementType());
}
Aggregations