From fe74b5f0808f3c8a3298ca26027388a5e0526d47 Mon Sep 17 00:00:00 2001 From: pjw Date: Wed, 26 Jan 2022 14:19:13 -0500 Subject: [PATCH] internal/template: return available semantic tokens even on template error Fixes golang/go#50801 Change-Id: Ic5c541d6244bd56b90bee204be7c2b2fdfa90264 Reviewed-on: https://go-review.googlesource.com/c/tools/+/381014 Run-TryBot: Peter Weinberger Trust: Peter Weinberger Reviewed-by: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot --- gopls/internal/regtest/template/template_test.go | 12 ++++++++++++ internal/lsp/fake/editor.go | 8 ++++++++ internal/lsp/template/implementations.go | 4 +--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/gopls/internal/regtest/template/template_test.go b/gopls/internal/regtest/template/template_test.go index 215c21c15e..1846d4ba41 100644 --- a/gopls/internal/regtest/template/template_test.go +++ b/gopls/internal/regtest/template/template_test.go @@ -33,6 +33,7 @@ Hello {{}} <-- missing body EditorConfig{ Settings: map[string]interface{}{ "templateExtensions": []string{"tmpl"}, + "semanticTokens": true, }, }, ).Run(t, files, func(t *testing.T, env *Env) { @@ -46,6 +47,17 @@ Hello {{}} <-- missing body if d[0].Source != "template" { t.Errorf("expected Source 'template', got %q", d[0].Source) } + // issue 50801 (even broken templates could return some semantic tokens) + var p protocol.SemanticTokensParams + p.TextDocument.URI = env.Sandbox.Workdir.URI("hello.tmpl") + toks, err := env.Editor.Server.SemanticTokensFull(env.Ctx, &p) + if err != nil { + t.Errorf("semantic token failed: %v", err) + } + if toks == nil || len(toks.Data) == 0 { + t.Errorf("got no semantic tokens") + } + env.WriteWorkspaceFile("hello.tmpl", "{{range .Planets}}\nHello {{.}}\n{{end}}") env.Await(EmptyDiagnostics("hello.tmpl")) }) diff --git a/internal/lsp/fake/editor.go b/internal/lsp/fake/editor.go index e47a1bf53d..c820f08a49 100644 --- a/internal/lsp/fake/editor.go +++ b/internal/lsp/fake/editor.go @@ -304,6 +304,14 @@ func (e *Editor) initialize(ctx context.Context, workspaceFolders []string) erro } params.Capabilities.TextDocument.Completion.CompletionItem.SnippetSupport = true + params.Capabilities.TextDocument.SemanticTokens.Requests.Full = true + // copied from lsp/semantic.go to avoid import cycle in tests + params.Capabilities.TextDocument.SemanticTokens.TokenTypes = []string{ + "namespace", "type", "class", "enum", "interface", + "struct", "typeParameter", "parameter", "variable", "property", "enumMember", + "event", "function", "method", "macro", "keyword", "modifier", "comment", + "string", "number", "regexp", "operator", + } // This is a bit of a hack, since the fake editor doesn't actually support // watching changed files that match a specific glob pattern. However, the diff --git a/internal/lsp/template/implementations.go b/internal/lsp/template/implementations.go index 97641d6adb..1de988871c 100644 --- a/internal/lsp/template/implementations.go +++ b/internal/lsp/template/implementations.go @@ -157,9 +157,7 @@ func SemanticTokens(ctx context.Context, snapshot source.Snapshot, spn span.URI, return nil, err } p := parseBuffer(buf) - if p.ParseErr != nil { - return nil, p.ParseErr - } + for _, t := range p.Tokens() { if t.Multiline { la, ca := p.LineCol(t.Start)