Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(typst): fix paragraph break spans (#2080)

Steve Kurch62629d856f7e
diff --git a/harper-core/src/span.rs b/harper-core/src/span.rs
index e95e815ba..ad558b459 100644
--- a/harper-core/src/span.rs
+++ b/harper-core/src/span.rs
@@ -29,12 +29,8 @@ pub struct Span<T> {
}
impl<T> Span<T> {
- /// An empty [`Span`].
- pub const EMPTY: Self = Self {
- start: 0,
- end: 0,
- span_type: PhantomData,
- };
+ /// A [`Span`] with a start and end index of 0.
+ pub const ZERO: Self = Self::empty(0);
/// Creates a new [`Span`] with the provided start and end indices.
///
@@ -61,6 +57,15 @@ impl<T> Span<T> {
}
}
+ /// Creates a new empty [`Span`] with the provided position.
+ pub const fn empty(pos: usize) -> Self {
+ Self {
+ start: pos,
+ end: pos,
+ span_type: PhantomData,
+ }
+ }
+
/// The length of the [`Span`].
pub fn len(&self) -> usize {
self.end - self.start
@@ -179,7 +184,7 @@ impl Span<Token> {
/// this span is required.
pub fn to_char_span(&self, source_document_tokens: &[Token]) -> Span<char> {
if self.is_empty() {
- Span::EMPTY
+ Span::ZERO
} else {
let target_tokens = &source_document_tokens[self.start..self.end];
Span::new(
@@ -263,7 +268,7 @@ mod tests {
let doc = Document::new_plain_english_curated("Hello world!");
// Empty span.
- let token_span = Span::EMPTY;
+ let token_span = Span::ZERO;
let converted = token_span.to_char_span(doc.get_tokens());
assert!(converted.is_empty());
diff --git a/harper-typst/src/typst_translator.rs b/harper-typst/src/typst_translator.rs
index 8a1409e3c..cc6fc0cc8 100644
--- a/harper-typst/src/typst_translator.rs
+++ b/harper-typst/src/typst_translator.rs
@@ -152,15 +152,17 @@ impl<'a> TypstTranslator<'a> {
};
}
- fn parbreak() -> Option<Vec<Token>> {
+ fn parbreak(pos: usize) -> Option<Vec<Token>> {
Some(vec![Token {
- span: harper_core::Span::EMPTY,
+ span: harper_core::Span::empty(pos),
kind: TokenKind::ParagraphBreak,
}])
}
fn isolate(inner: Option<Vec<Token>>) -> Option<Vec<Token>> {
- merge![parbreak(), inner, parbreak()]
+ let start = inner.as_ref()?.first().map_or(0, |token| token.span.start);
+ let end = inner.as_ref()?.last().map_or(0, |token| token.span.end);
+ merge![parbreak(start), inner, parbreak(end)]
}
// Recurse on each element of an iterator