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 <pjw@google.com>
Trust: Peter Weinberger <pjw@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
pjw 2022-01-26 14:19:13 -05:00 committed by Peter Weinberger
parent 772a39974b
commit fe74b5f080
3 changed files with 21 additions and 3 deletions

View File

@ -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"))
})

View File

@ -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

View File

@ -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)