Search in sources :

Example 86 with THashMap

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

the class EncodingProjectManagerImpl method setMapping.

public void setMapping(@NotNull final Map<VirtualFile, Charset> mapping) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    // consider all files as unmodified
    FileDocumentManager.getInstance().saveAllDocuments();
    final Map<VirtualFile, Charset> newMap = new THashMap<>(mapping.size());
    final Map<VirtualFile, Charset> oldMap = new THashMap<>(myMapping);
    // ChangeFileEncodingAction should not start progress "reload files..."
    suppressReloadDuring(() -> {
        ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
        for (Map.Entry<VirtualFile, Charset> entry : mapping.entrySet()) {
            VirtualFile virtualFile = entry.getKey();
            Charset charset = entry.getValue();
            if (charset == null)
                throw new IllegalArgumentException("Null charset for " + virtualFile + "; mapping: " + mapping);
            if (virtualFile == null) {
                myProjectCharset = charset;
            } else {
                if (!fileIndex.isInContent(virtualFile))
                    continue;
                if (!virtualFile.isDirectory() && !Comparing.equal(charset, oldMap.get(virtualFile))) {
                    Document document;
                    byte[] bytes;
                    try {
                        document = FileDocumentManager.getInstance().getDocument(virtualFile);
                        if (document == null)
                            throw new IOException();
                        bytes = virtualFile.contentsToByteArray();
                    } catch (IOException e) {
                        continue;
                    }
                    // ask whether to reload/convert when in doubt
                    boolean changed = new ChangeFileEncodingAction().chosen(document, null, virtualFile, bytes, charset);
                    if (!changed)
                        continue;
                }
                newMap.put(virtualFile, charset);
            }
        }
    });
    myMapping.clear();
    myMapping.putAll(newMap);
    final Set<VirtualFile> changed = new HashSet<>(oldMap.keySet());
    for (Map.Entry<VirtualFile, Charset> entry : newMap.entrySet()) {
        VirtualFile file = entry.getKey();
        Charset charset = entry.getValue();
        Charset oldCharset = oldMap.get(file);
        if (Comparing.equal(oldCharset, charset)) {
            changed.remove(file);
        }
    }
    Set<VirtualFile> added = new HashSet<>(newMap.keySet());
    added.removeAll(oldMap.keySet());
    Set<VirtualFile> removed = new HashSet<>(oldMap.keySet());
    removed.removeAll(newMap.keySet());
    changed.addAll(added);
    changed.addAll(removed);
    changed.remove(null);
    if (!changed.isEmpty()) {
        final Processor<VirtualFile> reloadProcessor = createChangeCharsetProcessor();
        tryStartReloadWithProgress(() -> {
            Set<VirtualFile> processed = new THashSet<>();
            next: for (VirtualFile changedFile : changed) {
                for (VirtualFile processedFile : processed) {
                    if (VfsUtilCore.isAncestor(processedFile, changedFile, false))
                        continue next;
                }
                processSubFiles(changedFile, reloadProcessor);
                processed.add(changedFile);
            }
        });
    }
    myModificationTracker.incModificationCount();
}
Also used : Charset(java.nio.charset.Charset) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) THashMap(gnu.trove.THashMap) THashSet(gnu.trove.THashSet)

Example 87 with THashMap

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

the class StubUpdatingIndex method getIndexer.

