Aaron
Aaron

Reputation: 7038

Custom syntax highlighting for certain YAML fields with VSCode

Our infrastructure uses YML files to configure various processes. For instance, a (very simplified) configuration block might look like the following:

process:
   my-process:
      requires: "@other-process::parameter"
      dockerfile: |
         FROM alpine
         RUN apk add git
      script: |
        #!/bin/bash
        if [ ! -f $file ]; then echo header > $file; fi

We use visual studio code to create/edit these file, and would really like to have custom syntax highlighting to help read them. For instance, in the above example, it would be great if VSCode would automatically:

I know that VSCode uses TextMate grammar definitions for syntax highlighting, but writing one myself is well beyond my level of expertise. Is there an easier way to inject custom syntax highlighting for specific fields?

While VSCode is our default editor, a solution involving a different text editor would be fine.

Upvotes: 10

Views: 3770

Answers (1)

Matt Bierner
Matt Bierner

Reputation: 65393

Two approaches:

  • Use decorators. These allow you to change the color and style of specific ranges in a document dynamically. Here's an example decorator extension

  • Write a textmate injection grammar. (see this comment). This is more advanced since it requires a lot of knowledge of textmate grammars. It is also less flexible but will be much more performant.

Upvotes: 4

Related Questions