| 1 | --- |
| 2 | title: "Gopls: Support for template files" |
| 3 | --- |
| 4 | |
| 5 | Gopls provides some support for Go template files, that is, files that |
| 6 | are parsed by [`text/template`](https://pkg.go.dev/text/template) or |
| 7 | [`html/template`](https://pkg.go.dev/html/template). |
| 8 | |
| 9 | ## Enabling template support |
| 10 | |
| 11 | Gopls recognizes template files based on their file extension, which |
| 12 | may be configured by the |
| 13 | [`templateExtensions`](../settings.md#templateExtensions) setting. If |
| 14 | this list is empty, template support is disabled. (This is the default |
| 15 | value, since Go templates don't have a canonical file extension.) |
| 16 | |
| 17 | Additional configuration may be necessary to ensure that your client |
| 18 | chooses the correct language kind when opening template files. |
| 19 | Gopls recognizes both `"tmpl"` and `"gotmpl"` for template files. |
| 20 | For example, in `VS Code` you will also need to add an |
| 21 | entry to the |
| 22 | [`files.associations`](https://code.visualstudio.com/docs/languages/identifiers) |
| 23 | mapping: |
| 24 | ```json |
| 25 | "files.associations": { |
| 26 | ".mytemplate": "gotmpl" |
| 27 | }, |
| 28 | ``` |
| 29 | |
| 30 | |
| 31 | ## Features |
| 32 | In template files, template support works inside |
| 33 | the default `{{` delimiters. (Go template parsing |
| 34 | allows the user to specify other delimiters, but |
| 35 | gopls does not know how to do that.) |
| 36 | |
| 37 | Gopls template support includes the following features: |
| 38 | + **Diagnostics**: if template parsing returns an error, |
| 39 | it is presented as a diagnostic. (Missing functions do not produce errors.) |
| 40 | + **Syntax Highlighting**: syntax highlighting is provided for template files. |
| 41 | + **Definitions**: gopls provides jump-to-definition inside templates, though it does not understand scoping (all templates are considered to be in one global scope). |
| 42 | + **References**: gopls provides find-references, with the same scoping limitation as definitions. |
| 43 | + **Completions**: gopls will attempt to suggest completions inside templates. |
| 44 | |
| 45 | TODO: also |
| 46 | + Hover |
| 47 | + SemanticTokens |
| 48 | + Symbol search |
| 49 | + DocumentHighlight |
| 50 | |
| 51 |