Search in sources :

Example 1 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)

Example 2 with PyFile

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

the class PythonBuiltinReferenceResolveProvider method resolveName.

@NotNull
@Override
public List<RatedResolveResult> resolveName(@NotNull PyQualifiedExpression element, @NotNull TypeEvalContext context) {
    final String referencedName = element.getReferencedName();
    if (referencedName == null) {
        return Collections.emptyList();
    }
    final List<RatedResolveResult> result = new ArrayList<>();
    final PyBuiltinCache builtinCache = PyBuiltinCache.getInstance(PyPsiUtils.getRealContext(element));
    // resolve to module __doc__
    if (PyNames.DOC.equals(referencedName)) {
        result.addAll(Optional.ofNullable(builtinCache.getObjectType()).map(type -> type.resolveMember(referencedName, element, AccessDirection.of(element), PyResolveContext.noImplicits().withTypeEvalContext(context))).orElse(Collections.emptyList()));
    }
    // ...as a builtin symbol
    final PyFile builtinsFile = builtinCache.getBuiltinsFile();
    if (builtinsFile != null && !PyUtil.isClassPrivateName(referencedName)) {
        for (RatedResolveResult resolveResult : builtinsFile.multiResolveName(referencedName)) {
            result.add(new ImportedResolveResult(resolveResult.getElement(), resolveResult.getRate(), null));
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) PyFile(com.jetbrains.python.psi.PyFile) PyBuiltinCache(com.jetbrains.python.psi.impl.PyBuiltinCache) NotNull(org.jetbrains.annotations.NotNull)

Example 3 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 4 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 5 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)

Aggregations

PyFile (com.jetbrains.python.psi.PyFile)50 PsiElement (com.intellij.psi.PsiElement)20 PsiFile (com.intellij.psi.PsiFile)17 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 ArrayList (java.util.ArrayList)8 NotNull (org.jetbrains.annotations.NotNull)8 TargetInfo (com.google.idea.blaze.base.dependencies.TargetInfo)4 BlazeCommandRunConfigurationCommonState (com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState)4 PsiDirectory (com.intellij.psi.PsiDirectory)4 PyFromImportStatement (com.jetbrains.python.psi.PyFromImportStatement)4 File (java.io.File)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PyStatement (com.jetbrains.python.psi.PyStatement)3 Nullable (org.jetbrains.annotations.Nullable)3 Test (org.junit.Test)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2