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>
diff --git a/internal/lsp/template/symbols.go b/internal/lsp/template/symbols.go
index 856f6e3..2730ebf 100644
--- a/internal/lsp/template/symbols.go
+++ b/internal/lsp/template/symbols.go
@@ -6,10 +6,12 @@
 
 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 @@
 			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?