-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(bash): keep heredoc continuations highlighted #4415
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,16 +50,6 @@ export default function(hljs) { | |
| } | ||
| } | ||
| ); | ||
| const HERE_DOC = { | ||
| begin: /<<-?\s*(?=\w+)/, | ||
| starts: { contains: [ | ||
| hljs.END_SAME_AS_BEGIN({ | ||
| begin: /(\w+)/, | ||
| end: /(\w+)/, | ||
| className: 'string' | ||
| }) | ||
| ] } | ||
| }; | ||
| const QUOTE_STRING = { | ||
| className: 'string', | ||
| begin: /"/, | ||
|
|
@@ -368,27 +358,60 @@ export default function(hljs) { | |
| "whoami", | ||
| "yes" | ||
| ]; | ||
| const BASH_KEYWORDS = { | ||
| $pattern: /\b[a-z][a-z0-9._-]+\b/, | ||
| keyword: KEYWORDS, | ||
| literal: LITERALS, | ||
| built_in: [ | ||
| ...SHELL_BUILT_INS, | ||
| ...BASH_BUILT_INS, | ||
| // Shell modifiers | ||
| "set", | ||
| "shopt", | ||
| ...ZSH_BUILT_INS, | ||
| ...GNU_CORE_UTILS | ||
| ] | ||
| }; | ||
| let heredocDelimiter = ""; | ||
| const HERE_DOC_BODY = { | ||
| className: 'string', | ||
| begin: /\n/, | ||
| end: /^\t*(\w+)$/m, | ||
| endsParent: true, | ||
| 'on:end': (m, resp) => { | ||
| if (m[1] !== heredocDelimiter) resp.ignoreMatch(); | ||
| } | ||
| }; | ||
| const HERE_DOC = { | ||
| begin: /<<-?\s*(\w+)/, | ||
| 'on:begin': (m) => { heredocDelimiter = m[1]; }, | ||
| end: /\n/, | ||
| returnEnd: true, | ||
| keywords: BASH_KEYWORDS, | ||
| contains: [ | ||
| ARITHMETIC, | ||
| COMMENT, | ||
| PATH_MODE, | ||
| QUOTE_STRING, | ||
| ESCAPED_QUOTE, | ||
| APOS_STRING, | ||
| ESCAPED_APOS, | ||
| VAR | ||
|
Comment on lines
+391
to
+399
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of this is because of the pipe and piping the string into any sequence of bash, right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not only pipes. The same split is needed for any shell syntax after the delimiter word, for example |
||
| ], | ||
| starts: { | ||
| contains: [ | ||
| HERE_DOC_BODY | ||
| ] | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| name: 'Bash', | ||
| aliases: [ | ||
| 'sh', | ||
| 'zsh' | ||
| ], | ||
| keywords: { | ||
| $pattern: /\b[a-z][a-z0-9._-]+\b/, | ||
| keyword: KEYWORDS, | ||
| literal: LITERALS, | ||
| built_in: [ | ||
| ...SHELL_BUILT_INS, | ||
| ...BASH_BUILT_INS, | ||
| // Shell modifiers | ||
| "set", | ||
| "shopt", | ||
| ...ZSH_BUILT_INS, | ||
| ...GNU_CORE_UTILS | ||
| ] | ||
| }, | ||
| keywords: BASH_KEYWORDS, | ||
| contains: [ | ||
| KNOWN_SHEBANG, // to catch known shells and boost relevancy | ||
| hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <span class="hljs-built_in">echo</span> <span class="hljs-string">"ok"</span> <<-EOF | <span class="hljs-built_in">cat</span><span class="hljs-string"> | ||
| Hello | ||
| EOF</span> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| echo "ok" <<-EOF | cat | ||
| Hello | ||
| EOF |
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 is the requirement for heredoc naming in Bash? Could this be tightened up any further than
\w+?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.
I think this needs broadening rather than tightening. Bash takes a
wordafter<<and applies quote removal; it is not limited to\w+.For example, this branch captures only
ENDhere and never finds the real terminator, soecho donestays inside the string:<<'EOF'is valid too, but is not recognized as a heredoc by this matcher. Could we add both as markup cases before settling on the delimiter match?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.
Sure. Add all the cases we know of, then try to get the highlighted correctly - that's the way to do it.