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

[Go] Add support for YAML Go templates #4014

Merged
merged 4 commits into from
Jul 25, 2024

Conversation

deathaxe
Copy link
Collaborator

This PR...

  1. fixes missing source.json.go scope in snippet selectors
  2. adds YAML (Go).sublime-syntax with YAML highlighted Go Templates

FichteFoll
FichteFoll previously approved these changes Jul 24, 2024
Copy link
Collaborator

@FichteFoll FichteFoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well for me.

@FichteFoll
Copy link
Collaborator

As for where this is used: I've come across YAML with Go templates in the context of Helm and Rancher.

@FichteFoll
Copy link
Collaborator

FichteFoll commented Jul 24, 2024

Actually, multi-line comments in blocks are not recognized (not sure if this is a common syntax):

{{/* 
Spread a key-value map into the "env" list 
*/}}

Edit: Turns out this is a limitation of the match-string-templates context in the Go syntax, which requires the closing punctuation to be on the same line. match-text-templates does not have that requirement but it also does not remove the string scope from the scope stack.


Furthermore, I think the string after define should probably be an entity.name subscope, i.e.

{{/* Spread a key-value map into the "env" list */}}
{{- define "chart.env-vars" -}}
{{- range $key, $val := . }}
- name: {{ $key }}
  value: {{ quote $val }}
{{- end }}
{{- end -}}

Currently it's a string. Though, this is more of a Go issue since it stems from the existing Go template contexts being included.

keith-hall
keith-hall previously approved these changes Jul 24, 2024
@deathaxe

This comment was marked as duplicate.

This commit...

1. adds a special `match-block-string-templates` with out closing `}}` checks,
   to support interpolation within multi-line strings (for use in YAML).

2. adds tests for multi-line comments in HTML and Markdown templates.
@deathaxe deathaxe dismissed stale reviews from keith-hall and FichteFoll via 3b7f2b7 July 24, 2024 14:47
@deathaxe
Copy link
Collaborator Author

Now using a string interpolation context without safe guards (no check for closing }}) in YAML. As we are explicitly using it in a template syntax, it should be safe enough.

@deathaxe
Copy link
Collaborator Author

... the string after define should probably be an entity.name subscope

Well, Go template syntax doesn't treat certain commands' arguments special by any means, currently to keep it lean.

According to https://docs.gomplate.ca/syntax/#nested-templates define is used do define inline templates. The counter-command template also treats first argument (the template name) as plain string.

grafik

Haven't investigeted which else commands would be affected.

That's certainly a separate feature request, not related with supporting YAML.

@FichteFoll
Copy link
Collaborator

That's certainly a separate feature request, not related with supporting YAML.

Yes, certainly.

FichteFoll
FichteFoll previously approved these changes Jul 24, 2024
@FichteFoll
Copy link
Collaborator

FichteFoll commented Jul 25, 2024

The lines with a sequence of : get scoped as meta.mapping.key by the YAML syntax even though the part where this occurs is part of the embedded section:

{{/*
Unroll a list of default environment variable settings
*/}}
{{- define "EnvironmentsHelper.defaults" -}}
{{- if .Values.environmentDefaults -}}
{{- range $key, $value := .Values.environmentDefaults }}
{{- printf "- name: %s" $key | nindent 20 -}}
{{- printf "  value: \"%v\"" $value | nindent 20 -}}
{{- end -}}
{{- end -}}
{{- end -}}

image

This is probably non-trivial and might require a refactoring of the YAML syntax since printf "- name is a valid plain scalar that can be interpreted as a key. I would be fine with not fixing this in this PR. Also note that because of the lookahead the entire line is scoped as a key.

This commit excludes interpolation content from YAML key detection patterns.

Everything within interpolation is ignored, even if it looks like a YAML key.
@deathaxe
Copy link
Collaborator Author

I've tweaked the key lookaheads so they ignore anything within {{ ... }}.

@deathaxe deathaxe merged commit 22107e1 into sublimehq:master Jul 25, 2024
2 checks passed
@deathaxe deathaxe deleted the pr/go/yaml-templates branch July 25, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants