Search in sources :

Example 6 with FactoryMap

use of com.intellij.util.containers.FactoryMap in project intellij-plugins by JetBrains.

the class Struts2ModelValidator method getFilesToProcess.

public Collection<VirtualFile> getFilesToProcess(final Project project, final CompileContext context) {
    final StrutsManager strutsManager = StrutsManager.getInstance(project);
    final PsiManager psiManager = PsiManager.getInstance(project);
    // cache validation settings per module
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final FactoryMap<Module, Boolean> enabledForModule = new FactoryMap<Module, Boolean>() {

        protected Boolean create(final Module module) {
            return isEnabledForModule(module);
        }
    };
    final Set<VirtualFile> files = new THashSet<>();
    for (final VirtualFile file : context.getCompileScope().getFiles(StdFileTypes.XML, false)) {
        final Module module = context.getModuleByFile(file);
        if (module != null && enabledForModule.get(module)) {
            final PsiFile psiFile = psiManager.findFile(file);
            if (psiFile instanceof XmlFile) {
                final StrutsModel model = strutsManager.getModelByFile((XmlFile) psiFile);
                if (model != null) {
                    for (final XmlFile configFile : model.getConfigFiles()) {
                        ContainerUtil.addIfNotNull(files, configFile.getVirtualFile());
                    }
                }
            }
        }
    }
    InspectionValidatorUtil.expandCompileScopeIfNeeded(files, context);
    return files;
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) PsiManager(com.intellij.psi.PsiManager) THashSet(gnu.trove.THashSet) StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 7 with FactoryMap

use of com.intellij.util.containers.FactoryMap in project intellij-plugins by JetBrains.

the class ValidatorModelValidator method getFilesToProcess.

public Collection<VirtualFile> getFilesToProcess(final Project project, final CompileContext context) {
    final PsiManager psiManager = PsiManager.getInstance(project);
    final ValidatorManager validatorManager = ValidatorManager.getInstance(project);
    // cache S2facet/validation settings per module
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final FactoryMap<Module, Boolean> enabledForModule = new FactoryMap<Module, Boolean>() {

        protected Boolean create(final Module module) {
            return isEnabledForModule(module) && StrutsFacet.getInstance(module) != null;
        }
    };
    // collect all validation.xml files located in sources of S2-modules
    final Set<VirtualFile> files = new THashSet<>();
    for (final VirtualFile file : context.getProjectCompileScope().getFiles(StdFileTypes.XML, true)) {
        if (StringUtil.endsWith(file.getName(), FILENAME_EXTENSION_VALIDATION_XML)) {
            final PsiFile psiFile = psiManager.findFile(file);
            if (psiFile instanceof XmlFile && validatorManager.isValidatorsFile((XmlFile) psiFile)) {
                final Module module = ModuleUtilCore.findModuleForFile(file, project);
                if (module != null && enabledForModule.get(module)) {
                    files.add(file);
                }
            }
        }
    }
    // add validator-config.xml if not default one from xwork.jar
    final Set<VirtualFile> descriptorFiles = new THashSet<>();
    for (final Module module : ModuleManager.getInstance(project).getModules()) {
        if (enabledForModule.get(module)) {
            final PsiFile psiFile = validatorManager.getValidatorConfigFile(module);
            if (psiFile != null && validatorManager.isCustomValidatorConfigFile(psiFile)) {
                InspectionValidatorUtil.addFile(descriptorFiles, psiFile);
            }
        }
    }
    files.addAll(InspectionValidatorUtil.expandCompileScopeIfNeeded(descriptorFiles, context));
    return files;
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ValidatorManager(com.intellij.struts2.dom.validator.ValidatorManager) XmlFile(com.intellij.psi.xml.XmlFile) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) THashSet(gnu.trove.THashSet)

Example 8 with FactoryMap

use of com.intellij.util.containers.FactoryMap in project intellij-community by JetBrains.

the class DependencyInspectionBase method checkFile.

