Search in sources :

Example 6 with LineMarkerInfo

use of com.intellij.codeInsight.daemon.LineMarkerInfo in project intellij-elixir by KronicDeth.

the class CallDefinition method getLineMarkerInfo.

@Nullable
private LineMarkerInfo getLineMarkerInfo(@NotNull AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
    LineMarkerInfo lineMarkerInfo = null;
    String moduleAttributeName = moduleAttributeName(atUnqualifiedNoParenthesesCall);
    if (moduleAttributeName.equals("@doc")) {
        PsiElement previousExpression = siblingExpression(atUnqualifiedNoParenthesesCall, PREVIOUS_SIBLING);
        boolean firstInGroup = true;
        if (previousExpression instanceof AtUnqualifiedNoParenthesesCall) {
            AtUnqualifiedNoParenthesesCall previousModuleAttribute = (AtUnqualifiedNoParenthesesCall) previousExpression;
            String previousModuleAttributeName = moduleAttributeName(previousModuleAttribute);
            if (previousModuleAttributeName.equals("@spec")) {
                Pair<String, Integer> moduleAttributeNameArity = moduleAttributeNameArity(previousModuleAttribute);
                if (moduleAttributeNameArity != null) {
                    Call nextSiblingCallDefinitionClause = siblingCallDefinitionClause(atUnqualifiedNoParenthesesCall, NEXT_SIBLING);
                    if (nextSiblingCallDefinitionClause != null) {
                        Pair<String, IntRange> nameArityRange = nameArityRange(nextSiblingCallDefinitionClause);
                        if (nameArityRange != null) {
                            IntRange arityRange = nameArityRange.second;
                            if (arityRange.containsInteger(moduleAttributeNameArity.second)) {
                                // the previous spec is part of the group
                                firstInGroup = false;
                            }
                        }
                    }
                }
            }
        }
        if (firstInGroup) {
            lineMarkerInfo = callDefinitionSeparator(atUnqualifiedNoParenthesesCall);
        }
    } else if (moduleAttributeName.equals("@spec")) {
        PsiElement previousExpression = siblingExpression(atUnqualifiedNoParenthesesCall, PREVIOUS_SIBLING);
        boolean firstInGroup = true;
        if (previousExpression instanceof AtUnqualifiedNoParenthesesCall) {
            AtUnqualifiedNoParenthesesCall previousModuleAttribute = (AtUnqualifiedNoParenthesesCall) previousExpression;
            String previousModuleAttributeName = moduleAttributeName(previousModuleAttribute);
            if (previousModuleAttributeName.equals("@doc")) {
                firstInGroup = false;
            } else if (previousModuleAttributeName.equals("@spec")) {
                Pair<String, Integer> moduleAttributeNameArity = moduleAttributeNameArity(atUnqualifiedNoParenthesesCall);
                if (moduleAttributeNameArity != null) {
                    Pair<String, Integer> previousModuleAttributeNameArity = moduleAttributeNameArity(previousModuleAttribute);
                    if (previousModuleAttributeNameArity != null) {
                        // name match, now check if the arities match.
                        if (moduleAttributeNameArity.first.equals(previousModuleAttributeNameArity.first)) {
                            Integer moduleAttributeArity = moduleAttributeNameArity.second;
                            Integer previousModuleAttributeArity = previousModuleAttributeNameArity.second;
                            if (moduleAttributeArity.equals(previousModuleAttributeArity)) {
                                /* same arity with different pattern is same function, so the previous @spec should
                                       check if it is first because this one isn't */
                                firstInGroup = false;
                            } else {
                                /* same name, but different arity needs to determine if the call definition has an
                                       arity range. */
                                Call specification = specification(atUnqualifiedNoParenthesesCall);
                                if (specification != null) {
                                    Call type = specificationType(specification);
                                    if (type != null) {
                                        PsiReference reference = type.getReference();
                                        if (reference != null) {
                                            List<PsiElement> resolvedList = null;
                                            if (reference instanceof PsiPolyVariantReference) {
                                                PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
                                                ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
                                                if (resolveResults.length > 0) {
                                                    resolvedList = new ArrayList<PsiElement>();
                                                    for (ResolveResult resolveResult : resolveResults) {
                                                        resolvedList.add(resolveResult.getElement());
                                                    }
                                                }
                                            } else {
                                                PsiElement resolved = reference.resolve();
                                                if (resolved != null) {
                                                    resolvedList = Collections.singletonList(resolved);
                                                }
                                            }
                                            if (resolvedList != null && resolvedList.size() > 0) {
                                                for (PsiElement resolved : resolvedList) {
                                                    if (resolved instanceof Call) {
                                                        Pair<String, IntRange> resolvedNameArityRange = nameArityRange((Call) resolved);
                                                        if (resolvedNameArityRange != null) {
                                                            IntRange resolvedArityRange = resolvedNameArityRange.second;
                                                            if (resolvedArityRange.containsInteger(moduleAttributeArity) && resolvedArityRange.containsInteger(previousModuleAttributeArity)) {
                                                                // the current @spec and the previous @spec apply to the same call definition clause
                                                                firstInGroup = false;
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } else {
            Call specification = specification(atUnqualifiedNoParenthesesCall);
            if (specification != null) {
                Call type = specificationType(specification);
                if (type != null) {
                    PsiReference reference = type.getReference();
                    if (reference != null) {
                        if (reference instanceof PsiPolyVariantReference) {
                            PsiPolyVariantReference polyVariantReference = (PsiPolyVariantReference) reference;
                            ResolveResult[] resolveResults = polyVariantReference.multiResolve(false);
                            PsiFile containingFile = type.getContainingFile();
                            for (ResolveResult resolveResult : resolveResults) {
                                PsiElement element = resolveResult.getElement();
                                if (element != null) {
                                    if (element.getContainingFile().equals(containingFile) && element.getTextOffset() < type.getTextOffset()) {
                                        firstInGroup = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (firstInGroup) {
            lineMarkerInfo = callDefinitionSeparator(atUnqualifiedNoParenthesesCall);
        }
    }
    return lineMarkerInfo;
}
Also used : Call(org.elixir_lang.psi.call.Call) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) AtUnqualifiedNoParenthesesCall(org.elixir_lang.psi.AtUnqualifiedNoParenthesesCall) IntRange(org.apache.commons.lang.math.IntRange) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with LineMarkerInfo

use of com.intellij.codeInsight.daemon.LineMarkerInfo in project intellij-community by JetBrains.

the class ExpectedHighlightingData method extractExpectedLineMarkerSet.

private void extractExpectedLineMarkerSet(Document document) {
    String text = document.getText();
    String pat = ".*?((<" + LINE_MARKER + ")(?: descr=\"((?:[^\"\\\\]|\\\\\")*)\")?>)(.*)";
    final Pattern p = Pattern.compile(pat, Pattern.DOTALL);
    final Pattern pat2 = Pattern.compile("(.*?)(</" + LINE_MARKER + ">)(.*)", Pattern.DOTALL);
    while (true) {
        Matcher m = p.matcher(text);
        if (!m.matches())
            break;
        int startOffset = m.start(1);
        final String descr = m.group(3) != null ? m.group(3) : ANY_TEXT;
        String rest = m.group(4);
        document.replaceString(startOffset, m.end(1), "");
        final Matcher matcher2 = pat2.matcher(rest);
        LOG.assertTrue(matcher2.matches(), "Cannot find closing </" + LINE_MARKER + ">");
        String content = matcher2.group(1);
        int endOffset = startOffset + matcher2.start(3);
        String endTag = matcher2.group(2);
        document.replaceString(startOffset, endOffset, content);
        endOffset -= endTag.length();
        LineMarkerInfo markerInfo = new LineMarkerInfo<PsiElement>(myFile, new TextRange(startOffset, endOffset), null, Pass.LINE_MARKERS, new ConstantFunction<>(descr), null, GutterIconRenderer.Alignment.RIGHT);
        myLineMarkerInfos.put(document.createRangeMarker(startOffset, endOffset), markerInfo);
        text = document.getText();
    }
}
Also used : Pattern(java.util.regex.Pattern) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) Matcher(java.util.regex.Matcher)

Example 8 with LineMarkerInfo

use of com.intellij.codeInsight.daemon.LineMarkerInfo in project intellij-plugins by JetBrains.

the class DartServerOverrideMarkerProvider method tryCreateOverrideMarker.

@Nullable
private static LineMarkerInfo tryCreateOverrideMarker(@NotNull final DartComponentName componentName, @Nullable final DartComponent superclassComponent, @NotNull final List<DartComponent> interfaceComponents) {
    if (superclassComponent == null && interfaceComponents.isEmpty()) {
        return null;
    }
    final String name = componentName.getName();
    final boolean overrides;
    final DartComponent superComponent;
    if (superclassComponent != null) {
        overrides = true;
        superComponent = superclassComponent;
    } else {
        overrides = false;
        superComponent = interfaceComponents.iterator().next();
    }
    final Icon icon = overrides ? AllIcons.Gutter.OverridingMethod : AllIcons.Gutter.ImplementingMethod;
    return new LineMarkerInfo<>(componentName, componentName.getTextRange(), icon, Pass.LINE_MARKERS, element -> {
        final DartClass superClass = PsiTreeUtil.getParentOfType(superComponent, DartClass.class);
        if (superClass == null)
            return "null";
        if (overrides) {
            return DartBundle.message(superclassComponent.isOperator() ? "overrides.operator.in" : "overrides.method.in", name, superClass.getName());
        }
        return DartBundle.message("implements.method.in", name, superClass.getName());
    }, (GutterIconNavigationHandler<PsiElement>) (e, elt) -> {
        List<DartComponent> superComponents = Lists.newArrayList();
        if (superclassComponent != null) {
            superComponents.add(superclassComponent);
        }
        superComponents.addAll(interfaceComponents);
        PsiElementListNavigator.openTargets(e, DartResolveUtil.getComponentNameArray(superComponents), DaemonBundle.message("navigation.title.super.method", name), DaemonBundle.message("navigation.findUsages.title.super.method", name), new DefaultPsiElementCellRenderer());
    }, GutterIconRenderer.Alignment.LEFT);
}
Also used : DartResolveUtil(com.jetbrains.lang.dart.util.DartResolveUtil) DartClass(com.jetbrains.lang.dart.psi.DartClass) AllIcons(com.intellij.icons.AllIcons) GutterIconRenderer(com.intellij.openapi.editor.markup.GutterIconRenderer) VirtualFile(com.intellij.openapi.vfs.VirtualFile) DartComponentName(com.jetbrains.lang.dart.psi.DartComponentName) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Lists(com.google.common.collect.Lists) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) DartHierarchyUtil(com.jetbrains.lang.dart.ide.hierarchy.DartHierarchyUtil) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) DartServerData(com.jetbrains.lang.dart.analyzer.DartServerData) PsiElementListNavigator(com.intellij.codeInsight.daemon.impl.PsiElementListNavigator) OverriddenMember(org.dartlang.analysis.server.protocol.OverriddenMember) Collection(java.util.Collection) DartComponent(com.jetbrains.lang.dart.psi.DartComponent) DartBundle(com.jetbrains.lang.dart.DartBundle) Pass(com.intellij.codeHighlighting.Pass) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) Nullable(org.jetbrains.annotations.Nullable) DaemonBundle(com.intellij.codeInsight.daemon.DaemonBundle) List(java.util.List) GutterIconNavigationHandler(com.intellij.codeInsight.daemon.GutterIconNavigationHandler) LineMarkerProvider(com.intellij.codeInsight.daemon.LineMarkerProvider) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) DartComponent(com.jetbrains.lang.dart.psi.DartComponent) DartClass(com.jetbrains.lang.dart.psi.DartClass) DefaultPsiElementCellRenderer(com.intellij.ide.util.DefaultPsiElementCellRenderer) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with LineMarkerInfo

use of com.intellij.codeInsight.daemon.LineMarkerInfo in project intellij-plugins by JetBrains.

the class DartMethodLineMarkerProvider method getLineMarkerInfo.

@Nullable
@Override
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) {
    if (!myDaemonSettings.SHOW_METHOD_SEPARATORS) {
        return null;
    }
    // only continue if element is one of the markable elements (such as methods)
    if (isMarkableElement(element)) {
        // the method line markers are not nestable, aka, methods inside of methods, are not marked
        if (PsiTreeUtil.findFirstParent(element, true, DartMethodLineMarkerProvider::isMarkableElement) != null) {
            return null;
        }
        // move the marker to previous siblings until comments have been included
        PsiElement markerLocation = element;
        while (markerLocation.getPrevSibling() != null && (markerLocation.getPrevSibling() instanceof PsiComment || (markerLocation.getPrevSibling() instanceof PsiWhiteSpace && markerLocation.getPrevSibling().getPrevSibling() != null && markerLocation.getPrevSibling().getPrevSibling() instanceof PsiComment))) {
            markerLocation = markerLocation.getPrevSibling();
        }
        // if the markerLocation element doesn't have a previous sibling (not whitespace), do not mark
        PsiElement prevElement = markerLocation;
        while (prevElement.getPrevSibling() != null && prevElement.getPrevSibling() instanceof PsiWhiteSpace) {
            prevElement = prevElement.getPrevSibling();
        }
        if (prevElement.getPrevSibling() == null) {
            return null;
        }
        // finally, create the marker
        LineMarkerInfo info = new LineMarkerInfo<>(markerLocation, markerLocation.getTextRange(), null, Pass.LINE_MARKERS, FunctionUtil.<Object, String>nullConstant(), null, GutterIconRenderer.Alignment.RIGHT);
        EditorColorsScheme scheme = myColorsManager.getGlobalScheme();
        info.separatorColor = scheme.getColor(CodeInsightColors.METHOD_SEPARATORS_COLOR);
        info.separatorPlacement = SeparatorPlacement.TOP;
        return info;
    }
    return null;
}
Also used : LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) PsiComment(com.intellij.psi.PsiComment) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with LineMarkerInfo

use of com.intellij.codeInsight.daemon.LineMarkerInfo in project android by JetBrains.

the class AndroidGotoRelatedTest method doCheckLineMarkers.

private void doCheckLineMarkers(List<VirtualFile> expectedTargetFiles, Class<?> targetElementClass) {
    final List<LineMarkerInfo> relatedMarkers = doGetRelatedLineMarkers();
    assertEquals(relatedMarkers.toString(), 1, relatedMarkers.size());
    final LineMarkerInfo marker = relatedMarkers.get(0);
    doCheckItems(expectedTargetFiles, ((AndroidLineMarkerProvider.MyNavigationHandler) marker.getNavigationHandler()).doComputeItems(), targetElementClass);
}
Also used : LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo)

Aggregations

LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)42 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)12 NotNull (org.jetbrains.annotations.NotNull)7 Pass (com.intellij.codeHighlighting.Pass)5 GutterIconNavigationHandler (com.intellij.codeInsight.daemon.GutterIconNavigationHandler)5 AllIcons (com.intellij.icons.AllIcons)5 Document (com.intellij.openapi.editor.Document)5 GutterIconRenderer (com.intellij.openapi.editor.markup.GutterIconRenderer)5 Collection (java.util.Collection)5 LineMarkerProvider (com.intellij.codeInsight.daemon.LineMarkerProvider)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 Project (com.intellij.openapi.project.Project)4 List (java.util.List)4 javax.swing (javax.swing)4 DaemonBundle (com.intellij.codeInsight.daemon.DaemonBundle)3 PsiElementListNavigator (com.intellij.codeInsight.daemon.impl.PsiElementListNavigator)3 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)3 MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3