Search in sources :

Example 1 with DartPackagesLibraryProperties

use of com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties in project intellij-plugins by JetBrains.

the class DartFileListener method updatePackagesLibraryRoots.

@NotNull
public static Library updatePackagesLibraryRoots(@NotNull final Project project, @NotNull final DartLibInfo libInfo) {
    final LibraryTable projectLibraryTable = ProjectLibraryTable.getInstance(project);
    final Library existingLibrary = projectLibraryTable.getLibraryByName(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME);
    final Library library = existingLibrary != null ? existingLibrary : WriteAction.compute(() -> {
        final LibraryTableBase.ModifiableModel libTableModel = ProjectLibraryTable.getInstance(project).getModifiableModel();
        final Library lib = libTableModel.createLibrary(DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME, DartPackagesLibraryType.LIBRARY_KIND);
        libTableModel.commit();
        return lib;
    });
    final String[] existingUrls = library.getUrls(OrderRootType.CLASSES);
    final Collection<String> libRootUrls = libInfo.getLibRootUrls();
    if ((!libInfo.isProjectWithoutPubspec() && isBrokenPackageMap(((LibraryEx) library).getProperties())) || existingUrls.length != libRootUrls.size() || !libRootUrls.containsAll(Arrays.asList(existingUrls))) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            final LibraryEx.ModifiableModelEx model = (LibraryEx.ModifiableModelEx) library.getModifiableModel();
            for (String url : existingUrls) {
                model.removeRoot(url, OrderRootType.CLASSES);
            }
            for (String url : libRootUrls) {
                model.addRoot(url, OrderRootType.CLASSES);
            }
            final DartPackagesLibraryProperties libraryProperties = new DartPackagesLibraryProperties();
            libraryProperties.setPackageNameToDirsMap(libInfo.getPackagesMap());
            model.setProperties(libraryProperties);
            model.commit();
        });
    }
    return library;
}
Also used : DartPackagesLibraryProperties(com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) Library(com.intellij.openapi.roots.libraries.Library) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DartPackagesLibraryProperties

use of com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties in project intellij-plugins by JetBrains.

the class DartUrlResolverImpl method initPackagesMapFromLib.

private void initPackagesMapFromLib(@NotNull final VirtualFile contextFile) {
    final Module module = ModuleUtilCore.findModuleForFile(contextFile, myProject);
    final List<OrderEntry> orderEntries = module != null ? Arrays.asList(ModuleRootManager.getInstance(module).getOrderEntries()) : ProjectRootManager.getInstance(myProject).getFileIndex().getOrderEntriesForFile(contextFile);
    for (OrderEntry orderEntry : orderEntries) {
        if (orderEntry instanceof LibraryOrderEntry && LibraryTablesRegistrar.PROJECT_LEVEL.equals(((LibraryOrderEntry) orderEntry).getLibraryLevel()) && DartPackagesLibraryType.DART_PACKAGES_LIBRARY_NAME.equals(((LibraryOrderEntry) orderEntry).getLibraryName())) {
            final LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
            final LibraryProperties properties = library == null ? null : library.getProperties();
            if (properties instanceof DartPackagesLibraryProperties) {
                for (Map.Entry<String, List<String>> entry : ((DartPackagesLibraryProperties) properties).getPackageNameToDirsMap().entrySet()) {
                    if (entry != null && entry.getKey() != null && entry.getValue() != null) {
                        myPackagesMapFromLib.put(entry.getKey(), entry.getValue());
                    }
                }
                return;
            }
        }
    }
}
Also used : DartPackagesLibraryProperties(com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) DartPackagesLibraryProperties(com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties) LibraryProperties(com.intellij.openapi.roots.libraries.LibraryProperties) LibraryEx(com.intellij.openapi.roots.impl.libraries.LibraryEx) List(java.util.List) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Module(com.intellij.openapi.module.Module) THashMap(gnu.trove.THashMap) Map(java.util.Map)

Aggregations

LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)2 DartPackagesLibraryProperties (com.jetbrains.lang.dart.sdk.DartPackagesLibraryProperties)2 Module (com.intellij.openapi.module.Module)1 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1 ProjectLibraryTable (com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable)1 Library (com.intellij.openapi.roots.libraries.Library)1 LibraryProperties (com.intellij.openapi.roots.libraries.LibraryProperties)1 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)1 THashMap (gnu.trove.THashMap)1 List (java.util.List)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1