documents: shortcodes read per DOM text node as GitHub reads them; HTML5's p closers end a p; a TOC press follows editor anchors
- The first `:name:`-shaped run in a text node decides for the whole node: blank text or a space before it turns every shortcode, else none. Filtered tags are text and do not split the node. Probed on GitHub's markdown API. - `fieldset`, `menu` and the rest of HTML5's list end an open `p`. - `CodeEditor` carries owner anchors through each edit; the document anchors every heading it reads, and a TOC press goes where its anchor went. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a744316274
commit
8e5dad8177
12 files changed
+396
-138
No files matched your search
@@ -12,27 +12,8 @@ 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.
|
||||
pub(super) fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
|
||||
fn moved(at: usize, edit: &Option<(Range<usize>, usize)>) -> Option<usize> {
|
||||
match edit {
|
||||
None => Some(at),
|
||||
Some((replaced, _)) if at < replaced.start => Some(at),
|
||||
@@ -200,15 +181,4 @@ 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)));
|
||||
}
|
||||
}
|
||||
@@ -316,6 +316,9 @@ impl MarkdownDocument {
|
||||
let old = self.parsed.blocks.len();
|
||||
self.list
|
||||
.splice(head..old - tail, read.blocks.len() - head - tail);
|
||||
let headings: Vec<usize> = read.headings.iter().map(|heading| heading.at).collect();
|
||||
self.editor
|
||||
.update(cx, |editor, _| editor.set_anchors(headings));
|
||||
self.parsed = Arc::new(read);
|
||||
cx.emit(DocumentEvent::Parsed);
|
||||
cx.notify();
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Context, Focusable as _, ListOffset, ScrollStrategy, Window, px};
|
||||
|
||||
use super::{
|
||||
MarkdownDocument, Placement,
|
||||
edit::{between, moved},
|
||||
};
|
||||
use super::{MarkdownDocument, Placement};
|
||||
use crate::documents::{MarkdownMode, modes::joint};
|
||||
|
||||
/// Line and column, from zero, of byte `at` in `text`.
|
||||
@@ -194,24 +189,31 @@ impl MarkdownDocument {
|
||||
.saturating_sub(1)
|
||||
}
|
||||
|
||||
/// Reveals the pressed heading, moved by the edit since.
|
||||
/// Reveals the pressed heading where the edits since carried it.
|
||||
pub(super) fn reveal_at(&mut self, at: usize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if !self.parsed.headings.iter().any(|heading| heading.at == at) {
|
||||
let headings = &self.parsed.headings;
|
||||
let Some(index) = headings.iter().position(|heading| heading.at == at) else {
|
||||
return log::warn!("markdown document: a heading read away");
|
||||
}
|
||||
// The heading list drew from this read.
|
||||
let drawn = Arc::clone(&self.parsed);
|
||||
if self.commit(cx).is_some() || !self.read_now(cx) {
|
||||
};
|
||||
self.commit(cx);
|
||||
let anchors = self.editor.read(cx).anchors();
|
||||
let Some(found) = *anchors.get(index).expect("an anchor per heading read") else {
|
||||
return log::warn!("markdown document: the pressed heading changed as one wrote");
|
||||
};
|
||||
if !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 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");
|
||||
};
|
||||
if !self
|
||||
.parsed
|
||||
.headings
|
||||
.iter()
|
||||
.any(|heading| heading.at == found)
|
||||
{
|
||||
return log::warn!("markdown document: the pressed heading reads as no heading now");
|
||||
}
|
||||
let line = self.editor.read(cx).buffer.line_of(found);
|
||||
self.reveal(line, window, cx);
|
||||
}
|
||||
|
||||
@@ -132,3 +132,59 @@ fn a_press_while_the_text_waits_to_be_read_finds_the_heading(cx: &mut TestAppCon
|
||||
(26, 0)
|
||||
);
|
||||
}
|
||||
|
||||
/// Makes `edits` while the text waits, caret at the top.
|
||||
fn edit_waiting<'a>(
|
||||
text: &str,
|
||||
edits: Vec<(std::ops::Range<usize>, String)>,
|
||||
cx: &'a mut TestAppContext,
|
||||
) -> (Entity<MarkdownDocument>, &'a mut VisualTestContext) {
|
||||
let (document, cx) = open(text, MarkdownMode::Split, cx);
|
||||
document.update(cx, |document, cx| {
|
||||
document
|
||||
.editor
|
||||
.update(cx, |editor, cx| editor.edit(edits, cx));
|
||||
document.set_caret(0, 0, cx);
|
||||
});
|
||||
frames(cx);
|
||||
assert!(!document.read_with(cx, |document, cx| document.read_now(cx)));
|
||||
(document, cx)
|
||||
}
|
||||
|
||||
fn caret(document: &Entity<MarkdownDocument>, cx: &mut VisualTestContext) -> (usize, usize) {
|
||||
document.read_with(cx, |document, cx| document.caret(cx))
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_twin_written_right_at_the_heading_goes_before_it(cx: &mut TestAppContext) {
|
||||
let edits = vec![(7..7, "## B\n\n".to_string())];
|
||||
let (document, cx) = edit_waiting("[toc]\n\n## B\n\ntext\n", edits, cx);
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert_eq!(caret(&document, cx), (4, 0));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn adjacent_twins_keep_who_was_deleted(cx: &mut TestAppContext) {
|
||||
let twins = "[toc]\n\n## B\n\n## B\n\ntext\n";
|
||||
let (document, cx) = edit_waiting(twins, vec![(7..13, String::new())], cx);
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert_eq!(
|
||||
caret(&document, cx),
|
||||
(0, 0),
|
||||
"the deleted one moves nothing"
|
||||
);
|
||||
press_entry(1, Modifiers::none(), cx);
|
||||
assert_eq!(caret(&document, cx), (2, 0), "the kept one moved up");
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_heading_between_two_edits_stays_found(cx: &mut TestAppContext) {
|
||||
let text = "before\n\n[toc]\n\n# A\n\nafter\n";
|
||||
let edits = vec![
|
||||
(0..0, "start\n".to_string()),
|
||||
(text.len()..text.len(), "end\n".to_string()),
|
||||
];
|
||||
let (document, cx) = edit_waiting(text, edits, cx);
|
||||
press_entry(0, Modifiers::none(), cx);
|
||||
assert_eq!(caret(&document, cx), (5, 0));
|
||||
}
|
||||
Reference in new issue
Block a user