Search in sources :

Example 1 with FactoryMap

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

the class InconsistentResourceBundleInspection method checkFile.

@Override
public void checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    Set<ResourceBundle> visitedBundles = globalContext.getUserData(VISITED_BUNDLES_KEY);
    if (!(file instanceof PropertiesFile))
        return;
    final PropertiesFile propertiesFile = (PropertiesFile) file;
    ResourceBundle resourceBundle = propertiesFile.getResourceBundle();
    assert visitedBundles != null;
    if (!visitedBundles.add(resourceBundle))
        return;
    List<PropertiesFile> files = resourceBundle.getPropertiesFiles();
    if (files.size() < 2)
        return;
    BidirectionalMap<PropertiesFile, PropertiesFile> parents = new BidirectionalMap<>();
    for (PropertiesFile f : files) {
        PropertiesFile parent = PropertiesUtil.getParent(f, files);
        if (parent != null) {
            parents.put(f, parent);
        }
    }
    final FactoryMap<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps = new FactoryMap<PropertiesFile, Map<String, String>>() {

        @Nullable
        @Override
        protected Map<String, String> create(PropertiesFile key) {
            return key.getNamesMap();
        }
    };
    Map<PropertiesFile, Set<String>> keysUpToParent = new THashMap<>();
    for (PropertiesFile f : files) {
        Set<String> keys = new THashSet<>(propertiesFilesNamesMaps.get(f).keySet());
        PropertiesFile parent = parents.get(f);
        while (parent != null) {
            keys.addAll(propertiesFilesNamesMaps.get(parent).keySet());
            parent = parents.get(parent);
        }
        keysUpToParent.put(f, keys);
    }
    for (final InconsistentResourceBundleInspectionProvider provider : myInspectionProviders.getValue()) {
        if (isProviderEnabled(provider.getName())) {
            provider.check(parents, files, keysUpToParent, propertiesFilesNamesMaps, manager, globalContext.getRefManager(), problemDescriptionsProcessor);
        }
    }
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) THashSet(gnu.trove.THashSet) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) FactoryMap(com.intellij.util.containers.FactoryMap) THashMap(gnu.trove.THashMap) BidirectionalMap(com.intellij.util.containers.BidirectionalMap) BidirectionalMap(com.intellij.util.containers.BidirectionalMap)

Example 2 with FactoryMap

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

the class ExternalSystemTaskActivator method runTasks.

public boolean runTasks(@NotNull Collection<String> modules, @NotNull Phase... phases) {
    final ExternalProjectsStateProvider stateProvider = ExternalProjectsManager.getInstance(myProject).getStateProvider();
    final Queue<Pair<ProjectSystemId, ExternalSystemTaskExecutionSettings>> tasksQueue = new LinkedList<>();
    //noinspection MismatchedQueryAndUpdateOfCollection
    Map<ProjectSystemId, Map<String, RunnerAndConfigurationSettings>> lazyConfigurationsMap = new FactoryMap<ProjectSystemId, Map<String, RunnerAndConfigurationSettings>>() {

        @Nullable
        @Override
        protected Map<String, RunnerAndConfigurationSettings> create(ProjectSystemId key) {
            final AbstractExternalSystemTaskConfigurationType configurationType = ExternalSystemUtil.findConfigurationType(key);
            if (configurationType == null)
                return null;
            return ContainerUtil.map2Map(RunManager.getInstance(myProject).getConfigurationSettingsList(configurationType), configurationSettings -> Pair.create(configurationSettings.getName(), configurationSettings));
        }
    };
    for (final ExternalProjectsStateProvider.TasksActivation activation : stateProvider.getAllTasksActivation()) {
        final boolean hashPath = modules.contains(activation.projectPath);
        final Set<String> tasks = ContainerUtil.newLinkedHashSet();
        for (Phase phase : phases) {
            List<String> activationTasks = activation.state.getTasks(phase);
            if (hashPath || (phase.isSyncPhase() && !activationTasks.isEmpty() && isShareSameRootPath(modules, activation))) {
                ContainerUtil.addAll(tasks, activationTasks);
            }
        }
        if (tasks.isEmpty())
            continue;
        for (Iterator<String> iterator = tasks.iterator(); iterator.hasNext(); ) {
            String task = iterator.next();
            if (task.length() > RUN_CONFIGURATION_TASK_PREFIX.length() && task.startsWith(RUN_CONFIGURATION_TASK_PREFIX)) {
                iterator.remove();
                final String configurationName = task.substring(RUN_CONFIGURATION_TASK_PREFIX.length());
                Map<String, RunnerAndConfigurationSettings> settings = lazyConfigurationsMap.get(activation.systemId);
                if (settings == null)
                    continue;
                RunnerAndConfigurationSettings configurationSettings = settings.get(configurationName);
                if (configurationSettings == null)
                    continue;
                final RunConfiguration runConfiguration = configurationSettings.getConfiguration();
                if (configurationName.equals(configurationSettings.getName()) && runConfiguration instanceof ExternalSystemRunConfiguration) {
                    tasksQueue.add(Pair.create(activation.systemId, ((ExternalSystemRunConfiguration) runConfiguration).getSettings()));
                }
            }
        }
        if (tasks.isEmpty())
            continue;
        if (ExternalProjectsManager.getInstance(myProject).isIgnored(activation.systemId, activation.projectPath))
            continue;
        ExternalSystemTaskExecutionSettings executionSettings = new ExternalSystemTaskExecutionSettings();
        executionSettings.setExternalSystemIdString(activation.systemId.toString());
        executionSettings.setExternalProjectPath(activation.projectPath);
        executionSettings.getTaskNames().addAll(tasks);
        tasksQueue.add(Pair.create(activation.systemId, executionSettings));
    }
    return runTasksQueue(tasksQueue);
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) ExternalSystemRunConfiguration(com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration) ExternalProjectsStateProvider(com.intellij.openapi.externalSystem.service.project.manage.ExternalProjectsManager.ExternalProjectsStateProvider) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) ExternalSystemRunConfiguration(com.intellij.openapi.externalSystem.service.execution.ExternalSystemRunConfiguration) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) ProjectSystemId(com.intellij.openapi.externalSystem.model.ProjectSystemId) FactoryMap(com.intellij.util.containers.FactoryMap) AbstractExternalSystemTaskConfigurationType(com.intellij.openapi.externalSystem.service.execution.AbstractExternalSystemTaskConfigurationType) Pair(com.intellij.openapi.util.Pair) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings)

