Search in sources :

Example 1 with SortResult

use of com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult in project intellij-plugins by JetBrains.

the class LibraryManager method sortLibraries.

@NotNull
private SortResult sortLibraries(LibrarySorter sorter, LibraryCollector collector, Condition<String> isExternal, String key, boolean isSdk) throws InitException {
    final List<Library> libraries = isSdk ? collector.sdkLibraries : collector.externalLibraries;
    try {
        final int id = data.librarySets.enumerate(key);
        SortResult result = data.librarySets.get(key);
        if (result == null) {
            result = sorter.sort(libraries, new File(appDir, LibrariesData.NAME_PREFIX + Integer.toString(id) + SWF_EXTENSION), isExternal, isSdk);
            data.librarySets.put(key, result);
        } else {
            final String[] libraryPaths = result.libraryPaths;
            final List<Library> filteredLibraries = new ArrayList<>(libraryPaths.length);
            for (Library library : libraries) {
                if (ArrayUtil.indexOf(libraryPaths, library.getFile().getPath()) != -1) {
                    filteredLibraries.add(library);
                }
            }
            result = new SortResult(result.definitionMap, filteredLibraries);
        }
        result.id = id;
        return result;
    } catch (ClosedByInterruptException e) {
        throw new InitException(e);
    } catch (Throwable e) {
        String technicalMessage = "Flex SDK " + collector.getFlexSdkVersion();
        final Attachment[] attachments = new Attachment[libraries.size()];
        try {
            for (int i = 0, librariesSize = libraries.size(); i < librariesSize; i++) {
                Library library = libraries.get(i);
                technicalMessage += " " + library.getFile().getPath();
                attachments[i] = AttachmentFactory.createAttachment(library.getFile());
            }
        } catch (Throwable innerE) {
            technicalMessage += " Cannot collect library catalog files due to " + ExceptionUtil.getThrowableText(innerE);
        }
        throw new InitException(e, "error.sort.libraries", attachments, technicalMessage);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SortResult(com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with SortResult

use of com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult in project intellij-plugins by JetBrains.

the class LibraryManager method registerModule.

@NotNull
public ProjectComponentReferenceCounter registerModule(@NotNull final Module module, ProblemsHolder problemsHolder, boolean collectLocalStyleHolders) throws InitException {
    final Project project = module.getProject();
    final StringRegistry.StringWriter stringWriter = new StringRegistry.StringWriter(16384);
    stringWriter.startChange();
    final AssetCounter assetCounter = new AssetCounter();
    final LibraryCollector libraryCollector = new LibraryCollector(this, new LibraryStyleInfoCollector(assetCounter, problemsHolder, module, stringWriter), module);
    final Client client;
    try {
        final AccessToken token = ReadAction.start();
        try {
            libraryCollector.collect(module);
        } finally {
            token.finish();
        }
        client = Client.getInstance();
        if (stringWriter.hasChanges()) {
            client.updateStringRegistry(stringWriter);
        } else {
            stringWriter.commit();
        }
    } catch (Throwable e) {
        stringWriter.rollback();
        throw new InitException(e, "error.collect.libraries");
    }
    assert !libraryCollector.sdkLibraries.isEmpty();
    final FlexLibrarySet flexLibrarySet = getOrCreateFlexLibrarySet(libraryCollector, assetCounter);
    final InfoMap<Project, ProjectInfo> registeredProjects = client.getRegisteredProjects();
    ProjectInfo info = registeredProjects.getNullableInfo(project);
    if (info == null) {
        info = new ProjectInfo(project);
        registeredProjects.add(info);
        client.openProject(project);
        DesignerApplicationManager.getInstance().projectRegistered(project);
    }
    LibrarySet librarySet;
    if (libraryCollector.externalLibraries.isEmpty()) {
        librarySet = null;
    } else {
        final String key = createKey(libraryCollector.externalLibraries, false);
        librarySet = librarySets.get(key);
        if (librarySet == null) {
            final SortResult sortResult = sortLibraries(new LibrarySorter(), libraryCollector, flexLibrarySet.contains, key, false);
            librarySet = new LibrarySet(sortResult.id, flexLibrarySet, sortResult.libraries);
            registerLibrarySet(key, librarySet);
        }
    }
    final ModuleInfo moduleInfo = new ModuleInfo(module, librarySet == null ? flexLibrarySet : librarySet, ModuleInfoUtil.isApp(module));
    final ProjectComponentReferenceCounter projectComponentReferenceCounter = new ProjectComponentReferenceCounter();
    if (collectLocalStyleHolders) {
        // client.registerModule finalize it
        stringWriter.startChange();
        try {
            moduleInfo.setLocalStyleHolders(ModuleInfoUtil.collectLocalStyle(moduleInfo, libraryCollector.getFlexSdkVersion(), stringWriter, problemsHolder, projectComponentReferenceCounter, assetCounter));
        } catch (Throwable e) {
            stringWriter.rollback();
            throw new InitException(e, "error.collect.local.style.holders");
        }
    }
    client.registerModule(project, moduleInfo, stringWriter);
    client.fillAssetClassPoolIfNeed(flexLibrarySet);
    module.getMessageBus().connect(moduleInfo).subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        @Override
        public void rootsChanged(ModuleRootEvent event) {
            new Notification(FlashUIDesignerBundle.message("plugin.name"), FlashUIDesignerBundle.message("plugin.name"), "Please reopen your project to update on library changes.", NotificationType.WARNING).notify(project);
        }
    });
    return projectComponentReferenceCounter;
}
Also used : ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener) Notification(com.intellij.notification.Notification) StringRegistry(com.intellij.flex.uiDesigner.io.StringRegistry) ProjectComponentReferenceCounter(com.intellij.flex.uiDesigner.mxml.ProjectComponentReferenceCounter) Project(com.intellij.openapi.project.Project) AccessToken(com.intellij.openapi.application.AccessToken) ModuleRootEvent(com.intellij.openapi.roots.ModuleRootEvent) SortResult(com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with SortResult

use of com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult in project intellij-plugins by JetBrains.

the class LibraryManager method getOrCreateFlexLibrarySet.

private FlexLibrarySet getOrCreateFlexLibrarySet(LibraryCollector libraryCollector, AssetCounter assetCounter) throws InitException {
    final String key = createKey(libraryCollector.sdkLibraries, true);
    FlexLibrarySet flexLibrarySet = (FlexLibrarySet) librarySets.get(key);
    if (flexLibrarySet == null) {
        final Set<CharSequence> globalDefinitions = getGlobalDefinitions(libraryCollector.getGlobalLibrary());
        final Condition<String> globalContains = name -> globalDefinitions.contains(name);
        final SortResult sortResult = sortLibraries(new LibrarySorter(new FlexDefinitionProcessor(libraryCollector.getFlexSdkVersion()), new FlexDefinitionMapProcessor(libraryCollector.getFlexSdkVersion(), globalContains)), libraryCollector, globalContains, key, true);
        flexLibrarySet = new FlexLibrarySet(sortResult, null, new ContainsCondition(globalDefinitions, sortResult.definitionMap), assetCounter, libraryCollector.getFlexSdkVersion());
        registerLibrarySet(key, flexLibrarySet);
    }
    return flexLibrarySet;
}
Also used : SortResult(com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult) FlexUtils(com.intellij.lang.javascript.flex.FlexUtils) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) ModuleRootListener(com.intellij.openapi.roots.ModuleRootListener) THashMap(gnu.trove.THashMap) ReadAction(com.intellij.openapi.application.ReadAction) ProjectTopics(com.intellij.ProjectTopics) PsiManager(com.intellij.psi.PsiManager) AccessToken(com.intellij.openapi.application.AccessToken) StringBuilderSpinAllocator(com.intellij.util.StringBuilderSpinAllocator) InfoMap(org.jetbrains.io.InfoMap) Project(com.intellij.openapi.project.Project) Module(com.intellij.openapi.module.Module) AttachmentFactory(com.intellij.diagnostic.AttachmentFactory) ContainsCondition(com.intellij.flex.uiDesigner.libraries.FlexLibrarySet.ContainsCondition) com.intellij.flex.uiDesigner(com.intellij.flex.uiDesigner) StringUtil(com.intellij.openapi.util.text.StringUtil) IOException(java.io.IOException) ProjectComponentReferenceCounter(com.intellij.flex.uiDesigner.mxml.ProjectComponentReferenceCounter) Disposable(com.intellij.openapi.Disposable) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) NotificationType(com.intellij.notification.NotificationType) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) ModuleRootEvent(com.intellij.openapi.roots.ModuleRootEvent) ExceptionUtil(com.intellij.util.ExceptionUtil) Pair(com.intellij.openapi.util.Pair) StringRegistry(com.intellij.flex.uiDesigner.io.StringRegistry) Attachment(com.intellij.openapi.diagnostic.Attachment) RetainCondition(org.jetbrains.io.RetainCondition) NotNull(org.jetbrains.annotations.NotNull) Condition(com.intellij.openapi.util.Condition) ContainsCondition(com.intellij.flex.uiDesigner.libraries.FlexLibrarySet.ContainsCondition) SortResult(com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult)

Aggregations

SortResult (com.intellij.flex.uiDesigner.libraries.LibrarySorter.SortResult)3 NotNull (org.jetbrains.annotations.NotNull)3 StringRegistry (com.intellij.flex.uiDesigner.io.StringRegistry)2 ProjectComponentReferenceCounter (com.intellij.flex.uiDesigner.mxml.ProjectComponentReferenceCounter)2 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)2 Notification (com.intellij.notification.Notification)2 AccessToken (com.intellij.openapi.application.AccessToken)2 Project (com.intellij.openapi.project.Project)2 ModuleRootEvent (com.intellij.openapi.roots.ModuleRootEvent)2 ModuleRootListener (com.intellij.openapi.roots.ModuleRootListener)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)2 ProjectTopics (com.intellij.ProjectTopics)1 AttachmentFactory (com.intellij.diagnostic.AttachmentFactory)1 com.intellij.flex.uiDesigner (com.intellij.flex.uiDesigner)1 ContainsCondition (com.intellij.flex.uiDesigner.libraries.FlexLibrarySet.ContainsCondition)1 FlexUtils (com.intellij.lang.javascript.flex.FlexUtils)1 NotificationType (com.intellij.notification.NotificationType)1 Disposable (com.intellij.openapi.Disposable)1