internal/lsp: handle empty buffers in template parsing

Fixes golang/vscode-go#1540

Change-Id: I64514915d555bf0c0ff791b213bb834e9e99a0af
Reviewed-on: https://go-review.googlesource.com/c/tools/+/324758
Trust: Rebecca Stambler <rstambler@golang.org>
Trust: Peter Weinberger <pjw@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Peter Weinberger <pjw@google.com>
diff --git a/internal/lsp/template/parse.go b/internal/lsp/template/parse.go
index 2b9c704..8077bfc 100644
--- a/internal/lsp/template/parse.go
+++ b/internal/lsp/template/parse.go
@@ -28,6 +28,7 @@
 	"golang.org/x/tools/internal/lsp/protocol"
 	"golang.org/x/tools/internal/lsp/source"
 	"golang.org/x/tools/internal/span"
+	errors "golang.org/x/xerrors"
 )
 
 var (
@@ -86,6 +87,10 @@
 		buf:   buf,
 		check: -1,
 	}
+	if len(buf) == 0 {
+		ans.ParseErr = errors.New("empty buffer")
+		return ans
+	}
 	// how to compute allAscii...
 	for _, b := range buf {
 		if b >= utf8.RuneSelf {