Search in sources :

Example 1 with LinkedHashMap

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

the class InspectionValidatorWrapper method runXmlFileSchemaValidation.

private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation(@NotNull XmlFile xmlFile) {
    final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile));
    final List<ExternalAnnotator> annotators = ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile);
    for (ExternalAnnotator<?, ?> annotator : annotators) {
        processAnnotator(xmlFile, holder, annotator);
    }
    if (!holder.hasAnnotations())
        return Collections.emptyMap();
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<>();
    for (final Annotation annotation : holder) {
        final HighlightInfo info = HighlightInfo.fromAnnotation(annotation);
        if (info.getSeverity() == HighlightSeverity.INFORMATION)
            continue;
        final PsiElement startElement = xmlFile.findElementAt(info.startOffset);
        final PsiElement endElement = info.startOffset == info.endOffset ? startElement : xmlFile.findElementAt(info.endOffset - 1);
        if (startElement == null || endElement == null)
            continue;
        final ProblemDescriptor descriptor = myInspectionManager.createProblemDescriptor(startElement, endElement, info.getDescription(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false);
        final HighlightDisplayLevel level = info.getSeverity() == HighlightSeverity.ERROR ? HighlightDisplayLevel.ERROR : HighlightDisplayLevel.WARNING;
        problemsMap.put(descriptor, level);
    }
    return problemsMap;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 2 with LinkedHashMap

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

the class QuickEditHandler method altCommitToOriginal.

private void altCommitToOriginal(@NotNull DocumentEvent e) {
    final PsiFile origPsiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myOrigDocument);
    String newText = myNewDocument.getText();
    // prepare guarded blocks
    LinkedHashMap<String, String> replacementMap = new LinkedHashMap<>();
    int count = 0;
    for (RangeMarker o : ContainerUtil.reverse(((DocumentEx) myNewDocument).getGuardedBlocks())) {
        String replacement = o.getUserData(REPLACEMENT_KEY);
        String tempText = "REPLACE" + (count++) + Long.toHexString(StringHash.calc(replacement));
        newText = newText.substring(0, o.getStartOffset()) + tempText + newText.substring(o.getEndOffset());
        replacementMap.put(tempText, replacement);
    }
    // run preformat processors
    final int hostStartOffset = myAltFullRange.getStartOffset();
    myEditor.getCaretModel().moveToOffset(hostStartOffset);
    for (CopyPastePreProcessor preProcessor : Extensions.getExtensions(CopyPastePreProcessor.EP_NAME)) {
        newText = preProcessor.preprocessOnPaste(myProject, origPsiFile, myEditor, newText, null);
    }
    myOrigDocument.replaceString(hostStartOffset, myAltFullRange.getEndOffset(), newText);
    // replace temp strings for guarded blocks
    for (String tempText : replacementMap.keySet()) {
        int idx = CharArrayUtil.indexOf(myOrigDocument.getCharsSequence(), tempText, hostStartOffset, myAltFullRange.getEndOffset());
        myOrigDocument.replaceString(idx, idx + tempText.length(), replacementMap.get(tempText));
    }
    // JAVA: fix occasional char literal concatenation
    fixDocumentQuotes(myOrigDocument, hostStartOffset - 1);
    fixDocumentQuotes(myOrigDocument, myAltFullRange.getEndOffset());
    // reformat
    PsiDocumentManager.getInstance(myProject).commitDocument(myOrigDocument);
    try {
        CodeStyleManager.getInstance(myProject).reformatRange(origPsiFile, hostStartOffset, myAltFullRange.getEndOffset(), true);
    } catch (IncorrectOperationException e1) {
    //LOG.error(e);
    }
    PsiElement newInjected = InjectedLanguageManager.getInstance(myProject).findInjectedElementAt(origPsiFile, hostStartOffset);
    DocumentWindow documentWindow = newInjected == null ? null : InjectedLanguageUtil.getDocumentWindow(newInjected);
    if (documentWindow != null) {
        myEditor.getCaretModel().moveToOffset(documentWindow.injectedToHost(e.getOffset()));
        myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    }
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) CopyPastePreProcessor(com.intellij.codeInsight.editorActions.CopyPastePreProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RelativePoint(com.intellij.ui.awt.RelativePoint) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 3 with LinkedHashMap

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

the class PsiSuperMethodImplUtil method buildMethodHierarchy.

@NotNull
private static Map<MethodSignature, HierarchicalMethodSignature> buildMethodHierarchy(@NotNull PsiClass aClass, @Nullable String nameHint, @NotNull PsiSubstitutor substitutor, final boolean includePrivates, @NotNull final Set<PsiClass> visited, boolean isInRawContext, GlobalSearchScope resolveScope) {
    ProgressManager.checkCanceled();
    Map<MethodSignature, HierarchicalMethodSignature> result = new LinkedHashMap<>(new EqualityPolicy<MethodSignature>() {

        @Override
        public int getHashCode(MethodSignature object) {
            return object.hashCode();
        }

        @Override
        public boolean isEqual(MethodSignature o1, MethodSignature o2) {
            if (o1.equals(o2)) {
                final PsiMethod method1 = ((MethodSignatureBackedByPsiMethod) o1).getMethod();
                final PsiType returnType1 = method1.getReturnType();
                final PsiMethod method2 = ((MethodSignatureBackedByPsiMethod) o2).getMethod();
                final PsiType returnType2 = method2.getReturnType();
                if (method1.hasModifierProperty(PsiModifier.STATIC) || method2.hasModifierProperty(PsiModifier.STATIC)) {
                    return true;
                }
                if (MethodSignatureUtil.isReturnTypeSubstitutable(o1, o2, returnType1, returnType2)) {
                    return true;
                }
                final PsiClass containingClass1 = method1.getContainingClass();
                final PsiClass containingClass2 = method2.getContainingClass();
                if (containingClass1 != null && containingClass2 != null) {
                    return containingClass1.isAnnotationType() || containingClass2.isAnnotationType();
                }
            }
            return false;
        }
    });
    final Map<MethodSignature, List<PsiMethod>> sameParameterErasureMethods = new THashMap<>(MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY);
    Map<MethodSignature, HierarchicalMethodSignatureImpl> map = new THashMap<>(new TObjectHashingStrategy<MethodSignature>() {

        @Override
        public int computeHashCode(MethodSignature signature) {
            return MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY.computeHashCode(signature);
        }

        @Override
        public boolean equals(MethodSignature o1, MethodSignature o2) {
            if (!MethodSignatureUtil.METHOD_PARAMETERS_ERASURE_EQUALITY.equals(o1, o2))
                return false;
            List<PsiMethod> list = sameParameterErasureMethods.get(o1);
            boolean toCheckReturnType = list != null && list.size() > 1;
            if (!toCheckReturnType)
                return true;
            PsiType returnType1 = ((MethodSignatureBackedByPsiMethod) o1).getMethod().getReturnType();
            PsiType returnType2 = ((MethodSignatureBackedByPsiMethod) o2).getMethod().getReturnType();
            if (returnType1 == null && returnType2 == null)
                return true;
            if (returnType1 == null || returnType2 == null)
                return false;
            PsiType erasure1 = TypeConversionUtil.erasure(o1.getSubstitutor().substitute(returnType1));
            PsiType erasure2 = TypeConversionUtil.erasure(o2.getSubstitutor().substitute(returnType2));
            return erasure1.equals(erasure2);
        }
    });
    PsiMethod[] methods = aClass.getMethods();
    if ((nameHint == null || "values".equals(nameHint)) && aClass instanceof PsiClassImpl) {
        final PsiMethod valuesMethod = ((PsiClassImpl) aClass).getValuesMethod();
        if (valuesMethod != null) {
            methods = ArrayUtil.append(methods, valuesMethod);
        }
    }
    for (PsiMethod method : methods) {
        if (!method.isValid()) {
            throw new PsiInvalidElementAccessException(method, "class.valid=" + aClass.isValid() + "; name=" + method.getName());
        }
        if (nameHint != null && !nameHint.equals(method.getName()))
            continue;
        if (!includePrivates && method.hasModifierProperty(PsiModifier.PRIVATE))
            continue;
        final MethodSignatureBackedByPsiMethod signature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY, isInRawContext);
        HierarchicalMethodSignatureImpl newH = new HierarchicalMethodSignatureImpl(MethodSignatureBackedByPsiMethod.create(method, substitutor, isInRawContext));
        List<PsiMethod> list = sameParameterErasureMethods.get(signature);
        if (list == null) {
            list = new SmartList<>();
            sameParameterErasureMethods.put(signature, list);
        }
        list.add(method);
        LOG.assertTrue(newH.getMethod().isValid());
        result.put(signature, newH);
        map.put(signature, newH);
    }
    final List<PsiClassType.ClassResolveResult> superTypes = PsiClassImplUtil.getScopeCorrectedSuperTypes(aClass, resolveScope);
    for (PsiClassType.ClassResolveResult superTypeResolveResult : superTypes) {
        PsiClass superClass = superTypeResolveResult.getElement();
        if (superClass == null)
            continue;
        // cyclic inheritance
        if (!visited.add(superClass))
            continue;
        final PsiSubstitutor superSubstitutor = superTypeResolveResult.getSubstitutor();
        PsiSubstitutor finalSubstitutor = PsiSuperMethodUtil.obtainFinalSubstitutor(superClass, superSubstitutor, substitutor, isInRawContext);
        final boolean isInRawContextSuper = (isInRawContext || PsiUtil.isRawSubstitutor(superClass, superSubstitutor)) && superClass.getTypeParameters().length != 0;
        Map<MethodSignature, HierarchicalMethodSignature> superResult = buildMethodHierarchy(superClass, nameHint, finalSubstitutor, false, visited, isInRawContextSuper, resolveScope);
        visited.remove(superClass);
        List<Pair<MethodSignature, HierarchicalMethodSignature>> flattened = new ArrayList<>();
        for (Map.Entry<MethodSignature, HierarchicalMethodSignature> entry : superResult.entrySet()) {
            HierarchicalMethodSignature hms = entry.getValue();
            MethodSignature signature = MethodSignatureBackedByPsiMethod.create(hms.getMethod(), hms.getSubstitutor(), hms.isRaw());
            PsiClass containingClass = hms.getMethod().getContainingClass();
            List<HierarchicalMethodSignature> supers = new ArrayList<>(hms.getSuperSignatures());
            for (HierarchicalMethodSignature aSuper : supers) {
                PsiClass superContainingClass = aSuper.getMethod().getContainingClass();
                if (containingClass != null && superContainingClass != null && !containingClass.isInheritor(superContainingClass, true)) {
                    // methods must be inherited from unrelated classes, so flatten hierarchy here
                    // class C implements SAM1, SAM2 { void methodimpl() {} }
                    //hms.getSuperSignatures().remove(aSuper);
                    flattened.add(Pair.create(signature, aSuper));
                }
            }
            putInMap(aClass, result, map, hms, signature);
        }
        for (Pair<MethodSignature, HierarchicalMethodSignature> pair : flattened) {
            putInMap(aClass, result, map, pair.second, pair.first);
        }
    }
    for (Map.Entry<MethodSignature, HierarchicalMethodSignatureImpl> entry : map.entrySet()) {
        HierarchicalMethodSignatureImpl hierarchicalMethodSignature = entry.getValue();
        MethodSignature methodSignature = entry.getKey();
        if (result.get(methodSignature) == null) {
            LOG.assertTrue(hierarchicalMethodSignature.getMethod().isValid());
            result.put(methodSignature, hierarchicalMethodSignature);
        }
    }
    return result;
}
Also used : PsiClassImpl(com.intellij.psi.impl.source.PsiClassImpl) HierarchicalMethodSignatureImpl(com.intellij.psi.impl.source.HierarchicalMethodSignatureImpl) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap) THashMap(gnu.trove.THashMap) Pair(com.intellij.openapi.util.Pair) FactoryMap(com.intellij.util.containers.FactoryMap) THashMap(gnu.trove.THashMap) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap) ConcurrentFactoryMap(com.intellij.util.containers.ConcurrentFactoryMap) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with LinkedHashMap

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

