Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): detect missing spaces after some punctuation (#2196)
Elijah Potter4b78139dc58a
diff --git a/harper-comments/tests/language_support_sources/basic_kotlin.kt b/harper-comments/tests/language_support_sources/basic_kotlin.ktindex c0c72269c..cbb075859 100644--- a/harper-comments/tests/language_support_sources/basic_kotlin.kt+++ b/harper-comments/tests/language_support_sources/basic_kotlin.kt@@ -1,6 +1,4 @@// *************************************************************************************************-// File: RogueScheduler.kt-//// A diminutive but fully-formed demonstration of idiomatic Kotlin.//// 1. Defines a sealed algebraic hierarchy to represent the discrete states of a taskdiff --git a/harper-core/src/lexing/hostname.rs b/harper-core/src/lexing/hostname.rsindex 961955da5..5b2ba923c 100644--- a/harper-core/src/lexing/hostname.rs+++ b/harper-core/src/lexing/hostname.rs@@ -19,6 +19,11 @@ pub fn lex_hostname_token(source: &[char]) -> Option<FoundToken> {return None;}+ // For the sake of semantics and downstream grammar checking.+ if !ends_with_common_tld(&source[0..len]) {+ return None;+ }+Some(FoundToken {next_index: len,token: TokenKind::Hostname,@@ -31,14 +36,14 @@ pub fn lex_hostname(source: &[char]) -> Option<usize> {// The beginning has different requirements from the rest of the hostname.let first = source.first()?;- if !matches!(first, 'A'..='Z' | 'a'..='z' | '0'..='9' ) {+ if !matches!(first, 'A'..='Z' | 'a'..='z' | '0'..='9' ) {return None;}for label in source.split(|c| *c == '.') {for c in label {passed_chars += 1;- if !matches!(c, 'A'..='Z' | 'a'..='z' | '0'..='9' | '-') {+ if !matches!(c, 'A'..='Z' | 'a'..='z' | '0'..='9' | '-') {return Some(passed_chars - 1);}}@@ -53,6 +58,34 @@ pub fn lex_hostname(source: &[char]) -> Option<usize> {}}+const COMMON_TLDS: &[&[char]] = &[+ &['c', 'o', 'm'],+ &['n', 'e', 't'],+ &['o', 'r', 'g'],+ &['e', 'd', 'u'],+ &['g', 'o', 'v'],+ &['m', 'i', 'l'],+ &['t', 'x', 't'],+ &['i', 'o'],+ &['c', 'o'],+ &['u', 's'],+ &['u', 'k'],+ &['d', 'e'],+ &['c', 'a'],+ &['a', 'u'],+ &['j', 'p'],+];++fn ends_with_common_tld(input: &[char]) -> bool {+ for tld in COMMON_TLDS {+ let n = tld.len();+ if input.len() >= n && &input[input.len() - n..] == *tld {+ return true;+ }+ }+ false+}+#[cfg(test)]pub mod tests {use super::lex_hostname;diff --git a/harper-core/src/lexing/mod.rs b/harper-core/src/lexing/mod.rsindex 0ff647032..94babbe3d 100644--- a/harper-core/src/lexing/mod.rs+++ b/harper-core/src/lexing/mod.rs@@ -426,7 +426,7 @@ mod tests {#[test]fn lexes_youtube_as_hostname() {- let source: Vec<_> = "YouTube.com".chars().collect();+ let source: Vec<_> = "youtube.com".chars().collect();assert_eq!(lex_token(&source),Some(FoundToken {diff --git a/harper-core/src/linting/missing_space.rs b/harper-core/src/linting/missing_space.rsnew file mode 100644index 000000000..85181f959--- /dev/null+++ b/harper-core/src/linting/missing_space.rs@@ -0,0 +1,201 @@+use itertools::Itertools;++use crate::{Document, Punctuation};++use super::{Lint, LintKind, Linter, Suggestion};++pub struct MissingSpace;++impl Linter for MissingSpace {+ fn lint(&mut self, document: &Document) -> Vec<Lint> {+ let mut lints = Vec::new();++ for (a, b, c) in document.tokens().tuple_windows() {+ if let Some(punct) = b.kind.as_punctuation()+ && [+ Punctuation::Period,+ Punctuation::Bang,+ Punctuation::Question,+ Punctuation::Semicolon,+ ]+ .contains(punct)+ && a.kind.is_word()+ && c.kind.is_word()+ {+ lints.push(Lint {+ span: b.span,+ lint_kind: LintKind::Formatting,+ suggestions: vec![Suggestion::InsertAfter(vec![' '])],+ message: "It looks like you're missing a space here.".to_owned(),+ priority: 31,+ });+ }+ }++ lints+ }++ fn description(&self) -> &str {+ "Looks for missing spaces after a comma or period."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::assert_suggestion_result;++ use super::MissingSpace;++ #[test]+ fn issue_2191() {+ assert_suggestion_result(+ "people that can help us.So I feel like there",+ MissingSpace,+ "people that can help us. So I feel like there",+ );+ }++ #[test]+ fn coffee_table() {+ assert_suggestion_result(+ "The coffee cooled on the table.The room stayed quiet.",+ MissingSpace,+ "The coffee cooled on the table. The room stayed quiet.",+ );+ }++ #[test]+ fn open_window() {+ assert_suggestion_result(+ "A small breeze moved through the open window.The curtains lifted and fell in slow waves.",+ MissingSpace,+ "A small breeze moved through the open window. The curtains lifted and fell in slow waves.",+ );+ }++ #[test]+ fn hallway_cat() {+ assert_suggestion_result(+ "The cat watched the hallway.Its tail twitched with steady focus.",+ MissingSpace,+ "The cat watched the hallway. Its tail twitched with steady focus.",+ );+ }++ #[test]+ fn rain_glass() {+ assert_suggestion_result(+ "Rain tapped against the glass.The sound made the afternoon feel longer.",+ MissingSpace,+ "Rain tapped against the glass. The sound made the afternoon feel longer.",+ );+ }++ #[test]+ fn cyclist_house() {+ assert_suggestion_result(+ "A cyclist passed by the house.The wheels hummed softly on the road.",+ MissingSpace,+ "A cyclist passed by the house. The wheels hummed softly on the road.",+ );+ }++ #[test]+ fn kettle_stove() {+ assert_suggestion_result(+ "The kettle hissed on the stove.A thin ribbon of steam curled toward the ceiling.",+ MissingSpace,+ "The kettle hissed on the stove. A thin ribbon of steam curled toward the ceiling.",+ );+ }++ #[test]+ fn sparrow_fence() {+ assert_suggestion_result(+ "A sparrow landed on the fence.Its wings fluttered once before it settled.",+ MissingSpace,+ "A sparrow landed on the fence. Its wings fluttered once before it settled.",+ );+ }++ #[test]+ fn streetlamp_dusk() {+ assert_suggestion_result(+ "The streetlamp flickered at dusk.A pale glow spread across the sidewalk.",+ MissingSpace,+ "The streetlamp flickered at dusk. A pale glow spread across the sidewalk.",+ );+ }++ #[test]+ fn distant_laughter() {+ assert_suggestion_result(+ "Someone laughed in the distance.The echo drifted between the buildings.",+ MissingSpace,+ "Someone laughed in the distance. The echo drifted between the buildings.",+ );+ }++ #[test]+ fn notebook_desk() {+ assert_suggestion_result(+ "A notebook lay open on the desk.Its blank pages waited for a pen.",+ MissingSpace,+ "A notebook lay open on the desk. Its blank pages waited for a pen.",+ );+ }++ #[test]+ fn question_mark_mid_sentence() {+ assert_suggestion_result(+ "Where are you?I looked around the room.",+ MissingSpace,+ "Where are you? I looked around the room.",+ );+ }++ #[test]+ fn question_mark_before_name() {+ assert_suggestion_result(+ "Are you coming?Elijah is already waiting.",+ MissingSpace,+ "Are you coming? Elijah is already waiting.",+ );+ }++ #[test]+ fn exclamation_mid_sentence() {+ assert_suggestion_result(+ "The door slammed shut!Everyone in the hall jumped.",+ MissingSpace,+ "The door slammed shut! Everyone in the hall jumped.",+ );+ }++ #[test]+ fn exclamation_before_clause() {+ assert_suggestion_result(+ "You actually solved it!That changes everything.",+ MissingSpace,+ "You actually solved it! That changes everything.",+ );+ }++ #[test]+ fn semicolon_before_adverb() {+ assert_suggestion_result(+ "He wanted to leave;however, he stayed until the end.",+ MissingSpace,+ "He wanted to leave; however, he stayed until the end.",+ );+ }++ #[test]+ fn semicolon_connecting_clauses() {+ assert_suggestion_result(+ "The night was cold;stars glittered above the dark field.",+ MissingSpace,+ "The night was cold; stars glittered above the dark field.",+ );+ }+}diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 4f9ac4f64..14b96fa24 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -96,6 +96,7 @@ mod mass_plurals;mod merge_linters;mod merge_words;mod missing_preposition;+mod missing_space;mod missing_to;mod misspell;mod mixed_bag;diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rsindex a8a9110b0..3a858fb40 100644--- a/harper-core/tests/run_tests.rs+++ b/harper-core/tests/run_tests.rs@@ -98,4 +98,4 @@ create_test!(issue_2151.md, 4, Dialect::British);create_test!(lukas_homework.md, 3, Dialect::American);// Org mode tests-create_org_test!(index.org, 40, Dialect::American);+create_org_test!(index.org, 50, Dialect::American);diff --git a/harper-core/tests/text/linters/The Constitution of the United States.snap.yml b/harper-core/tests/text/linters/The Constitution of the United States.snap.ymlindex 6a0e9832c..4df3d006a 100644--- a/harper-core/tests/text/linters/The Constitution of the United States.snap.yml+++ b/harper-core/tests/text/linters/The Constitution of the United States.snap.yml@@ -960,7 +960,14 @@ Message: |367 | and such person shall act accordingly until a President or Vice President shall| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~368 | have qualified.The Congress may by law provide for the case of the death of any- | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ | ~~~~~~~~~~~~~~~ This sentence is 101 words long.++++Lint: Readability (127 priority)+Message: |+ 368 | have qualified.The Congress may by law provide for the case of the death of any+ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~369 | of the persons from whom the House of Representatives may choose a President| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~370 | whenever the right of choice shall have devolved upon them, and for the case of@@ -968,7 +975,7 @@ Message: |371 | the death of any of the persons from whom the Senate may choose a Vice| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~372 | President whenever the right of choice shall have devolved upon them.]The- | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This sentence is 167 words long.+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This sentence is 68 words long.diff --git a/harper-core/tests/text/tagged/The Constitution of the United States.md b/harper-core/tests/text/tagged/The Constitution of the United States.mdindex 67fcc48e8..95eda21eb 100644--- a/harper-core/tests/text/tagged/The Constitution of the United States.md+++ b/harper-core/tests/text/tagged/The Constitution of the United States.md@@ -792,10 +792,10 @@# NPr/VB+ NSg/R NSg/VB+ . NPr/C D NSg+ NPr/J/P I/C+ NSg/I/VB/J NPr/I+ VL3 P NPr/VB VXB NSg/VXB VP/J .> and such person shall act accordingly until a President or Vice President shall# VB/C NSg/I NSg/VB+ VXB NPr/VB+ R C/P D/P NSg/VB NPr/C NSg/VB/J/P+ NSg/VB+ VXB-> have qualified.The Congress may by law provide for the case of the death of any-# NSg/VXB Hostname NPr/VB+ NPr/VXB NSg/J/P N🅪Sg/VB+ VB C/P D NPr🅪Sg/VB P D NPr🅪Sg P I/R/Dq-> of the persons from whom the House of Representatives may choose a President-# P D NPl/V3+ P I+ D NPr/VB P NPl+ NPr/VXB NSg/VB/C D/P NSg/VB++> have qualified . The Congress may by law provide for the case of the death of any+# NSg/VXB VP/J . D+ NPr/VB+ NPr/VXB NSg/J/P N🅪Sg/VB+ VB C/P D NPr🅪Sg/VB P D NPr🅪Sg P I/R/Dq+> of the persons from whom the House of Representatives may choose a President+# P D NPl/V3+ P I+ D NPr/VB P NPl+ NPr/VXB NSg/VB/C D/P+ NSg/VB+> whenever the right of choice shall have devolved upon them , and for the case of# C D NPr/VB/J P N🅪Sg/J+ VXB NSg/VXB VP/J P NSg/IPl+ . VB/C C/P D NPr🅪Sg/VB P> the death of any of the persons from whom the Senate may choose a Vicediff --git a/packages/vscode-plugin/package.json b/packages/vscode-plugin/package.jsonindex 3bd3ecaef..15445298a 100644--- a/packages/vscode-plugin/package.json+++ b/packages/vscode-plugin/package.json@@ -261,7 +261,7 @@"scope": "resource","type": "boolean","default": true,- "description": "When referring to the various products of Amazon.com, make sure to treat them as a proper noun."+ "description": "When referring to the various products of amazon.com, make sure to treat them as a proper noun."},"harper.linters.Americas": {"scope": "resource",