Skip to content
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

Syntax highlight for multiline string filled with code #714

Open
macabeus opened this issue Mar 13, 2022 · 3 comments
Open

Syntax highlight for multiline string filled with code #714

macabeus opened this issue Mar 13, 2022 · 3 comments

Comments

@macabeus
Copy link

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"
@michaeltlombardi
Copy link

michaeltlombardi commented Sep 9, 2022

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.

@connorjs
Copy link

connorjs commented Aug 5, 2024

Does anyone know if comments are included in the YAML AST and, if so, if they could be used as a hint for syntax highlighter?

For example, I was thinking of lang=sh or lang=js, which some markdown renderers use.

#lang=sh
foo: |
  echo hi

#lang=js
bar: |
  console.log('hi')

I like the shebang idea, but I feel that would require O(n) in number of supported languages.

I also like the media type approach. It would provide a way to configure it for some use cases. However, I do like the idea of an in-document hint.

Again, I have no idea about how the YAML AST (and dependent on the parser I guess) handles the comment token in my proposal.

Context: Thinking about this for GitHub workflows/actions specifically.


Edit: Hypothetically, the #lang=js approach could also syntax highlight a one-line or chomp string, where the inclusion of prefix (like shebang) would be invalid.

#lang=sh
baz: >-
  command
  with
  args

#lang=js
qux: console.log('hi')

@connorjs
Copy link

connorjs commented Aug 9, 2024

I came across Volar.js which seeks to simplify "embedded languages" concept. I also found their (1.5 year ago) blog post interesting: Volar: a New Beginning.

Sharing in case it or its concept inspires others for solutioning. 🤞🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants