MarkdownDocument: typed source reads after a pause off the main thread; texts past 2 MiB are never read
CI / platforms (windows-latest) (push) Failing after 9s
CI / site (push) Successful in 25s
CI / crate (push) Failing after 11m23s
CI / platforms (ubuntu-24.04) (push) Canceled after 0s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-12 02:33:49 -04:00
1 parent abac8b41b3
commit 69b11d7852
5 files changed
+57 -9

No files matched your search

+24 -6
View File
@@ -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))