Search in sources :

Example 21 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-community by JetBrains.

the class PyBuiltinCache method findSdkForNonModuleFile.

@Nullable
public static Sdk findSdkForNonModuleFile(PsiFileSystemItem psiFile) {
    Project project = psiFile.getProject();
    Sdk sdk = null;
    final VirtualFile vfile = psiFile instanceof PsiFile ? ((PsiFile) psiFile).getOriginalFile().getVirtualFile() : psiFile.getVirtualFile();
    if (vfile != null) {
        // reality
        final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
        sdk = projectRootManager.getProjectSdk();
        if (sdk == null) {
            final List<OrderEntry> orderEntries = projectRootManager.getFileIndex().getOrderEntriesForFile(vfile);
            for (OrderEntry orderEntry : orderEntries) {
                if (orderEntry instanceof JdkOrderEntry) {
                    sdk = ((JdkOrderEntry) orderEntry).getJdk();
                } else if (orderEntry instanceof ModuleLibraryOrderEntryImpl) {
                    sdk = PythonSdkType.findPythonSdk(orderEntry.getOwnerModule());
                }
            }
        }
    }
    return sdk;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OrderEntry(com.intellij.openapi.roots.OrderEntry) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) JdkOrderEntry(com.intellij.openapi.roots.JdkOrderEntry) ModuleLibraryOrderEntryImpl(com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl) Sdk(com.intellij.openapi.projectRoots.Sdk) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project kotlin by JetBrains.

the class MoveKotlinNestedClassesToUpperLevelDialog method getTargetContainer.

@Nullable
private PsiElement getTargetContainer() {
    if (targetContainer instanceof PsiDirectory) {
        PsiDirectory psiDirectory = (PsiDirectory) targetContainer;
        FqName oldPackageFqName = getTargetPackageFqName();
        String targetName = packageNameField.getText();
        if (!Comparing.equal(oldPackageFqName != null ? oldPackageFqName.asString() : null, targetName)) {
            ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
            List<VirtualFile> contentSourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project);
            final PackageWrapper newPackage = new PackageWrapper(PsiManager.getInstance(project), targetName);
            final VirtualFile targetSourceRoot;
            if (contentSourceRoots.size() > 1) {
                PsiDirectory initialDir = null;
                PsiPackage oldPackage = oldPackageFqName != null ? JavaPsiFacade.getInstance(project).findPackage(oldPackageFqName.asString()) : null;
                if (oldPackage != null) {
                    PsiDirectory[] directories = oldPackage.getDirectories();
                    VirtualFile root = projectRootManager.getFileIndex().getContentRootForFile(psiDirectory.getVirtualFile());
                    for (PsiDirectory dir : directories) {
                        if (Comparing.equal(projectRootManager.getFileIndex().getContentRootForFile(dir.getVirtualFile()), root)) {
                            initialDir = dir;
                        }
                    }
                }
                VirtualFile sourceRoot = MoveClassesOrPackagesUtil.chooseSourceRoot(newPackage, contentSourceRoots, initialDir);
                if (sourceRoot == null)
                    return null;
                targetSourceRoot = sourceRoot;
            } else {
                targetSourceRoot = contentSourceRoots.get(0);
            }
            PsiDirectory dir = RefactoringUtil.findPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
            if (dir == null) {
                dir = ApplicationManager.getApplication().runWriteAction(new NullableComputable<PsiDirectory>() {

                    @Override
                    public PsiDirectory compute() {
                        try {
                            return RefactoringUtil.createPackageDirectoryInSourceRoot(newPackage, targetSourceRoot);
                        } catch (IncorrectOperationException e) {
                            return null;
                        }
                    }
                });
            }
            return dir;
        }
        return targetContainer;
    }
    if (targetContainer instanceof KtFile || targetContainer instanceof KtClassOrObject)
        return targetContainer;
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FqName(org.jetbrains.kotlin.name.FqName) NullableComputable(com.intellij.openapi.util.NullableComputable) PackageWrapper(com.intellij.refactoring.PackageWrapper) IncorrectOperationException(com.intellij.util.IncorrectOperationException) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-elixir by KronicDeth.

the class MixExUnitRunConfigurationProducer method setupConfigurationFromContextImpl.

