Search in sources :

Example 1 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyUserSkeletonsUtil method getUserSkeletonForFile.

@Nullable
private static PyFile getUserSkeletonForFile(@NotNull PyFile file) {
    final Boolean hasSkeleton = file.getUserData(HAS_SKELETON);
    if (hasSkeleton != null && !hasSkeleton) {
        return null;
    }
    final VirtualFile moduleVirtualFile = file.getVirtualFile();
    if (moduleVirtualFile != null) {
        String moduleName = QualifiedNameFinder.findShortestImportableName(file, moduleVirtualFile);
        if (moduleName != null) {
            // TODO: Delete user-skeletons altogether, meanwhile disabled user-skeletons for modules already covered by PyTypeShed
            if (PyTypeShed.INSTANCE.getWHITE_LIST().contains(moduleName)) {
                return null;
            }
            final QualifiedName qName = QualifiedName.fromDottedString(moduleName);
            for (PyCanonicalPathProvider provider : Extensions.getExtensions(PyCanonicalPathProvider.EP_NAME)) {
                final QualifiedName restored = provider.getCanonicalPath(qName, null);
                if (restored != null) {
                    moduleName = restored.toString();
                }
            }
            final PyFile skeletonFile = getUserSkeletonForModuleQName(moduleName, file);
            file.putUserData(HAS_SKELETON, skeletonFile != null);
            return skeletonFile;
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class AddImportHelper method getSortNames.

@NotNull
private static List<String> getSortNames(@NotNull PyImportStatementBase importStatement) {
    final List<String> result = new ArrayList<>();
    final PyFromImportStatement fromImport = as(importStatement, PyFromImportStatement.class);
    if (fromImport != null) {
        // because of that relative imports go to the end of an import block
        result.add(StringUtil.repeatSymbol('.', fromImport.getRelativeLevel()));
        final QualifiedName source = fromImport.getImportSourceQName();
        result.add(Objects.toString(source, ""));
        if (fromImport.isStarImport()) {
            result.add("*");
        }
    } else {
        // fake relative level
        result.add("");
    }
    for (PyImportElement importElement : importStatement.getImportElements()) {
        final QualifiedName qualifiedName = importElement.getImportedQName();
        result.add(Objects.toString(qualifiedName, ""));
        result.add(StringUtil.notNullize(importElement.getAsName()));
    }
    return result;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class AddImportHelper method addImportStatement.

/**
   * Adds an import statement, if it doesn't exist yet, presumably below all other initial imports in the file.
   *
   * @param file   where to operate
   * @param name   which to import (qualified is OK)
   * @param asName optional name for 'as' clause
   * @param anchor place where the imported name was used. It will be used to determine proper block where new import should be inserted,
   *               e.g. inside conditional block or try/except statement. Also if anchor is another import statement, new import statement
   *               will be inserted right after it.
   * @return whether import statement was actually added
   */
public static boolean addImportStatement(@NotNull PsiFile file, @NotNull String name, @Nullable String asName, @Nullable ImportPriority priority, @Nullable PsiElement anchor) {
    if (!(file instanceof PyFile)) {
        return false;
    }
    final List<PyImportElement> existingImports = ((PyFile) file).getImportTargets();
    for (PyImportElement element : existingImports) {
        final QualifiedName qName = element.getImportedQName();
        if (qName != null && name.equals(qName.toString())) {
            if ((asName != null && asName.equals(element.getAsName())) || (asName == null && element.getAsName() == null)) {
                return false;
            }
        }
    }
    final PyElementGenerator generator = PyElementGenerator.getInstance(file.getProject());
    final LanguageLevel languageLevel = LanguageLevel.forElement(file);
    final PyImportStatement importNodeToInsert = generator.createImportStatement(languageLevel, name, asName);
    final PyImportStatementBase importStatement = PsiTreeUtil.getParentOfType(anchor, PyImportStatementBase.class, false);
    final PsiElement insertParent = importStatement != null && importStatement.getContainingFile() == file ? importStatement.getParent() : file;
    try {
        if (anchor instanceof PyImportStatementBase) {
            insertParent.addAfter(importNodeToInsert, anchor);
        } else {
            insertParent.addBefore(importNodeToInsert, getInsertPosition(insertParent, importNodeToInsert, priority));
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return true;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 4 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PythonImportUtils method addSymbolImportCandidates.

private static void addSymbolImportCandidates(PyElement node, String refText, @Nullable String asName, AutoImportQuickFix fix, Set<String> seenCandidateNames, PsiFile existingImportFile) {
    Project project = node.getProject();
    List<PsiElement> symbols = new ArrayList<>();
    symbols.addAll(PyClassNameIndex.find(refText, project, true));
    GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(node);
    if (!isQualifier(node)) {
        symbols.addAll(PyFunctionNameIndex.find(refText, project, scope));
    }
    symbols.addAll(PyVariableNameIndex.find(refText, project, scope));
    if (isPossibleModuleReference(node)) {
        symbols.addAll(findImportableModules(node.getContainingFile(), refText, project, scope));
    }
    if (!symbols.isEmpty()) {
        for (PsiElement symbol : symbols) {
            if (isIndexableTopLevel(symbol)) {
                // we only want top-level symbols
                PsiFileSystemItem srcfile = symbol instanceof PsiFileSystemItem ? ((PsiFileSystemItem) symbol).getParent() : symbol.getContainingFile();
                if (srcfile != null && isAcceptableForImport(node, existingImportFile, srcfile)) {
                    QualifiedName importPath = QualifiedNameFinder.findCanonicalImportPath(symbol, node);
                    if (importPath == null) {
                        continue;
                    }
                    if (symbol instanceof PsiFileSystemItem) {
                        importPath = importPath.removeTail(1);
                    }
                    final String symbolImportQName = importPath.append(refText).toString();
                    if (!seenCandidateNames.contains(symbolImportQName)) {
                        // a new, valid hit
                        fix.addImport(symbol, srcfile, importPath, asName);
                        seenCandidateNames.add(symbolImportQName);
                    }
                }
            }
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 5 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class CompatibilityVisitor method visitPyImportStatement.

@Override
public void visitPyImportStatement(PyImportStatement node) {
    super.visitPyImportStatement(node);
    final PyIfStatement ifParent = PsiTreeUtil.getParentOfType(node, PyIfStatement.class);
    if (ifParent != null)
        return;
    for (PyImportElement importElement : node.getImportElements()) {
        final QualifiedName qName = importElement.getImportedQName();
        if (qName != null) {
            if (qName.matches("builtins")) {
                registerForAllMatchingVersions(level -> !level.isPy3K(), " not have module builtins", node, new ReplaceBuiltinsQuickFix());
            } else if (qName.matches("__builtin__")) {
                registerForAllMatchingVersions(LanguageLevel::isPy3K, " not have module __builtin__", node, new ReplaceBuiltinsQuickFix());
            }
        }
    }
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName)

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