fix(dockerfile): correct bash syntax highlighting

when a dockerfile contains several RUN statements, the syntax
highlighting would sometimes run over and encompass dockerfile
statements after the RUN command (typically everything between the first
and last RUN statement, though the bash syntax bleed can be broken by
terminating a RUN statement with a semicolon, or a space 🙄)

we fix this by using a slightly different approach to recognizing /
grouping (the content of) RUN commands:

- instead of having shell_command at the top level of the block we
  changed, we're using run_instruction - so parsing happens per `RUN`
  statement, which is really what we want
- inseade of `injection.combined`, we use `injection.include-children`,
  which combines all the shell fragments (the text parts of a shell
  command, including after newline continuations), which is really what
  we want, as that's all the text of a RUN statement

that fixes the highlight bleed, and preserves correct highlighting for
each RUN statement.

we also add a regression test for highlighting.

Closes #6530, #6975
This commit is contained in:
Valentin Krasontovitsch 2026-02-25 13:57:40 +01:00
parent 493890b87a
commit b760eef0ba
No known key found for this signature in database
3 changed files with 29 additions and 3 deletions

View file

@ -1,10 +1,10 @@
((comment) @injection.content
(#set! injection.language "comment"))
((shell_command
(shell_fragment) @injection.content)
((run_instruction
(shell_command) @injection.content)
(#set! injection.language "bash")
(#set! injection.combined))
(#set! injection.include-children))
((run_instruction
(heredoc_block) @injection.content)

View file

@ -0,0 +1,21 @@
FROM python AS base
# <- @keyword
RUN pip install requests && \
# <- @keyword
# ^^^ @function.call
# ^^^ @variable.parameter
echo all done
# ^^^^ @function.call
FROM alpine
# <- @keyword
# <- @!variable.parameter
RUN apk add openssh-client
# <- @keyword
# ^^^ @function.call
CMD ["/usr/bin/sh"]
# <- @keyword
# <- @!variable.parameter

View file

@ -1,6 +1,11 @@
FROM foo
RUN bar
# ^ @bash
FROM haha
# ^ @!bash
RUN \
baz
# ^ @bash
COPY apt apt
# ^ @!bash