Search in sources :

Example 26 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class ModulesConfigurator method apply.

public void apply() throws ConfigurationException {
    // validate content and source roots 
    final Map<VirtualFile, String> contentRootToModuleNameMap = new HashMap<>();
    final Map<VirtualFile, VirtualFile> srcRootsToContentRootMap = new HashMap<>();
    for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
        final ModifiableRootModel rootModel = moduleEditor.getModifiableRootModel();
        final ContentEntry[] contents = rootModel.getContentEntries();
        final String moduleName = moduleEditor.getName();
        Set<VirtualFile> sourceRoots = new HashSet<>();
        for (ContentEntry content : contents) {
            for (VirtualFile root : content.getSourceFolderFiles()) {
                if (!sourceRoots.add(root)) {
                    throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.source.root.in.same.module.error", root.getPresentableUrl(), moduleName));
                }
            }
        }
        for (ContentEntry contentEntry : contents) {
            final VirtualFile contentRoot = contentEntry.getFile();
            if (contentRoot == null) {
                continue;
            }
            final String previousName = contentRootToModuleNameMap.put(contentRoot, moduleName);
            if (previousName != null && !previousName.equals(moduleName)) {
                throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.content.error", contentRoot.getPresentableUrl(), previousName, moduleName));
            }
            for (VirtualFile srcRoot : contentEntry.getSourceFolderFiles()) {
                final VirtualFile anotherContentRoot = srcRootsToContentRootMap.put(srcRoot, contentRoot);
                if (anotherContentRoot != null) {
                    final String problematicModule;
                    final String correctModule;
                    if (VfsUtilCore.isAncestor(anotherContentRoot, contentRoot, true)) {
                        problematicModule = contentRootToModuleNameMap.get(anotherContentRoot);
                        correctModule = contentRootToModuleNameMap.get(contentRoot);
                    } else {
                        problematicModule = contentRootToModuleNameMap.get(contentRoot);
                        correctModule = contentRootToModuleNameMap.get(anotherContentRoot);
                    }
                    throw new ConfigurationException(ProjectBundle.message("module.paths.validation.duplicate.source.root.error", problematicModule, srcRoot.getPresentableUrl(), correctModule));
                }
            }
        }
    }
    // additional validation: directories marked as src roots must belong to the same module as their corresponding content root
    for (Map.Entry<VirtualFile, VirtualFile> entry : srcRootsToContentRootMap.entrySet()) {
        final VirtualFile srcRoot = entry.getKey();
        final VirtualFile correspondingContent = entry.getValue();
        final String expectedModuleName = contentRootToModuleNameMap.get(correspondingContent);
        for (VirtualFile candidateContent = srcRoot; candidateContent != null && !candidateContent.equals(correspondingContent); candidateContent = candidateContent.getParent()) {
            final String moduleName = contentRootToModuleNameMap.get(candidateContent);
            if (moduleName != null && !moduleName.equals(expectedModuleName)) {
                throw new ConfigurationException(ProjectBundle.message("module.paths.validation.source.root.belongs.to.another.module.error", srcRoot.getPresentableUrl(), expectedModuleName, moduleName));
            }
        }
    }
    for (ModuleEditor moduleEditor : myModuleEditors.values()) {
        moduleEditor.canApply();
    }
    final Map<Sdk, Sdk> modifiedToOriginalMap = new THashMap<>();
    final ProjectSdksModel projectJdksModel = ProjectStructureConfigurable.getInstance(myProject).getProjectJdksModel();
    for (Map.Entry<Sdk, Sdk> entry : projectJdksModel.getProjectSdks().entrySet()) {
        modifiedToOriginalMap.put(entry.getValue(), entry.getKey());
    }
    final Ref<ConfigurationException> exceptionRef = Ref.create();
    ApplicationManager.getApplication().runWriteAction(() -> {
        final List<ModifiableRootModel> models = new ArrayList<>(myModuleEditors.size());
        try {
            for (final ModuleEditor moduleEditor : myModuleEditors.values()) {
                final ModifiableRootModel model = moduleEditor.apply();
                if (model != null) {
                    if (!model.isSdkInherited()) {
                        // make sure the sdk is set to original SDK stored in the JDK Table
                        final Sdk modelSdk = model.getSdk();
                        if (modelSdk != null) {
                            final Sdk original = modifiedToOriginalMap.get(modelSdk);
                            if (original != null) {
                                model.setSdk(original);
                            }
                        }
                    }
                    models.add(model);
                }
            }
            myFacetsConfigurator.applyEditors();
        } catch (ConfigurationException e) {
            exceptionRef.set(e);
            return;
        }
        try {
            ModifiableModelCommitter.multiCommit(models, myModuleModel);
            myModuleModelCommitted = true;
            myFacetsConfigurator.commitFacets();
        } finally {
            ModuleStructureConfigurable.getInstance(myProject).getFacetEditorFacade().clearMaps(false);
            myFacetsConfigurator = createFacetsConfigurator();
            myModuleModel = ModuleManager.getInstance(myProject).getModifiableModel();
            myModuleModelCommitted = false;
        }
    });
    if (!exceptionRef.isNull()) {
        throw exceptionRef.get();
    }
    myModified = false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectSdksModel(com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap) ModifiableRootModel(com.intellij.openapi.roots.ModifiableRootModel) THashMap(gnu.trove.THashMap) ConfigurationException(com.intellij.openapi.options.ConfigurationException) ContentEntry(com.intellij.openapi.roots.ContentEntry) Sdk(com.intellij.openapi.projectRoots.Sdk) HashMap(com.intellij.util.containers.HashMap) THashMap(gnu.trove.THashMap)

