Setting up a Go template code formatter in VS Code

See Setting up tempate formatting with prettier-plugin-go-template in VS Code · Issue #1656 · gohugoio/hugoDocs · GitHub

The thing with this is, that it breaks a lot of special formats. In my specific pet-peeve case it will “crazy format” any JSON template. So I made it my habit to check in the footer of VSCode in all non-HTML-templates to select “plain text” so Prettier is not messing up the templates. Good for us, that VSCode lets us undo even pre-save-state, so nothing is too broken, but that prettier solution works only on HTML files that not only end on HTML but also are actual HTML formats. (note: yep, my json template ends in HTML, and yep, I know I could rename it to something JSON-related).

I wish there would be some form of “config-comment” that tells VSCode how to treat a file.

I tried playing around with disabling Prettier and enabling several Golang plugins, but the point, in the end, is that VSCode sets the filetype by file extension and if it’s .html you won’t be able to mix things up. It has no way to describe the content type in any way.

Not ranting, just noting the limits of it. Setting those files to .json-endings will format nicely, but then the errors about wrong JSON-formatting are starting.

This sounds like more of an issue with Prettier rather than VS Code. I think you may be able to deal with your particular issue with a custom .prettierrc config if you want to adjust how Prettier works on certain files/folders.

@davidsneighbour I have learned that no setup is perfect and to be a little bit pragmatic …

I have that, but prettier too has no “find the content type of the file” method and is working off regexpes of file names. I think what I need to do is to use the proper filename with the extension in the end of the name being the proper content-type, not *.html.

It’s a convenience thing. One day I will have learnt it :slight_smile:

Yeah, it would be a boring world if everything would be one size fits it all :slight_smile: no worries.

1 Like

By the way, I am not complaining. I just point out the big walls on the side of the race track in case you wish to do cool swerves. VSCode is perfect for working with Hugo and I have yet to meet another program that is of better use.

3 Likes