Skip to content

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

fix: testing rig takes a parser instead of assuming markdown

Grant Lemonsa29b879d9dbf
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index 0478fdf47..8a99951d8 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -157,7 +157,7 @@ fn main() -> anyhow::Result<()> {
fn load_file(file: &Path) -> anyhow::Result<(Document, String)> {
let source = std::fs::read_to_string(file)?;
- let mut parser: Box<dyn harper_core::parsers::Parser> =
+ let parser: Box<dyn harper_core::parsers::Parser> =
match file.extension().map(|v| v.to_str().unwrap()) {
Some("md") => Box::new(Markdown),
Some("typ") => Box::new(Typst),
@@ -168,5 +168,5 @@ fn load_file(file: &Path) -> anyhow::Result<(Document, String)> {
),
};
- Ok((Document::new_curated(&source, &mut parser), source))
+ Ok((Document::new_curated(&source, &parser), source))
}
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 612de9edf..05392495b 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -21,14 +21,14 @@ pub struct Document {
impl Default for Document {
fn default() -> Self {
- Self::new("", &mut PlainEnglish, &FstDictionary::curated())
+ Self::new("", &PlainEnglish, &FstDictionary::curated())
}
}
impl Document {
/// Lexes and parses text to produce a document using a provided language
/// parser and dictionary.
- pub fn new(text: &str, parser: &mut impl Parser, dictionary: &impl Dictionary) -> Self {
+ pub fn new(text: &str, parser: &impl Parser, dictionary: &impl Dictionary) -> Self {
let source: Vec<_> = text.chars().collect();
Self::new_from_vec(Lrc::new(source), parser, dictionary)
@@ -36,7 +36,7 @@ impl Document {
/// Lexes and parses text to produce a document using a provided language
/// parser and the included curated dictionary.
- pub fn new_curated(text: &str, parser: &mut impl Parser) -> Self {
+ pub fn new_curated(text: &str, parser: &impl Parser) -> Self {
let source: Vec<_> = text.chars().collect();
Self::new_from_vec(Lrc::new(source), parser, &FstDictionary::curated())
@@ -46,7 +46,7 @@ impl Document {
/// parser and dictionary.
pub fn new_from_vec(
source: Lrc<Vec<char>>,
- parser: &mut impl Parser,
+ parser: &impl Parser,
dictionary: &impl Dictionary,
) -> Self {
let tokens = parser.parse(&source);
@@ -60,25 +60,25 @@ impl Document {
/// Parse text to produce a document using the built-in [`PlainEnglish`]
/// parser and curated dictionary.
pub fn new_plain_english_curated(text: &str) -> Self {
- Self::new(text, &mut PlainEnglish, &FstDictionary::curated())
+ Self::new(text, &PlainEnglish, &FstDictionary::curated())
}
/// Parse text to produce a document using the built-in [`PlainEnglish`]
/// parser and a provided dictionary.
pub fn new_plain_english(text: &str, dictionary: &impl Dictionary) -> Self {
- Self::new(text, &mut PlainEnglish, dictionary)
+ Self::new(text, &PlainEnglish, dictionary)
}
/// Parse text to produce a document using the built-in [`Markdown`] parser
/// and curated dictionary.
pub fn new_markdown_curated(text: &str) -> Self {
- Self::new(text, &mut Markdown, &FstDictionary::curated())
+ Self::new(text, &Markdown, &FstDictionary::curated())
}
/// Parse text to produce a document using the built-in [`PlainEnglish`]
/// parser and the curated dictionary.
pub fn new_markdown(text: &str, dictionary: &impl Dictionary) -> Self {
- Self::new(text, &mut Markdown, dictionary)
+ Self::new(text, &Markdown, dictionary)
}
/// Re-parse important language constructs.
diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rs
index 2b6034595..eb4f21c55 100644
--- a/harper-core/tests/run_tests.rs
+++ b/harper-core/tests/run_tests.rs
@@ -1,10 +1,13 @@
use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
-use harper_core::{Document, FstDictionary};
+use harper_core::{
+ parsers::{Markdown, Typst},
+ Document, FstDictionary,
+};
/// Creates a unit test checking that the linting of a document in
/// `tests_sources` produces the expected number of lints.
macro_rules! create_test {
- ($filename:ident, $ext:literal, $correct_expected:expr) => {
+ ($filename:ident, $ext:literal, $parser:expr, $correct_expected:expr) => {
paste::paste! {
#[test]
fn [<lints_ $filename _correctly>](){
@@ -16,7 +19,7 @@ macro_rules! create_test {
);
let dict = FstDictionary::curated();
- let document = Document::new_markdown(&source, &dict);
+ let document = Document::new(&source, $parser, &dict);
let mut linter = LintGroup::new(
LintGroupConfig::default(),
@@ -35,10 +38,10 @@ macro_rules! create_test {
}
};
($filename:ident.md, $correct_expected:expr) => {
- create_test!($filename, ".md", $correct_expected);
+ create_test!($filename, ".md", &Markdown, $correct_expected);
};
($filename:ident.typ, $correct_expected:expr) => {
- create_test!($filename, ".typ", $correct_expected);
+ create_test!($filename, ".typ", &Typst, $correct_expected);
};
}
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index 7357d4f24..422dec2c5 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -229,7 +229,7 @@ impl Backend {
parser = Box::new(IsolateEnglish::new(parser, doc_state.dict.clone()));
}
- doc_state.document = Document::new(text, &mut parser, &doc_state.dict);
+ doc_state.document = Document::new(text, &parser, &doc_state.dict);
}
}
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index cd243f4e4..18d6ff96f 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -72,7 +72,7 @@ impl Linter {
pub fn isolate_english(&self, text: String) -> String {
let document = Document::new(
&text,
- &mut IsolateEnglish::new(Box::new(PlainEnglish), self.dictionary.clone()),
+ &IsolateEnglish::new(Box::new(PlainEnglish), self.dictionary.clone()),
&self.dictionary,
);
@@ -107,7 +107,7 @@ impl Linter {
let source = Lrc::new(source);
let document =
- Document::new_from_vec(source.clone(), &mut Markdown, &FullDictionary::curated());
+ Document::new_from_vec(source.clone(), &Markdown, &FullDictionary::curated());
let mut lints = self.lint_group.lint(&document);