Skip to content

Commit

Permalink
inlines: first fixes to emphasis sourcepos.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk authored and charlottia committed Feb 28, 2025
1 parent 82cb6e6 commit 642ba7b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
44 changes: 27 additions & 17 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ pub struct Delimiter<'a: 'd, 'd> {
next: Cell<Option<&'d Delimiter<'a, 'd>>>,
}

impl<'a: 'd, 'd> std::fmt::Debug for Delimiter<'a, 'd> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"[pos {}, len {}, delim_char {:?}, open? {} close? {} -- {}]",
self.position,
self.length,
self.delim_char,
self.can_open,
self.can_close,
self.inl.data.borrow().sourcepos
)
}
}

struct Bracket<'a> {
inl_text: &'a AstNode<'a>,
position: usize,
Expand Down Expand Up @@ -1136,22 +1151,14 @@ impl<'a, 'r, 'o, 'd, 'i, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {
self.pos,
self.pos,
);
{
// if we have `___` or `***` then we need to adjust the sourcepos colums by 1
let triple_adjustment = if opener_num_chars > 0 && use_delims == 2 {
1
} else {
0
};

emph.data.borrow_mut().sourcepos = (
opener.inl.data.borrow().sourcepos.start.line,
opener.inl.data.borrow().sourcepos.start.column + triple_adjustment,
closer.inl.data.borrow().sourcepos.end.line,
closer.inl.data.borrow().sourcepos.end.column - triple_adjustment,
)
.into();
}
emph.data.borrow_mut().sourcepos = (
opener.inl.data.borrow().sourcepos.start.line,
opener.inl.data.borrow().sourcepos.start.column + opener_num_chars,
closer.inl.data.borrow().sourcepos.end.line,
closer.inl.data.borrow().sourcepos.end.column - closer_num_chars,
)
.into();

// Drop all the interior AST nodes into the emphasis node
// and then insert the emphasis node
Expand All @@ -1167,18 +1174,21 @@ impl<'a, 'r, 'o, 'd, 'i, 'c> Subject<'a, 'r, 'o, 'd, 'i, 'c> {
}
opener.inl.insert_after(emph);

// Drop the delimiters and return the next closer to process

// Drop completely "used up" delimiters, adjust sourcepos of those not,
// and return the next closest one for processing.
if opener_num_chars == 0 {
opener.inl.detach();
self.remove_delimiter(opener);
} else {
opener.inl.data.borrow_mut().sourcepos.end.column -= use_delims;
}

if closer_num_chars == 0 {
closer.inl.detach();
self.remove_delimiter(closer);
closer.next.get()
} else {
closer.inl.data.borrow_mut().sourcepos.start.column += use_delims;
Some(closer)
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/tests/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,41 @@ fn echaw4() {
])
);
}

#[test]
fn echaw5() {
assert_ast_match!(
[],
// [extension.autolink],
"_#[email protected]",
(document (1:1-1:9) [
(paragraph (1:1-1:9) [
(emph (1:1-1:3) [
(text (1:2-1:2) "#")
])
// (link (1:1-1:14) "mailto:-@_.e" [
// (text (1:1-1:14) "-@_.e")
// ])
(text (1:4-1:9) "[email protected]")
])
])
);
}

#[test]
fn echaw6() {
assert_ast_match!(
[extension.autolink],
"_#[email protected]",
(document (1:1-1:9) [
(paragraph (1:1-1:9) [
(emph (1:1-1:3) [
(text (1:2-1:2) "#")
])
(link (1:4-1:9) "mailto:[email protected]" [
(text (1:4-1:9) "[email protected]")
])
])
])
);
}
4 changes: 3 additions & 1 deletion src/tests/sourcepos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@ const SPOILERED_TEXT: TestCase = (
after"#,
);

// NOTE: I've adjusted this from its original asserted sourcepos (2:1-2:8) while
// fixing emphasis sourcepos. I am not even sure what it is, really.
const ESCAPED_TAG: TestCase = (
&[sourcepos!((2:1-2:8))],
&[sourcepos!((2:2-2:8))],
r#"before
||hello|
after"#,
Expand Down

0 comments on commit 642ba7b

Please sign in to comment.