MarkdownDocument: typed source reads after a pause off the main thread; texts past 2 MiB are never read
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
abac8b41b3
commit
69b11d7852
5 files changed
+57
-9
No files matched your search
@@ -36,7 +36,7 @@ impl MarkdownDocument {
|
||||
};
|
||||
let written = self.commit(cx);
|
||||
if written.is_some() {
|
||||
self.reparse(cx);
|
||||
self.reparse(true, cx);
|
||||
}
|
||||
if !self.read_now(cx) {
|
||||
return log::warn!("markdown document: pressed while the text is read again");
|
||||
|
||||
@@ -7,7 +7,7 @@ mod visual;
|
||||
|
||||
pub(crate) use visual::bind_keys;
|
||||
|
||||
use std::{ops::Range, path::Path, rc::Rc, sync::Arc};
|
||||
use std::{ops::Range, path::Path, rc::Rc, sync::Arc, time::Duration};
|
||||
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, EventEmitter, FocusHandle, Focusable, ListAlignment,
|
||||
@@ -26,6 +26,12 @@ use crate::{
|
||||
/// Texts this long parse at once; longer ones elsewhere.
|
||||
const AT_ONCE: usize = 128 << 10;
|
||||
|
||||
/// Longer texts are never read: source alone.
|
||||
pub const LARGEST_READ: usize = 2 << 20;
|
||||
|
||||
/// Typing waits this long for a pause before a read.
|
||||
const PAUSE: Duration = Duration::from_millis(300);
|
||||
|
||||
/// What the document tells its owner.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum DocumentEvent {
|
||||
@@ -121,7 +127,7 @@ impl MarkdownDocument {
|
||||
on_link: None,
|
||||
_subscriptions: subscriptions,
|
||||
};
|
||||
document.reparse(cx);
|
||||
document.reparse(true, cx);
|
||||
document
|
||||
}
|
||||
|
||||
@@ -148,6 +154,9 @@ impl MarkdownDocument {
|
||||
pub fn set_mode(&mut self, mode: MarkdownMode, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.commit(cx);
|
||||
log::info!("markdown document: {:?} to {mode:?}", self.mode);
|
||||
if !self.read_now(cx) {
|
||||
self.reparse(true, cx);
|
||||
}
|
||||
let listed = |mode| matches!(mode, MarkdownMode::Visual | MarkdownMode::Preview);
|
||||
// Visual and preview share one list and its scroll.
|
||||
let top = (!(listed(self.mode) && listed(mode))).then(|| self.top_line(cx));
|
||||
@@ -215,17 +224,26 @@ impl MarkdownDocument {
|
||||
self.replace_top();
|
||||
self.edits += 1;
|
||||
cx.emit(DocumentEvent::Changed);
|
||||
self.reparse(cx);
|
||||
// Typed source reads after a pause.
|
||||
let typed = matches!(self.mode, MarkdownMode::Source | MarkdownMode::Split);
|
||||
self.reparse(!typed, cx);
|
||||
}
|
||||
|
||||
/// Reads the text again, at once when short.
|
||||
fn reparse(&mut self, cx: &mut Context<Self>) {
|
||||
/// Reads the text: short ones now, else after a pause.
|
||||
fn reparse(&mut self, now: bool, cx: &mut Context<Self>) {
|
||||
let (text, edits) = (self.editor.read(cx).text().to_string(), self.edits);
|
||||
if text.len() <= AT_ONCE {
|
||||
if text.len() > LARGEST_READ {
|
||||
self._parsing = None;
|
||||
return log::info!("markdown document: {} bytes, too long to read", text.len());
|
||||
}
|
||||
if now && text.len() <= AT_ONCE {
|
||||
self._parsing = None;
|
||||
return self.adopt(parsed(&text), edits, cx);
|
||||
}
|
||||
self._parsing = Some(cx.spawn(async move |document, cx| {
|
||||
if !now {
|
||||
cx.background_executor().timer(PAUSE).await;
|
||||
}
|
||||
let read = cx.background_spawn(async move { parsed(&text) }).await;
|
||||
document
|
||||
.update(cx, |document, cx| document.adopt(read, edits, cx))
|
||||
|
||||
@@ -366,3 +366,33 @@ fn a_press_after_a_write_back_waits_for_the_long_read(cx: &mut TestAppContext) {
|
||||
}
|
||||
|
||||
mod place;
|
||||
|
||||
#[gpui::test]
|
||||
fn typed_source_reads_after_a_pause(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(TEXT, MarkdownMode::Source, cx);
|
||||
let editor = document.read_with(cx, |document, _| document.editor().clone());
|
||||
editor.update(cx, |editor, cx| editor.edit([(0..0, "x".to_string())], cx));
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
!document.read_with(cx, |document, cx| document.read_now(cx)),
|
||||
"no read per key"
|
||||
);
|
||||
cx.executor().advance_clock(super::PAUSE);
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
document.read_with(cx, |document, cx| document.read_now(cx)),
|
||||
"read after the pause"
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_text_past_the_largest_read_is_never_read(cx: &mut TestAppContext) {
|
||||
let long = "a\n".repeat(super::LARGEST_READ / 2 + 1);
|
||||
let (document, cx) = open(&long, MarkdownMode::Source, cx);
|
||||
cx.executor().advance_clock(super::PAUSE);
|
||||
cx.run_until_parked();
|
||||
let (read, outline) = document.read_with(cx, |document, cx| {
|
||||
(document.read_now(cx), document.outline(cx).len())
|
||||
});
|
||||
assert!(!read && outline == 0);
|
||||
}
|
||||
@@ -34,7 +34,7 @@ pub use blocks::{Align, BlockData, BlockEditor, BlockEvent, BlockKind, Media};
|
||||
pub use contents::outline;
|
||||
pub use counts::{ReadingTime, WordCount};
|
||||
pub use diagram::Diagram;
|
||||
pub use document::{DocumentEvent, DocumentHeading, MarkdownDocument};
|
||||
pub use document::{DocumentEvent, DocumentHeading, LARGEST_READ, MarkdownDocument};
|
||||
pub use epub::{Chapter, EpubReader};
|
||||
pub use format::{Format, format};
|
||||
pub(crate) use history::Change;
|
||||
|
||||
Reference in new issue
Block a user