vscode-yaml icon indicating copy to clipboard operation
vscode-yaml copied to clipboard

Syntax highlight for multiline string filled with code

Open macabeus opened this issue 3 years ago • 5 comments

Is your enhancement related to a problem? Please describe.

It's pretty common to use a YAML file having a multiline string filled with code.

For instance, when is working with GitHub Actions, the run field can be filled with a shell script code:

jobs:
  example:
    runs-on: ubuntu-20.04
    steps:
    - name: Blah
      run: |
        echo "foo"
        echo "bar"

But since there is no way to use this extension and have syntax highlight on this part, it became monochrome. It's okayish for simple code, but it makes harder to read a more complex code: image

Describe the solution you would like

We can read the first line for a multiline string and check if there is a shebang. If there is one, it imports the syntax highlight for the given language.

jobs:
  example:
    runs-on: ubuntu-20.04
    steps:
    - name: Blah
      run: |
        #!/bin/sh
        # from now on, this multiline string is syntax highlighted with the shell script grammar
        echo "foo"
        echo "bar"

macabeus avatar Mar 13 '22 14:03 macabeus

A potentially useful idea for an alternative implementation is to use the contentMediaType key to add syntax highlighting for a string if the property is schematized with it.

So for example, with this schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "foo": {
      "title": "Foo",
      "description": "A non-markdown string",
      "type": "string"
    },
    "some_markdown": {
      "title": "Some Markdown",
      "description": "The text of this property is always treated as markdown",
      "type": "string",
      "contentMediaType": "text/markdown"
    }
  }
}

This YAML file would have the some_markdown key highlighted as markdown but not the foo key:

foo: This highlights normally (i.e. it _doesn't_)
some_markdown: |
  This highlights as `markdown`.

This would only work with defined media types, which is a limited list and mostly doesn't include code, but I often find myself wishing my markdown strings in yaml at least had highlighting.

I think the shebang idea would be useful. It's also worth noting that, for GHA files in particular, the GitHub Action extension itself could provide highlighting with injection grammar, since that extension isn't trying to support arbitrary YAML files with a schema. There's also an issue filed in that repo for this same feature.

michaeltlombardi avatar Sep 09 '22 02:09 michaeltlombardi