@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull final PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (file.getViewProvider().getPsi(JavaLanguage.INSTANCE) == null) {
        return null;
    }
    final DependencyValidationManager validationManager = DependencyValidationManager.getInstance(file.getProject());
    if (!validationManager.hasRules() || validationManager.getApplicableRules(file).length == 0) {
        return null;
    }
    final List<ProblemDescriptor> problems = ContainerUtil.newSmartList();
    DependenciesBuilder.analyzeFileDependencies(file, new DependenciesBuilder.DependencyProcessor() {

        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
        private final Map<PsiFile, DependencyRule[]> violations = new FactoryMap<PsiFile, DependencyRule[]>() {

            @Nullable
            @Override
            protected DependencyRule[] create(PsiFile dependencyFile) {
                return validationManager.getViolatorDependencyRules(file, dependencyFile);
            }
        };

        @Override
        public void process(PsiElement place, PsiElement dependency) {
            PsiFile dependencyFile = dependency.getContainingFile();
            if (dependencyFile != null && dependencyFile.isPhysical() && dependencyFile.getVirtualFile() != null) {
                for (DependencyRule dependencyRule : violations.get(dependencyFile)) {
                    String message = InspectionsBundle.message("inspection.dependency.violator.problem.descriptor", dependencyRule.getDisplayText());
                    LocalQuickFix[] fixes = createEditDependencyFixes(dependencyRule);
                    problems.add(manager.createProblemDescriptor(place, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
        }
    });
    return problems.isEmpty() ? null : problems.toArray(new ProblemDescriptor[problems.size()]);
}
Also used : DependenciesBuilder(com.intellij.packageDependencies.DependenciesBuilder) FactoryMap(com.intellij.util.containers.FactoryMap) DependencyValidationManager(com.intellij.packageDependencies.DependencyValidationManager) PsiFile(com.intellij.psi.PsiFile) DependencyRule(com.intellij.packageDependencies.DependencyRule) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with FactoryMap

use of com.intellij.util.containers.FactoryMap in project intellij-community by JetBrains.

the class DeleteUtil method generateWarningMessage.

public static String generateWarningMessage(String messageTemplate, final PsiElement[] elements) {
    if (elements.length == 1) {
        String name = ElementDescriptionUtil.getElementDescription(elements[0], DeleteNameDescriptionLocation.INSTANCE);
        String type = ElementDescriptionUtil.getElementDescription(elements[0], DeleteTypeDescriptionLocation.SINGULAR);
        return MessageFormat.format(messageTemplate, type + " \"" + name + "\"");
    }
    FactoryMap<String, Integer> countMap = new FactoryMap<String, Integer>() {

        @Override
        protected Integer create(final String key) {
            return 0;
        }
    };
    Map<String, String> pluralToSingular = new HashMap<>();
    int directoryCount = 0;
    String containerType = null;
    for (final PsiElement elementToDelete : elements) {
        String type = ElementDescriptionUtil.getElementDescription(elementToDelete, DeleteTypeDescriptionLocation.PLURAL);
        pluralToSingular.put(type, ElementDescriptionUtil.getElementDescription(elementToDelete, DeleteTypeDescriptionLocation.SINGULAR));
        int oldCount = countMap.get(type).intValue();
        countMap.put(type, oldCount + 1);
        if (elementToDelete instanceof PsiDirectoryContainer) {
            containerType = type;
            directoryCount += ((PsiDirectoryContainer) elementToDelete).getDirectories().length;
        }
    }
    StringBuilder buffer = new StringBuilder();
    for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
        if (buffer.length() > 0) {
            if (buffer.length() > 0) {
                buffer.append(" ").append(IdeBundle.message("prompt.delete.and")).append(" ");
            }
        }
        final int count = entry.getValue().intValue();
        buffer.append(count).append(" ");
        if (count == 1) {
            buffer.append(pluralToSingular.get(entry.getKey()));
        } else {
            buffer.append(entry.getKey());
        }
        if (entry.getKey().equals(containerType)) {
            buffer.append(" ").append(IdeBundle.message("prompt.delete.directory.paren", directoryCount));
        }
    }
    return MessageFormat.format(messageTemplate, buffer.toString());
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) HashMap(com.intellij.util.containers.HashMap) PsiDirectoryContainer(com.intellij.psi.PsiDirectoryContainer) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) FactoryMap(com.intellij.util.containers.FactoryMap) PsiElement(com.intellij.psi.PsiElement)

Example 10 with FactoryMap

use of com.intellij.util.containers.FactoryMap in project intellij-plugins by JetBrains.

the class FlexTestUtils method createConfigEditor.

public static FlexProjectConfigurationEditor createConfigEditor(final Module... modules) {
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") final Map<Module, ModifiableRootModel> models = new FactoryMap<Module, ModifiableRootModel>() {

        @Override
        protected ModifiableRootModel create(final Module module) {
            final ModifiableRootModel result = ModuleRootManager.getInstance(module).getModifiableModel();
            Disposer.register(module, new Disposable() {

                @Override
                public void dispose() {
                    if (!result.isDisposed()) {
                        result.dispose();
                    }
                }
            });
            return result;
        }
    };
    return new FlexProjectConfigurationEditor(modules[0].getProject(), new FlexProjectConfigurationEditor.ProjectModifiableModelProvider() {

        @Override
        public Module[] getModules() {
            return modules;
        }

        @Override
        public ModifiableRootModel getModuleModifiableModel(Module module) {
            return models.get(module);
        }

        @Override
        public void addListener(FlexBCConfigurator.Listener listener, Disposable parentDisposable) {
        // ignore
        }

        @Override
        public void commitModifiableModels() throws ConfigurationException {
            ApplicationManager.getApplication().runWriteAction(() -> {
                for (ModifiableRootModel model : models.values()) {
                    if (model.isChanged()) {
                        model.commit();
                    }
                }
            });
        }

        public Library findSourceLibraryForLiveName(final String name, final String level) {
            return findSourceLibrary(name, level);
        }

        public Library findSourceLibrary(final String name, final String level) {
            return getLibrariesTable(level).getLibraryByName(name);
        }

        private LibraryTable getLibrariesTable(final String level) {
            if (LibraryTablesRegistrar.APPLICATION_LEVEL.equals(level)) {
                return ApplicationLibraryTable.getApplicationTable();
            } else {
                assert LibraryTablesRegistrar.PROJECT_LEVEL.equals(level);
                return ProjectLibraryTable.getInstance(modules[0].getProject());
            }
        }
    });
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) Disposable(com.intellij.openapi.Disposable) FlexBCConfigurator(com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator) FlexProjectConfigurationEditor(com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor) ProjectLibraryTable(com.intellij.openapi.roots.impl.libraries.ProjectLibraryTable) ApplicationLibraryTable(com.intellij.openapi.roots.impl.libraries.ApplicationLibraryTable) LibraryTable(com.intellij.openapi.roots.libraries.LibraryTable) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Library(com.intellij.openapi.roots.libraries.Library) Module(com.intellij.openapi.module.Module)

Aggregations

FactoryMap (com.intellij.util.containers.FactoryMap)10 THashSet (gnu.trove.THashSet)5 Module (com.intellij.openapi.module.Module)4 PsiFile (com.intellij.psi.PsiFile)3 THashMap (gnu.trove.THashMap)3 AnAction (com.intellij.openapi.actionSystem.AnAction)2 Keymap (com.intellij.openapi.keymap.Keymap)2 MacOSDefaultKeymap (com.intellij.openapi.keymap.impl.MacOSDefaultKeymap)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiElement (com.intellij.psi.PsiElement)2 PsiManager (com.intellij.psi.PsiManager)2 XmlFile (com.intellij.psi.xml.XmlFile)2 NotNull (org.jetbrains.annotations.NotNull)2 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)1 FlexBCConfigurator (com.intellij.lang.javascript.flex.projectStructure.FlexBCConfigurator)1 FlexProjectConfigurationEditor (com.intellij.lang.javascript.flex.projectStructure.model.impl.FlexProjectConfigurationEditor)1 ResourceBundle (com.intellij.lang.properties.ResourceBundle)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 Disposable (com.intellij.openapi.Disposable)1