Skip to content

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

feat: imho→in my humble/honest opinion (#2468)

Andrew Dunbar6426ef508327
diff --git a/harper-core/src/linting/initialism_linter.rs b/harper-core/src/linting/initialism_linter.rs
index 2f34880b1..65c961c94 100644
--- a/harper-core/src/linting/initialism_linter.rs
+++ b/harper-core/src/linting/initialism_linter.rs
@@ -1,30 +1,39 @@
use crate::expr::Expr;
-use itertools::Itertools;
use crate::{Token, patterns::Word};
use super::{ExprLinter, Lint, LintKind, Suggestion};
use crate::linting::expr_linter::Chunk;
+/// Alias for a word in an initialism expansion
+type InitialismWord = Vec<char>;
+/// Alias for a phrase an initialism expands to
+type InitialismPhrase = Vec<InitialismWord>;
+
/// A struct that can be composed to expand initialisms, respecting the capitalization of each
/// item.
pub struct InitialismLinter {
expr: Box<dyn Expr>,
/// The lowercase-normalized expansion of the initialism.
- expansion_lower: Vec<Vec<char>>,
+ expansions_lower: Vec<InitialismPhrase>,
}
impl InitialismLinter {
/// Construct a linter that can correct an initialism to
- pub fn new(initialism: &str, expansion: &str) -> Self {
- let expansion_lower = expansion
- .split(' ')
- .map(|s| s.chars().map(|v| v.to_ascii_lowercase()).collect())
+ pub fn new(initialism: &str, expansions: &[&str]) -> Self {
+ let expansions_lower = expansions
+ .iter()
+ .map(|expansion| {
+ expansion
+ .split(' ')
+ .map(|s| s.chars().map(|v| v.to_ascii_lowercase()).collect())
+ .collect()
+ })
.collect();
Self {
expr: Box::new(Word::from_char_string(initialism.chars().collect())),
- expansion_lower,
+ expansions_lower,
}
}
}
@@ -40,26 +49,31 @@ impl ExprLinter for InitialismLinter {
let tok = matched_tokens.first()?;
let source = tok.span.get_content(source);
- let mut expansion_lower = self.expansion_lower.to_owned();
- let first_letter = &mut expansion_lower[0][0];
-
- *first_letter = if source[0].is_ascii_uppercase() {
- first_letter.to_ascii_uppercase()
- } else {
- first_letter.to_ascii_lowercase()
- };
-
- let phrase = Itertools::intersperse_with(expansion_lower.into_iter(), || vec![' '])
- .reduce(|mut left, mut right| {
- left.append(&mut right);
- left
+ let suggestions = self
+ .expansions_lower
+ .iter()
+ .map(|expansion_lower| {
+ let mut expansion = expansion_lower.clone();
+ let first_letter = &mut expansion[0][0];
+ *first_letter = if source[0].is_ascii_uppercase() {
+ first_letter.to_ascii_uppercase()
+ } else {
+ first_letter.to_ascii_lowercase()
+ };
+ Suggestion::ReplaceWith(
+ expansion
+ .iter()
+ .flat_map(|word| std::iter::once(' ').chain(word.iter().copied()))
+ .skip(1)
+ .collect::<Vec<_>>(),
+ )
})
- .unwrap();
+ .collect::<Vec<_>>();
Some(Lint {
span: tok.span,
lint_kind: LintKind::Miscellaneous,
- suggestions: vec![Suggestion::ReplaceWith(phrase)],
+ suggestions,
message: "Try expanding this initialism.".to_owned(),
priority: 127,
})
diff --git a/harper-core/src/linting/initialisms.rs b/harper-core/src/linting/initialisms.rs
index 23106af94..dbda90225 100644
--- a/harper-core/src/linting/initialisms.rs
+++ b/harper-core/src/linting/initialisms.rs
@@ -17,26 +17,26 @@ pub fn lint_group() -> LintGroup {
}
add_initialism_mappings!(group, {
- "ByTheWay" => ("btw", "by the way"),
- "ForYourInformation" => ("fyi", "for your information"),
- "AsSoonAsPossible" => ("asap", "as soon as possible"),
- "InMyOpinion" => ("imo", "in my opinion"),
- "InMyHumbleOpinion" => ("imho", "in my humble opinion"),
- "OhMyGod" => ("omg", "oh my god"),
- "BeRightBack" => ("brb", "be right back"),
- "TalkToYouLater" => ("ttyl", "talk to you later"),
- "NeverMind" => ("nvm", "never mind"),
- "ToBeHonest" => ("tbh", "to be honest"),
- "AsFarAsIKnow" => ("afaik", "as far as I know"),
- "Really" => ("rly", "really"),
- "ExplainLikeImFive" => ("eli5", "explain like i'm five"),
- "ForWhatItsWorth" => ("fwiw", "for what it's worth"),
- "IDontKnow" => ("idk", "I don't know"),
- "IfIRecallCorrectly" => ("iirc", "if I recall correctly"),
- "IfYouKnowYouKnow" => ("iykyk", "if you know, you know"),
- "InCaseYouMissedIt" => ("icymi", "in case you missed it"),
- "InRealLife" => ("irl", "in real life"),
- "PleaseTakeALook" => ("ptal", "please take a look"),
+ "ByTheWay" => ("btw", &["by the way"]),
+ "ForYourInformation" => ("fyi", &["for your information"]),
+ "AsSoonAsPossible" => ("asap", &["as soon as possible"]),
+ "InMyOpinion" => ("imo", &["in my opinion"]),
+ "InMyHumbleOpinion" => ("imho", &["in my humble opinion", "in my honest opinion"]),
+ "OhMyGod" => ("omg", &["oh my god"]),
+ "BeRightBack" => ("brb", &["be right back"]),
+ "TalkToYouLater" => ("ttyl", &["talk to you later"]),
+ "NeverMind" => ("nvm", &["never mind"]),
+ "ToBeHonest" => ("tbh", &["to be honest"]),
+ "AsFarAsIKnow" => ("afaik", &["as far as I know"]),
+ "Really" => ("rly", &["really"]),
+ "ExplainLikeImFive" => ("eli5", &["explain like i'm five"]),
+ "ForWhatItsWorth" => ("fwiw", &["for what it's worth"]),
+ "IDontKnow" => ("idk", &["I don't know"]),
+ "IfIRecallCorrectly" => ("iirc", &["if I recall correctly"]),
+ "IfYouKnowYouKnow" => ("iykyk", &["if you know, you know"]),
+ "InCaseYouMissedIt" => ("icymi", &["in case you missed it"]),
+ "InRealLife" => ("irl", &["in real life"]),
+ "PleaseTakeALook" => ("ptal", &["please take a look"]),
});
group.set_all_rules_to(Some(true));
@@ -46,7 +46,7 @@ pub fn lint_group() -> LintGroup {
#[cfg(test)]
mod tests {
- use crate::linting::tests::assert_suggestion_result;
+ use crate::linting::tests::{assert_good_and_bad_suggestions, assert_suggestion_result};
use super::lint_group;
@@ -207,4 +207,17 @@ mod tests {
"Please take a look at the document I sent.",
);
}
+
+ #[test]
+ fn expands_imho_both_ways() {
+ assert_good_and_bad_suggestions(
+ "Imho, this is a good idea.",
+ lint_group(),
+ &[
+ "In my humble opinion, this is a good idea.",
+ "In my honest opinion, this is a good idea.",
+ ],
+ &["In my horrible opinion, this is a good idea."],
+ );
+ }
}