Search in sources :

Example 61 with QualifiedName

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

the class PythonStringUtil method intersect.

@Nullable
public static String intersect(String fullName, String 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()]);
    for (int i = s1.length - 1; i >= 0; i--) {
        boolean flag = true;
        if (i > s2.length - 1) {
            continue;
        }
        for (int j = 0; j <= i; j++) {
            if (!s1[i - j].equals(s2[s2.length - j - 1])) {
                flag = false;
                break;
            }
        }
        if (flag) {
            StringBuilder res = new StringBuilder("");
            for (int j = 0; j <= i; j++) {
                if (j > 0) {
                    res.append(".");
                }
                res.append(s1[j]);
            }
            return res.toString();
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with QualifiedName

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

the class PyMoveFileHandler method updateRelativeImportsInModule.

private static void updateRelativeImportsInModule(@NotNull PyFile module) {
    final String originalLocation = module.getUserData(ORIGINAL_FILE_LOCATION);
    if (originalLocation == null) {
        return;
    }
    //module.putUserData(ORIGINAL_FILE_LOCATION, null);
    for (PyFromImportStatement statement : module.getFromImports()) {
        if (!canBeRelative(statement)) {
            continue;
        }
        final int relativeLevel = Math.max(statement.getRelativeLevel(), 1);
        final PsiFileSystemItem sourceElement = resolveRelativeImportFromModuleLocation(statement.getManager(), originalLocation, statement.getImportSource(), relativeLevel);
        if (sourceElement == null) {
            continue;
        }
        final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(sourceElement);
        replaceRelativeImportSourceWithQualifiedExpression(statement, newName);
    }
    for (PyImportElement importElement : module.getImportTargets()) {
        final PyReferenceExpression referenceExpr = importElement.getImportReferenceExpression();
        if (!canBeRelative(importElement) || referenceExpr == null) {
            continue;
        }
        final PsiFileSystemItem resolved = resolveRelativeImportFromModuleLocation(importElement.getManager(), originalLocation, referenceExpr, 1);
        if (resolved == null) {
            continue;
        }
        final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(resolved);
        replaceWithQualifiedExpression(referenceExpr, newName);
        final QualifiedName oldQualifiedName = referenceExpr.asQualifiedName();
        if (!Comparing.equal(oldQualifiedName, newName)) {
            final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(importElement);
            if (scopeOwner == null) {
                continue;
            }
            scopeOwner.accept(new PyRecursiveElementVisitor() {

                @Override
                public void visitPyReferenceExpression(PyReferenceExpression node) {
                    if (Comparing.equal(node.asQualifiedName(), oldQualifiedName)) {
                        replaceWithQualifiedExpression(node, newName);
                    } else {
                        super.visitPyReferenceExpression(node);
                    }
                }
            });
        }
    }
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 63 with QualifiedName

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

the class PyUserSkeletonsUtil method getUserSkeletonForModuleQName.

@Nullable
public static PyFile getUserSkeletonForModuleQName(@NotNull String qName, @NotNull PsiElement foothold) {
    final Sdk sdk = PythonSdkType.findPythonSdk(foothold);
    if (sdk != null) {
        final Project project = foothold.getProject();
        final PythonSdkPathCache cache = PythonSdkPathCache.getInstance(project, sdk);
        final QualifiedName cacheQName = QualifiedName.fromDottedString(USER_SKELETONS_DIR + "." + qName);
        final List<PsiElement> results = cache.get(cacheQName);
        if (results != null) {
            final PsiElement element = results.isEmpty() ? null : results.get(0);
            if (element instanceof PyFile) {
                return (PyFile) element;
            }
        }
        final VirtualFile directory = getUserSkeletonsDirectory();
        if (directory != null) {
            final PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(directory);
            PsiElement fileSkeleton = PyResolveImportUtil.resolveModuleAt(QualifiedName.fromDottedString(qName), psiDirectory, PyResolveImportUtil.fromFoothold(foothold)).stream().findFirst().orElse(null);
            if (fileSkeleton instanceof PsiDirectory) {
                fileSkeleton = PyUtil.getPackageElement((PsiDirectory) fileSkeleton, foothold);
            }
            if (fileSkeleton instanceof PyFile) {
                cache.put(cacheQName, Collections.singletonList(fileSkeleton));
                return (PyFile) fileSkeleton;
            }
        }
        cache.put(cacheQName, Collections.<PsiElement>emptyList());
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) QualifiedName(com.intellij.psi.util.QualifiedName) Sdk(com.intellij.openapi.projectRoots.Sdk) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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