Search in sources :

Example 16 with MarkupModelEx

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

the class DaemonRespondToChangesTest method testRangeMarkersDoNotGetAddedOrRemovedWhenUserIsJustTypingInsideHighlightedRegionAndEspeciallyInsideInjectedFragmentsWhichAreColoredGreenAndUsersComplainEndlesslyThatEditorFlickersThere.

public void testRangeMarkersDoNotGetAddedOrRemovedWhenUserIsJustTypingInsideHighlightedRegionAndEspeciallyInsideInjectedFragmentsWhichAreColoredGreenAndUsersComplainEndlesslyThatEditorFlickersThere() throws Throwable {
    configureByText(JavaFileType.INSTANCE, "class S { int f() {\n" + "    return <caret>hashCode();\n" + "}}");
    Collection<HighlightInfo> infos = doHighlighting(HighlightInfoType.SYMBOL_TYPE_SEVERITY);
    assertEquals(3, infos.size());
    final int[] count = { 0 };
    MarkupModelEx modelEx = (MarkupModelEx) DocumentMarkupModel.forDocument(getDocument(getFile()), getProject(), true);
    modelEx.addMarkupModelListener(getTestRootDisposable(), new MarkupModelListener.Adapter() {

        @Override
        public void afterAdded(@NotNull RangeHighlighterEx highlighter) {
            count[0]++;
        }

        @Override
        public void beforeRemoved(@NotNull RangeHighlighterEx highlighter) {
            count[0]++;
        }
    });
    type(' ');
    highlightErrors();
    assertEquals(0, count[0]);
}
Also used : MarkupModelListener(com.intellij.openapi.editor.impl.event.MarkupModelListener) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx)

Example 17 with MarkupModelEx

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

the class DaemonRespondToChangesTest method testLineMarkersReuse.

public void testLineMarkersReuse() throws Throwable {
    configureByFile(BASE_PATH + "LineMarkerChange.java");
    List<HighlightInfo> errors = highlightErrors();
    assertEmpty(errors);
    List<LineMarkerInfo> lineMarkers = DaemonCodeAnalyzerImpl.getLineMarkers(myEditor.getDocument(), getProject());
    assertSize(5, lineMarkers);
    type('X');
    final Collection<String> changed = new ArrayList<>();
    MarkupModelEx modelEx = (MarkupModelEx) DocumentMarkupModel.forDocument(getDocument(getFile()), getProject(), true);
    modelEx.addMarkupModelListener(getTestRootDisposable(), new MarkupModelListener() {

        @Override
        public void afterAdded(@NotNull RangeHighlighterEx highlighter) {
            changed(highlighter, ExceptionUtil.getThrowableText(new Throwable("after added")));
        }

        @Override
        public void beforeRemoved(@NotNull RangeHighlighterEx highlighter) {
            changed(highlighter, ExceptionUtil.getThrowableText(new Throwable("before removed")));
        }

        @Override
        public void attributesChanged(@NotNull RangeHighlighterEx highlighter, boolean renderersChanged, boolean fontStyleChanged) {
            changed(highlighter, ExceptionUtil.getThrowableText(new Throwable("changed")));
        }

        private void changed(@NotNull RangeHighlighterEx highlighter, String reason) {
            // not line marker
            if (highlighter.getTargetArea() != HighlighterTargetArea.LINES_IN_RANGE)
                return;
            List<LineMarkerInfo> lineMarkers = DaemonCodeAnalyzerImpl.getLineMarkers(myEditor.getDocument(), getProject());
            // not line marker
            if (ContainerUtil.find(lineMarkers, lm -> lm.highlighter == highlighter) == null)
                return;
            changed.add(highlighter + ": \n" + reason);
        }
    });
    PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
    List<HighlightInfo> infosAfter = CodeInsightTestFixtureImpl.instantiateAndRun(myFile, myEditor, new int[] {}, false);
    assertNotEmpty(filter(infosAfter, HighlightSeverity.ERROR));
    assertEmpty(changed);
    List<LineMarkerInfo> lineMarkersAfter = DaemonCodeAnalyzerImpl.getLineMarkers(myEditor.getDocument(), getProject());
    assertEquals(lineMarkersAfter.size(), lineMarkers.size());
}
Also used : MarkupModelListener(com.intellij.openapi.editor.impl.event.MarkupModelListener) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) List(java.util.List)

