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

Feature Request: Go Templates Support #609

Closed
timkofu opened this issue Sep 4, 2020 · 25 comments
Closed

Feature Request: Go Templates Support #609

timkofu opened this issue Sep 4, 2020 · 25 comments
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@timkofu
Copy link

timkofu commented Sep 4, 2020

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

@hyangah
Copy link
Contributor

hyangah commented Sep 4, 2020

The prior discussion history is here microsoft/vscode-go#228

We hope gopls can support formatting and autocompletion (golang/go#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 hyangah added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 4, 2020
@stamblerre
Copy link
Contributor

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.

@hyangah hyangah added this to To Triage in vscode-go: gopls by default Sep 17, 2020
@hyangah hyangah moved this from Needs Triage to Non-critical in vscode-go: gopls by default Sep 17, 2020
@stamblerre
Copy link
Contributor

@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 stamblerre moved this from Non-critical to Needs Triage in vscode-go: gopls by default Nov 11, 2020
@stamblerre stamblerre removed this from Needs Triage in vscode-go: gopls by default Nov 11, 2020
@gopherbot
Copy link
Collaborator

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

gopherbot pushed a commit that referenced this issue Jan 15, 2021
https://go-review.googlesource.com/c/vscode-go/+/281392 is a prep work
for template support. However, the handling is not available in gopls
yet, and LSP registration is activated only in the preview mode (per
https://go-review.googlesource.com/c/vscode-go/+/283639)

This CL is the remaining task from cl/283639 to disable the experimental
feature from the stable release. We will revert this change when gopls
is ready to handle templates.

Update #609

Change-Id: I72d04b11f14fde25b7ad64e6a9a82f7c20e32b4c
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/284095
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
@inliquid
Copy link

inliquid commented Apr 4, 2021

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.

@stamblerre
Copy link
Contributor

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.

@inliquid
Copy link

inliquid commented Apr 4, 2021

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.

@gopherbot gopherbot added this to the Untriaged milestone Apr 8, 2021
@stamblerre stamblerre modified the milestones: Untriaged, On Deck Apr 9, 2021
@gopherbot
Copy link
Collaborator

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

gopherbot pushed a commit that referenced this issue Aug 16, 2021
filenamePatterns allows glob matching.
Gopls processes any '.*tmpl' files as go template file.

For #609

Change-Id: I73334c1fdb7d23544591bbbdb5a3ad78397fa227
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/342069
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Trust: Peter Weinberger <pjw@google.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
@gopherbot
Copy link
Collaborator

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

gopherbot pushed a commit that referenced this issue Dec 14, 2021
And narrow the set of file extensions to match the current
gopls default (.tmpl and .gotmpl). We will keep working on
settings plumbing.

For #609

Change-Id: I8d613e52dc2eeecb072339d53624178a5ed01ecb
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/371914
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
Trust: Peter Weinberger <pjw@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
@hyangah hyangah modified the milestones: On Deck, v0.31.0 Dec 16, 2021
@oliverpool
Copy link

@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).

@inliquid
Copy link

@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.

@oliverpool
Copy link

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)

@IanVS
Copy link

IanVS commented Dec 20, 2021

It looks like the referenced golang/go#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...

@hyangah
Copy link
Contributor

hyangah commented Jan 24, 2022

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

@gopherbot
Copy link
Collaborator

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

hyangah added a commit to hyangah/vscode-go that referenced this issue Jan 27, 2022
…pport

We already have a section in the advanced.md doc abount default formatting
and imports behavior. I hope this additional reference helps users to discover
the control knob.

Didn't mention completion, diagnostics for Go template files yet.
We need to update the diagnostics section to discuss gopls's diagnostics
feature.

Updates golang#609

Change-Id: Ib2e695d847c56bd4f562d91d2938cb5c1a032764
gopherbot pushed a commit that referenced this issue Jan 31, 2022
…pport

We already have a section in the advanced.md doc abount default formatting
and imports behavior. I hope this additional reference helps users to discover
the control knob.

Didn't mention completion, diagnostics for Go template files yet.
We need to update the diagnostics section to discuss gopls's diagnostics
feature.

Updates #609

Change-Id: Ib2e695d847c56bd4f562d91d2938cb5c1a032764
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/380615
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Peter Weinberger <pjw@google.com>
@pd93
Copy link

pd93 commented Mar 16, 2022

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! :)

@pjweinb
Copy link

pjweinb commented Mar 16, 2022 via email

@hyangah
Copy link
Contributor

hyangah commented Mar 18, 2022

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

@frederikhors
Copy link

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: jinliming2/vscode-go-template#16.

@lehuuthoct
Copy link

lehuuthoct commented Mar 14, 2023

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

@tmc
Copy link

tmc commented Sep 11, 2023

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

@hyangah
Copy link
Contributor

hyangah commented Sep 11, 2023

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

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 #609 (comment)), outlined in https://code.visualstudio.com/api/language-extensions/embedded-languages#language-services or ideas discussed in microsoft/vscode#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 hyangah closed this as completed Sep 11, 2023
@tmc
Copy link

tmc commented Sep 12, 2023

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.

@mortenson
Copy link

I just published a vscode plugin for go templates that supports autocomplete, to some degree, by transpiling go templates to go: https://github.com/mortenson/go-template-transpiler-extension

@ewh0
Copy link

ewh0 commented Jul 6, 2024

checking back again in 2024... any update on this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests