Skip to content

fix: LaTeX diagnostics halt after unmatched inline math ($) inside a macro - #3835

Open
mvanhorn wants to merge 1 commit into
Automattic:masterfrom
mvanhorn:fix/3791-tex-inline-math-halt
Open

fix: LaTeX diagnostics halt after unmatched inline math ($) inside a macro#3835
mvanhorn wants to merge 1 commit into
Automattic:masterfrom
mvanhorn:fix/3791-tex-inline-math-halt

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Issues

Fixes #3791

Description

The tex masker lives in harper-tex/src/masker.rs. Its top-level create_mask loop reaches \caption{...} and calls deconstruct_command, whose curly-content scan stops at the FIRST } (the inner } of N_{THC}), truncating the caption argument so the cursor lands on the second, now-unpaired $. The loop then calls math_mode_at_cursor on that lone $; because there is no closing $ left in the document it counts every remaining character and returns remaining + 2, so the following PushMaskAndIncBy masks the entire rest of the file - dropping all later tokens and hence all diagnostics. Primary fix: make math_mode_at_cursor require a matching closing $ - when no closing $ follows the opener, return None (or mask only the single $) instead of consuming to end-of-file, mirroring the running-off-the-end guard already present in equation_at_cursor. This restores diagnostics for the remainder of the document exactly as the reporter expects, and is a minimal, convergent change. The deeper root cause (naive first-} curly-argument parsing in deconstruct_command not balancing nested braces) may optionally be hardened in the same function, but the reported halt is fully resolved by the math_mode_at_cursor guard, so scope stays tight to avoid regressing existing tex snapshots.

Demo

N/A - behavioral fix; validated by the tests below.

How Has This Been Tested?

  • Happy path: add harper-tex/tests/test_sources/issue_3791.tex reproducing the issue snippet (two errawr typos around a \caption{ ... ($N_{THC}$ block) and a create_test!(issue_3791.tex, N) entry in run_tests.rs asserting both typos are still linted (rest-of-file diagnostics no longer suppressed), following the existing issue_2835.tex precedent.
  • Unit test in the #[cfg(test)] mod tests block of masker.rs: a lone/unmatched $ does not mask the remainder - text after the stray $ remains in the allowed spans of the mask.
  • Edge cases: a correctly paired top-level $...$ is still fully masked; \$ ... \$ and \( ... \) continue to work; empty $$ and a trailing $ at end-of-file neither panic nor over-consume.
  • Error path: create_mask does not panic on a document that ends with an unclosed $ (extends the existing trailing-backslash non-panic guarantees).

AI Disclosure

  • I am a human and didn't use any AI.
  • I used LLM features of my editor, but not an agent.
  • I used an AI agent interactively.
  • I am an agent or I got an agent to do the work autonomously.

If Your PR Implements or Enhances a Linter

  • I made up the sentences in the unit tests.
  • The sentences in the unit tests were generated by an AI.
  • I'm using examples from the bug report / feature request.
  • I collected real-world sentences for the unit tests.

Checklist

  • I have performed a self-review of my own code
  • I have added tests to cover my changes
  • I have considered splitting this into smaller pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tex Support for TeX and LaTeX document parsing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LaTeX: Diagnostics halt for the rest of the file after inline math ($...$) inside a macro

2 participants