(Neovim) Proper formatting of Go conditionals in HTML files?

Hoping someone here has had this issue and solved formatting these templates. Currently, my setup will format on save, but in a terrible way. Example, say I have some variables defined like so:

{{/* Variables */}}
{{- $respSizes := slice "375" "640" "960" "1280" "1600" "1920" -}}
{{- $src := .Params.image -}}
{{- $alt := .Params.alt -}}
{{- $cap := .Params.caption -}}
{{- $dataSzes := "(min-width: 1024px) 50vw, 100vw" -}}

When I save, if everything in the file looks good with no errors, it will format lines joined together, but at parts that make no sense, sometimes causing Hugo errors

{{/* Variables */}} {{- $respSizes := slice "375" "640" "960" "1280" "1600"
"1920" -}} {{- $src := .Params.image -}} {{- $alt := .Params.alt -}} {{- $cap :=
.Params.caption -}} {{- $dataSzes := "(min-width: 1024px) 50vw, 100vw" -}}

This happens in any HTML file with some Go templating in it. And in any part of the file.

Using LazyVim, I have gotmpl and go installed and in ensure_installed in Treesitter. I have prettier formatting with Conform. Prettier RC file currently like so

{
  "singleQuote": false,
  "bracketSpacing": true,
  "htmlWhitespaceSensitivity": "ignore",
  "singleAttributePerLine": true,
  {
    "plugins": ["prettier-plugin-go-template"],
    "overrides":
      [{ "files": ["*.html"], "options": { "parser": "go-template" } }],
  },
}

Any help or being pointed in the right direction is very much appreciated! Thanks

Turns out that I needed to do the same thing as this person, which was to convert my .prettierrc to be a yaml file.

Just for others coming here.

This seems not to be a general issue with prettier and the go-html-template plugin but special ro the used Editor

I have that combination running on

  • windows 11
  • vscode current version
  • vscode prettier plugin
  • shell is powershell 7.4
  • global prettier installation (npm)
  • .prettierrc in JSON format

Works in vscode and cmdline

Project local install also works but has issues with multirooted vscode workspaces.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.