Search in sources :

Example 26 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project intellij-community by JetBrains.

the class MarkerCache method applyEvents.

private static UpdatedRanges applyEvents(@NotNull List<DocumentEvent> events, final UpdatedRanges struct) {
    FrozenDocument frozen = struct.myResultDocument;
    ManualRangeMarker[] resultMarkers = struct.myMarkers.clone();
    for (DocumentEvent event : events) {
        final FrozenDocument before = frozen;
        final DocumentEvent corrected;
        if (event instanceof RetargetRangeMarkers) {
            RetargetRangeMarkers retarget = (RetargetRangeMarkers) event;
            corrected = new RetargetRangeMarkers(frozen, retarget.getStartOffset(), retarget.getEndOffset(), retarget.getMoveDestinationOffset());
        } else {
            corrected = new DocumentEventImpl(frozen, event.getOffset(), event.getOldFragment(), event.getNewFragment(), event.getOldTimeStamp(), event.isWholeTextReplaced(), ((DocumentEventImpl) event).getInitialStartOffset(), ((DocumentEventImpl) event).getInitialOldLength());
            frozen = frozen.applyEvent(event, 0);
        }
        int i = 0;
        while (i < resultMarkers.length) {
            ManualRangeMarker currentRange = resultMarkers[i];
            int sameMarkersEnd = i + 1;
            while (sameMarkersEnd < resultMarkers.length && resultMarkers[sameMarkersEnd] == currentRange) {
                sameMarkersEnd++;
            }
            ManualRangeMarker updatedRange = currentRange == null ? null : currentRange.getUpdatedRange(corrected, before);
            while (i < sameMarkersEnd) {
                resultMarkers[i] = updatedRange;
                i++;
            }
        }
    }
    return new UpdatedRanges(struct.myEventCount + events.size(), frozen, struct.mySortedInfos, resultMarkers);
}
Also used : FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument) DocumentEventImpl(com.intellij.openapi.editor.impl.event.DocumentEventImpl) ManualRangeMarker(com.intellij.openapi.editor.impl.ManualRangeMarker) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) RetargetRangeMarkers(com.intellij.openapi.editor.impl.event.RetargetRangeMarkers)

Example 27 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project intellij-community by JetBrains.

the class SelfElementInfo method getRange.

@Override
@Nullable
public Segment getRange() {
    if (hasRange()) {
        Document document = getDocumentToSynchronize();
        if (document != null) {
            PsiDocumentManagerBase documentManager = myManager.getPsiDocumentManager();
            List<DocumentEvent> events = documentManager.getEventsSinceCommit(document);
            if (!events.isEmpty()) {
                SmartPointerTracker tracker = myManager.getTracker(getVirtualFile());
                if (tracker != null) {
                    return tracker.getUpdatedRange(this, (FrozenDocument) documentManager.getLastCommittedDocument(document), events);
                }
            }
        }
    }
    return calcPsiRange();
}
Also used : Document(com.intellij.openapi.editor.Document) FrozenDocument(com.intellij.openapi.editor.impl.FrozenDocument) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) PsiDocumentManagerBase(com.intellij.psi.impl.PsiDocumentManagerBase) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project intellij-community by JetBrains.

the class DocumentTest method testCorrectlyAddingAndRemovingListeners.

public void testCorrectlyAddingAndRemovingListeners() throws Exception {
    new WriteCommandAction.Simple(getProject()) {

        @Override
        protected void run() throws Throwable {
            final Document doc = new DocumentImpl("");
            final StringBuilder b = new StringBuilder();
            doc.addDocumentListener(new DocumentAdapter() {

                @Override
                public void beforeDocumentChange(DocumentEvent e) {
                    b.append("before1 ");
                }

                @Override
                public void documentChanged(DocumentEvent e) {
                    b.append("after1 ");
                }
            });
            doc.addDocumentListener(new DocumentAdapter() {

                @Override
                public void beforeDocumentChange(DocumentEvent event) {
                    doc.removeDocumentListener(this);
                }
            });
            doc.addDocumentListener(new DocumentAdapter() {

                @Override
                public void documentChanged(DocumentEvent e) {
                    doc.removeDocumentListener(this);
                }
            });
            doc.addDocumentListener(new DocumentAdapter() {

                @Override
                public void beforeDocumentChange(DocumentEvent e) {
                    b.append("before2 ");
                }

                @Override
                public void documentChanged(DocumentEvent e) {
                    b.append("after2 ");
                }
            });
            doc.setText("foo");
            assertEquals("before2 before1 after1 after2 ", b.toString());
        }
    }.execute().throwException();
}
Also used : DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) DocumentImpl(com.intellij.openapi.editor.impl.DocumentImpl)

