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);
}
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();
}
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();
}
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());
}
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);
}
Aggregations