Example 3 with FactoryMap

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

the class KeymapsTestCase method testValidActionIds.

public void testValidActionIds() {
    THashSet<String> unknownActions = new THashSet<>();
    collectUnknownActions(unknownActions);
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") Map<String, List<String>> missingActions = new FactoryMap<String, List<String>>() {

        @Override
        protected Map<String, List<String>> createMap() {
            return new LinkedHashMap<>();
        }

        @Nullable
        @Override
        protected List<String> create(String key) {
            return new ArrayList<>();
        }
    };
    for (Keymap keymap : KeymapManagerEx.getInstanceEx().getAllKeymaps()) {
        List<String> ids = new ArrayList<>(keymap.getActionIdList());
        ids.sort(null);
        assertThat(ids).isEqualTo(new ArrayList<>(new LinkedHashSet<>(ids)));
        for (String cid : ids) {
            if (unknownActions.contains(cid))
                continue;
            AnAction action = ActionManager.getInstance().getAction(cid);
            if (action == null) {
                if (OUTPUT_TEST_DATA) {
                    System.out.print("\"" + cid + "\", ");
                } else {
                    missingActions.get(keymap.getName()).add(cid);
                }
            }
        }
    }
    List<String> reappearedAction = new ArrayList<>();
    for (String id : unknownActions) {
        AnAction action = ActionManager.getInstance().getAction(id);
        if (action != null) {
            reappearedAction.add(id);
        }
    }
    if (!missingActions.isEmpty() || !reappearedAction.isEmpty()) {
        StringBuilder message = new StringBuilder();
        if (!missingActions.isEmpty()) {
            for (Map.Entry<String, List<String>> keymapAndActions : missingActions.entrySet()) {
                message.append("Unknown actions in keymap ").append(keymapAndActions.getKey()).append(", add them to unknown actions list:\n");
                for (String action : keymapAndActions.getValue()) {
                    message.append("\"").append(action).append("\",").append("\n");
                }
            }
        }
        if (!reappearedAction.isEmpty()) {
            message.append("The following actions have reappeared, remove them from unknown action list:\n");
            for (String action : reappearedAction) {
                message.append(action).append("\n");
            }
        }
        fail("\n" + message);
    }
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) AnAction(com.intellij.openapi.actionSystem.AnAction) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) FactoryMap(com.intellij.util.containers.FactoryMap) MacOSDefaultKeymap(com.intellij.openapi.keymap.impl.MacOSDefaultKeymap) Keymap(com.intellij.openapi.keymap.Keymap)

Example 4 with FactoryMap

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

