File: templates.md

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (49 lines) | stat: -rw-r--r-- 1,871 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Gopls: Support for template files

Gopls provides some support for Go template files, that is, files that
are parsed by [`text/template`](https://pkg.go.dev/text/template) or
[`html/template`](https://pkg.go.dev/html/template).

## Enabling template support

Gopls recognizes template files based on their file extension, which
may be configured by the
[`templateExtensions`](../settings.md#templateExtensions) setting. If
this list is empty, template support is disabled. (This is the default
value, since Go templates don't have a canonical file extension.)

Additional configuration may be necessary to ensure that your client
chooses the correct language kind when opening template files.
Gopls recogizes both `"tmpl"` and `"gotmpl"` for template files.
For example, in `VS Code` you will also need to add an
entry to the
[`files.associations`](https://code.visualstudio.com/docs/languages/identifiers)
mapping:
```json
"files.associations": {
  ".mytemplate": "gotmpl"
},
```


## Features
In template files, template support works inside
the default `{{` delimiters. (Go template parsing
allows the user to specify other delimiters, but
gopls does not know how to do that.)

Gopls template support includes the following features:
+ **Diagnostics**: if template parsing returns an error,
it is presented as a diagnostic. (Missing functions do not produce errors.)
+ **Syntax Highlighting**: syntax highlighting is provided for template files.
+ **Definitions**: gopls provides jump-to-definition inside templates, though it does not understand scoping (all templates are considered to be in one global scope).
+ **References**: gopls provides find-references, with the same scoping limitation as definitions.
+ **Completions**: gopls will attempt to suggest completions inside templates.

TODO: also
+ Hover
+ SemanticTokens
+ Symbol search
+ DocumentHighlight