Skip to content

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

Merge remote-tracking branch 'upstream/master' into ignore-link-title-cfg

Grant Lemonsfe390137702f
diff --cc harper-cli/src/main.rs
index bd2b7b248,ef57427b6..9d61c036d
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@@ -74,10 -77,10 +79,10 @@@ fn main() -> anyhow::Result<()>
let report = report_builder.finish();
report.print((&filename, Source::from(source)))?;
- std::process::exit(1);
+ Ok(())
}
Args::Parse { file } => {
- let (doc, _) = load_file(&file)?;
+ let (doc, _) = load_file(&file, markdown_options)?;
for token in doc.tokens() {
let json = serde_json::to_string(&token)?;
diff --cc harper-wasm/src/lib.rs
index 0e421e6d4,cd243f4e4..50cf054da
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@@ -25,63 -19,111 +19,115 @@@ use wasm_bindgen::JsValue
pub fn setup() {
console_error_panic_hook::set_once();
- tracing_wasm::set_as_global_default();
+ // If `setup` gets called more than once, we want to allow this error to fall through.
+ let _ = tracing_wasm::try_set_as_global_default();
+ }
+
+ macro_rules! make_serialize_fns_for {
+ ($name:ident) => {
+ #[wasm_bindgen]
+ impl $name {
+ pub fn to_json(&self) -> String {
+ serde_json::to_string(&self).unwrap()
+ }
+
+ pub fn from_json(json: String) -> Result<Self, String> {
+ serde_json::from_str(&json).map_err(|err| err.to_string())
+ }
+ }
+ };
}
- /// Helper method to quickly check if a plain string is likely intended to be English
+ make_serialize_fns_for!(Suggestion);
+ make_serialize_fns_for!(Lint);
+ make_serialize_fns_for!(Span);
+
#[wasm_bindgen]
- pub fn is_likely_english(text: String) -> bool {
- let document = Document::new_plain_english_curated(&text);
- is_doc_likely_english(&document, &FullDictionary::curated())
+ pub struct Linter {
+ lint_group: LintGroup<Arc<FstDictionary>>,
+ dictionary: Arc<FstDictionary>,
}
- /// Helper method to remove non-English text from a plain English document.
#[wasm_bindgen]
- pub fn isolate_english(text: String) -> String {
- let dict = FullDictionary::curated();
+ impl Linter {
+ /// Construct a new `Linter`.
+ /// Note that this can mean constructing the curated dictionary, which is the most expensive operation
+ /// in Harper.
+ pub fn new() -> Self {
+ let dictionary = FstDictionary::curated();
+
+ Self {
+ lint_group: LintGroup::new(LintGroupConfig::default(), dictionary.clone()),
+ dictionary,
+ }
+ }
- let document = Document::new_curated(
- &text,
- &mut IsolateEnglish::new(Box::new(PlainEnglish), dict.clone()),
- );
+ /// Helper method to quickly check if a plain string is likely intended to be English
+ pub fn is_likely_english(&self, text: String) -> bool {
+ let document = Document::new_plain_english(&text, &self.dictionary);
+ is_doc_likely_english(&document, &self.dictionary)
+ }
- document.to_string()
- }
+ /// Helper method to remove non-English text from a plain English document.
+ pub fn isolate_english(&self, text: String) -> String {
+ let document = Document::new(
+ &text,
+ &mut IsolateEnglish::new(Box::new(PlainEnglish), self.dictionary.clone()),
+ &self.dictionary,
+ );
- #[wasm_bindgen]
- pub fn get_lint_config_as_object() -> JsValue {
- let linter = LINTER.lock().unwrap();
- serde_wasm_bindgen::to_value(&linter.config).unwrap()
- }
+ document.to_string()
+ }
- #[wasm_bindgen]
- pub fn set_lint_config_from_object(object: JsValue) -> Result<(), String> {
- let mut linter = LINTER.lock().unwrap();
- linter.config = serde_wasm_bindgen::from_value(object).map_err(|v| v.to_string())?;
- Ok(())
- }
+ pub fn get_lint_config_as_json(&self) -> String {
+ serde_json::to_string(&self.lint_group.config).unwrap()
+ }
- /// Perform the configured linting on the provided text.
- #[wasm_bindgen]
- pub fn lint(text: String) -> Vec<Lint> {
- let source: Vec<_> = text.chars().collect();
- let source = Lrc::new(source);
+ pub fn set_lint_config_from_json(&mut self, json: String) -> Result<(), String> {
+ self.lint_group.config = serde_json::from_str(&json).map_err(|v| v.to_string())?;
+ Ok(())
+ }
+
+ pub fn get_lint_config_as_object(&self) -> JsValue {
+ // Important for downstream JSON serialization
+ let serializer = serde_wasm_bindgen::Serializer::json_compatible();
+
+ self.lint_group.config.serialize(&serializer).unwrap()
+ }
+
+ pub fn set_lint_config_from_object(&mut self, object: JsValue) -> Result<(), String> {
+ self.lint_group.config =
+ serde_wasm_bindgen::from_value(object).map_err(|v| v.to_string())?;
+ Ok(())
+ }
+
+ /// Perform the configured linting on the provided text.
+ pub fn lint(&mut self, text: String) -> Vec<Lint> {
+ let source: Vec<_> = text.chars().collect();
+ let source = Lrc::new(source);
+
- let document =
- Document::new_from_vec(source.clone(), &mut Markdown, &FullDictionary::curated());
++ // TODO: Have a way to configure the markdown parser
++ let document = Document::new_from_vec(
++ source.clone(),
++ &mut Markdown::default(),
++ &FullDictionary::curated(),
++ );
- // TODO: Have a way to configure the markdown parser
- let document = Document::new_from_vec(
- source.clone(),
- &mut Markdown::default(),
- &FullDictionary::curated(),
- );
+ let mut lints = self.lint_group.lint(&document);
- let mut lints = LINTER.lock().unwrap().lint(&document);
+ remove_overlaps(&mut lints);
- remove_overlaps(&mut lints);
+ lints
+ .into_iter()
+ .map(|l| Lint::new(l, source.to_vec()))
+ .collect()
+ }
+ }
- lints
- .into_iter()
- .map(|l| Lint::new(l, source.clone()))
- .collect()
+ impl Default for Linter {
+ fn default() -> Self {
+ Self::new()
+ }
}
#[wasm_bindgen]