Example 27 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class IElementTypeTest method testCount.

// load all parser definitions, instantiate all lexers & parsers to initialize all IElementType constants
@SuppressWarnings("UnusedDeclaration")
public void testCount() throws Exception {
    int count = IElementType.getAllocatedTypesCount();
    System.out.println("Preloaded: " + count + " element types");
    LanguageExtensionPoint[] extensions = Extensions.getExtensions(new ExtensionPointName<LanguageExtensionPoint>("com.intellij.lang.parserDefinition"));
    System.out.println("ParserDefinitions: " + extensions.length);
    THashMap<Language, String> languageMap = new THashMap<>();
    languageMap.put(Language.ANY, "platform");
    final TObjectIntHashMap<String> map = new TObjectIntHashMap<>();
    for (LanguageExtensionPoint e : extensions) {
        String key = e.getPluginDescriptor().getPluginId().getIdString();
        int curCount = IElementType.getAllocatedTypesCount();
        ParserDefinition definition = (ParserDefinition) e.getInstance();
        IFileElementType type = definition.getFileNodeType();
        Language language = type.getLanguage();
        languageMap.put(language, key);
        if (language.getBaseLanguage() != null && !languageMap.containsKey(language.getBaseLanguage())) {
            languageMap.put(language.getBaseLanguage(), key);
        }
        try {
            Lexer lexer = definition.createLexer(getProject());
            PsiParser parser = definition.createParser(getProject());
        } catch (UnsupportedOperationException e1) {
        }
    // language-based calculation: per-class-loading stuff commented
    //int diff = IElementType.getAllocatedTypesCount() - curCount;
    //map.put(key, map.get(key) + diff);
    }
    // language-based calculation
    count = IElementType.getAllocatedTypesCount();
    for (short i = 0; i < count; i++) {
        IElementType type = IElementType.find(i);
        Language language = type.getLanguage();
        String key = null;
        for (Language cur = language; cur != null && key == null; cur = cur.getBaseLanguage()) {
            key = languageMap.get(cur);
        }
        key = StringUtil.notNullize(key, "unknown");
        map.put(key, map.get(key) + 1);
    //if (key.equals("unknown")) System.out.println(type +"   " + language);
    }
    System.out.println("Total: " + IElementType.getAllocatedTypesCount() + " element types");
    // Show per-plugin statistics
    Object[] keys = map.keys();
    Arrays.sort(keys, (o1, o2) -> map.get((String) o2) - map.get((String) o1));
    int sum = 0;
    for (Object key : keys) {
        int value = map.get((String) key);
        if (value == 0)
            continue;
        sum += value;
        System.out.println("  " + key + ": " + value);
    }
    // leave some index-space for plugin developers
    assertTrue(IElementType.getAllocatedTypesCount() < 10000);
    assertEquals(IElementType.getAllocatedTypesCount(), sum);
// output on 11.05.2012
//   Preloaded: 3485 types
//   95 definitions
//   Total: 7694 types
// 14.05.2015:
// Preloaded: 4106 element types
// ParserDefinitions: 128
// Total: 8864 element types
// 19.04.2016:
// Preloaded: 4397 element types
// ParserDefinitions: 135
// Total: 9231 element types
}
Also used : Lexer(com.intellij.lexer.Lexer) THashMap(gnu.trove.THashMap) TObjectIntHashMap(gnu.trove.TObjectIntHashMap)

Example 28 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class ValueContainerImpl method removeValue.

private void removeValue(int inputId, Object fileSet, Value value) {
    if (fileSet == null) {
        return;
    }
    if (fileSet instanceof ChangeBufferingList) {
        final ChangeBufferingList changesList = (ChangeBufferingList) fileSet;
        changesList.remove(inputId);
        if (!changesList.isEmpty())
            return;
    } else if (fileSet instanceof Integer) {
        if (((Integer) fileSet).intValue() != inputId) {
            return;
        }
    }
    if (!(myInputIdMapping instanceof THashMap)) {
        myInputIdMapping = null;
        myInputIdMappingValue = null;
    } else {
        THashMap<Value, Object> mapping = (THashMap<Value, Object>) myInputIdMapping;
        mapping.remove(value);
        if (mapping.size() == 1) {
            myInputIdMapping = mapping.keySet().iterator().next();
            myInputIdMappingValue = mapping.get((Value) myInputIdMapping);
        }
    }
}
Also used : ChangeBufferingList(com.intellij.util.indexing.containers.ChangeBufferingList) THashMap(gnu.trove.THashMap)

