Search in sources :

Example 6 with MarkupModelEx

use of com.intellij.openapi.editor.ex.MarkupModelEx in project intellij-community by JetBrains.

the class UpdateHighlightersUtil method setHighlightersInRange.

static void setHighlightersInRange(@NotNull final Project project, @NotNull final Document document, @NotNull final TextRange range, // if null global scheme will be used
@Nullable final EditorColorsScheme colorsScheme, @NotNull final List<HighlightInfo> infos, @NotNull final MarkupModelEx markup, final int group) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    final SeverityRegistrar severityRegistrar = SeverityRegistrar.getSeverityRegistrar(project);
    final HighlightersRecycler infosToRemove = new HighlightersRecycler();
    DaemonCodeAnalyzerEx.processHighlights(document, project, null, range.getStartOffset(), range.getEndOffset(), info -> {
        if (info.getGroup() == group) {
            RangeHighlighter highlighter = info.getHighlighter();
            int hiStart = highlighter.getStartOffset();
            int hiEnd = highlighter.getEndOffset();
            boolean willBeRemoved = hiEnd == document.getTextLength() && range.getEndOffset() == document.getTextLength() || range.containsRange(hiStart, hiEnd);
            if (willBeRemoved) {
                infosToRemove.recycleHighlighter(highlighter);
                info.setHighlighter(null);
            }
        }
        return true;
    });
    ContainerUtil.quickSort(infos, BY_START_OFFSET_NODUPS);
    final Map<TextRange, RangeMarker> ranges2markersCache = new THashMap<>(10);
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
    final DaemonCodeAnalyzerEx codeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(project);
    final boolean[] changed = { false };
    RangeMarkerTree.sweep((RangeMarkerTree.Generator<HighlightInfo>) processor -> ContainerUtil.process(infos, processor), (offset, info, atStart, overlappingIntervals) -> {
        if (!atStart) {
            return true;
        }
        if (info.isFileLevelAnnotation() && psiFile != null && psiFile.getViewProvider().isPhysical()) {
            codeAnalyzer.addFileLevelHighlight(project, group, info, psiFile);
            changed[0] = true;
            return true;
        }
        if (isWarningCoveredByError(info, overlappingIntervals, severityRegistrar)) {
            return true;
        }
        if (info.getStartOffset() >= range.getStartOffset() && info.getEndOffset() <= range.getEndOffset() && psiFile != null) {
            createOrReuseHighlighterFor(info, colorsScheme, document, group, psiFile, markup, infosToRemove, ranges2markersCache, severityRegistrar);
            changed[0] = true;
        }
        return true;
    });
    for (RangeHighlighter highlighter : infosToRemove.forAllInGarbageBin()) {
        highlighter.dispose();
        changed[0] = true;
    }
    if (changed[0]) {
        clearWhiteSpaceOptimizationFlag(document);
    }
    assertMarkupConsistent(markup, project);
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) GutterMark(com.intellij.codeInsight.daemon.GutterMark) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) RangeMarker(com.intellij.openapi.editor.RangeMarker) RedBlackTree(com.intellij.openapi.editor.impl.RedBlackTree) com.intellij.openapi.editor.markup(com.intellij.openapi.editor.markup) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarkerTree(com.intellij.openapi.editor.impl.RangeMarkerTree) RangeMarker(com.intellij.openapi.editor.RangeMarker) THashMap(gnu.trove.THashMap) PsiFile(com.intellij.psi.PsiFile)

Example 7 with MarkupModelEx

use of com.intellij.openapi.editor.ex.MarkupModelEx in project intellij-community by JetBrains.

the class DocumentMarkupModelTest method testPersistentHighlighterUpdateOnPartialDocumentUpdate.

public void testPersistentHighlighterUpdateOnPartialDocumentUpdate() {
    Document document = new DocumentImpl("line0\nline1\nline2");
    MarkupModelEx model = (MarkupModelEx) DocumentMarkupModel.forDocument(document, getProject(), true);
    RangeHighlighterEx highlighter = model.addPersistentLineHighlighter(2, 0, null);
    new WriteCommandAction<Void>(getProject()) {

        @Override
        protected void run(@NotNull Result<Void> result) throws Throwable {
            document.deleteString(document.getLineStartOffset(1), document.getTextLength());
        }
    }.execute();
    assertTrue(highlighter.isValid());
    assertEquals(6, highlighter.getStartOffset());
}
Also used : MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Document(com.intellij.openapi.editor.Document)

Example 8 with MarkupModelEx

use of com.intellij.openapi.editor.ex.MarkupModelEx in project intellij-community by JetBrains.

the class EditorHyperlinkSupport method linkFollowed.

