vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Feature Request: Go Templates Support

Open timkofu opened this issue 5 years ago • 19 comments

Would it be possible to add formatting, autocomplete, and colorization for Go templates? (.tmpl)

timkofu avatar Sep 04 '20 10:09 timkofu

The prior discussion history is here https://github.com/microsoft/vscode-go/issues/228

We hope gopls can support formatting and autocompletion (https://github.com/golang/go/issues/36911)

How to provide syntax highlighting based on textmate or semantic highlighting provided through LSP future release - that still need to be figured out.

/cc @stamblerre

hyangah avatar Sep 04 '20 12:09 hyangah

How to provide syntax highlighting based on textmate or semantic highlighting provided through LSP future release - that still need to be figured out.

I wonder if we could do this through LSP's upcoming semantic tokens feature: https://microsoft.github.io/language-server-protocol/specifications/specification-3-16/#textDocument_semanticTokens.

stamblerre avatar Sep 04 '20 16:09 stamblerre

@hyangah: Should this be in the gopls on by default project? It seems to me like this isn't necessary to turn on gopls by default.

stamblerre avatar Nov 10 '20 23:11 stamblerre

Change https://golang.org/cl/284095 mentions this issue: [release] package.json: do not claim for .tmpl files in stable version

gopherbot avatar Jan 15 '21 03:01 gopherbot

Any reason why *.tmpl extension is chosen? Most of the time I saw either *.gohtml or just *.html when Go templates used on web. And when working with html templates one would want both HTML and Go language features be present in editor at the same time, otherwise you'd have to manually switch language back and forth.

inliquid avatar Apr 04 '21 16:04 inliquid

We are adding incremental support for templates, starting with files with the .tmpl extension (https://golang.org/cl/297871). In the future, we will expand this to include other file extensions and to include templates within strings in Go files.

stamblerre avatar Apr 04 '21 19:04 stamblerre

Any way to make vscode-go understand other extensions at an early stage? I'm building html templates and I can test how new features work in real life, but in order to do so, if I understand correctly, I would have to rename all templates to have *.tmpl? That would be not very convenient.

inliquid avatar Apr 04 '21 19:04 inliquid

Change https://golang.org/cl/342069 mentions this issue: package.json: recognize *.*tmpl as go template files

gopherbot avatar Aug 13 '21 18:08 gopherbot

Change https://golang.org/cl/371914 mentions this issue: src/goLanguageServer: enable tmpl (go template) processing

gopherbot avatar Dec 14 '21 20:12 gopherbot

@inliquid in vscode, you can Change language mode (from the command palette). You can adjust for a specific file or for all files matching the current extension (I think there is also a JSON settings for this).

oliverpool avatar Dec 17 '21 08:12 oliverpool

@oliverpool I'm aware of this option, but not sure how it helps. Generally I would like to have support of go templates when working on *.html files with HTML language features enabled by default. Switching from HTML language to templates back and forth is far from good user experience.

Example: please see how it's done in Goland.

inliquid avatar Dec 17 '21 12:12 inliquid

Sorry, I only replied to this part of your comment:

I would have to rename all templates to have *.tmpl

I agree with you that the supporting a mix of HTML and go template would greatly improve the developer experience (I am using esbenp.prettier-vscode with https://www.npmjs.com/package/prettier-plugin-go-template for now)

oliverpool avatar Dec 17 '21 14:12 oliverpool

It looks like the referenced https://github.com/golang/go/issues/36911 has been closed, does that mean template highlighting should work now? Sorry, I'm new to working with golang, and just trying to figure out how to get syntax highlighting in my html go templates. Whether that means using a specific naming convention or enabling a setting...

IanVS avatar Dec 20 '21 18:12 IanVS

gopls v0.7.5 has template support for files with language id gotmpl. (Go Template File)

VSCode Go extension v0.31.0 will classify files with extension *.tmpl or *.gotmpl as the gotmpl language file by default. You can override the default by eithre using

         "files.associations": {
             "*.tmpl": "plaintext",
             "*.mycoolgotemplate": "gotmpl"
         }

We are very close to v0.31.0 - RC1 is available for testing at https://github.com/golang/vscode-go/releases/tag/v0.31.0-rc.1

Supported features are

  • syntax highlighting (when used in combination with"gopls": { "ui.semanticTokens": true })
  • some autocompletion
  • diagnostics (if you want the diagnostics on all files, not only open files, consider setting "gopls" : { "build.templateExtensions": true } in your settings.json.

Templates embedded in .go file, diagnostics beyond the template file itself (e.g. detecting runtime error such as validation of data, associated templates, custom functions, ...) are not in the scope of supported features.

cc @pjweinb @findleyr

hyangah avatar Jan 24 '22 20:01 hyangah

Change https://golang.org/cl/380615 mentions this issue: docs/features.md: discuss default formatting behavior and template support

gopherbot avatar Jan 25 '22 01:01 gopherbot

Just curious on how this work is going? I've been messing around with the HTML templating support for Go and have found that I still can't quite get it to work how I want.

Given the following basic HTML template:

<!-- test.html -->
<!DOCTYPE html>
    <head>
    </head>
    <body>
        <div>
            {{ .SomeVal }}
        </div>
    </body>
</html>

and the following settings.json file:

// .vscode/settings.json
{
    "editor.formatOnSave": true,
}

upon saving the file, the default HTML formatter runs and works as expected. It even deals with {{ x }} correctly. (Also works for any other template syntax I can throw at it):

image

However, once I change my settings as per the documentation:

{
    "editor.formatOnSave": true,
    "files.associations": {
        "*.html": "gotmpl"
    },
    "gopls": {
        "build.templateExtensions": [
            ".html"
        ],
    },
}

This happens:

image

Syntax highlighting for the templating handlebars works as expected, but not for the HTML. Also, because its not considered to be an HTML file, none of the formatters run. This also applies to other embedded languages like JS:

image

At the moment I'm stuck not using the go templating features because of this. Usually, I would just accept this and continue to use a regular HTML formatter as it works with most templating features. However, the default VSCode beautifier has its own issues when it comes to formatting template handlebars in embedded Javascript:

image

I'd be grateful if you're able to share any existing solutions to any of the above and if not, whether these are known problems and whether they are on the roadmap. Thanks! :)

pd93 avatar Mar 16 '22 13:03 pd93

I don't understand what vscode does when there are conflicting rules about what language a file is (in this case you want both 'gotmpl' and 'html'). I'll try to find someone more expert than I am.

On Wed, Mar 16, 2022 at 9:18 AM Pete Davison @.***> wrote:

Just curious on how this work is going? I've been messing around with the HTML templating support for Go and have found that I still can't quite get it to work how I want.

Given the following basic HTML template:

<head>
</head>
<body>
    <div>
        {{ .SomeVal }}
    </div>
</body></html>

and the following settings.json file:

// .vscode/settings.json{ "editor.formatOnSave": true,}

upon saving the file, the default HTML formatter runs and works as expected. It even deals with {{ x }} correctly. (Also works for any other template syntax I can throw at it):

[image: image] https://user-images.githubusercontent.com/9294862/158594024-4431f27a-d8c3-462f-960a-5aab54409372.png

However, once I change my settings as per the documentation https://github.com/golang/tools/blob/master/gopls/doc/features.md#template-files :

{ "editor.formatOnSave": true, "files.associations": { "*.html": "gotmpl" }, "gopls": { "build.templateExtensions": [ ".html" ], }, }

This happens:

[image: image] https://user-images.githubusercontent.com/9294862/158594981-bbf85910-8dea-4d7e-8353-b487ca72554d.png

Syntax highlighting for the templating handlebars works as expected, but not for the HTML. Also, because its not considered to be an HTML file, none of the formatters run. This also applies to other embedded languages like JS:

[image: image] https://user-images.githubusercontent.com/9294862/158595631-b66e67b1-1c0d-44a6-aa41-edac5d0780da.png

At the moment I'm stuck not using the go templating features because of this. Usually, I would just accept this and continue to use a regular HTML formatter as it works with most templating features. However, the default VSCode beautifier has its own issues when it comes to formatting template handlebars in embedded Javascript:

[image: image] https://user-images.githubusercontent.com/9294862/158597438-7eb79cdb-8769-4098-abee-ee4f63c4778a.png

I'd be grateful if you're able to share any existing solutions to any of the above and if not, whether these are known problems and whether they are on the roadmap. Thanks! :)

— Reply to this email directly, view it on GitHub https://github.com/golang/vscode-go/issues/609#issuecomment-1069119156, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJIAI32P57HKSO5ILKJJLTVAHNR5ANCNFSM4QX6A2YA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

pjweinb avatar Mar 16 '22 14:03 pjweinb

In VSCode, I toggle between languages using the language tab at the bottom of the window rather than the settings.json. https://github.com/golang/vscode-go/blob/master/docs/features.md#go-template-syntax-highlighting

hyangah avatar Mar 18 '22 17:03 hyangah

I do not know if this can help but I'm using successfully this VSCode extension: https://github.com/jinliming2/vscode-go-template.

I had a problem today: https://github.com/jinliming2/vscode-go-template/issues/16.

frederikhors avatar Jun 21 '22 08:06 frederikhors

Another approach with Prettier & Emmet.

Add this to user settings (cmd , -> Open User Settings)

"files.associations": {
    "*.tmpl": "html"
  },

cmd + shift + p: change language mode -> configure

image image

cmd+ alt + f: It will be like this image

lehuuthoct avatar Mar 14 '23 00:03 lehuuthoct

Since this is some support for this in gopls now, can this be worked on (or documented, if it's just a matter of documentation?) https://github.com/golang/tools/blob/master/gopls/doc/features.md#template-files

tmc avatar Sep 11 '23 21:09 tmc

That's already documented. https://github.com/golang/vscode-go/wiki/features#go-template-syntax-highlighting (commented in https://github.com/golang/vscode-go/issues/609#issuecomment-1072608480)

It is under syntax highlighting, but some completion or simple diagnostics are offered.

The only reason we left this issue open was some users wanted to see the go template support coexist along with other languages (for example html? in https://github.com/golang/vscode-go/issues/609#issuecomment-1069119156), outlined in https://code.visualstudio.com/api/language-extensions/embedded-languages#language-services or ideas discussed in https://github.com/microsoft/vscode/issues/1751. But none of them seem like a trivial project.

Will close this issue since we don't have a plan to work on this. If a new easy way to handle this universal language injection from the extension side, please let us know by opening a new issue with the details.

Thanks.

hyangah avatar Sep 11 '23 22:09 hyangah

It doesn't seem like the state of integration supports intelligent autocomplete, I think another issue to improve the quality of completions in templates would be appropriate.

tmc avatar Sep 12 '23 20:09 tmc