Skip to content

Commit 1e71a25

Browse files
committed
gopls: template suffix flags and documentation
Adds 'templateExtensions' (with default ["tmpl", "gotmpl"]) to control which files gopls considers template files. Adds template support documentation to features.md. Fixes golang/go#36911 Change-Id: If0920912bf3748d1c231b3b29e7a008da186bede Reviewed-on: https://go-review.googlesource.com/c/tools/+/363360 Run-TryBot: Peter Weinberger <[email protected]> Trust: Peter Weinberger <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent c2c92fd commit 1e71a25

File tree

7 files changed

+40
-40
lines changed

7 files changed

+40
-40
lines changed

gopls/doc/features.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ This document describes some of the features supported by `gopls`. It is
44
currently under construction, so, for a comprehensive list, see the
55
[Language Server Protocol](https://microsoft.github.io/language-server-protocol/).
66

7-
For now, only special features outside of the LSP are described below.
8-
97
## Special features
108

9+
Here, only special features outside of the LSP are described.
10+
1111
### Symbol Queries
1212

1313
Gopls supports some extended syntax for `workspace/symbol` requests, when using
@@ -21,4 +21,26 @@ supported within symbol queries:
2121
| `^` | `^printf` | exact prefix |
2222
| `$` | `printf$` | exact suffix |
2323

24+
## Template Files
25+
26+
Gopls provides some support for Go template files, that is, files that
27+
are parsed by `text/template` or `html/template`.
28+
Gopls recognizes template files based on their file extension.
29+
By default it looks for files ending in `.tmpl` or `.gotmpl`,
30+
but this list may be configured by the
31+
[`templateExtensions`](https://github.com/golang/tools/blob/master/gopls/doc/settings.md#templateextensions-string) setting.
32+
Making this list empty turns off template support.
33+
34+
In template files, template support works inside
35+
the default `{{` delimiters. (Go template parsing
36+
allows the user to specify other delimiters, but
37+
gopls does not know how to do that.)
38+
39+
Gopls template support includes the following features:
40+
+ **Diagnostics**: if template parsing returns an error,
41+
it is presented as a diagnostic. (Missing functions do not produce errors.)
42+
+ **Syntax Highlighting**: syntax highlighting is provided for template files.
43+
+ **Definitions**: gopls provides jump-to-definition inside templates, though it does not understand scoping (all templates are considered to be in one global scope).
44+
+ **References**: gopls provides find-references, with the same scoping limitation as definitions.
45+
+ **Completions**: gopls will attempt to suggest completions inside templates.
2446
<!--TODO(rstambler): Automatically generate a list of supported features.-->

gopls/doc/settings.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ Include only project_a, but not node_modules inside it: `-`, `+project_a`, `-pro
7272

7373
Default: `["-node_modules"]`.
7474

75-
#### **templateSupport** *bool*
76-
77-
templateSupport can be used to turn off support for template files.
78-
79-
Default: `true`.
80-
8175
#### **templateExtensions** *[]string*
8276

8377
templateExtensions gives the extensions of file names that are treateed

internal/lsp/cache/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *snapshot) ModFiles() []span.URI {
159159
}
160160

161161
func (s *snapshot) Templates() map[span.URI]source.VersionedFileHandle {
162-
if !s.view.Options().TemplateSupport {
162+
if len(s.view.Options().TemplateExtensions) == 0 {
163163
return nil
164164
}
165165

internal/lsp/cache/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func fileHasExtension(path string, suffixes []string) bool {
347347
}
348348

349349
func (s *snapshot) locateTemplateFiles(ctx context.Context) {
350-
if !s.view.Options().TemplateSupport {
350+
if len(s.view.Options().TemplateExtensions) == 0 {
351351
return
352352
}
353353
suffixes := s.view.Options().TemplateExtensions

internal/lsp/source/api_json.go

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/source/options.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ func DefaultOptions() *Options {
114114
ExperimentalPackageCacheKey: true,
115115
MemoryMode: ModeNormal,
116116
DirectoryFilters: []string{"-node_modules"},
117-
TemplateSupport: true,
118117
TemplateExtensions: []string{"tmpl", "gotmpl"},
119118
},
120119
UIOptions: UIOptions{
@@ -233,9 +232,6 @@ type BuildOptions struct {
233232
// Include only project_a, but not node_modules inside it: `-`, `+project_a`, `-project_a/node_modules`
234233
DirectoryFilters []string
235234

236-
// TemplateSupport can be used to turn off support for template files.
237-
TemplateSupport bool
238-
239235
// TemplateExtensions gives the extensions of file names that are treateed
240236
// as template files. (The extension
241237
// is the part of the file name after the final dot.)
@@ -940,22 +936,23 @@ func (o *Options) set(name string, value interface{}, seen map[string]struct{})
940936
case "experimentalWorkspaceModule":
941937
result.setBool(&o.ExperimentalWorkspaceModule)
942938

943-
case "experimentalTemplateSupport", // remove after June 2022
944-
"templateSupport":
945-
if name == "experimentalTemplateSupport" {
946-
result.State = OptionDeprecated
947-
result.Replacement = "templateSupport"
948-
}
949-
result.setBool(&o.TemplateSupport)
939+
case "experimentalTemplateSupport": // remove after June 2022
940+
result.State = OptionDeprecated
950941

951942
case "templateExtensions":
952-
iexts, ok := value.([]string)
953-
if !ok {
954-
result.errorf("invalid type %T, expect []string", value)
943+
if iexts, ok := value.([]interface{}); ok {
944+
ans := []string{}
945+
for _, x := range iexts {
946+
ans = append(ans, fmt.Sprint(x))
947+
}
948+
o.TemplateExtensions = ans
955949
break
956950
}
957-
o.TemplateExtensions = iexts
958-
951+
if value == nil {
952+
o.TemplateExtensions = nil
953+
break
954+
}
955+
result.errorf(fmt.Sprintf("unexpected type %T not []string", value))
959956
case "experimentalDiagnosticsDelay", "diagnosticsDelay":
960957
if name == "experimentalDiagnosticsDelay" {
961958
result.State = OptionDeprecated

internal/lsp/template/implementations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Diagnose(f source.VersionedFileHandle) []*source.Diagnostic {
6666
}
6767

6868
func skipTemplates(s source.Snapshot) bool {
69-
return !s.View().Options().TemplateSupport
69+
return len(s.View().Options().TemplateExtensions) == 0
7070
}
7171

7272
// Definition finds the definitions of the symbol at loc. It

0 commit comments

Comments
 (0)