the class KeymapsTestCase method testIdsListIsConsistent.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void testIdsListIsConsistent() {
    Map<String, Map<String, List<String>>> duplicates = getKnownDuplicates();
    Set<String> allMaps = new THashSet<>(ContainerUtil.map(KeymapManagerEx.getInstanceEx().getAllKeymaps(), keymap -> keymap.getName()));
    assertThat(ContainerUtil.subtract(allMaps, duplicates.keySet())).overridingErrorMessage("Modify 'known duplicates list' test data. Keymaps were added: %s", ContainerUtil.subtract(allMaps, duplicates.keySet())).isEmpty();
    assertThat(ContainerUtil.subtract(duplicates.keySet(), allMaps)).overridingErrorMessage("Modify 'known duplicates list' test data. Keymaps were removed: %s", ContainerUtil.subtract(duplicates.keySet(), allMaps)).isEmpty();
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") Map<Keymap, List<Shortcut>> reassignedShortcuts = new FactoryMap<Keymap, List<Shortcut>>() {

        @Override
        protected Map<Keymap, List<Shortcut>> createMap() {
            return new LinkedHashMap<>();
        }

        @Nullable
        @Override
        protected List<Shortcut> create(Keymap key) {
            return new ArrayList<>();
        }
    };
    for (String name : duplicates.keySet()) {
        Keymap keymap = KeymapManagerEx.getInstanceEx().getKeymap(name);
        assertThat(keymap).overridingErrorMessage("KeyMap %s not found", name).isNotNull();
        Map<String, List<String>> duplicateIdsList = duplicates.get(name);
        Set<String> mentionedShortcuts = new THashSet<>();
        for (Map.Entry<String, List<String>> shortcutMappings : duplicateIdsList.entrySet()) {
            String shortcutString = shortcutMappings.getKey();
            if (!mentionedShortcuts.add(shortcutString)) {
                TestCase.fail("Shortcut '" + shortcutString + "' duplicate in keymap '" + keymap + "'. Please modify 'known duplicates list'");
            }
            Shortcut shortcut = parse(shortcutString);
            String[] ids = keymap.getActionIds(shortcut);
            Set<String> actualSc = new HashSet<>(Arrays.asList(ids));
            removeBoundActionIds(actualSc);
            Set<String> expectedSc = new HashSet<>(shortcutMappings.getValue());
            for (String s : actualSc) {
                if (!expectedSc.contains(s)) {
                    reassignedShortcuts.get(keymap).add(shortcut);
                }
            }
            for (String s : expectedSc) {
                if (!actualSc.contains(s)) {
                    System.out.println("Expected action '" + s + "' does not reassign shortcut " + getText(shortcut) + " in keymap " + keymap + " or is not registered");
                }
            }
        }
    }
    if (!reassignedShortcuts.isEmpty()) {
        StringBuilder message = new StringBuilder();
        for (Map.Entry<Keymap, List<Shortcut>> keymapToShortcuts : reassignedShortcuts.entrySet()) {
            Keymap keymap = keymapToShortcuts.getKey();
            message.append("The following shortcuts was reassigned in keymap ").append(keymap.getName()).append(". Please modify known duplicates list:\n");
            for (Shortcut eachShortcut : keymapToShortcuts.getValue()) {
                message.append(" { ").append(StringUtil.wrapWithDoubleQuote(getText(eachShortcut))).append(",\t").append(StringUtil.join(keymap.getActionIds(eachShortcut), s -> StringUtil.wrapWithDoubleQuote(s), ", ")).append("},\n");
            }
        }
        TestCase.fail("\n" + message.toString());
    }
}
Also used : InputEvent(java.awt.event.InputEvent) MacOSDefaultKeymap(com.intellij.openapi.keymap.impl.MacOSDefaultKeymap) java.util(java.util) Assertions.assertThat(com.intellij.testFramework.assertions.Assertions.assertThat) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) Keymap(com.intellij.openapi.keymap.Keymap) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) ActionManager(com.intellij.openapi.actionSystem.ActionManager) ActionManagerEx(com.intellij.openapi.actionSystem.ex.ActionManagerEx) KeymapImpl(com.intellij.openapi.keymap.impl.KeymapImpl) TestCase(junit.framework.TestCase) KeymapManagerEx(com.intellij.openapi.keymap.ex.KeymapManagerEx) StringUtil(com.intellij.openapi.util.text.StringUtil) PlatformTestCase(com.intellij.testFramework.PlatformTestCase) FactoryMap(com.intellij.util.containers.FactoryMap) AnAction(com.intellij.openapi.actionSystem.AnAction) KeyEvent(java.awt.event.KeyEvent) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) KeyStrokeAdapter(com.intellij.ui.KeyStrokeAdapter) Nullable(org.jetbrains.annotations.Nullable) Shortcut(com.intellij.openapi.actionSystem.Shortcut) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) FactoryMap(com.intellij.util.containers.FactoryMap) THashSet(gnu.trove.THashSet) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) Shortcut(com.intellij.openapi.actionSystem.Shortcut) THashMap(gnu.trove.THashMap) FactoryMap(com.intellij.util.containers.FactoryMap) MacOSDefaultKeymap(com.intellij.openapi.keymap.impl.MacOSDefaultKeymap) Keymap(com.intellij.openapi.keymap.Keymap) THashSet(gnu.trove.THashSet)