// todo fix link followed here!
private static void linkFollowed(Editor editor, Collection<RangeHighlighter> ranges, final RangeHighlighter link) {
    MarkupModelEx markupModel = (MarkupModelEx) editor.getMarkupModel();
    for (RangeHighlighter range : ranges) {
        TextAttributes oldAttr = range.getUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES);
        if (oldAttr != null) {
            markupModel.setRangeHighlighterAttributes(range, oldAttr);
            range.putUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES, null);
        }
        if (range == link) {
            range.putUserData(OLD_HYPERLINK_TEXT_ATTRIBUTES, range.getTextAttributes());
            markupModel.setRangeHighlighterAttributes(range, getFollowedHyperlinkAttributes(range));
        }
    }
    //refresh highlighter text attributes
    markupModel.addRangeHighlighter(0, 0, link.getLayer(), getHyperlinkAttributes(), HighlighterTargetArea.EXACT_RANGE).dispose();
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 9 with MarkupModelEx

use of com.intellij.openapi.editor.ex.MarkupModelEx in project intellij-community by JetBrains.

the class EditorHyperlinkSupport method getHyperlinks.

private static List<RangeHighlighter> getHyperlinks(int startOffset, int endOffset, final Editor editor) {
    final MarkupModelEx markupModel = (MarkupModelEx) editor.getMarkupModel();
    final CommonProcessors.CollectProcessor<RangeHighlighterEx> processor = new CommonProcessors.CollectProcessor<>();
    markupModel.processRangeHighlightersOverlappingWith(startOffset, endOffset, new FilteringProcessor<>(rangeHighlighterEx -> rangeHighlighterEx.isValid() && getHyperlinkInfo(rangeHighlighterEx) != null, processor));
    return new ArrayList<>(processor.getResults());
}
Also used : EditorMouseAdapter(com.intellij.openapi.editor.event.EditorMouseAdapter) UIUtil(com.intellij.util.ui.UIUtil) HyperlinkInfoBase(com.intellij.execution.filters.HyperlinkInfoBase) Document(com.intellij.openapi.editor.Document) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap) ArrayList(java.util.ArrayList) NavigatableAdapter(com.intellij.pom.NavigatableAdapter) FilteringProcessor(com.intellij.util.FilteringProcessor) CodeInsightColors(com.intellij.openapi.editor.colors.CodeInsightColors) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) Map(java.util.Map) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) OccurenceNavigator(com.intellij.ide.OccurenceNavigator) CommonProcessors(com.intellij.util.CommonProcessors) Filter(com.intellij.execution.filters.Filter) Collection(java.util.Collection) Key(com.intellij.openapi.util.Key) Editor(com.intellij.openapi.editor.Editor) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) MouseEvent(java.awt.event.MouseEvent) EditorMouseEvent(com.intellij.openapi.editor.event.EditorMouseEvent) HighlighterTargetArea(com.intellij.openapi.editor.markup.HighlighterTargetArea) java.awt(java.awt) HyperlinkInfo(com.intellij.execution.filters.HyperlinkInfo) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) NotNull(org.jetbrains.annotations.NotNull) RelativePoint(com.intellij.ui.awt.RelativePoint) Consumer(com.intellij.util.Consumer) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) ArrayList(java.util.ArrayList) CommonProcessors(com.intellij.util.CommonProcessors)

Example 10 with MarkupModelEx

use of com.intellij.openapi.editor.ex.MarkupModelEx in project intellij-community by JetBrains.

the class RangeMarkerTest method testRangeHighlighterIteratorOrder.

public void testRangeHighlighterIteratorOrder() throws Exception {
    Document document = EditorFactory.getInstance().createDocument("1234567890");
    final MarkupModelEx markupModel = (MarkupModelEx) DocumentMarkupModel.forDocument(document, ourProject, true);
    RangeHighlighter exact = markupModel.addRangeHighlighter(3, 6, 0, null, HighlighterTargetArea.EXACT_RANGE);
    RangeHighlighter line = markupModel.addRangeHighlighter(4, 5, 0, null, HighlighterTargetArea.LINES_IN_RANGE);
    List<RangeHighlighter> list = new ArrayList<>();
    markupModel.processRangeHighlightersOverlappingWith(2, 9, new CommonProcessors.CollectProcessor<>(list));
    assertEquals(Arrays.asList(line, exact), list);
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) CommonProcessors(com.intellij.util.CommonProcessors)

Aggregations

MarkupModelEx (com.intellij.openapi.editor.ex.MarkupModelEx)33 RangeHighlighterEx (com.intellij.openapi.editor.ex.RangeHighlighterEx)17 Document (com.intellij.openapi.editor.Document)13 List (java.util.List)11 Project (com.intellij.openapi.project.Project)10 NotNull (org.jetbrains.annotations.NotNull)10 java.awt (java.awt)8 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)7 DocumentMarkupModel (com.intellij.openapi.editor.impl.DocumentMarkupModel)7 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)7 PsiFile (com.intellij.psi.PsiFile)7 Processor (com.intellij.util.Processor)7 ContainerUtil (com.intellij.util.containers.ContainerUtil)7 Nullable (org.jetbrains.annotations.Nullable)7 GutterMark (com.intellij.codeInsight.daemon.GutterMark)6 java.util (java.util)6 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)5 ApplicationManager (com.intellij.openapi.application.ApplicationManager)5 Editor (com.intellij.openapi.editor.Editor)5 RangeMarker (com.intellij.openapi.editor.RangeMarker)5