Conversation
The regex is a bit gnarly, so to break it down, the old regex has two
alternatives:
- `^#\{1,6}`
Matches 1–6 opening hashes at the start of the line.
- `\\\@<!#\+\(\s*.*$\)\@=`
Matches one or more unescaped hashes when followed by `\s*.*$`
It's trying to match the `##` at the end of a line, to allow
highlighting code like this:
```markdown
## Some heading ##
```
The problem is that this second condition is effectively always true
(to see, consider that the regex engine could choose to apply `\s`
zero times, then `.*` until the end of the line).
That bug causes something like `vim-pandoc#404` in
an ATX heading to treat the `#` as an additional heading marker.
The new regex fixes that by ensuring that there's at least one space
character before the `#`, and ensuring that the match highlighting does
not include that required space character (with `\zs`).
So the full replacement is now:
```vim
\(^#\{1,6}\|\s\zs\\\@<!#\+\s*$\)
```
And its second alternative works like this:
- `\s` — whitespace before the closing hashes
- `\zs` — begin the highlighted match here, excluding that whitespace
- `\\\@<!` — the hash is not escaped
- `#\+` — one or more hashes
- `\s*$` — only optional whitespace may follow before the line ends
The test fixture adds an example, but to my knowledge these are just
used for visual inspection, there's nothing asserting these tests.
Screenshot before:
Screenshot after:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The regex is a bit gnarly, so to break it down, the old regex has two
alternatives:
^#\{1,6}Matches 1–6 opening hashes at the start of the line.
\\\@<!#\+\(\s*.*$\)\@=Matches one or more unescaped hashes when followed by
\s*.*$It's trying to match the
##at the end of a line, to allowhighlighting code like this:
## Some heading ##The problem is that this second condition is effectively always true
(to see, consider that the regex engine could choose to apply
\szero times, then
.*until the end of the line).That bug causes something like
vim-pandoc/vim-pandoc-syntax#404inan ATX heading to treat the
#as an additional heading marker.The new regex fixes that by ensuring that there's at least one space
character before the
#, and ensuring that the match highlighting doesnot include that required space character (with
\zs).So the full replacement is now:
And its second alternative works like this:
\s— whitespace before the closing hashes\zs— begin the highlighted match here, excluding that whitespace\\\@<!— the hash is not escaped#\+— one or more hashes\s*$— only optional whitespace may follow before the line endsThe test fixture adds an example, but to my knowledge these are just
used for visual inspection, there's nothing asserting these tests.