Search in sources :

Example 21 with QualifiedName

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);
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) StringRef(com.intellij.util.io.StringRef) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with QualifiedName

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);
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with QualifiedName

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with QualifiedName

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);
    }
}
Also used : AddImportHelper(com.jetbrains.python.codeInsight.imports.AddImportHelper) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 25 with QualifiedName

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());
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

QualifiedName (com.intellij.psi.util.QualifiedName)63 Nullable (org.jetbrains.annotations.Nullable)22 NotNull (org.jetbrains.annotations.NotNull)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Project (com.intellij.openapi.project.Project)8 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)7 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFile (com.intellij.psi.PsiFile)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 StringRef (com.intellij.util.io.StringRef)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1