private boolean setupConfigurationFromContextImpl(@NotNull MixExUnitRunConfiguration configuration, @NotNull PsiElement psiElement) {
    boolean contextApplicable = false;
    if (psiElement instanceof PsiDirectory) {
        PsiDirectory psiDirectory = (PsiDirectory) psiElement;
        Module module = ModuleUtilCore.findModuleForPsiElement(psiDirectory);
        Sdk sdk;
        if (module != null) {
            ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
            sdk = moduleRootManager.getSdk();
        } else {
            ProjectRootManager projectRootManager = ProjectRootManager.getInstance(psiDirectory.getProject());
            sdk = projectRootManager.getProjectSdk();
        }
        SdkTypeId sdkTypeId = null;
        if (sdk != null) {
            sdkTypeId = sdk.getSdkType();
        }
        if ((sdkTypeId == null || sdkTypeId.equals(ElixirSdkType.getInstance())) && ProjectRootsUtil.isInTestSource(psiDirectory.getVirtualFile(), psiDirectory.getProject())) {
            String basePath = psiElement.getProject().getBasePath();
            String workingDirectory = workingDirectory(psiElement, basePath);
            configuration.setWorkingDirectory(workingDirectory);
            PsiDirectory dir = (PsiDirectory) psiElement;
            configuration.setName(configurationName(dir, workingDirectory, basePath));
            configuration.setProgramParameters(programParameters(dir, workingDirectory));
            contextApplicable = true;
        }
    } else {
        PsiFile containingFile = psiElement.getContainingFile();
        if (!(containingFile instanceof ElixirFile || containingFile instanceof PsiDirectory))
            return false;
        if (ProjectRootsUtil.isInTestSource(containingFile)) {
            String basePath = psiElement.getProject().getBasePath();
            String workingDirectory = workingDirectory(psiElement, basePath);
            configuration.setWorkingDirectory(workingDirectory);
            int lineNumber = lineNumber(psiElement);
            configuration.setName(configurationName(containingFile, lineNumber, workingDirectory, basePath));
            configuration.setProgramParameters(programParameters(containingFile, lineNumber, workingDirectory));
            contextApplicable = true;
        }
    }
    return contextApplicable;
}
Also used : ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) Sdk(com.intellij.openapi.projectRoots.Sdk) SdkTypeId(com.intellij.openapi.projectRoots.SdkTypeId) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) ElixirFile(org.elixir_lang.psi.ElixirFile)

Example 24 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project intellij-plugins by JetBrains.

the class RubyMotionFacetConfigurator method configureSdk.

private static void configureSdk(final Project project) {
    final ProjectRootManager manager = ProjectRootManager.getInstance(project);
    final Sdk sdk = manager.getProjectSdk();
    if (!RubySdkUtil.isNativeRuby19(sdk)) {
        for (final Sdk newSdk : ProjectJdkTable.getInstance().getSdksOfType(RubySdkType.getInstance())) {
            if (RubySdkUtil.isNativeRuby19(newSdk) && !RubySdkUtil.isNativeRuby20(newSdk) && !RubyRemoteInterpreterManager.getInstance().isRemoteSdk(newSdk)) {
                new WriteAction() {

                    @Override
                    protected void run(@NotNull Result result) throws Throwable {
                        ProjectRootManager.getInstance(project).setProjectSdk(newSdk);
                    }
                }.execute();
            }
        }
    }
}
Also used : WriteAction(com.intellij.openapi.application.WriteAction) Sdk(com.intellij.openapi.projectRoots.Sdk) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) Result(com.intellij.openapi.application.Result)

Example 25 with ProjectRootManager

use of com.intellij.openapi.roots.ProjectRootManager in project flutter-intellij by flutter.

the class IntelliJAndroidSdk method setCurrent.

/**
 * Changes the project's Java SDK to this one.
 */
public void setCurrent(@NotNull Project project) {
    assert ApplicationManager.getApplication().isWriteAccessAllowed();
    final ProjectRootManager roots = ProjectRootManager.getInstance(project);
    roots.setProjectSdk(sdk);
}
Also used : ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager)

Aggregations

ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)29 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 Project (com.intellij.openapi.project.Project)8 Module (com.intellij.openapi.module.Module)7 Sdk (com.intellij.openapi.projectRoots.Sdk)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)4 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)4 File (java.io.File)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)2 NullableComputable (com.intellij.openapi.util.NullableComputable)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiManager (com.intellij.psi.PsiManager)2 PackageWrapper (com.intellij.refactoring.PackageWrapper)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CheckStylePlugin (org.infernus.idea.checkstyle.CheckStylePlugin)2 ScanScope (org.infernus.idea.checkstyle.model.ScanScope)2