Example 29 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class SmartFMap method doPlus.

private static Object doPlus(Object oldMap, Object key, Object value) {
    if (oldMap instanceof Map) {
        Map newMap = new THashMap((Map) oldMap);
        newMap.put(key, value);
        return newMap;
    }
    Object[] array = (Object[]) oldMap;
    for (int i = 0; i < array.length; i += 2) {
        if (key.equals(array[i])) {
            Object[] newArray = new Object[array.length];
            System.arraycopy(array, 0, newArray, 0, array.length);
            newArray[i + 1] = value;
            return newArray;
        }
    }
    if (array.length == 2 * ARRAY_THRESHOLD) {
        THashMap map = new THashMap();
        for (int i = 0; i < array.length; i += 2) {
            map.put(array[i], array[i + 1]);
        }
        map.put(key, value);
        return map;
    }
    Object[] newArray = new Object[array.length + 2];
    System.arraycopy(array, 0, newArray, 0, array.length);
    newArray[array.length] = key;
    newArray[array.length + 1] = value;
    return newArray;
}
Also used : THashMap(gnu.trove.THashMap) THashMap(gnu.trove.THashMap)

Example 30 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class DirectoryStorageUtil method loadFrom.

@NotNull
public static Map<String, Element> loadFrom(@Nullable VirtualFile dir, @Nullable PathMacroSubstitutor pathMacroSubstitutor) {
    if (dir == null || !dir.exists()) {
        return Collections.emptyMap();
    }
    StringInterner interner = new StringInterner();
    Map<String, Element> fileToState = new THashMap<>();
    for (VirtualFile file : dir.getChildren()) {
        // ignore system files like .DS_Store on Mac
        if (!StringUtilRt.endsWithIgnoreCase(file.getNameSequence(), FileStorageCoreUtil.DEFAULT_EXT)) {
            continue;
        }
        try {
            if (file.getLength() == 0) {
                LOG.warn("Ignore empty file " + file.getPath());
                continue;
            }
            Element element = JDOMUtil.load(file.getInputStream());
            String componentName = FileStorageCoreUtil.getComponentNameIfValid(element);
            if (componentName == null) {
                continue;
            }
            if (!element.getName().equals(FileStorageCoreUtil.COMPONENT)) {
                LOG.error("Incorrect root tag name (" + element.getName() + ") in " + file.getPresentableUrl());
                continue;
            }
            List<Element> elementChildren = element.getChildren();
            if (elementChildren.isEmpty()) {
                continue;
            }
            Element state = (Element) elementChildren.get(0).detach();
            if (JDOMUtil.isEmpty(state)) {
                continue;
            }
            JDOMUtil.internElement(state, interner);
            if (pathMacroSubstitutor != null) {
                pathMacroSubstitutor.expandPaths(state);
                if (pathMacroSubstitutor instanceof TrackingPathMacroSubstitutor) {
                    ((TrackingPathMacroSubstitutor) pathMacroSubstitutor).addUnknownMacros(componentName, PathMacrosCollector.getMacroNames(state));
                }
            }
            fileToState.put(file.getName(), state);
        } catch (Throwable e) {
            LOG.warn("Unable to load state", e);
        }
    }
    return fileToState;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) StringInterner(com.intellij.util.containers.StringInterner) THashMap(gnu.trove.THashMap) Element(org.jdom.Element) TrackingPathMacroSubstitutor(com.intellij.openapi.components.TrackingPathMacroSubstitutor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

THashMap (gnu.trove.THashMap)132 NotNull (org.jetbrains.annotations.NotNull)33 THashSet (gnu.trove.THashSet)27 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Element (org.jdom.Element)18 PsiElement (com.intellij.psi.PsiElement)17 IOException (java.io.IOException)17 File (java.io.File)15 Map (java.util.Map)11 Nullable (org.jetbrains.annotations.Nullable)10 Project (com.intellij.openapi.project.Project)9 List (java.util.List)9 Module (com.intellij.openapi.module.Module)8 Pair (com.intellij.openapi.util.Pair)7 PsiFile (com.intellij.psi.PsiFile)7 ArrayList (java.util.ArrayList)7 java.util (java.util)6 ApplicationManager (com.intellij.openapi.application.ApplicationManager)5 Document (com.intellij.openapi.editor.Document)5 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)5