Search in sources :

Example 81 with HashMap

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

the class PackageAnnotator method annotate.

//get read lock myself when needed
public void annotate(final CoverageSuitesBundle suite, Annotator annotator) {
    final ProjectData data = suite.getCoverageData();
    if (data == null)
        return;
    final String qualifiedName = myPackage.getQualifiedName();
    boolean filtered = false;
    for (CoverageSuite coverageSuite : suite.getSuites()) {
        if (((JavaCoverageSuite) coverageSuite).isPackageFiltered(qualifiedName)) {
            filtered = true;
            break;
        }
    }
    if (!filtered)
        return;
    final GlobalSearchScope scope = suite.getSearchScope(myProject);
    final Module[] modules = myCoverageManager.doInReadActionIfProjectOpen(() -> ModuleManager.getInstance(myProject).getModules());
    if (modules == null)
        return;
    Map<String, PackageCoverageInfo> packageCoverageMap = new HashMap<>();
    Map<String, PackageCoverageInfo> flattenPackageCoverageMap = new HashMap<>();
    for (final Module module : modules) {
        if (!scope.isSearchInModuleContent(module))
            continue;
        final String rootPackageVMName = qualifiedName.replaceAll("\\.", "/");
        final VirtualFile output = myCoverageManager.doInReadActionIfProjectOpen(() -> CompilerModuleExtension.getInstance(module).getCompilerOutputPath());
        if (output != null) {
            File outputRoot = findRelativeFile(rootPackageVMName, output);
            if (outputRoot.exists()) {
                collectCoverageInformation(outputRoot, packageCoverageMap, flattenPackageCoverageMap, data, rootPackageVMName, annotator, module, suite.isTrackTestFolders(), false);
            }
        }
        if (suite.isTrackTestFolders()) {
            final VirtualFile testPackageRoot = myCoverageManager.doInReadActionIfProjectOpen(() -> CompilerModuleExtension.getInstance(module).getCompilerOutputPathForTests());
            if (testPackageRoot != null) {
                final File outputRoot = findRelativeFile(rootPackageVMName, testPackageRoot);
                if (outputRoot.exists()) {
                    collectCoverageInformation(outputRoot, packageCoverageMap, flattenPackageCoverageMap, data, rootPackageVMName, annotator, module, suite.isTrackTestFolders(), true);
                }
            }
        }
    }
    for (Map.Entry<String, PackageCoverageInfo> entry : packageCoverageMap.entrySet()) {
        final String packageFQName = entry.getKey().replaceAll("/", ".");
        final PackageCoverageInfo info = entry.getValue();
        annotator.annotatePackage(packageFQName, info);
    }
    for (Map.Entry<String, PackageCoverageInfo> entry : flattenPackageCoverageMap.entrySet()) {
        final String packageFQName = entry.getKey().replaceAll("/", ".");
        final PackageCoverageInfo info = entry.getValue();
        annotator.annotatePackage(packageFQName, info, true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(com.intellij.util.containers.HashMap) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) ProjectData(com.intellij.rt.coverage.data.ProjectData)

Example 82 with HashMap

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

the class Converter01 method convert.

/**
   * Converts keymap from version "0" (no version specified)
   * to version "1".
   * @param keymapElement XML element that corresponds to "keymap" tag.
   */
public static void convert(Element keymapElement) throws InvalidDataException {
    if (!KEY_MAP.equals(keymapElement.getName())) {
        throw new IllegalArgumentException(UNKNOWN_ELEMENT + keymapElement);
    }
    String version = keymapElement.getAttributeValue(VERSION);
    if (version != null) {
        throw new InvalidDataException(UNKNOWN_VERSION + version);
    }
    // Add version
    keymapElement.setAttribute(VERSION, Integer.toString(1));
    // disableMnemonics -> disable-mnemonics
    boolean disableMnemonics = Boolean.valueOf(DISABLE_MNEMONICS).booleanValue();
    keymapElement.removeAttribute(DISABLE_MNEMONICS);
    keymapElement.setAttribute(DISABLE_MNEMONICS_ATTRIBUTE, Boolean.toString(disableMnemonics));
    // Now we have to separate all shortcuts by action's ID and convert binding to keyboard-shortcut
    String name = keymapElement.getAttributeValue(NAME_ATTRIBUTE);
    if (name == null) {
        throw new InvalidDataException("Attribute 'name' of <keymap> must be specified");
    }
    HashMap id2elements = new HashMap();
    for (Iterator i = keymapElement.getChildren().iterator(); i.hasNext(); ) {
        Element oldChild = (Element) i.next();
        if (BINDING.equals(oldChild.getName())) {
            // binding -> keyboard-shortcut
            // Remove attribute "id"
            String id = oldChild.getAttributeValue(ID_ATTRIBUTE);
            if (id == null) {
                throw new InvalidDataException("attribute 'id' must be specified");
            }
            // keystroke -> first-keystroke
            String keystroke = oldChild.getAttributeValue(KEYSTROKE_ATTRIBUTE);
            // suffix -> second-keystroke
            String suffix = oldChild.getAttributeValue(SUFFIX_ATTRIBUTE);
            if (keystroke != null) {
                Element newChild = new Element(KEYBOARD_SHORTCUT);
                newChild.setAttribute(FIRST_KEYSTROKE_ATTRIBUTE, keystroke);
                if (suffix != null) {
                    newChild.setAttribute(SECOND_KEYSTROKE_ATTRIBUTE, suffix);
                }
                // Put new child into the map
                ArrayList elements = (ArrayList) id2elements.get(id);
                if (elements == null) {
                    elements = new ArrayList(2);
                    id2elements.put(id, elements);
                }
                elements.add(newChild);
            } else {
                id2elements.put(id, new ArrayList(0));
            }
            // Remove old child
            i.remove();
        } else if (MOUSE_SHORTCUT.equals(oldChild.getName())) {
            // Remove attribute "id"
            String id = oldChild.getAttributeValue(ID_ATTRIBUTE);
            if (id == null) {
                throw new InvalidDataException("Attribute 'id' of <mouse-shortcut> must be specified; keymap name=" + name);
            }
            oldChild.removeAttribute(ID_ATTRIBUTE);
            // Remove old child
            i.remove();
            // Put new child into the map
            ArrayList elements = (ArrayList) id2elements.get(id);
            if (elements == null) {
                elements = new ArrayList(2);
                id2elements.put(id, elements);
            }
            elements.add(oldChild);
        } else {
            throw new InvalidDataException("unknown element : " + oldChild.getName());
        }
    }
    for (Iterator i = id2elements.keySet().iterator(); i.hasNext(); ) {
        String id = (String) i.next();
        Element actionElement = new Element(ACTION);
        actionElement.setAttribute(ID_ATTRIBUTE, id);
        ArrayList elements = (ArrayList) id2elements.get(id);
        for (Iterator j = elements.iterator(); j.hasNext(); ) {
            Element newChild = (Element) j.next();
            actionElement.addContent(newChild);
        }
        keymapElement.addContent(actionElement);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) Element(org.jdom.Element) InvalidDataException(com.intellij.openapi.util.InvalidDataException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList)

Example 83 with HashMap

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

the class CopyrightProfilesPanel method getAllProfiles.

public Map<String, CopyrightProfile> getAllProfiles() {
    final Map<String, CopyrightProfile> profiles = new HashMap<>();
    if (!myInitialized.get()) {
        for (CopyrightProfile profile : myManager.getCopyrights()) {
            profiles.put(profile.getName(), profile);
        }
    } else {
        for (int i = 0; i < myRoot.getChildCount(); i++) {
            MyNode node = (MyNode) myRoot.getChildAt(i);
            final CopyrightProfile copyrightProfile = ((CopyrightConfigurable) node.getConfigurable()).getEditableObject();
            profiles.put(copyrightProfile.getName(), copyrightProfile);
        }
    }
    return profiles;
}
Also used : HashMap(com.intellij.util.containers.HashMap) CopyrightProfile(com.maddyhome.idea.copyright.CopyrightProfile)

Example 84 with HashMap

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

the class InvertBooleanProcessor method findUsages.

@Override
@NotNull
protected UsageInfo[] findUsages() {
    final List<SmartPsiElementPointer> toInvert = new ArrayList<>();
    final LinkedHashSet<PsiElement> elementsToInvert = new LinkedHashSet<>();
    myDelegate.collectRefElements(myElement, myRenameProcessor, myNewName, elementsToInvert);
    for (PsiElement element : elementsToInvert) {
        toInvert.add(mySmartPointerManager.createSmartPsiElementPointer(element));
    }
    final UsageInfo[] renameUsages = myRenameProcessor != null ? myRenameProcessor.findUsages() : UsageInfo.EMPTY_ARRAY;
    final SmartPsiElementPointer[] usagesToInvert = toInvert.toArray(new SmartPsiElementPointer[toInvert.size()]);
    //merge rename and invert usages
    Map<PsiElement, UsageInfo> expressionsToUsages = new HashMap<>();
    List<UsageInfo> result = new ArrayList<>();
    for (UsageInfo renameUsage : renameUsages) {
        expressionsToUsages.put(renameUsage.getElement(), renameUsage);
        result.add(renameUsage);
    }
    for (SmartPsiElementPointer pointer : usagesToInvert) {
        final PsiElement expression = pointer.getElement();
        if (!expressionsToUsages.containsKey(expression)) {
            final UsageInfo usageInfo = new UsageInfo(expression);
            expressionsToUsages.put(expression, usageInfo);
            //fake UsageInfo
            result.add(usageInfo);
            myToInvert.put(usageInfo, pointer);
        } else {
            myToInvert.put(expressionsToUsages.get(expression), pointer);
        }
    }
    return result.toArray(new UsageInfo[result.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(com.intellij.util.containers.HashMap) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with HashMap

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

the class RenameUtil method renameNonCodeUsages.

public static void renameNonCodeUsages(@NotNull Project project, @NotNull NonCodeUsageInfo[] usages) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    Map<Document, List<UsageOffset>> docsToOffsetsMap = new HashMap<>();
    final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
    for (NonCodeUsageInfo usage : usages) {
        PsiElement element = usage.getElement();
        if (element == null)
            continue;
        element = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element, true);
        if (element == null)
            continue;
        final ProperTextRange rangeInElement = usage.getRangeInElement();
        if (rangeInElement == null)
            continue;
        final PsiFile containingFile = element.getContainingFile();
        final Document document = psiDocumentManager.getDocument(containingFile);
        final Segment segment = usage.getSegment();
        LOG.assertTrue(segment != null);
        int fileOffset = segment.getStartOffset();
        List<UsageOffset> list = docsToOffsetsMap.get(document);
        if (list == null) {
            list = new ArrayList<>();
            docsToOffsetsMap.put(document, list);
        }
        list.add(new UsageOffset(fileOffset, fileOffset + rangeInElement.getLength(), usage.newText));
    }
    for (Document document : docsToOffsetsMap.keySet()) {
        List<UsageOffset> list = docsToOffsetsMap.get(document);
        LOG.assertTrue(list != null, document);
        UsageOffset[] offsets = list.toArray(new UsageOffset[list.size()]);
        Arrays.sort(offsets);
        for (int i = offsets.length - 1; i >= 0; i--) {
            UsageOffset usageOffset = offsets[i];
            document.replaceString(usageOffset.startOffset, usageOffset.endOffset, usageOffset.newText);
        }
        PsiDocumentManager.getInstance(project).commitDocument(document);
    }
    PsiDocumentManager.getInstance(project).commitAllDocuments();
}
Also used : HashMap(com.intellij.util.containers.HashMap) Document(com.intellij.openapi.editor.Document) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5