Search in sources :

Example 1 with MarkupModelImpl

use of com.intellij.openapi.editor.impl.MarkupModelImpl in project watchdog by TestRoots.

the class IntelliJMarkupModelListener method initializeAfterAnalysisFinished.

/**
 * Create a new {@link IntelliJMarkupModelListener} for an editor. This listener is only attached after the
 * {@link DaemonCodeAnalyzer} has finished analyzing this file.
 *
 * The listener is attached to the {@link com.intellij.openapi.editor.markup.MarkupModel} of the document,
 * which contains all {@link RangeHighlighterEx}s that represent the Static Analysis warnings.
 *
 * @param project The project the file exists in.
 * @param disposable The disposable to clean up the listeners and any potential {@link MessageBusConnection}.
 * @param editor The editor of the document.
 * @param trackingEventManager The manager that can process all the events generated.
 * @return A newly initiated listener that will later be attached to the {@link com.intellij.openapi.editor.markup.MarkupModel} of the document
 */
public static IntelliJMarkupModelListener initializeAfterAnalysisFinished(Project project, Disposable disposable, Editor editor, TrackingEventManager trackingEventManager) {
    final com.intellij.openapi.editor.Document intellijDocument = editor.getDocument();
    final IntelliJMarkupModelListener markupModelListener = new IntelliJMarkupModelListener(DocumentCreator.createDocument(editor), trackingEventManager, intellijDocument);
    // We need to run this in smart mode, because the very first time you start your editor, it is very briefly
    // in dumb mode and the codeAnalyzer thinks (incorrectly) it is  finished.
    // Therefore, wait for smart mode and only then start listening, to make sure the codeAnalyzer actually did its thing.
    // For more information see https://www.jetbrains.org/intellij/sdk/docs/basics/indexing_and_psi_stubs.html
    DumbServiceImpl.getInstance(project).runWhenSmart(() -> {
        final DaemonCodeAnalyzerImpl analyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzer.getInstance(project);
        final MessageBusConnection codeAnalyzerMessageBusConnection = project.getMessageBus().connect(disposable);
        final MessageBusConnection documentMessageBusConnection = project.getMessageBus().connect(disposable);
        codeAnalyzerMessageBusConnection.subscribe(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC, new DaemonCodeAnalyzer.DaemonListenerAdapter() {

            @Override
            public void daemonFinished() {
                // the analyzer actually finished. In this case, `null` indicates: the file is not dirty.
                if (analyzer.getFileStatusMap().getFileDirtyScope(intellijDocument, Pass.UPDATE_ALL) == null) {
                    final MarkupModelImpl markupModel = (MarkupModelImpl) DocumentMarkupModel.forDocument(intellijDocument, project, true);
                    markupModelListener.processWarningSnapshot(markupModel.getAllHighlighters());
                    markupModel.addMarkupModelListener(disposable, markupModelListener);
                    // We batch up changes and only transfer them on every save. This is in-line with the Eclipse
                    // interface, which only exposes listeners for `POST_BUILD`. Therefore, cache all warnings in
                    // {@link IntelliJMarkupModelListener#generatedWarnings} and {@link IntelliJMarkupModelListener#warnings}
                    // and flush these warnings after the fact.
                    documentMessageBusConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {

                        @Override
                        public void beforeDocumentSaving(@NotNull com.intellij.openapi.editor.Document savedDocument) {
                            if (intellijDocument.equals(savedDocument)) {
                                markupModelListener.flushForDocument();
                            }
                        }
                    });
                    codeAnalyzerMessageBusConnection.disconnect();
                }
            }
        });
    });
    return markupModelListener;
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) DaemonCodeAnalyzerImpl(com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl) Document(nl.tudelft.watchdog.core.logic.document.Document) NotNull(org.jetbrains.annotations.NotNull) FileDocumentManagerAdapter(com.intellij.openapi.fileEditor.FileDocumentManagerAdapter) DaemonCodeAnalyzer(com.intellij.codeInsight.daemon.DaemonCodeAnalyzer) MarkupModelImpl(com.intellij.openapi.editor.impl.MarkupModelImpl)

Aggregations

DaemonCodeAnalyzer (com.intellij.codeInsight.daemon.DaemonCodeAnalyzer)1 DaemonCodeAnalyzerImpl (com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl)1 MarkupModelImpl (com.intellij.openapi.editor.impl.MarkupModelImpl)1 FileDocumentManagerAdapter (com.intellij.openapi.fileEditor.FileDocumentManagerAdapter)1 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)1 Document (nl.tudelft.watchdog.core.logic.document.Document)1 NotNull (org.jetbrains.annotations.NotNull)1