internal/lsp/templates: replace panic by logging

There were two panics that cannot happen (meaning they are inconsistent
woith my understanding of the documentation). These are being
replaced by logging, as if they happen, they will happen deterministically
again if the user restarts gopls and does the same thing.

Change-Id: Ia405ad2883cedcf3de6cb1376eedb0f9250c61e0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/350110
Trust: Peter Weinberger <pjw@google.com>
Run-TryBot: Peter Weinberger <pjw@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
pjw 2021-09-15 12:41:12 -04:00 committed by Peter Weinberger
parent a0016a2ade
commit 464a8874ca
1 changed files with 10 additions and 2 deletions

View File

@ -6,10 +6,12 @@ package template
import (
"bytes"
"context"
"fmt"
"text/template/parse"
"unicode/utf8"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
)
@ -49,10 +51,16 @@ func (p *Parsed) fields(flds []string, x parse.Node) []symbol {
lookfor += "." + f // quadratic, but probably ok
}
default:
panic(fmt.Sprintf("%T unexpected in fields()", x))
// If these happen they will happen even if gopls is restarted
// and the users does the same thing, so it is better not to panic.
// context.Background() is used because we don't have access
// to any other context. [we could, but it would be complicated]
event.Log(context.Background(), fmt.Sprintf("%T unexpected in fields()", x))
return nil
}
if len(lookfor) == 0 {
panic(fmt.Sprintf("no strings in fields() %#v", x))
event.Log(context.Background(), fmt.Sprintf("no strings in fields() %#v", x))
return nil
}
startsAt := int(x.Position())
ix := bytes.Index(p.buf[startsAt:], []byte(lookfor)) // HasPrefix? PJW?