micro icon indicating copy to clipboard operation
micro copied to clipboard

fix: bash heredoc ending prematurely on single-word content lines

Open molloyzak13 opened this issue 5 months ago • 0 comments

A bash heredoc like this gets its syntax highlighting cut off at "DoesNotWork":

cat <<HELLO
# comment
DoesNotWork

HELLO

The heredoc end regex in sh.yaml (^[^\s]+[A-Za-z0-9]+$) matched any line that was a single word, regardless of whether it was actually the delimiter. So a content line with one word and nothing after it ended the heredoc, turning the rest of the block into un-highlighted text. The same bug fires for any line where a word sits alone without spaces around it and no trailing words.

The fix has two parts:

  1. Engine-side: added a delimiter tracking mechanism to the highlighter. The region struct gets a delimiter field. When a region starts with a regex that has a capture group (like the heredoc start), regionWithDelimiter() captures the matched delimiter word. Then in highlightRegion(), when the end regex fires, the matched text is verified against the stored delimiter before the region actually closes. This means the end regex only ever closes the region that the start regex opened, which is how heredocs are supposed to work.

  2. Syntax-side: updated the heredoc rules in sh.yaml. The start regex now captures the delimiter into a group: <<-?([A-Za-z_][A-Za-z0-9_]*). The end regex is simplified to match identifiers: ^([A-Za-z_][A-Za-z0-9_]*). The engine's delimiter check handles the rest.

Added a test in pkg/highlight/heredoc_bug_test.go that loads the sh.yaml syntax, highlights a heredoc with a single-word content line, and asserts the line stays inside the constant.string group and that the real delimiter line properly ends the region. Without the fix the test fails: "DoesNotWork" gets assigned group 0 (un-highlighted) instead of constant.string. With the fix it passes cleanly. All existing tests pass too.

Closes #4114

molloyzak13 avatar Jun 15 '26 11:06 molloyzak13