@NotNull
@Override
public DataIndexer<Integer, SerializedStubTree, FileContent> getIndexer() {
    return new DataIndexer<Integer, SerializedStubTree, FileContent>() {

        @Override
        @NotNull
        public Map<Integer, SerializedStubTree> map(@NotNull final FileContent inputData) {
            final Map<Integer, SerializedStubTree> result = new THashMap<Integer, SerializedStubTree>() {

                StubUpdatingIndexKeys myKeySet;

                @Override
                public Set<Integer> keySet() {
                    if (myKeySet == null) {
                        myKeySet = new StubUpdatingIndexKeys(super.keySet());
                    }
                    return myKeySet;
                }
            };
            ApplicationManager.getApplication().runReadAction(() -> {
                final Stub rootStub = StubTreeBuilder.buildStubTree(inputData);
                if (rootStub == null)
                    return;
                VirtualFile file = inputData.getFile();
                int contentLength;
                if (file.getFileType().isBinary()) {
                    contentLength = -1;
                } else {
                    contentLength = ((FileContentImpl) inputData).getPsiFileForPsiDependentIndex().getTextLength();
                }
                rememberIndexingStamp(file, contentLength);
                final BufferExposingByteArrayOutputStream bytes = new BufferExposingByteArrayOutputStream();
                SerializationManagerEx.getInstanceEx().serialize(rootStub, bytes);
                if (DebugAssertions.DEBUG) {
                    try {
                        Stub deserialized = SerializationManagerEx.getInstanceEx().deserialize(new ByteArrayInputStream(bytes.getInternalBuffer(), 0, bytes.size()));
                        check(deserialized, rootStub);
                    } catch (ProcessCanceledException pce) {
                        throw pce;
                    } catch (Throwable t) {
                        LOG.error("Error indexing:" + file, t);
                    }
                }
                final int key = Math.abs(FileBasedIndex.getFileId(file));
                SerializedStubTree serializedStubTree = new SerializedStubTree(bytes.getInternalBuffer(), bytes.size(), rootStub, file.getLength(), contentLength);
                result.put(key, serializedStubTree);
                try {
                    ((StubUpdatingIndexKeys) result.keySet()).myStubIndicesValueMap = calcStubIndicesValueMap(serializedStubTree, key);
                } catch (StorageException ex) {
                    throw new RuntimeException(ex);
                }
            });
            return result;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) BufferExposingByteArrayOutputStream(com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream) NotNull(org.jetbrains.annotations.NotNull) THashMap(gnu.trove.THashMap) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with THashMap

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

the class Win32FsCache method getAttributes.

@Nullable
FileAttributes getAttributes(@NotNull VirtualFile file) {
    VirtualFile parent = file.getParent();
    int parentId = parent instanceof VirtualFileWithId ? ((VirtualFileWithId) parent).getId() : -((VirtualFileWithId) file).getId();
    TIntObjectHashMap<THashMap<String, FileAttributes>> map = getMap();
    THashMap<String, FileAttributes> nestedMap = map.get(parentId);
    String name = file.getName();
    FileAttributes attributes = nestedMap != null ? nestedMap.get(name) : null;
    if (attributes == null) {
        if (nestedMap != null && !(nestedMap instanceof IncompleteChildrenMap)) {
            // our info from parent doesn't mention the child in this refresh session
            return null;
        }
        FileInfo info = myKernel.getInfo(file.getPath());
        if (info == null) {
            return null;
        }
        attributes = info.toFileAttributes();
        if (nestedMap == null) {
            nestedMap = new IncompleteChildrenMap<>(FileUtil.PATH_HASHING_STRATEGY);
            map.put(parentId, nestedMap);
        }
        nestedMap.put(name, attributes);
    }
    return attributes;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashMap(gnu.trove.THashMap) FileInfo(com.intellij.openapi.util.io.win32.FileInfo) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) FileAttributes(com.intellij.openapi.util.io.FileAttributes) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with THashMap

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

the class Win32FsCache method list.

@NotNull
String[] list(@NotNull VirtualFile file) {
    String path = file.getPath();
    FileInfo[] fileInfo = myKernel.listChildren(path);
    if (fileInfo == null || fileInfo.length == 0) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
    }
    String[] names = new String[fileInfo.length];
    TIntObjectHashMap<THashMap<String, FileAttributes>> map = getMap();
    int parentId = ((VirtualFileWithId) file).getId();
    THashMap<String, FileAttributes> nestedMap = map.get(parentId);
    if (nestedMap == null) {
        nestedMap = new THashMap<>(fileInfo.length, FileUtil.PATH_HASHING_STRATEGY);
        map.put(parentId, nestedMap);
    }
    for (int i = 0, length = fileInfo.length; i < length; i++) {
        FileInfo info = fileInfo[i];
        String name = info.getName();
        nestedMap.put(name, info.toFileAttributes());
        names[i] = name;
    }
    return names;
}
Also used : FileInfo(com.intellij.openapi.util.io.win32.FileInfo) THashMap(gnu.trove.THashMap) VirtualFileWithId(com.intellij.openapi.vfs.VirtualFileWithId) FileAttributes(com.intellij.openapi.util.io.FileAttributes) NotNull(org.jetbrains.annotations.NotNull)

Example 90 with THashMap

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

the class FileColorsModel method load.

public void load(@NotNull Element e, boolean isProjectLevel) {
    List<FileColorConfiguration> configurations = isProjectLevel ? myProjectLevelConfigurations : myApplicationLevelConfigurations;
    configurations.clear();
    Map<String, String> predefinedScopeNameToPropertyKey = new THashMap<>(FileColorsModel.predefinedScopeNameToPropertyKey);
    for (Element child : e.getChildren(FILE_COLOR)) {
        FileColorConfiguration configuration = FileColorConfiguration.load(child);
        if (configuration != null) {
            if (!isProjectLevel) {
                predefinedScopeNameToPropertyKey.remove(configuration.getScopeName());
            }
            configurations.add(configuration);
        }
    }
    if (!isProjectLevel) {
        PropertiesComponent properties = PropertiesComponent.getInstance();
        for (String scopeName : predefinedScopeNameToPropertyKey.keySet()) {
            String colorName = properties.getValue(predefinedScopeNameToPropertyKey.get(scopeName));
            if (colorName == null) {
                // backward compatibility, previously it was saved incorrectly as scope name instead of specified property key
                colorName = properties.getValue(scopeName);
                // so, default value
                if (colorName == null) {
                    colorName = predefinedScopeNameToColor.get(scopeName);
                }
            }
            // empty means that value deleted
            if (!StringUtil.isEmpty(colorName)) {
                configurations.add(new FileColorConfiguration(scopeName, colorName));
            }
        }
    }
}
Also used : THashMap(gnu.trove.THashMap) Element(org.jdom.Element) PropertiesComponent(com.intellij.ide.util.PropertiesComponent)

Aggregations

THashMap (gnu.trove.THashMap)129 NotNull (org.jetbrains.annotations.NotNull)33 THashSet (gnu.trove.THashSet)26 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 Element (org.jdom.Element)18 PsiElement (com.intellij.psi.PsiElement)16 IOException (java.io.IOException)16 File (java.io.File)15 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 Map (java.util.Map)7 java.util (java.util)6 ArrayList (java.util.ArrayList)6 ApplicationManager (com.intellij.openapi.application.ApplicationManager)5 Document (com.intellij.openapi.editor.Document)5 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)5