Search in sources :

Example 86 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class PythonImportUtils method proposeImportFix.

@Nullable
public static AutoImportQuickFix proposeImportFix(final PyElement node, PsiReference reference) {
    final String text = reference.getElement().getText();
    // text of the part we're working with
    final String refText = reference.getRangeInElement().substring(text);
    // don't propose meaningless auto imports if no interpreter is configured
    final Module module = ModuleUtilCore.findModuleForPsiElement(node);
    if (module != null && PythonSdkType.findPythonSdk(module) == null) {
        return null;
    }
    // don't show auto-import fix if we're trying to reference a variable which is defined below in the same scope
    ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(node, ScopeOwner.class);
    if (scopeOwner != null && ControlFlowCache.getScope(scopeOwner).containsDeclaration(refText)) {
        return null;
    }
    AutoImportQuickFix fix = addCandidates(node, reference, refText, null);
    if (fix != null)
        return fix;
    final String packageName = PyPackageAliasesProvider.commonImportAliases.get(refText);
    if (packageName != null) {
        fix = addCandidates(node, reference, packageName, refText);
        if (fix != null)
            return fix;
    }
    return null;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 87 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class AddImportHelper method getImportPriority.

@NotNull
public static ImportPriority getImportPriority(@NotNull PsiElement importLocation, @NotNull PsiFileSystemItem toImport) {
    final VirtualFile vFile = toImport.getVirtualFile();
    if (vFile == null) {
        return UNRESOLVED_SYMBOL_PRIORITY;
    }
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(toImport.getProject());
    final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    if (fileIndex.isInContent(vFile) && !fileIndex.isInLibraryClasses(vFile)) {
        return ImportPriority.PROJECT;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(importLocation);
    final Sdk pythonSdk = module != null ? PythonSdkType.findPythonSdk(module) : projectRootManager.getProjectSdk();
    return PythonSdkType.isStdLib(vFile, pythonSdk) ? ImportPriority.BUILTIN : ImportPriority.THIRD_PARTY;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class ImportCandidateHolder method getRelevance.

int getRelevance() {
    final Project project = myImportable.getProject();
    final PsiFile psiFile = myImportable.getContainingFile();
    final VirtualFile vFile = psiFile == null ? null : psiFile.getVirtualFile();
    if (vFile == null)
        return 0;
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    // files under project source are most relevant
    final Module module = fileIndex.getModuleForFile(vFile);
    if (module != null)
        return 3;
    // then come files directly under Lib
    if (vFile.getParent().getName().equals("Lib"))
        return 2;
    // tests we don't want
    if (vFile.getParent().getName().equals("test"))
        return 0;
    return 1;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 89 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class PythonTestLegacyConfigurationProducer method isTestFolder.

protected boolean isTestFolder(@NotNull final VirtualFile virtualFile, @NotNull final Project project) {
    @NonNls final String name = virtualFile.getName();
    final HashSet<VirtualFile> roots = Sets.newHashSet();
    final Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        roots.addAll(PyUtil.getSourceRoots(module));
    }
    Collections.addAll(roots, ProjectRootManager.getInstance(project).getContentRoots());
    return name.toLowerCase().contains("test") || roots.contains(virtualFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) Module(com.intellij.openapi.module.Module)

Example 90 with Module

use of com.intellij.openapi.module.Module in project intellij-community by JetBrains.

the class PyInterpreterUsagesCollector method getProjectUsages.

@NotNull
@Override
public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) throws CollectUsagesException {
    Set<UsageDescriptor> result = new HashSet<>();
    for (Module m : ModuleManager.getInstance(project).getModules()) {
        Sdk pythonSdk = PythonSdkType.findPythonSdk(m);
        if (pythonSdk != null) {
            String versionString = pythonSdk.getVersionString();
            if (StringUtil.isEmpty(versionString)) {
                versionString = "unknown version";
            }
            if (PythonSdkType.isRemote(pythonSdk)) {
                versionString = versionString + " (" + getRemoteSuffix(pythonSdk) + ")";
            }
            if (PythonSdkType.isVirtualEnv(pythonSdk)) {
                versionString += " [virtualenv]";
            }
            if (PythonSdkType.isCondaVirtualEnv(pythonSdk)) {
                versionString += " [condavenv]";
            }
            result.add(new UsageDescriptor(versionString, 1));
        }
    }
    return result;
}
Also used : Sdk(com.intellij.openapi.projectRoots.Sdk) UsageDescriptor(com.intellij.internal.statistic.beans.UsageDescriptor) Module(com.intellij.openapi.module.Module) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Module (com.intellij.openapi.module.Module)1893 VirtualFile (com.intellij.openapi.vfs.VirtualFile)585 Project (com.intellij.openapi.project.Project)379 NotNull (org.jetbrains.annotations.NotNull)330 Nullable (org.jetbrains.annotations.Nullable)267 File (java.io.File)185 PsiFile (com.intellij.psi.PsiFile)147 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)134 ArrayList (java.util.ArrayList)118 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)112 Sdk (com.intellij.openapi.projectRoots.Sdk)95 PsiElement (com.intellij.psi.PsiElement)89 PsiDirectory (com.intellij.psi.PsiDirectory)77 ModuleManager (com.intellij.openapi.module.ModuleManager)65 PsiClass (com.intellij.psi.PsiClass)65 IOException (java.io.IOException)61 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)57 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)57 List (java.util.List)57 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)51