documents: alerts read their marker from parsed text; GFM's filtered tags show as text; block elements close what opened inside them; a TOC press relocates its heading after a write-back or a pending read and acts on press
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2eec25140d
commit
98f19a8aac
7 files changed
+211
-69
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
|
||||
|
||||
use super::{MarkdownDocument, Placement};
|
||||
use super::{DocumentHeading, MarkdownDocument, Placement};
|
||||
use crate::documents::{MarkdownMode, modes::joint};
|
||||
|
||||
/// Line and column, from zero, of byte `at` in `text`.
|
||||
@@ -189,13 +189,27 @@ impl MarkdownDocument {
|
||||
.saturating_sub(1)
|
||||
}
|
||||
|
||||
/// Reveals the line holding byte `at` of the text.
|
||||
/// Reveals the pressed heading, found again in the text.
|
||||
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let editor = self.editor.read(cx);
|
||||
if at > editor.text().len() {
|
||||
return log::error!("markdown document: a heading past the text");
|
||||
let headings = &self.parsed.headings;
|
||||
let Some(ix) = headings.iter().position(|heading| heading.at == at) else {
|
||||
return log::warn!("markdown document: a heading read away");
|
||||
};
|
||||
let pressed = headings[ix].clone();
|
||||
let same = |heading: &&DocumentHeading| {
|
||||
(heading.depth, &heading.title) == (pressed.depth, &pressed.title)
|
||||
};
|
||||
let nth = headings[..ix].iter().filter(same).count();
|
||||
if self.commit(cx).is_some() || !self.read_now(cx) {
|
||||
self.reparse(true, cx);
|
||||
}
|
||||
let line = editor.buffer.line_of(at);
|
||||
if !self.read_now(cx) {
|
||||
return log::warn!("markdown document: a heading pressed while the text is read again");
|
||||
}
|
||||
let Some(found) = self.parsed.headings.iter().filter(same).nth(nth) else {
|
||||
return log::warn!("markdown document: the pressed heading changed as one wrote");
|
||||
};
|
||||
let line = self.editor.read(cx).buffer.line_of(found.at);
|
||||
self.reveal(line, window, cx);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
|
||||
use super::{MarkdownMode, open};
|
||||
use super::{MarkdownMode, frames, open, type_in};
|
||||
|
||||
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
|
||||
|
||||
@@ -34,3 +34,35 @@ fn visual_reveals_on_a_secondary_press_and_edits_on_a_plain_one(cx: &mut TestApp
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert!(document.read_with(cx, |document, _| document.open.is_some()));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_after_an_earlier_block_grew_finds_the_heading(cx: &mut TestAppContext) {
|
||||
let text = "before\n\n[toc]\n\n# A\n\ntext\n\n## B\n";
|
||||
let (document, cx) = open(text, MarkdownMode::Visual, cx);
|
||||
type_in(&document, Some(0), Some("before\nextra\nextra"), cx);
|
||||
frames(cx);
|
||||
press_entry(1, Modifiers::secondary_key(), cx);
|
||||
frames(cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(10, 0)
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_while_the_text_waits_to_be_read_finds_the_heading(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(LISTED, MarkdownMode::Split, cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document.editor.update(cx, |editor, cx| {
|
||||
editor.edit([(0..0, "inserted\n".repeat(20))], cx);
|
||||
});
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
|
||||
press_entry(1, Modifiers::none(), cx);
|
||||
frames(cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(26, 0)
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user