diff --git a/gopls/doc/features.md b/gopls/doc/features.md index 9cb686436e..ccfe138b2f 100644 --- a/gopls/doc/features.md +++ b/gopls/doc/features.md @@ -4,10 +4,10 @@ This document describes some of the features supported by `gopls`. It is currently under construction, so, for a comprehensive list, see the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/). -For now, only special features outside of the LSP are described below. - ## Special features +Here, only special features outside of the LSP are described. + ### Symbol Queries Gopls supports some extended syntax for `workspace/symbol` requests, when using @@ -21,4 +21,26 @@ supported within symbol queries: | `^` | `^printf` | exact prefix | | `$` | `printf$` | exact suffix | +## Template Files + +Gopls provides some support for Go template files, that is, files that +are parsed by `text/template` or `html/template`. +Gopls recognizes template files based on their file extension. +By default it looks for files ending in `.tmpl` or `.gotmpl`, +but this list may be configured by the +[`templateExtensions`](https://github.com/golang/tools/blob/master/gopls/doc/settings.md#templateextensions-string) setting. +Making this list empty turns off template support. + +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. diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index 375aab3700..542eb1642c 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -72,12 +72,6 @@ Include only project_a, but not node_modules inside it: `-`, `+project_a`, `-pro Default: `["-node_modules"]`. -#### **templateSupport** *bool* - -templateSupport can be used to turn off support for template files. - -Default: `true`. - #### **templateExtensions** *[]string* templateExtensions gives the extensions of file names that are treateed diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 51b00d5b61..53f97f4d7f 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -159,7 +159,7 @@ func (s *snapshot) ModFiles() []span.URI { } func (s *snapshot) Templates() map[span.URI]source.VersionedFileHandle { - if !s.view.Options().TemplateSupport { + if len(s.view.Options().TemplateExtensions) == 0 { return nil } diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index 881d7f12b1..b9695732e6 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -347,7 +347,7 @@ func fileHasExtension(path string, suffixes []string) bool { } func (s *snapshot) locateTemplateFiles(ctx context.Context) { - if !s.view.Options().TemplateSupport { + if len(s.view.Options().TemplateExtensions) == 0 { return } suffixes := s.view.Options().TemplateExtensions diff --git a/internal/lsp/source/api_json.go b/internal/lsp/source/api_json.go index b536df28b2..b7c51632d7 100755 --- a/internal/lsp/source/api_json.go +++ b/internal/lsp/source/api_json.go @@ -44,19 +44,6 @@ var GeneratedAPIJSON = &APIJSON{ Status: "", Hierarchy: "build", }, - { - Name: "templateSupport", - Type: "bool", - Doc: "templateSupport can be used to turn off support for template files.\n", - EnumKeys: EnumKeys{ - ValueType: "", - Keys: nil, - }, - EnumValues: nil, - Default: "true", - Status: "", - Hierarchy: "build", - }, { Name: "templateExtensions", Type: "[]string", diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go index 41a02884d6..147bb9e448 100644 --- a/internal/lsp/source/options.go +++ b/internal/lsp/source/options.go @@ -114,7 +114,6 @@ func DefaultOptions() *Options { ExperimentalPackageCacheKey: true, MemoryMode: ModeNormal, DirectoryFilters: []string{"-node_modules"}, - TemplateSupport: true, TemplateExtensions: []string{"tmpl", "gotmpl"}, }, UIOptions: UIOptions{ @@ -233,9 +232,6 @@ type BuildOptions struct { // Include only project_a, but not node_modules inside it: `-`, `+project_a`, `-project_a/node_modules` DirectoryFilters []string - // TemplateSupport can be used to turn off support for template files. - TemplateSupport bool - // TemplateExtensions gives the extensions of file names that are treateed // as template files. (The extension // 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{}) case "experimentalWorkspaceModule": result.setBool(&o.ExperimentalWorkspaceModule) - case "experimentalTemplateSupport", // remove after June 2022 - "templateSupport": - if name == "experimentalTemplateSupport" { - result.State = OptionDeprecated - result.Replacement = "templateSupport" - } - result.setBool(&o.TemplateSupport) + case "experimentalTemplateSupport": // remove after June 2022 + result.State = OptionDeprecated case "templateExtensions": - iexts, ok := value.([]string) - if !ok { - result.errorf("invalid type %T, expect []string", value) + if iexts, ok := value.([]interface{}); ok { + ans := []string{} + for _, x := range iexts { + ans = append(ans, fmt.Sprint(x)) + } + o.TemplateExtensions = ans break } - o.TemplateExtensions = iexts - + if value == nil { + o.TemplateExtensions = nil + break + } + result.errorf(fmt.Sprintf("unexpected type %T not []string", value)) case "experimentalDiagnosticsDelay", "diagnosticsDelay": if name == "experimentalDiagnosticsDelay" { result.State = OptionDeprecated diff --git a/internal/lsp/template/implementations.go b/internal/lsp/template/implementations.go index 193b973814..66dcc4baaa 100644 --- a/internal/lsp/template/implementations.go +++ b/internal/lsp/template/implementations.go @@ -66,7 +66,7 @@ func Diagnose(f source.VersionedFileHandle) []*source.Diagnostic { } func skipTemplates(s source.Snapshot) bool { - return !s.View().Options().TemplateSupport + return len(s.View().Options().TemplateExtensions) == 0 } // Definition finds the definitions of the symbol at loc. It