Search in sources :

Example 11 with Document

use of nl.tudelft.watchdog.core.logic.document.Document in project watchdog by TestRoots.

the class IntervalJsonConverterTest method testJsonTypingIntervalDiffsAddition.

@Test
public void testJsonTypingIntervalDiffsAddition() {
    ITextEditor editor = Mockito.mock(ITextEditor.class);
    TypingInterval interval = new TypingInterval(new EditorWrapper(editor), new Date(1));
    interval.setDocument(new Document("Project", "filepath", "Production.java", "blah-document"));
    interval.setEndingDocument(new Document("Project", "filepath", "Production.java", "blah-document-add"));
    interval.close();
    sleepABit();
    ArrayList<WatchDogItem> intervals = new ArrayList<>();
    intervals.add(interval);
    assertEquals("[{\"endingDocument\":{\"pn\":\"f6f4da8d93e88a08220e03b7810451d3ba540a34\",\"fn\":\"e4afa075bb910c8ecb427e9950426a4599b21d7e\",\"sloc\":1,\"dt\":\"un\"},\"diff\":4,\"modCountDiff\":0,\"charLengthDiff\":4,\"doc\":{\"pn\":\"f6f4da8d93e88a08220e03b7810451d3ba540a34\",\"fn\":\"e4afa075bb910c8ecb427e9950426a4599b21d7e\",\"sloc\":1,\"dt\":\"un\"},\"it\":\"ty\",\"ts\":1," + pasteWDVAndClient() + "}]", transferer.toJson(intervals));
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) TypingInterval(nl.tudelft.watchdog.core.logic.interval.intervaltypes.TypingInterval) EditorWrapper(nl.tudelft.watchdog.eclipse.logic.document.EditorWrapper) ArrayList(java.util.ArrayList) Document(nl.tudelft.watchdog.core.logic.document.Document) Date(java.util.Date) WatchDogItem(nl.tudelft.watchdog.core.logic.storage.WatchDogItem) Test(org.junit.Test)

Example 12 with Document

use of nl.tudelft.watchdog.core.logic.document.Document in project watchdog by TestRoots.

the class IntervalJsonConverterTest method createSampleIntervals.

private ArrayList<WatchDogItem> createSampleIntervals(EditorIntervalBase interval) {
    interval.setDocument(new Document("Project", "Production.java", "filepath", "blah-document"));
    ArrayList<WatchDogItem> intervals = createSampleIntervals((IntervalBase) interval);
    return intervals;
}
Also used : Document(nl.tudelft.watchdog.core.logic.document.Document) WatchDogItem(nl.tudelft.watchdog.core.logic.storage.WatchDogItem)

Example 13 with Document

use of nl.tudelft.watchdog.core.logic.document.Document 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

Document (nl.tudelft.watchdog.core.logic.document.Document)13 Test (org.junit.Test)7 WatchDogItem (nl.tudelft.watchdog.core.logic.storage.WatchDogItem)6 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)6 Date (java.util.Date)5 TypingInterval (nl.tudelft.watchdog.core.logic.interval.intervaltypes.TypingInterval)5 EditorWrapper (nl.tudelft.watchdog.eclipse.logic.document.EditorWrapper)5 ArrayList (java.util.ArrayList)4 IFile (org.eclipse.core.resources.IFile)2 IPath (org.eclipse.core.runtime.IPath)2 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 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1