Example 29 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testDocumentUnsavedInsideChangeListener.

public void testDocumentUnsavedInsideChangeListener() throws IOException {
    VirtualFile file = createFile("a.txt", "a");
    FileDocumentManager manager = FileDocumentManager.getInstance();
    Document document = manager.getDocument(file);
    assertFalse(manager.isDocumentUnsaved(document));
    AtomicInteger invoked = new AtomicInteger();
    AtomicBoolean expectUnsaved = new AtomicBoolean(true);
    DocumentListener listener = new DocumentListener() {

        @Override
        public void beforeDocumentChange(DocumentEvent e) {
            assertFalse(manager.isDocumentUnsaved(document));
        }

        @Override
        public void documentChanged(DocumentEvent event) {
            invoked.incrementAndGet();
            assertEquals(expectUnsaved.get(), manager.isDocumentUnsaved(document));
        }
    };
    document.addDocumentListener(listener, getTestRootDisposable());
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(listener, getTestRootDisposable());
    WriteCommandAction.runWriteCommandAction(myProject, () -> document.insertString(0, "b"));
    assertTrue(manager.isDocumentUnsaved(document));
    assertEquals(2, invoked.get());
    expectUnsaved.set(false);
    FileDocumentManager.getInstance().saveAllDocuments();
    FileUtil.writeToFile(VfsUtilCore.virtualToIoFile(file), "something");
    file.refresh(false, false);
    assertEquals("something", document.getText());
    assertFalse(manager.isDocumentUnsaved(document));
    assertEquals(4, invoked.get());
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DocumentListener(com.intellij.openapi.editor.event.DocumentListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent)

Example 30 with DocumentEvent

use of com.intellij.openapi.editor.event.DocumentEvent in project intellij-community by JetBrains.

the class EncodingPanel method install.

@Override
public void install(@NotNull StatusBar statusBar) {
    super.install(statusBar);
    // should update to reflect encoding-from-content
    EncodingManager.getInstance().addPropertyChangeListener(evt -> {
        if (evt.getPropertyName().equals(EncodingManagerImpl.PROP_CACHED_ENCODING_CHANGED)) {
            Document document = evt.getSource() instanceof Document ? (Document) evt.getSource() : null;
            updateForDocument(document);
        }
    }, this);
    ApplicationManager.getApplication().getMessageBus().connect(this).subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(new VirtualFileAdapter() {

        @Override
        public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
            if (VirtualFile.PROP_ENCODING.equals(event.getPropertyName())) {
                updateForFile(event.getFile());
            }
        }
    }));
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            Document document = e.getDocument();
            updateForDocument(document);
        }
    }, this);
}
Also used : DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) BulkVirtualFileListenerAdapter(com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter) Document(com.intellij.openapi.editor.Document) VirtualFilePropertyEvent(com.intellij.openapi.vfs.VirtualFilePropertyEvent) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) NotNull(org.jetbrains.annotations.NotNull) VirtualFileAdapter(com.intellij.openapi.vfs.VirtualFileAdapter)

Aggregations

DocumentEvent (com.intellij.openapi.editor.event.DocumentEvent)54 DocumentAdapter (com.intellij.openapi.editor.event.DocumentAdapter)40 Document (com.intellij.openapi.editor.Document)14 DocumentListener (com.intellij.openapi.editor.event.DocumentListener)12 EditorTextField (com.intellij.ui.EditorTextField)8 ActionEvent (java.awt.event.ActionEvent)8 ActionListener (java.awt.event.ActionListener)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Nullable (org.jetbrains.annotations.Nullable)5 Disposable (com.intellij.openapi.Disposable)4 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)4 Editor (com.intellij.openapi.editor.Editor)4 ReferenceEditorComboWithBrowseButton (com.intellij.ui.ReferenceEditorComboWithBrowseButton)4 NotNull (org.jetbrains.annotations.NotNull)4 Language (com.intellij.lang.Language)3 AnAction (com.intellij.openapi.actionSystem.AnAction)3 Module (com.intellij.openapi.module.Module)3 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 FrozenDocument (com.intellij.openapi.editor.impl.FrozenDocument)2 LanguageFileType (com.intellij.openapi.fileTypes.LanguageFileType)2