Skip to content

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

fix(harper-typst): condense contractions (#2558)

Elijah Potterbeed1538ac3d
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 174366c7a..c3986efec 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -999,6 +999,11 @@ mod tests {
assert_condensed_contractions("There's", 1);
}
+ #[test]
+ fn simple_contraction4() {
+ assert_condensed_contractions("doesn't", 1);
+ }
+
#[test]
fn medium_contraction() {
assert_condensed_contractions("isn't wasn't", 3);
diff --git a/harper-typst/src/lib.rs b/harper-typst/src/lib.rs
index 02040f752..0219041d2 100644
--- a/harper-typst/src/lib.rs
+++ b/harper-typst/src/lib.rs
@@ -1,7 +1,6 @@
mod offset_cursor;
mod typst_translator;
-use offset_cursor::OffsetCursor;
use typst_translator::TypstTranslator;
use harper_core::{Token, parsers::Parser};
@@ -28,11 +27,7 @@ impl Parser for Typst {
let mut buf = Vec::new();
let exprs = typst_tree.exprs().collect_vec();
let exprs = convert_parbreaks(&mut buf, &exprs);
- exprs
- .into_iter()
- .filter_map(|ex| parse_helper.parse_expr(ex, OffsetCursor::new(&typst_document)))
- .flatten()
- .collect_vec()
+ parse_helper.parse_exprs(&exprs)
}
}
diff --git a/harper-typst/src/typst_translator.rs b/harper-typst/src/typst_translator.rs
index cc6fc0cc8..a35f193e8 100644
--- a/harper-typst/src/typst_translator.rs
+++ b/harper-typst/src/typst_translator.rs
@@ -1,4 +1,4 @@
-use crate::OffsetCursor;
+use crate::offset_cursor::OffsetCursor;
use harper_core::{
Punctuation, Token, TokenKind,
parsers::{PlainEnglish, StrParser},
@@ -50,6 +50,64 @@ impl<'a> TypstTranslator<'a> {
Self { doc }
}
+ pub fn parse_exprs(self, exprs: &[Expr]) -> Vec<Token> {
+ let base_offset = OffsetCursor::new(self.doc);
+ let mut tokens = Vec::new();
+ let mut index = 0;
+
+ while index < exprs.len() {
+ // Treat Text + apostrophe + Text as a single contraction token stream.
+ if let Some((mut parsed, consumed)) = self.parse_contraction(exprs, index) {
+ tokens.append(&mut parsed);
+ index += consumed;
+ continue;
+ }
+
+ if let Some(mut parsed) = self.parse_expr(exprs[index], base_offset) {
+ tokens.append(&mut parsed);
+ }
+ index += 1;
+ }
+
+ tokens
+ }
+
+ fn parse_contraction(self, exprs: &[Expr], index: usize) -> Option<(Vec<Token>, usize)> {
+ let exprs = exprs.get(index..index + 3)?;
+ let [expr1, expr2, expr3] = exprs else {
+ return None;
+ };
+
+ let (Expr::Text(left), Expr::SmartQuote(quote), Expr::Text(right)) =
+ (*expr1, *expr2, *expr3)
+ else {
+ return None;
+ };
+
+ if quote.double() {
+ return None;
+ }
+
+ let left_char = left.get().chars().last()?;
+ let right_char = right.get().chars().next()?;
+ if !left_char.is_alphabetic() || !right_char.is_alphabetic() {
+ return None;
+ }
+
+ let left_range = self.doc.range(left.span())?;
+ let quote_range = self.doc.range(quote.span())?;
+ let right_range = self.doc.range(right.span())?;
+ if left_range.end != quote_range.start || quote_range.end != right_range.start {
+ return None;
+ }
+
+ let joined = self.doc.get(left_range.start..right_range.end)?;
+ let offset = OffsetCursor::new(self.doc).push_to_span(left.span())?;
+ let parsed = self.parse_english(joined, offset)?;
+
+ Some((parsed, 3))
+ }
+
/// Use the [`PlainEnglish`] parser to parse plain text from a Typst expression.
fn parse_english(self, str: impl Into<String>, offset: OffsetCursor) -> Option<Vec<Token>> {
Some(
diff --git a/harper-typst/tests/run_tests.rs b/harper-typst/tests/run_tests.rs
index ff766a141..8a3abbb1d 100644
--- a/harper-typst/tests/run_tests.rs
+++ b/harper-typst/tests/run_tests.rs
@@ -41,3 +41,4 @@ create_test!(complex_document_with_spelling_mistakes.typ, 4);
// create_test!(issue_399.typ, 3);
create_test!(function_with_ignorable_args.typ, 9);
create_test!(issue_1926.typ, 1);
+create_test!(contractions.typ, 0);
diff --git a/harper-typst/tests/test_sources/contractions.typ b/harper-typst/tests/test_sources/contractions.typ
new file mode 100644
index 000000000..24a97227b
--- /dev/null
+++ b/harper-typst/tests/test_sources/contractions.typ
@@ -0,0 +1 @@
+Typst used to have problems with contractions. Doesn't it still?