Skip to content

Commit

Permalink
Try using a line offset vector
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Aug 13, 2024
1 parent 9f4d391 commit e0b0ddd
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ fn main() -> Result<(), Box<dyn Error>> {
.ignore_setext(cli.ignore_setext)
.ignore_empty_links(cli.ignore_empty_links)
.gfm_quirks(cli.gfm_quirks || cli.gfm)
// .experimental_inline_sourcepos(cli.sourcepos)
.build()?;

let options = Options {
Expand Down
2 changes: 2 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ pub struct Ast {
pub(crate) open: bool,
pub(crate) last_line_blank: bool,
pub(crate) table_visited: bool,
pub(crate) line_offsets: Vec<usize>,
}

/// Represents the position in the source Markdown this node was rendered from.
Expand Down Expand Up @@ -609,6 +610,7 @@ impl Ast {
open: true,
last_line_blank: false,
table_visited: false,
line_offsets: Vec::new(),
}
}
}
Expand Down
33 changes: 30 additions & 3 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Subject<'a: 'd, 'r, 'o, 'c, 'd, 'i> {
pub pos: usize,
block_offset: usize,
column_offset: isize,
line_offset: usize,
flags: Flags,
pub refmap: &'r mut RefMap,
delimiter_arena: &'d Arena<Delimiter<'a, 'd>>,
Expand Down Expand Up @@ -128,6 +129,7 @@ impl<'a, 'r, 'o, 'c, 'd, 'i> Subject<'a, 'r, 'o, 'c, 'd, 'i> {
pos: 0,
block_offset,
column_offset: 0,
line_offset: 0,
flags: Flags::default(),
refmap,
delimiter_arena,
Expand Down Expand Up @@ -182,6 +184,11 @@ impl<'a, 'r, 'o, 'c, 'd, 'i> Subject<'a, 'r, 'o, 'c, 'd, 'i> {
None => return false,
Some(ch) => *ch as char,
};

let node_ast = node.data.borrow();
let adjusted_line = self.line - node_ast.sourcepos.start.line;
self.line_offset = *node_ast.line_offsets.get(adjusted_line).unwrap_or(&0);

let new_inl: Option<&'a AstNode<'a>> = match c {
'\0' => return false,
'\r' | '\n' => Some(self.handle_newline()),
Expand Down Expand Up @@ -627,6 +634,7 @@ impl<'a, 'r, 'o, 'c, 'd, 'i> Subject<'a, 'r, 'o, 'c, 'd, 'i> {
};
self.line += 1;
self.column_offset = -(self.pos as isize);
// self.block_offset = 0;
self.skip_spaces();
inl
}
Expand Down Expand Up @@ -1846,9 +1854,26 @@ impl<'a, 'r, 'o, 'c, 'd, 'i> Subject<'a, 'r, 'o, 'c, 'd, 'i> {
start_column: usize,
end_column: usize,
) -> &'a AstNode<'a> {
let start_column =
start_column as isize + 1 + self.column_offset + self.block_offset as isize;
let end_column = end_column as isize + 1 + self.column_offset + self.block_offset as isize;
// let start_column =
// start_column as isize + 1 + self.column_offset + self.block_offset as isize;
// let end_column = end_column as isize + 1 + self.column_offset + self.block_offset as isize;

// let start_column =
// start_column as isize + 1 + self.column_offset;
// let end_column = end_column as isize + 1 + self.column_offset;
// let start_column = start_column as isize + 1;
// let end_column = end_column as isize + 1;

let mut start_column = start_column as isize + 1;
let mut end_column = end_column as isize + 1;

if self.column_offset >= 0 {
start_column = start_column + self.column_offset + self.block_offset as isize;
end_column = end_column + self.column_offset + self.block_offset as isize;
} else {
start_column = start_column + self.column_offset + self.line_offset as isize;
end_column = end_column + self.column_offset + self.line_offset as isize;
}

let ast = Ast {
value,
Expand All @@ -1864,6 +1889,7 @@ impl<'a, 'r, 'o, 'c, 'd, 'i> Subject<'a, 'r, 'o, 'c, 'd, 'i> {
open: false,
last_line_blank: false,
table_visited: false,
line_offsets: Vec::new(),
};
self.arena.alloc(Node::new(RefCell::new(ast)))
}
Expand Down Expand Up @@ -1972,6 +1998,7 @@ pub fn make_inline<'a>(
open: false,
last_line_blank: false,
table_visited: false,
line_offsets: Vec::new(),
};
arena.alloc(Node::new(RefCell::new(ast)))
}
Expand Down
6 changes: 6 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn parse_document<'a>(
open: true,
last_line_blank: false,
table_visited: false,
line_offsets: Vec::new(),
})));
let mut parser = Parser::new(arena, root, options);
let mut linebuf = Vec::with_capacity(buffer.len());
Expand Down Expand Up @@ -1998,6 +1999,11 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
}
}
if self.offset < line.len() {
// TODO: add first_non_space or first_non_space_column or offset or column into
// a vector that tracks the offset for each added line.
// ast.line_offsets.push(self.first_nonspace);
ast.line_offsets.push(self.offset);

ast.content
.push_str(str::from_utf8(&line[self.offset..]).unwrap());
}
Expand Down
65 changes: 65 additions & 0 deletions src/tests/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,68 @@ fn link_sourcepos_truffle_bergamot() {
])
);
}

#[test]
fn link_sourcepos_inline_1() {
assert_ast_match!(
[],
" A\n"
" B\n",
(document (1:1-2:4) [
(paragraph (1:3-2:4) [
(text (1:3-1:3) "A")
(softbreak (1:4-1:4))
(text (2:4-2:4) "B")
])
])
);
}

#[test]
fn link_sourcepos_inline_2() {
assert_ast_match!(
[],
"- A\n"
"B\n",
(document (1:1-2:1) [
(list (1:1-2:1) [
(item (1:1-2:1) [
(paragraph (1:3-2:1) [
(text (1:3-1:3) "A")
(softbreak (1:4-1:4))
(text (2:1-2:1) "B")
])
])
])
])
);
}

#[test]
fn link_sourcepos_inline_3() {
assert_ast_match!(
[],
"- A\n"
" B\n"
"- C\n"
" D",
(document (1:1-4:2) [
(list (1:1-4:2) [
(item (1:1-2:4) [
(paragraph (1:3-2:4) [
(text (1:3-1:3) "A")
(softbreak (1:4-1:4))
(text (2:4-2:4) "B")
])
])
(item (3:1-4:2) [
(paragraph (3:4-4:2) [
(text (3:4-3:4) "C")
(softbreak (3:5-3:5))
(text (4:2-4:2) "D")
])
])
])
])
);
}

0 comments on commit e0b0ddd

Please sign in to comment.