Example 5 with FactoryMap

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

the class GradleResourceCompilerConfigurationGenerator method generateAffectedGradleModulesConfiguration.

@NotNull
private Map<String, GradleModuleResourceConfiguration> generateAffectedGradleModulesConfiguration(@NotNull CompileContext context) {
    final Map<String, GradleModuleResourceConfiguration> affectedGradleModuleConfigurations = ContainerUtil.newTroveMap();
    //noinspection MismatchedQueryAndUpdateOfCollection
    final Map<String, ExternalProject> lazyExternalProjectMap = new FactoryMap<String, ExternalProject>() {

        @Nullable
        @Override
        protected ExternalProject create(String gradleProjectPath) {
            return externalProjectDataCache.getRootExternalProject(GradleConstants.SYSTEM_ID, new File(gradleProjectPath));
        }
    };
    for (Module module : context.getCompileScope().getAffectedModules()) {
        if (!ExternalSystemApiUtil.isExternalSystemAwareModule(GradleConstants.SYSTEM_ID, module))
            continue;
        final String gradleProjectPath = ExternalSystemApiUtil.getExternalRootProjectPath(module);
        assert gradleProjectPath != null;
        if (shouldBeBuiltByExternalSystem(module))
            continue;
        final ExternalProject externalRootProject = lazyExternalProjectMap.get(gradleProjectPath);
        if (externalRootProject == null) {
            context.addMessage(CompilerMessageCategory.ERROR, String.format("Unable to make the module: %s, related gradle configuration was not found. " + "Please, re-import the Gradle project and try again.", module.getName()), VfsUtilCore.pathToUrl(gradleProjectPath), -1, -1);
            continue;
        }
        Map<String, ExternalSourceSet> externalSourceSets = externalProjectDataCache.findExternalProject(externalRootProject, module);
        if (externalSourceSets.isEmpty()) {
            LOG.debug("Unable to find source sets config for module: " + module.getName());
            continue;
        }
        GradleModuleResourceConfiguration resourceConfig = new GradleModuleResourceConfiguration();
        resourceConfig.id = new ModuleVersion(ExternalSystemApiUtil.getExternalProjectGroup(module), ExternalSystemApiUtil.getExternalProjectId(module), ExternalSystemApiUtil.getExternalProjectVersion(module));
        for (ExternalSourceSet sourceSet : externalSourceSets.values()) {
            addResources(resourceConfig.resources, sourceSet.getSources().get(ExternalSystemSourceType.RESOURCE), sourceSet.getSources().get(ExternalSystemSourceType.SOURCE));
            addResources(resourceConfig.testResources, sourceSet.getSources().get(ExternalSystemSourceType.TEST_RESOURCE), sourceSet.getSources().get(ExternalSystemSourceType.TEST));
        }
        final CompilerModuleExtension compilerModuleExtension = CompilerModuleExtension.getInstance(module);
        if (compilerModuleExtension != null && compilerModuleExtension.isCompilerOutputPathInherited()) {
            String outputPath = VfsUtilCore.urlToPath(compilerModuleExtension.getCompilerOutputUrl());
            for (ResourceRootConfiguration resource : resourceConfig.resources) {
                resource.targetPath = outputPath;
            }
            String testOutputPath = VfsUtilCore.urlToPath(compilerModuleExtension.getCompilerOutputUrlForTests());
            for (ResourceRootConfiguration resource : resourceConfig.testResources) {
                resource.targetPath = testOutputPath;
            }
        }
        affectedGradleModuleConfigurations.put(module.getName(), resourceConfig);
    }
    return affectedGradleModuleConfigurations;
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) ExternalSourceSet(org.jetbrains.plugins.gradle.model.ExternalSourceSet) ExternalProject(org.jetbrains.plugins.gradle.model.ExternalProject) CompilerModuleExtension(com.intellij.openapi.roots.CompilerModuleExtension) Module(com.intellij.openapi.module.Module) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

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