documents: a TOC press follows the edit since its read; filtered tags keep shortcodes; tag names end as HTML5 ends them; block starts end p, li, dd and dt; owed space shifts only what opened after it
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
98f19a8aac
commit
a744316274
7 files changed
+242
-42
No files matched your search
@@ -12,8 +12,27 @@ use crate::{
|
||||
forms::{InputEvent, TextInput},
|
||||
};
|
||||
|
||||
/// The one edit from `old` to `new`, by shared ends.
|
||||
pub(super) fn between(old: &str, new: &str) -> Option<(Range<usize>, usize)> {
|
||||
let head: usize = old
|
||||
.chars()
|
||||
.zip(new.chars())
|
||||
.take_while(|(a, b)| a == b)
|
||||
.map(|(ch, _)| ch.len_utf8())
|
||||
.sum();
|
||||
let room = old.len().min(new.len()) - head;
|
||||
let mut tail = 0;
|
||||
for (a, b) in old.chars().rev().zip(new.chars().rev()) {
|
||||
if a != b || tail + a.len_utf8() > room {
|
||||
break;
|
||||
}
|
||||
tail += a.len_utf8();
|
||||
}
|
||||
(old != new).then(|| (head..old.len() - tail, new.len() - tail - head))
|
||||
}
|
||||
|
||||
/// Where byte `at` lies after `edit`, if it survives.
|
||||
fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
|
||||
pub(super) fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
|
||||
match edit {
|
||||
None => Some(at),
|
||||
Some((replaced, _)) if at < replaced.start => Some(at),
|
||||
@@ -181,4 +200,15 @@ mod tests {
|
||||
assert_eq!(super::moved(3, &edit), None, "inside is gone");
|
||||
assert_eq!(super::moved(7, &None), Some(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_texts_part_at_their_shared_ends() {
|
||||
assert_eq!(super::between("abc", "abc"), None);
|
||||
assert_eq!(super::between("before\nB", "## B\nB"), Some((0..6, 4)));
|
||||
assert_eq!(super::between("ab", "aXb"), Some((1..1, 1)));
|
||||
assert_eq!(super::between("aa", "aaa"), Some((2..2, 1)));
|
||||
assert_eq!(super::between("aXYb", "ab"), Some((1..3, 0)));
|
||||
assert_eq!(super::between("é😄", "é"), Some((2..6, 0)));
|
||||
assert_eq!(super::between("x😄", "y😄"), Some((0..1, 1)));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
|
||||
|
||||
use super::{DocumentHeading, MarkdownDocument, Placement};
|
||||
use super::{
|
||||
MarkdownDocument, Placement,
|
||||
edit::{between, moved},
|
||||
};
|
||||
use crate::documents::{MarkdownMode, modes::joint};
|
||||
|
||||
/// Line and column, from zero, of byte `at` in `text`.
|
||||
@@ -189,27 +194,25 @@ impl MarkdownDocument {
|
||||
.saturating_sub(1)
|
||||
}
|
||||
|
||||
/// Reveals the pressed heading, found again in the text.
|
||||
/// Reveals the pressed heading, moved by the edit since.
|
||||
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let headings = &self.parsed.headings;
|
||||
let Some(ix) = headings.iter().position(|heading| heading.at == at) else {
|
||||
if !self.parsed.headings.iter().any(|heading| heading.at == at) {
|
||||
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();
|
||||
}
|
||||
// The heading list drew from this read.
|
||||
let drawn = Arc::clone(&self.parsed);
|
||||
if self.commit(cx).is_some() || !self.read_now(cx) {
|
||||
self.reparse(true, cx);
|
||||
}
|
||||
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 {
|
||||
let edit = between(&drawn.text, self.editor.read(cx).text());
|
||||
let headings = &self.parsed.headings;
|
||||
let Some(found) = moved(at, &edit).filter(|at| headings.iter().any(|h| h.at == *at)) else {
|
||||
return log::warn!("markdown document: the pressed heading changed as one wrote");
|
||||
};
|
||||
let line = self.editor.read(cx).buffer.line_of(found.at);
|
||||
let line = self.editor.read(cx).buffer.line_of(found);
|
||||
self.reveal(line, window, cx);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
use gpui::{Entity, Modifiers, MouseButton, TestAppContext, VisualTestContext, point, px};
|
||||
|
||||
use super::{MarkdownMode, frames, open, type_in};
|
||||
use super::{MarkdownDocument, MarkdownMode, frames, open, type_in};
|
||||
|
||||
const LISTED: &str = "[toc]\n\n# A\n\ntext\n\n## B\n";
|
||||
|
||||
@@ -11,10 +11,76 @@ fn press_entry(ix: usize, modifiers: Modifiers, cx: &mut VisualTestContext) {
|
||||
.expect("the entry is drawn");
|
||||
let at = point(bounds.left() + px(2.0), bounds.top() + px(2.0));
|
||||
cx.simulate_mouse_down(at, MouseButton::Left, modifiers);
|
||||
frames(cx);
|
||||
cx.simulate_mouse_up(at, MouseButton::Left, modifiers);
|
||||
cx.run_until_parked();
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_heading_written_above_with_the_same_title_is_not_the_pressed_one(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("## B"), cx);
|
||||
frames(cx);
|
||||
press_entry(1, Modifiers::secondary_key(), cx);
|
||||
let (caret, now) =
|
||||
document.read_with(cx, |document, cx| (document.caret(cx), document.text(cx)));
|
||||
assert_eq!(now, "## B\n\n[toc]\n\n# A\n\ntext\n\n## B\n");
|
||||
assert_eq!(caret, (8, 0));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_twin_typed_above_while_the_text_waits_is_not_the_pressed_one(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, "## B\n\n".to_string())], cx);
|
||||
});
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
|
||||
press_entry(1, Modifiers::none(), cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(8, 0)
|
||||
);
|
||||
}
|
||||
|
||||
const TWINS: &str = "[toc]\n\n## B\n\ntext\n\n## B\n";
|
||||
|
||||
/// Deletes the first twin while the text waits.
|
||||
fn delete_first_twin(document: &Entity<MarkdownDocument>, cx: &mut VisualTestContext) {
|
||||
document.update(cx, |document, cx| {
|
||||
document.editor.update(cx, |editor, cx| {
|
||||
editor.edit([(7..13, String::new())], cx);
|
||||
});
|
||||
});
|
||||
cx.run_until_parked();
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_deleted_twin_leaves_the_other_found(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(TWINS, MarkdownMode::Split, cx);
|
||||
delete_first_twin(&document, cx);
|
||||
press_entry(1, Modifiers::none(), cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
(4, 0)
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_on_a_deleted_twin_moves_nothing(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(TWINS, MarkdownMode::Split, cx);
|
||||
delete_first_twin(&document, cx);
|
||||
let before = document.read_with(cx, |document, cx| document.caret(cx));
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert_eq!(
|
||||
document.read_with(cx, |document, cx| document.caret(cx)),
|
||||
before
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_press_in_preview_reveals_the_heading(cx: &mut TestAppContext) {
|
||||
let (document, cx) = open(LISTED, MarkdownMode::Preview, cx);
|
||||
|
||||
Reference in new issue
Block a user