Example 18 with MarkupModelEx

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

the class Bookmark method findMyHighlighter.

private RangeHighlighterEx findMyHighlighter() {
    final Document document = getDocument();
    if (document == null)
        return null;
    RangeHighlighterEx result = SoftReference.dereference(myHighlighterRef);
    if (result != null) {
        return result;
    }
    MarkupModelEx markup = (MarkupModelEx) DocumentMarkupModel.forDocument(document, myProject, true);
    final Document markupDocument = markup.getDocument();
    final int startOffset = 0;
    final int endOffset = markupDocument.getTextLength();
    final Ref<RangeHighlighterEx> found = new Ref<>();
    markup.processRangeHighlightersOverlappingWith(startOffset, endOffset, highlighter -> {
        GutterMark renderer = highlighter.getGutterIconRenderer();
        if (renderer instanceof MyGutterIconRenderer && ((MyGutterIconRenderer) renderer).myBookmark == this) {
            found.set(highlighter);
            return false;
        }
        return true;
    });
    result = found.get();
    myHighlighterRef = result == null ? null : new WeakReference<>(result);
    return result;
}
Also used : Ref(com.intellij.openapi.util.Ref) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) WeakReference(java.lang.ref.WeakReference) Document(com.intellij.openapi.editor.Document) GutterMark(com.intellij.codeInsight.daemon.GutterMark)

Example 19 with MarkupModelEx

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

the class Bookmark method release.

public void release() {
    int line = getLine();
    if (line < 0) {
        return;
    }
    final Document document = getDocument();
    if (document == null)
        return;
    MarkupModelEx markup = (MarkupModelEx) DocumentMarkupModel.forDocument(document, myProject, true);
    final Document markupDocument = markup.getDocument();
    if (markupDocument.getLineCount() <= line)
        return;
    RangeHighlighterEx highlighter = findMyHighlighter();
    if (highlighter != null) {
        myHighlighterRef = null;
        highlighter.dispose();
    }
}
Also used : MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) Document(com.intellij.openapi.editor.Document)

Example 20 with MarkupModelEx

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

the class DocumentMarkupModel method forDocument.

/**
   * Returns the markup model for the specified project. A document can have multiple markup
   * models for different projects if the file to which it corresponds belongs to multiple projects
   * opened in different IDEA frames at the same time.
   *
   * @param document the document for which the markup model is requested.
   * @param project the project for which the markup model is requested, or null if the default markup
   *                model is requested.
   * @return the markup model instance.
   * @see com.intellij.openapi.editor.Editor#getMarkupModel()
   */
public static MarkupModel forDocument(@NotNull Document document, @Nullable Project project, boolean create) {
    if (document instanceof DocumentWindow) {
        final Document delegate = ((DocumentWindow) document).getDelegate();
        final MarkupModelEx baseMarkupModel = (MarkupModelEx) forDocument(delegate, project, true);
        return new MarkupModelWindow(baseMarkupModel, (DocumentWindow) document);
    }
    if (project == null) {
        MarkupModelEx markupModel = document.getUserData(MARKUP_MODEL_KEY);
        if (create && markupModel == null) {
            MarkupModelEx newModel = new MarkupModelImpl((DocumentEx) document);
            if ((markupModel = ((UserDataHolderEx) document).putUserDataIfAbsent(MARKUP_MODEL_KEY, newModel)) != newModel) {
                newModel.dispose();
            }
        }
        return markupModel;
    }
    final DocumentMarkupModelManager documentMarkupModelManager = project.isDisposed() ? null : DocumentMarkupModelManager.getInstance(project);
    if (documentMarkupModelManager == null || documentMarkupModelManager.isDisposed() || project.isDisposed()) {
        return new EmptyMarkupModel(document);
    }
    ConcurrentMap<Project, MarkupModelImpl> markupModelMap = getMarkupModelMap(document);
    MarkupModelImpl model = markupModelMap.get(project);
    if (create && model == null) {
        MarkupModelImpl newModel = new MarkupModelImpl((DocumentEx) document);
        if ((model = ConcurrencyUtil.cacheOrGet(markupModelMap, project, newModel)) == newModel) {
            documentMarkupModelManager.registerDocument(document);
        } else {
            newModel.dispose();
        }
    }
    return model;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) MarkupModelWindow(com.intellij.injected.editor.MarkupModelWindow) Document(com.intellij.openapi.editor.Document)

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