Skip to content

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

fix(core): make changes backwards-compatible

Elijah Potterbcd6bf581dd5
diff --git a/Cargo.lock b/Cargo.lock
index 57455c758..395b861a6 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -17,6 +17,18 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
+[[package]]
+name = "ahash"
+version = "0.8.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
+dependencies = [
+ "cfg-if",
+ "once_cell",
+ "version_check",
+ "zerocopy",
+]
+
[[package]]
name = "aho-corasick"
version = "1.1.3"
@@ -188,6 +200,39 @@ version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b"
+[[package]]
+name = "cached"
+version = "0.54.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9718806c4a2fe9e8a56fd736f97b340dd10ed1be8ed733ed50449f351dc33cae"
+dependencies = [
+ "ahash",
+ "cached_proc_macro",
+ "cached_proc_macro_types",
+ "hashbrown 0.14.5",
+ "once_cell",
+ "thiserror 1.0.69",
+ "web-time",
+]
+
+[[package]]
+name = "cached_proc_macro"
+version = "0.23.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2f42a145ed2d10dce2191e1dcf30cfccfea9026660e143662ba5eec4017d5daa"
+dependencies = [
+ "darling",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.96",
+]
+
+[[package]]
+name = "cached_proc_macro_types"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0"
+
[[package]]
name = "cast"
version = "0.3.0"
@@ -354,6 +399,41 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
+[[package]]
+name = "darling"
+version = "0.20.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
+dependencies = [
+ "darling_core",
+ "darling_macro",
+]
+
+[[package]]
+name = "darling_core"
+version = "0.20.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
+dependencies = [
+ "fnv",
+ "ident_case",
+ "proc-macro2",
+ "quote",
+ "strsim",
+ "syn 2.0.96",
+]
+
+[[package]]
+name = "darling_macro"
+version = "0.20.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
+dependencies = [
+ "darling_core",
+ "quote",
+ "syn 2.0.96",
+]
+
[[package]]
name = "dashmap"
version = "5.5.3"
@@ -450,6 +530,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
+[[package]]
+name = "fnv"
+version = "1.0.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
+
[[package]]
name = "foldhash"
version = "0.1.4"
@@ -648,6 +734,7 @@ name = "harper-core"
version = "0.22.0"
dependencies = [
"blanket",
+ "cached",
"criterion",
"fst",
"hashbrown 0.15.2",
@@ -756,6 +843,10 @@ name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
+dependencies = [
+ "ahash",
+ "allocator-api2",
+]
[[package]]
name = "hashbrown"
@@ -905,6 +996,12 @@ dependencies = [
"syn 2.0.96",
]
+[[package]]
+name = "ident_case"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
+
[[package]]
name = "idna"
version = "1.0.3"
@@ -2235,6 +2332,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
+[[package]]
+name = "version_check"
+version = "0.9.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
+
[[package]]
name = "walkdir"
version = "2.5.0"
@@ -2309,6 +2412,16 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "web-time"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
+dependencies = [
+ "js-sys",
+ "wasm-bindgen",
+]
+
[[package]]
name = "winapi"
version = "0.3.9"
diff --git a/Cargo.toml b/Cargo.toml
index 05f7ccd19..c40249357 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,4 +1,5 @@
[workspace]
+ #[cached]
members = [ "harper-cli", "harper-core", "harper-ls", "harper-comments", "harper-wasm", "harper-tree-sitter", "harper-html", "harper-literate-haskell", "harper-typst" ]
resolver = "2"
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index cb1599de7..461b87542 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -25,6 +25,7 @@ unicode-blocks = "0.1.9"
unicode-script = "0.5.7"
unicode-width = "0.2.0"
levenshtein_automata = { version = "0.2.1", features = ["fst_automaton"] }
+cached = "0.54.0"
[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
diff --git a/harper-core/benches/parse_demo.rs b/harper-core/benches/parse_demo.rs
index c9739072c..e3b269202 100644
--- a/harper-core/benches/parse_demo.rs
+++ b/harper-core/benches/parse_demo.rs
@@ -12,7 +12,7 @@ fn parse_demo(c: &mut Criterion) {
fn lint_demo(c: &mut Criterion) {
let dictionary = FstDictionary::curated();
- let mut lint_set = LintGroup::new_curated(Default::default(), dictionary);
+ let mut lint_set = LintGroup::new_curated(dictionary);
let document = Document::new_markdown_default_curated(black_box(DEMO));
c.bench_function("lint_demo", |b| {
@@ -24,8 +24,7 @@ fn lint_demo_uncached(c: &mut Criterion) {
c.bench_function("lint_demo_uncached", |b| {
b.iter(|| {
let dictionary = FstDictionary::curated();
- let mut lint_set =
- LintGroup::new_curated(LintGroupConfig::default(), dictionary.clone());
+ let mut lint_set = LintGroup::new_curated(dictionary.clone());
let document = Document::new_markdown_default(black_box(DEMO), &dictionary);
lint_set.lint(&document)
})
@@ -33,9 +32,9 @@ fn lint_demo_uncached(c: &mut Criterion) {
}
pub fn criterion_benchmark(c: &mut Criterion) {
- //parse_demo(c);
+ parse_demo(c);
lint_demo(c);
- //lint_demo_uncached(c);
+ lint_demo_uncached(c);
}
criterion_group!(benches, criterion_benchmark);
diff --git a/harper-core/src/linting/closed_compounds.rs b/harper-core/src/linting/closed_compounds.rs
index 1b293f482..f09c21b9e 100644
--- a/harper-core/src/linting/closed_compounds.rs
+++ b/harper-core/src/linting/closed_compounds.rs
@@ -6,17 +6,20 @@ pub fn lint_group() -> LintGroup {
let mut group = LintGroup::empty();
macro_rules! add_compound_mappings {
- ($group:expr, { $($name:expr => ($bad:expr, $good:expr)),+ $(,)? }) => {
- $(
- $group.add(
- $name,
- Box::new(MapPhraseLinter::new_closed_compound($bad, $good)),
- );
- )+
- };
-}
-
- // Use the macro with a grid of mappings:
+ ($group:expr, { $($name:expr => ($bad:expr, $good:expr)),+ $(,)? }) => {
+ $(
+ $group.add(
+ $name,
+ Box::new(MapPhraseLinter::new_closed_compound($bad, $good)),
+ );
+ )+
+ };
+ }
+
+ // These are compound words that should be condensed.
+ // The first column is the name of the rule (which shows up in settings).
+ // The second column is the incorrect form of the word and the third column is the correct
+ // form.
add_compound_mappings!(group, {
"Itself" => ("it self", "itself"),
"Myself" => ("my self", "myself"),
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 367b5e7f6..49e953c16 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -1,5 +1,6 @@
use std::sync::Arc;
+use cached::proc_macro::cached;
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
@@ -50,18 +51,25 @@ use super::wrong_quotes::WrongQuotes;
use super::Lint;
use super::{CurrencyPlacement, Linter, NoOxfordComma, OxfordComma};
use crate::linting::{closed_compounds, phrase_corrections};
-use crate::Dictionary;
use crate::Document;
+use crate::{Dictionary, MutableDictionary};
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
#[serde(transparent)]
pub struct LintGroupConfig {
- inner: HashMap<String, bool>,
+ inner: HashMap<String, Option<bool>>,
+}
+
+#[cached]
+fn curated_config() -> LintGroupConfig {
+ // Dictionary does not matter, we're just after the config.
+ let group = LintGroup::new_curated(MutableDictionary::new().into());
+ group.config
}
impl LintGroupConfig {
pub fn set_rule_enabled(&mut self, key: impl ToString, val: bool) {
- self.inner.insert(key.to_string(), val);
+ self.inner.insert(key.to_string(), Some(val));
}
/// Remove any configuration attached to a rule.
@@ -77,11 +85,15 @@ impl LintGroupConfig {
}
pub fn is_rule_enabled(&self, key: &str) -> bool {
- self.inner.get(key).cloned().unwrap_or(false)
+ self.inner.get(key).cloned().flatten().unwrap_or(false)
}
+ /// Clear all config options.
+ /// This will reset them all to disabled.
pub fn clear(&mut self) {
- self.inner.clear();
+ for val in self.inner.values_mut() {
+ *val = None
+ }
}
/// Merge the contents of another [`LintGroupConfig`] into this one.
@@ -91,6 +103,15 @@ impl LintGroupConfig {
pub fn merge_from(&mut self, other: &mut LintGroupConfig) {
self.inner.extend(other.inner.drain());
}
+
+ /// Fill the group with the values for the curated lint group.
+ pub fn fill_with_curated(&mut self) {
+ self.merge_from(&mut Self::new_curated());
+ }
+
+ pub fn new_curated() -> Self {
+ curated_config()
+ }
}
#[derive(Default)]
@@ -166,6 +187,7 @@ impl LintGroup {
));
out.merge_from(&mut closed_compounds::lint_group());
+ // Add all of the more complex rules to the group.
insert_struct_rule!(WordPressDotcom, true);
insert_struct_rule!(OutOfDate, true);
insert_struct_rule!(ThenThan, true);
@@ -217,6 +239,13 @@ impl LintGroup {
out
}
+
+ /// Create a new curated group with all config values cleared out.
+ pub fn new_curated_empty_config(dictionary: Arc<impl Dictionary + 'static>) -> Self {
+ let mut group = Self::new_curated(dictionary);
+ group.config.clear();
+ group
+ }
}
impl Linter for LintGroup {
@@ -239,13 +268,15 @@ impl Linter for LintGroup {
#[cfg(test)]
mod tests {
- use crate::{linting::Linter, Document, FstDictionary, Lrc, MutableDictionary};
+ use std::sync::Arc;
+
+ use crate::{linting::Linter, Document, FstDictionary, MutableDictionary};
use super::LintGroup;
#[test]
fn can_get_all_descriptions() {
- let group = LintGroup::new_curated(Lrc::new(MutableDictionary::default()));
+ let group = LintGroup::new_curated(Arc::new(MutableDictionary::default()));
group.all_descriptions();
}
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 5693bcf72..db0ec1a0a 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -61,7 +61,7 @@ pub fn lint_group() -> LintGroup {
add_exact_mappings!(group, {
// The name of the rule
- "ChangeTack" => (
+ "ChangeTack" => (
// The exact phrase to look for.
"change tact",
// The corrections to provide.
@@ -71,7 +71,7 @@ pub fn lint_group() -> LintGroup {
// A description of the rule.
"Locates minor errors in the sailing idiom `change tack`."
),
- "WantBe" => (
+ "WantBe" => (
"want be",
["won't be", "want to be"],
"Did you mean `won't be` or `want to be`?",
@@ -83,35 +83,41 @@ pub fn lint_group() -> LintGroup {
"Did you mean `state of the art`?",
"Detects incorrect usage of `state of art` and suggests `state of the art` as the correct phrase."
),
- "FaceFirst" => (
+ "FaceFirst" => (
"face first into",
["face-first into"],
"Should this be `face-first`?",
"Ensures `face first` is correctly hyphenated as `face-first` when used before `into`."
),
- "EludedTo" => (
+ "EludedTo" => (
"eluded to",
["alluded to"],
"Did you mean `alluded to`?",
"Corrects `eluded to` to `alluded to` in contexts referring to indirect references."
),
- "BaitedBreath" => (
+ "BaitedBreath" => (
"baited breath",
["bated breath"],
"Did you mean `bated breath`?",
"Ensures `bated breath` is written correctly, as `baited breath` is incorrect."
),
- "BareInMind" => (
+ "BareInMind" => (
"bare in mind",
["bear in mind"],
"Did you mean `bear in mind`?",
"Ensures the phrase `bear in mind` is used correctly instead of `bare in mind`."
),
- "MutePoint" => (
+ "MutePoint" => (
"mute point",
["moot point"],
"Did you mean `moot point`?",
"Ensures `moot point` is used instead of `mute point`, as `moot` means debatable or irrelevant."
+ ),
+ "RoadMap" => (
+ "roadmap",
+ ["road map"],
+ "Did you mean `road map`?",
+ "Detects when `roadmap` is used instead of `road map`, prompting the correct spacing."
)
});
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 13645bbd7..90cd5f484 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -80,9 +80,10 @@ impl Linter {
/// in Harper.
pub fn new() -> Self {
let dictionary = Self::construct_merged_dict(MutableDictionary::default());
+ let lint_group = LintGroup::new_curated_empty_config(dictionary.clone());
Self {
- lint_group: LintGroup::new_curated(dictionary.clone()),
+ lint_group,
user_dictionary: MutableDictionary::new(),
dictionary,
ignored_lints: IgnoredLints::default(),
@@ -92,10 +93,10 @@ impl Linter {
/// Update the dictionary inside [`Self::lint_group`] to include [`Self::user_dictionary`].
/// This clears any linter caches, so use it sparingly.
fn synchronize_lint_dict(&mut self) {
- let lint_config = self.lint_group.config.clone();
+ let mut lint_config = self.lint_group.config.clone();
self.dictionary = Self::construct_merged_dict(self.user_dictionary.clone());
- self.lint_group =
- LintGroup::new_curated(self.dictionary.clone()).with_lint_config(lint_config);
+ self.lint_group = LintGroup::new_curated_empty_config(self.dictionary.clone());
+ self.lint_group.config.merge_from(&mut lint_config);
}
/// Construct the actual dictionary to be used for linting and parsing from the curated dictionary
@@ -136,13 +137,19 @@ impl Linter {
}
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())?;
+ self.lint_group
+ .config
+ .merge_from(&mut serde_json::from_str(&json).map_err(|v| v.to_string())?);
Ok(())
}
/// Get a Record containing the descriptions of all the linting rules.
pub fn get_lint_descriptions_as_object(&self) -> JsValue {
- serde_wasm_bindgen::to_value(&self.lint_group.all_descriptions()).unwrap()
+ let serializer = serde_wasm_bindgen::Serializer::json_compatible();
+ self.lint_group
+ .all_descriptions()
+ .serialize(&serializer)
+ .unwrap()
}
pub fn get_lint_config_as_object(&self) -> JsValue {
@@ -153,8 +160,9 @@ impl Linter {
}
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())?;
+ self.lint_group
+ .config
+ .merge_from(&mut serde_wasm_bindgen::from_value(object).map_err(|v| v.to_string())?);
Ok(())
}
@@ -178,8 +186,13 @@ impl Linter {
let document =
Document::new_from_vec(source.clone(), &parser, &MutableDictionary::curated());
+ let temp = self.lint_group.config.clone();
+ self.lint_group.config.fill_with_curated();
+
let mut lints = self.lint_group.lint(&document);
+ self.lint_group.config = temp;
+
remove_overlaps(&mut lints);
self.ignored_lints.remove_ignored(&mut lints, &document);
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index bca9a9093..df13dbcc8 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -75,7 +75,7 @@ for (const [linterName, Linter] of Object.entries(linters)) {
const linter = new Linter();
const lintConfig = await linter.getLintConfig();
- expect(lintConfig).toHaveProperty('repeated_words');
+ expect(lintConfig).toHaveProperty('RepeatedWords');
});
test(`${linterName} can both get and set its configuration`, async () => {
diff --git a/packages/web/src/routes/docs/rules/+page.svelte b/packages/web/src/routes/docs/rules/+page.svelte
index c47d737ab..212f72e9e 100644
--- a/packages/web/src/routes/docs/rules/+page.svelte
+++ b/packages/web/src/routes/docs/rules/+page.svelte
@@ -7,28 +7,17 @@
};
let info: Record<string, string> = $state({});
- let titles: Record<string, string> = $state({});
let linter = new LocalLinter();
linter.getLintDescriptions().then(async (v) => {
info = v;
-
- for (let key of Object.keys(info)) {
- titles[key] = await makeTitleCase(key);
- console.log(key, titles[key]);
- }
+ console.log(v);
});
-
- /** Make a snake case string title case. */
- function makeTitleCase(snakeCase: string): Promise<string> {
- return linter.toTitleCase(snakeCase.replaceAll('_', ' '));
- }
</script>
<p>This page is an incomplete list of the various grammatical rules Harper checks for.</p>
{#each Object.entries(info) as [name, description]}
- <h2>{titles[name]}</h2>
+ <h2>{name}</h2>
<p>{description}</p>
- <p>This rule is also often referred to as <code>{name}</code>.</p>
{/each}