-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(bash): correctly highlight doctags in comments again (#4234) #4239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wolfgang42
wants to merge
1
commit into
highlightjs:main
Choose a base branch
from
wolfgang42:wolf/bash-fix-todo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<span class="hljs-built_in">echo</span> asdf#qwert yuiop | ||
|
||
<span class="hljs-built_in">echo</span> asdf <span class="hljs-comment">#qwert yuiop</span> | ||
|
||
<span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> this *is* a comment</span> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
echo asdf#qwert yuiop | ||
|
||
echo asdf #qwert yuiop | ||
|
||
# TODO: this *is* a 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use look-behind until v12 because it's a breaking change in older Safari.
How did we do this previously? I also think we do not want any spacing in front of the comment to be scoped as part of the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought as much. This is not a particularly pressing bug for me so I’d be fine waiting for whenever that happens, unless we can find another way to implement this first.
Prior to d78749a it was just using
hljs.HASH_COMMENT_MODE
directly, but that doesn’t handle the whitespace requirement.Agreed; if it was OK to scope it as part of the comment we wouldn’t need the lookbehind, the regex could just be
/(^|\s)#/
. There are also some existing test cases which cover this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was breaking if we just matched it as
/#/
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3918 and associated tests:
highlight.js/test/markup/bash/not-comments.expect.txt
Lines 1 to 3 in d21e285
For example,
echo foo#bar
will printfoo#bar
; the#bar
part is not a comment.Presumably the author of that change had a specific scenario in mind, but I went looking for some real-world examples in the Oil Shell test corpus, and instead found another example of something that it has broken the highlighting for:
...which ought to be highlighted as a comment (since it's not inside a command), even though it doesn't have whitespace before it.
So, while that change was well-meaning, on closer inspection I think very careful reading of POSIX §2.3 would be needed to figure out what the actual requirements are here; at a glance I see at least
|;<>&
; sometimes()
can also be immediately followed by a comment, but (I think) only when indicating a standalone subshell; I haven’t looked at it closely but I think in the general case you may need a full-on stateful parser to determine exactly where in the grammar you are and what should be done with a#
character in that state.Given that, this is maybe a question of determining which kinds of brokenness are most acceptable? I certainly don't figure like wrangling the grammar to make sure that every edge case is covered correctly...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Also cc'ing @iFreilicht as author of that PR in case they have any more insight into their use case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wolfgang42 Thank you! The important part for me is that a command like
nix run nixpkgs#btop
should be correctly highlighted, i.e.#btop
is not a comment in this case, andnixpkgs#btop
is a single token. POSIX-compatible shells require a comment to be at the beginning of a line or be preceeded by whitespace, or at least I thought that was the whole of it at the time.