Search in sources :

Example 1 with EncodingProjectManagerImpl

use of com.intellij.openapi.vfs.encoding.EncodingProjectManagerImpl in project intellij-community by JetBrains.

the class CompilerEncodingServiceImpl method computeModuleCharsetMap.

@NotNull
private Map<Module, Set<Charset>> computeModuleCharsetMap() {
    final Map<Module, Set<Charset>> map = new THashMap<>();
    final Map<VirtualFile, Charset> mappings = ((EncodingProjectManagerImpl) EncodingProjectManager.getInstance(myProject)).getAllMappings();
    ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
    final CompilerManager compilerManager = CompilerManager.getInstance(myProject);
    for (Map.Entry<VirtualFile, Charset> entry : mappings.entrySet()) {
        final VirtualFile file = entry.getKey();
        final Charset charset = entry.getValue();
        if (file == null || charset == null || (!file.isDirectory() && !compilerManager.isCompilableFileType(file.getFileType())) || !index.isUnderSourceRootOfType(file, JavaModuleSourceRootTypes.SOURCES))
            continue;
        final Module module = index.getModuleForFile(file);
        if (module == null)
            continue;
        Set<Charset> set = map.get(module);
        if (set == null) {
            set = new LinkedHashSet<>();
            map.put(module, set);
            final VirtualFile sourceRoot = index.getSourceRootForFile(file);
            VirtualFile current = file.getParent();
            Charset parentCharset = null;
            while (current != null) {
                final Charset currentCharset = mappings.get(current);
                if (currentCharset != null) {
                    parentCharset = currentCharset;
                }
                if (current.equals(sourceRoot)) {
                    break;
                }
                current = current.getParent();
            }
            if (parentCharset != null) {
                set.add(parentCharset);
            }
        }
        set.add(charset);
    }
    //todo[nik,jeka] perhaps we should take into account encodings of source roots only not individual files
    for (Module module : ModuleManager.getInstance(myProject).getModules()) {
        for (VirtualFile file : ModuleRootManager.getInstance(module).getSourceRoots(true)) {
            Charset encoding = EncodingProjectManager.getInstance(myProject).getEncoding(file, true);
            if (encoding != null) {
                Set<Charset> charsets = map.get(module);
                if (charsets == null) {
                    charsets = new LinkedHashSet<>();
                    map.put(module, charsets);
                }
                charsets.add(encoding);
            }
        }
    }
    return map;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) CompilerManager(com.intellij.openapi.compiler.CompilerManager) Charset(java.nio.charset.Charset) THashMap(gnu.trove.THashMap) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) EncodingProjectManagerImpl(com.intellij.openapi.vfs.encoding.EncodingProjectManagerImpl) Module(com.intellij.openapi.module.Module) THashMap(gnu.trove.THashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CompilerManager (com.intellij.openapi.compiler.CompilerManager)1 Module (com.intellij.openapi.module.Module)1 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 EncodingProjectManagerImpl (com.intellij.openapi.vfs.encoding.EncodingProjectManagerImpl)1 THashMap (gnu.trove.THashMap)1 Charset (java.nio.charset.Charset)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 NotNull (org.jetbrains.annotations.NotNull)1