Search in sources :

Example 51 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project ballerina by ballerina-lang.

the class BallerinaUtil method suggestPackageNameForDirectory.

/**
 * Suggest the package name for a directory.
 *
 * @param directory directory which needs to be processed
 * @return suggested package name or empty string if package name cannot be determined.
 */
@NotNull
public static String suggestPackageNameForDirectory(@Nullable PsiDirectory directory) {
    // If the directory is not null, get the package name
    if (directory != null) {
        VirtualFile currentDirectory = directory.getVirtualFile();
        Module module = ModuleUtilCore.findModuleForPsiElement(directory);
        // Check directories in module content roots.
        if (module != null) {
            VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots();
            for (VirtualFile contentRoot : contentRoots) {
                if (!directory.getVirtualFile().getPath().startsWith(contentRoot.getPath())) {
                    continue;
                }
                return getImportPath(currentDirectory, contentRoot);
            }
        }
        Project project = directory.getProject();
        // Check directories in project content roots.
        VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
        for (VirtualFile contentRoot : contentRoots) {
            if (!directory.getVirtualFile().getPath().startsWith(contentRoot.getPath())) {
                continue;
            }
            return getImportPath(currentDirectory, contentRoot);
        }
        // Then we check the sources of module sdk.
        if (module != null) {
            Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk();
            String root = getImportPath(directory, currentDirectory, moduleSdk);
            if (root != null) {
                return root;
            }
            // Suggest package name for packages in BALLERINA_REPOSITORY.
            // Todo - This can be used to get package names for packages in SDK, BALLERINA_REPO, etc.
            OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
            for (OrderEntry entry : entries) {
                for (VirtualFile file : entry.getFiles(OrderRootType.SOURCES)) {
                    if (currentDirectory.getPath().startsWith(file.getPath())) {
                        return getImportPath(currentDirectory, file);
                    }
                }
            }
        }
        // Then we check the sources of project sdk.
        Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
        String root = getImportPath(directory, currentDirectory, projectSdk);
        if (root != null) {
            return root;
        }
        String sdkHomePath = BallerinaSdkService.getInstance(project).getSdkHomePath(null);
        if (sdkHomePath == null) {
            return "";
        }
        VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(sdkHomePath + "/src");
        if (virtualFile == null) {
            return "";
        }
        return getImportPath(currentDirectory, virtualFile);
    }
    // If the directory is null, return empty string
    return "";
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OrderEntry(com.intellij.openapi.roots.OrderEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method getMatchingPackagesFromSDK.

@NotNull
private static List<PsiDirectory> getMatchingPackagesFromSDK(@NotNull Project project, @NotNull Module module, @NotNull List<PsiElement> packages) {
    List<PsiDirectory> results = new ArrayList<>();
    // First we check the sources of module SDK.
    Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk();
    if (moduleSdk != null) {
        VirtualFile[] roots = moduleSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
        for (VirtualFile root : roots) {
            VirtualFile match = getMatchingDirectory(root, packages);
            if (match != null) {
                results.add(PsiManager.getInstance(project).findDirectory(match));
            }
        }
    }
    // return that result.
    if (!results.isEmpty()) {
        return results;
    }
    // Then we check the sources of project SDK.
    Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
    if (projectSdk != null) {
        VirtualFile[] roots = projectSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
        for (VirtualFile root : roots) {
            VirtualFile match = getMatchingDirectory(root, packages);
            if (match != null) {
                results.add(PsiManager.getInstance(project).findDirectory(match));
            }
        }
    }
    String sdkHomePath = BallerinaSdkService.getInstance(project).getSdkHomePath(null);
    if (sdkHomePath == null) {
        return results;
    }
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(sdkHomePath + "/src");
    if (virtualFile == null) {
        return results;
    }
    VirtualFile match = getMatchingDirectory(virtualFile, packages);
    if (match != null) {
        results.add(PsiManager.getInstance(project).findDirectory(match));
    }
    // Find matching packages in BALLERINA_REPOSITORY.
    // Todo - This can be used to find all matching packages in SDK, BALLERINA_REPOSITORY, etc.
    OrderEntry[] entries = ModuleRootManager.getInstance(module).getOrderEntries();
    for (OrderEntry entry : entries) {
        for (VirtualFile file : entry.getFiles(OrderRootType.SOURCES)) {
            virtualFile = LocalFileSystem.getInstance().findFileByPath(file.getPath());
            if (virtualFile != null) {
                match = getMatchingDirectory(virtualFile, packages);
                if (match != null) {
                    results.add(PsiManager.getInstance(project).findDirectory(match));
                }
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEntry(com.intellij.openapi.roots.OrderEntry) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) Sdk(com.intellij.openapi.projectRoots.Sdk) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project azure-tools-for-java by Microsoft.

the class LibrariesConfigurationDialog method addLibrary.

private void addLibrary() {
    AddLibraryWizardModel model = new AddLibraryWizardModel(module);
    AddLibraryWizardDialog wizard = new AddLibraryWizardDialog(model);
    wizard.setTitle(message("addLibraryTitle"));
    wizard.show();
    if (wizard.isOK()) {
        AzureLibrary azureLibrary = model.getSelectedLibrary();
        final LibrariesContainer.LibraryLevel level = LibrariesContainer.LibraryLevel.MODULE;
        AccessToken token = WriteAction.start();
        try {
            final ModifiableRootModel modifiableModel = ModuleRootManager.getInstance(module).getModifiableModel();
            for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
                if (orderEntry instanceof ModuleLibraryOrderEntryImpl && azureLibrary.getName().equals(((ModuleLibraryOrderEntryImpl) orderEntry).getLibraryName())) {
                    AzureTaskManager.getInstance().runLater(() -> PluginUtil.displayErrorDialog(message("error"), message("libraryExistsError")));
                    return;
                }
            }
            Library newLibrary = LibrariesContainerFactory.createContainer(modifiableModel).createLibrary(azureLibrary.getName(), level, new ArrayList<OrderRoot>());
            if (model.isExported()) {
                for (OrderEntry orderEntry : modifiableModel.getOrderEntries()) {
                    if (orderEntry instanceof ModuleLibraryOrderEntryImpl && azureLibrary.getName().equals(((ModuleLibraryOrderEntryImpl) orderEntry).getLibraryName())) {
                        ((ModuleLibraryOrderEntryImpl) orderEntry).setExported(true);
                        break;
                    }
                }
            }
            Library.ModifiableModel newLibraryModel = newLibrary.getModifiableModel();
            // if there is separate resources folder
            if (azureLibrary.getLocation() != null) {
                File file = new File(String.format("%s%s%s", AzurePlugin.pluginFolder, File.separator, azureLibrary.getLocation()));
                AddLibraryUtility.addLibraryRoot(file, newLibraryModel);
            }
            // if some files already contained in plugin dependencies, take them from there - true for azure sdk library
            if (azureLibrary.getFiles().length > 0) {
                AddLibraryUtility.addLibraryFiles(new File(PluginHelper.getAzureLibLocation()), newLibraryModel, azureLibrary.getFiles());
            }
            newLibraryModel.commit();
            modifiableModel.commit();
            ((DefaultListModel) librariesList.getModel()).addElement(azureLibrary);
            tempList.add(azureLibrary);
        } catch (Exception ex) {
            PluginUtil.displayErrorDialogAndLog(message("error"), message("addLibraryError"), ex);
        } finally {
            token.finish();
        }
        LocalFileSystem.getInstance().findFileByPath(PluginUtil.getModulePath(module)).refresh(true, true);
    }
}
Also used : ModuleLibraryOrderEntryImpl(com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) OrderEntry(com.intellij.openapi.roots.OrderEntry) AccessToken(com.intellij.openapi.application.AccessToken) LibrariesContainer(com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer) Library(com.intellij.openapi.roots.libraries.Library) File(java.io.File)

Example 54 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project sonarlint-intellij by SonarSource.

the class JavaAnalysisConfigurator method getProjectClasspath.

private static VirtualFile[] getProjectClasspath(@Nullable final Module module) {
    if (module == null) {
        return new VirtualFile[0];
    }
    final List<VirtualFile> found = new LinkedList<>();
    final ModuleRootManager mrm = ModuleRootManager.getInstance(module);
    final OrderEntry[] orderEntries = mrm.getOrderEntries();
    for (final OrderEntry entry : orderEntries) {
        if (entry instanceof ModuleOrderEntry) {
            // Add dependent module output dir as library
            Module dependentModule = ((ModuleOrderEntry) entry).getModule();
            found.addAll(getModuleEntries(dependentModule));
        } else if (entry instanceof LibraryOrderEntry) {
            Library lib = ((LibraryOrderEntry) entry).getLibrary();
            found.addAll(getLibraryEntries(lib));
        }
    }
    return found.toArray(new VirtualFile[found.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module) LinkedList(java.util.LinkedList)

Aggregations

OrderEntry (com.intellij.openapi.roots.OrderEntry)54 Module (com.intellij.openapi.module.Module)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)18 Project (com.intellij.openapi.project.Project)14 Library (com.intellij.openapi.roots.libraries.Library)12 ArrayList (java.util.ArrayList)11 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)10 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)10 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)8 ModuleOrderEntry (com.intellij.openapi.roots.ModuleOrderEntry)8 Nullable (org.jetbrains.annotations.Nullable)8 NotNull (org.jetbrains.annotations.NotNull)7 ModuleLibraryOrderEntryImpl (com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl)5 List (java.util.List)5 Sdk (com.intellij.openapi.projectRoots.Sdk)4 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)4 AccessToken (com.intellij.openapi.application.AccessToken)3 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)3 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)3