Skip to content

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

fix(harper-tex): math mode problems (#2899)

Elijah Potterd5bd230c5c73
diff --git a/harper-tex/src/masker.rs b/harper-tex/src/masker.rs
index 55c991707..e0697f60e 100644
--- a/harper-tex/src/masker.rs
+++ b/harper-tex/src/masker.rs
@@ -1,6 +1,6 @@
use std::collections::VecDeque;
-use harper_core::{Mask, Span};
+use harper_core::{CharStringExt, Mask, Span};
#[derive(Debug, Default)]
pub struct Masker {}
@@ -22,11 +22,11 @@ impl harper_core::Masker for Masker {
if matches!(c, '%') {
actions.push_back(CursorAction::PushMaskAndIncBy(1));
- } else if !command_at_cursor(cursor, source, &mut actions)
- && let Some(s) = math_mode_at_cursor(cursor, source)
- {
+ } 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) {
actions.push_back(CursorAction::PushMaskAndIncBy(s));
- } else {
+ } else if !command_at_cursor(cursor, source, &mut actions) {
actions.push_back(CursorAction::IncBy(1));
}
@@ -55,8 +55,7 @@ impl harper_core::Masker for Masker {
}
}
-/// Check whether there is a math mode block at the current cursor. If so, this function will return the
-/// index of the next non-math-block index.
+/// 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)? != '$' {
return None;
@@ -65,48 +64,24 @@ fn math_mode_at_cursor(cursor: usize, source: &[char]) -> Option<usize> {
Some(
source
.iter()
- .skip(cursor)
+ .skip(cursor + 1)
.take_while(|t| **t != '$')
.count()
- + cursor
- + 1,
+ + 2,
)
}
/// Check whether there is a command at the current cursor. If so, this function will update the action queue to mask out the hidden elements.
/// Returns whether the action queue was modified.
-fn command_at_cursor(
- mut cursor: usize,
- source: &[char],
- actions: &mut VecDeque<CursorAction>,
-) -> bool {
- let orig_cursor = cursor;
-
- if source.get(cursor) != Some(&'\\') {
+fn command_at_cursor(cursor: usize, source: &[char], actions: &mut VecDeque<CursorAction>) -> bool {
+ let Some(CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ }) = deconstruct_command(&source[cursor..])
+ else {
return false;
- }
-
- cursor += 1;
-
- // The name of the command
- let name_len = source
- .iter()
- .skip(cursor + 1)
- .take_while(|t| t.is_alphabetic())
- .count();
- let name = &source[cursor..cursor + 1 + name_len];
-
- cursor += name_len + 1;
-
- // The optional square braces
- if source.get(cursor) == Some(&'[') {
- cursor += source
- .iter()
- .skip(cursor)
- .take_while(|t| **t != ']')
- .count()
- + 1;
- }
+ };
let content_commands = [
"section",
@@ -125,31 +100,134 @@ fn command_at_cursor(
.iter()
.any(|c| name.iter().copied().eq(c.chars()));
- // The optional curly braces
- if source.get(cursor) == Some(&'{') {
- let brace_len = source
- .iter()
- .skip(cursor)
- .take_while(|t| **t != '}')
- .count();
+ let diff = 1 + name.len() + square_content.map(|c| c.len() + 2).unwrap_or_default();
+ if let Some(curly_content) = curly_content {
if is_content_command {
- actions.push_back(CursorAction::PushMaskAndIncBy(cursor - orig_cursor));
- actions.push_back(CursorAction::IncBy(brace_len));
+ actions.push_back(CursorAction::PushMaskAndIncBy(diff + 1));
+ actions.push_back(CursorAction::IncBy(curly_content.len()));
actions.push_back(CursorAction::PushMaskAndIncBy(1));
true
} else {
actions.push_back(CursorAction::PushMaskAndIncBy(
- brace_len + cursor + 1 - orig_cursor,
+ curly_content.len() + diff + 1,
));
true
}
} else {
- actions.push_back(CursorAction::PushMaskAndIncBy(cursor - orig_cursor));
+ actions.push_back(CursorAction::PushMaskAndIncBy(diff));
true
}
}
+fn equation_at_cursor(cursor: usize, source: &[char]) -> Option<usize> {
+ let CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ } = deconstruct_command(&source[cursor..])?;
+
+ if name.eq_ignore_ascii_case_str("begin")
+ && curly_content.is_some_and(|cc| cc.eq_ignore_ascii_case_str("equation"))
+ {
+ let mut diff = 1
+ + name.len()
+ + curly_content.unwrap().len()
+ + square_content.map(|sc| sc.len()).unwrap_or_default();
+
+ loop {
+ if let Some(CommandComponents {
+ name,
+ curly_content,
+ ..
+ }) = deconstruct_command(&source[cursor + diff..])
+ && name.eq_ignore_ascii_case_str("end")
+ && curly_content.is_some_and(|cc| cc.eq_ignore_ascii_case_str("equation"))
+ {
+ break;
+ }
+
+ diff += 1;
+ }
+
+ Some(diff)
+ } else {
+ None
+ }
+}
+
+struct CommandComponents<'a> {
+ /// The command's name.
+ pub name: &'a [char],
+ /// The content of the command's square bracket arguments.
+ pub square_content: Option<&'a [char]>,
+ /// The content of the command's curly bracket arguments.
+ pub curly_content: Option<&'a [char]>,
+}
+
+/// Deconstruct a command into its constituent components.
+/// Assumes the command is at the beginning of the slice.
+/// Returns `None` if not command is present at the expected position.
+fn deconstruct_command<'a>(source: &'a [char]) -> Option<CommandComponents<'a>> {
+ let mut cursor = 0;
+
+ if source.get(cursor) != Some(&'\\') {
+ return None;
+ }
+
+ cursor += 1;
+
+ // The name of the command
+ let name_len = source
+ .iter()
+ .skip(cursor + 1)
+ .take_while(|t| t.is_alphabetic())
+ .count();
+ let name = &source[cursor..cursor + 1 + name_len];
+
+ cursor += name_len + 1;
+
+ // The optional square braces
+ let square_content = if source.get(cursor) == Some(&'[') {
+ cursor += 1;
+
+ let brace_len = source
+ .iter()
+ .skip(cursor)
+ .take_while(|t| **t != ']')
+ .count();
+
+ let content = &source[cursor..cursor + brace_len];
+
+ cursor += brace_len + 1;
+ Some(content)
+ } else {
+ None
+ };
+
+ // The optional square braces
+ let curly_content = if source.get(cursor) == Some(&'{') {
+ cursor += 1;
+
+ let brace_len = source
+ .iter()
+ .skip(cursor)
+ .take_while(|t| **t != '}')
+ .count();
+
+ let content = &source[cursor..cursor + brace_len];
+ Some(content)
+ } else {
+ None
+ };
+
+ Some(CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ })
+}
+
#[derive(Debug)]
enum CursorAction {
IncBy(usize),
@@ -160,7 +238,9 @@ enum CursorAction {
mod tests {
use harper_core::Masker as _;
- use super::Masker;
+ use crate::masker::CommandComponents;
+
+ use super::{Masker, deconstruct_command};
#[test]
fn ignores_many_comment_signs() {
@@ -193,4 +273,63 @@ mod tests {
assert_eq!(mask.iter_allowed(&source).count(), 2)
}
+
+ #[test]
+ fn emits_all_command_components_correctly() {
+ let source: Vec<_> = r"\begin[some]{math}".chars().collect();
+ let CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ } = deconstruct_command(&source).unwrap();
+
+ assert_eq!(name.iter().collect::<String>(), "begin");
+ assert_eq!(square_content.unwrap().iter().collect::<String>(), "some");
+ assert_eq!(curly_content.unwrap().iter().collect::<String>(), "math");
+ }
+
+ #[test]
+ fn emits_command_curly_component_correctly() {
+ let source: Vec<_> = r"\begin{math}".chars().collect();
+ let CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ } = deconstruct_command(&source).unwrap();
+
+ assert_eq!(name.iter().collect::<String>(), "begin");
+ assert_eq!(square_content, None);
+ assert_eq!(curly_content.unwrap().iter().collect::<String>(), "math");
+ }
+
+ #[test]
+ fn emits_command_square_component_correctly() {
+ let source: Vec<_> = r"\begin[some]".chars().collect();
+ let CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ } = deconstruct_command(&source).unwrap();
+
+ assert_eq!(name.iter().collect::<String>(), "begin");
+ assert_eq!(square_content.unwrap().iter().collect::<String>(), "some");
+ assert_eq!(curly_content, None);
+ }
+
+ #[test]
+ fn emits_section_correctly() {
+ let source: Vec<_> = r"\section{Energy and Environment}".chars().collect();
+ let CommandComponents {
+ name,
+ square_content,
+ curly_content,
+ } = deconstruct_command(&source).unwrap();
+
+ assert_eq!(name.iter().collect::<String>(), "section");
+ assert_eq!(square_content, None);
+ assert_eq!(
+ curly_content.unwrap().iter().collect::<String>(),
+ "Energy and Environment"
+ );
+ }
}
diff --git a/harper-tex/tests/run_tests.rs b/harper-tex/tests/run_tests.rs
index cfe8f72ac..396fa50ba 100644
--- a/harper-tex/tests/run_tests.rs
+++ b/harper-tex/tests/run_tests.rs
@@ -43,3 +43,4 @@ create_test!(city.tex, 0);
create_test!(many_tags.tex, 6);
create_test!(many_more_tags.tex, 11);
create_test!(em_dash.tex, 0);
+create_test!(issue_2835.tex, 3);
diff --git a/harper-tex/tests/test_sources/issue_2835.tex b/harper-tex/tests/test_sources/issue_2835.tex
new file mode 100644
index 000000000..b808efff2
--- /dev/null
+++ b/harper-tex/tests/test_sources/issue_2835.tex
@@ -0,0 +1,10 @@
+\documentclass[11pt]{article}
+
+\begin{document}
+\section{TEST}
+errawr % this will work
+this is an \test
+errawr % this will work
+this is an $\test$
+errawr % this will break
+\end{document}