the class StatisticsUploadAssistantTest method createDescriptors.

protected static Map<GroupDescriptor, Set<UsageDescriptor>> createDescriptors(String... strs) {
    Map<GroupDescriptor, Set<UsageDescriptor>> set = new LinkedHashMap<>();
    for (String str : strs) {
        final List<String> list = StringUtil.split(str, ":");
        final GroupDescriptor g = GroupDescriptor.create(list.get(0));
        if (!set.containsKey(g)) {
            set.put(g, new LinkedHashSet<>());
        }
        set.get(g).add(createDescriptor(list.get(1), Integer.parseInt(list.get(2))));
    }
    return set;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) GroupDescriptor(com.intellij.internal.statistic.beans.GroupDescriptor) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 5 with LinkedHashMap

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

the class ReplaceDialog method getVariablesFromListeners.

@Override
protected List<Variable> getVariablesFromListeners() {
    List<Variable> vars = getVarsFrom(replaceCriteriaEdit);
    List<Variable> searchVars = super.getVariablesFromListeners();
    Map<String, Variable> varsMap = new LinkedHashMap<>(searchVars.size());
    for (Variable var : searchVars) varsMap.put(var.getName(), var);
    for (Variable var : vars) {
        if (!varsMap.containsKey(var.getName())) {
            String newVarName = var.getName() + ReplaceConfiguration.REPLACEMENT_VARIABLE_SUFFIX;
            varsMap.put(newVarName, new Variable(newVarName, null, null, false, false));
        }
    }
    return new ArrayList<>(varsMap.values());
}
Also used : Variable(com.intellij.codeInsight.template.impl.Variable) ArrayList(java.util.ArrayList) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Aggregations

LinkedHashMap (com.intellij.util.containers.hash.LinkedHashMap)12 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)2 AnAction (com.intellij.openapi.actionSystem.AnAction)2 RelativePoint (com.intellij.ui.awt.RelativePoint)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2 PrefixMatcher (com.intellij.codeInsight.completion.PrefixMatcher)1 AnnotationHolderImpl (com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 CopyPastePreProcessor (com.intellij.codeInsight.editorActions.CopyPastePreProcessor)1 Variable (com.intellij.codeInsight.template.impl.Variable)1 HyperlinkInfo (com.intellij.execution.filters.HyperlinkInfo)1 ProcessInfo (com.intellij.execution.process.ProcessInfo)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 GroupDescriptor (com.intellij.internal.statistic.beans.GroupDescriptor)1 Annotation (com.intellij.lang.annotation.Annotation)1 AnnotationSession (com.intellij.lang.annotation.AnnotationSession)1 ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1