Search in sources :

Example 11 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyModuleFindUsagesHandler method findReferencesToHighlight.

@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull PsiElement target, @NotNull SearchScope searchScope) {
    if (target instanceof PyImportedModule) {
        target = ((PyImportedModule) target).resolve();
    }
    if (target instanceof PyFile && PyNames.INIT_DOT_PY.equals(((PyFile) target).getName())) {
        List<PsiReference> result = new ArrayList<>();
        result.addAll(super.findReferencesToHighlight(target, searchScope));
        PsiElement targetDir = PyUtil.turnInitIntoDir(target);
        if (targetDir != null) {
            result.addAll(ReferencesSearch.search(targetDir, searchScope, false).findAll());
        }
        return result;
    }
    return super.findReferencesToHighlight(target, searchScope);
}
Also used : PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyDunderAllReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final List<LookupElement> result = new ArrayList<>();
    PyFile containingFile = (PyFile) getElement().getContainingFile().getOriginalFile();
    final List<String> dunderAll = containingFile.getDunderAll();
    final Set<String> seenNames = new HashSet<>();
    if (dunderAll != null) {
        seenNames.addAll(dunderAll);
    }
    containingFile.processDeclarations(new PsiScopeProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof PsiNamedElement && !(element instanceof LightNamedElement)) {
                final String name = ((PsiNamedElement) element).getName();
                if (name != null && PyUtil.getInitialUnderscores(name) == 0 && !seenNames.contains(name)) {
                    seenNames.add(name);
                    result.add(LookupElementBuilder.createWithSmartPointer(name, element).withIcon(element.getIcon(0)));
                }
            } else if (element instanceof PyImportElement) {
                final String visibleName = ((PyImportElement) element).getVisibleName();
                if (visibleName != null && !seenNames.contains(visibleName)) {
                    seenNames.add(visibleName);
                    result.add(LookupElementBuilder.createWithSmartPointer(visibleName, element));
                }
            }
            return true;
        }

        @Override
        public <T> T getHint(@NotNull Key<T> hintKey) {
            return null;
        }

        @Override
        public void handleEvent(@NotNull Event event, @Nullable Object associated) {
        }
    }, ResolveState.initial(), null, containingFile);
    return ArrayUtil.toObjectArray(result);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyImportElement(com.jetbrains.python.psi.PyImportElement) ArrayList(java.util.ArrayList) PsiScopeProcessor(com.intellij.psi.scope.PsiScopeProcessor) PyFile(com.jetbrains.python.psi.PyFile) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveState(com.intellij.psi.ResolveState) LightNamedElement(com.jetbrains.python.psi.impl.LightNamedElement) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyContainingFileRenamerFactory method isApplicable.

@Override
public boolean isApplicable(PsiElement element) {
    if (!(element instanceof PyClass)) {
        return false;
    }
    ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class);
    if (scopeOwner instanceof PyFile) {
        String className = ((PyClass) element).getName();
        String fileName = FileUtil.getNameWithoutExtension(scopeOwner.getName());
        return fileName.equalsIgnoreCase(className);
    }
    return false;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyFile(com.jetbrains.python.psi.PyFile)

Example 14 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class UnresolvedRefAddFutureImportQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    PyFile file = (PyFile) element.getContainingFile();
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class, "from __future__ import with_statement");
    file.addBefore(statement, file.getStatements().get(0));
}
Also used : PyFromImportStatement(com.jetbrains.python.psi.PyFromImportStatement) PyFile(com.jetbrains.python.psi.PyFile) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PyFile

use of com.jetbrains.python.psi.PyFile in project intellij-community by JetBrains.

the class PyModuleNameIndex method find.

@NotNull
public static List<PyFile> find(@NotNull String name, @NotNull Project project, boolean includeNonProjectItems) {
    final List<PyFile> results = new ArrayList<>();
    final GlobalSearchScope scope = includeNonProjectItems ? PyProjectScopeBuilder.excludeSdkTestsScope(project) : GlobalSearchScope.projectScope(project);
    final Collection<VirtualFile> files = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
    for (VirtualFile virtualFile : files) {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        if (psiFile instanceof PyFile) {
            if (!PyUserSkeletonsUtil.isUnderUserSkeletonsDirectory(psiFile)) {
                results.add((PyFile) psiFile);
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) PyFile(com.jetbrains.python.psi.PyFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyFile (com.jetbrains.python.psi.PyFile)42 PsiElement (com.intellij.psi.PsiElement)15 PsiFile (com.intellij.psi.PsiFile)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Project (com.intellij.openapi.project.Project)9 PyFunction (com.jetbrains.python.psi.PyFunction)9 PyClass (com.jetbrains.python.psi.PyClass)8 NotNull (org.jetbrains.annotations.NotNull)8 ArrayList (java.util.ArrayList)7 PsiDirectory (com.intellij.psi.PsiDirectory)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PyStatement (com.jetbrains.python.psi.PyStatement)3 PyBlockEvaluator (com.jetbrains.python.psi.impl.blockEvaluator.PyBlockEvaluator)3 File (java.io.File)3 Nullable (org.jetbrains.annotations.Nullable)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 Editor (com.intellij.openapi.editor.Editor)2 Module (com.intellij.openapi.module.Module)2 PsiNamedElement (com.intellij.psi.PsiNamedElement)2