Skip to content

Commit

Permalink
fix: only condense adjacent spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
grantlemons committed Dec 29, 2024
1 parent 4457bff commit 1d0b972
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
16 changes: 11 additions & 5 deletions harper-core/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,17 @@ impl Document {
}

let child_tok = &copy[cursor];
if let TokenKind::Space(n) = child_tok.kind {
*start_count += n;
start_tok.span.end = child_tok.span.end;
remove_these.push_back(cursor);
cursor += 1;

// Only condense adjacent spans
if start_tok.span.end == child_tok.span.start {
if let TokenKind::Space(n) = child_tok.kind {
*start_count += n;
start_tok.span.end = child_tok.span.end;
remove_these.push_back(cursor);
cursor += 1;
} else {
break;
}
} else {
break;
};
Expand Down
28 changes: 26 additions & 2 deletions harper-core/src/parsers/typst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ impl<'a> ParseHelper<'a> {
}
Expr::Str(text) => {
let offset = offset.push_to_span(text.span()).char + 1;
let text = text.to_untyped().text().to_string();
let string = text.to_untyped().text().to_string();

Some(
self.parser
.parse_str(&text[1..text.len() - 1])
.parse_str(&string[1..string.len() - 1])
.into_iter()
.map(|mut t| {
t.span.push_by(offset);
Expand Down Expand Up @@ -562,6 +563,29 @@ mod tests {
))
}

#[test]
fn non_adjacent_spaces_not_condensed() {
let source = r#"#authors_slice.join(", ", last: ", and ") "#;

let token_kinds = Typst.parse_str(source).iter().map(|t| t.kind).collect_vec();
dbg!(&token_kinds);

assert!(matches!(
&token_kinds.as_slice(),
&[
TokenKind::Unlintable, // authors_slice.join
TokenKind::Punctuation(Punctuation::Comma),
TokenKind::Space(1),
TokenKind::Unlintable, // Ident
TokenKind::Punctuation(Punctuation::Comma),
TokenKind::Space(1),
TokenKind::Word(_), // and
TokenKind::Space(1),
TokenKind::Space(1)
]
))
}

#[test]
fn header_parsing() {
let source = r"= Header
Expand Down

0 comments on commit 1d0b972

Please sign in to comment.