Skip to content

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

fix(harper-tex): mask indentation before LaTeX commands (#3106)

Juraj Klaric62e3c4e7442e
diff --git a/harper-tex/src/masker.rs b/harper-tex/src/masker.rs
index 7adb292a2..f1b3ff370 100644
--- a/harper-tex/src/masker.rs
+++ b/harper-tex/src/masker.rs
@@ -22,6 +22,8 @@ impl harper_core::Masker for Masker {
if matches!(c, '%') {
actions.push_back(CursorAction::PushMaskAndIncBy(1));
+ } else if let Some(ws) = newline_whitespace_at_cursor(cursor, source) {
+ actions.push_back(CursorAction::PushMaskAndIncBy(ws));
} else if let Some(s) = math_mode_at_cursor(cursor, source) {
actions.push_back(CursorAction::PushMaskAndIncBy(s));
} else if let Some(s) = equation_at_cursor(cursor, source) {
@@ -55,6 +57,21 @@ impl harper_core::Masker for Masker {
}
}
+/// If the cursor is at a newline, mask the newline and any following whitespace (indentation).
+/// This prevents indentation at the start of lines from being grammar-checked.
+fn newline_whitespace_at_cursor(cursor: usize, source: &[char]) -> Option<usize> {
+ if source.get(cursor) != Some(&'\n') {
+ return None;
+ }
+ let ws_len = source[cursor..]
+ .iter()
+ .take_while(|&&c| c.is_whitespace())
+ .count();
+ // Only mask if there is actual indentation after the newline, not a bare line break.
+ // A bare \n between sentences should remain visible to preserve sentence boundaries.
+ if ws_len > 1 { Some(ws_len) } else { None }
+}
+
/// Check whether there is a math mode block at the current cursor. If so, this function will return the amount cursor needs to be incremented by in order to escape the block.
fn math_mode_at_cursor(cursor: usize, source: &[char]) -> Option<usize> {
if *source.get(cursor)? != '$' {
@@ -263,6 +280,19 @@ mod tests {
use super::{Masker, deconstruct_command};
+ #[test]
+ fn does_not_mask_spaces_within_sentence() {
+ // Spaces between words (not before %) must remain in allowed spans
+ // so that lints like double-space detection can still fire.
+ let source: Vec<_> = "word word".chars().collect();
+ let mask = Masker::default().create_mask(&source);
+ let allowed: String = mask
+ .iter_allowed(&source)
+ .flat_map(|(_, chars)| chars.iter().copied())
+ .collect();
+ assert_eq!(allowed, "word word");
+ }
+
#[test]
fn ignores_many_comment_signs() {
let source: Vec<_> = "%%%".chars().collect();
@@ -323,6 +353,25 @@ mod tests {
masks_math_env("align");
}
+ #[test]
+ fn masks_indentation_before_item() {
+ let source: Vec<_> = "\\begin{itemize}\n \\item Hello world.\n\\end{itemize}"
+ .chars()
+ .collect();
+ let mask = Masker::default().create_mask(&source);
+ let allowed: Vec<String> = mask
+ .iter_allowed(&source)
+ .map(|(_, chars)| chars.iter().collect::<String>())
+ .collect();
+ for span in &allowed {
+ let trimmed = span.trim();
+ assert!(
+ !trimmed.is_empty() || span.len() <= 1,
+ "Whitespace-only span with multiple spaces leaked through: {span:?}"
+ );
+ }
+ }
+
#[test]
fn emits_all_command_components_correctly() {
let source: Vec<_> = r"\begin[some]{math}".chars().collect();