Repository: go
Issue: x/tools/gopls: `TestXxx()` completions not working when location followed by newline, due to parser error recovery
Labels: NeedsInvestigation,gopls,Tools,gopls/parsing,gopls/completion

<!--
Please answer these questions before submitting your issue. Thanks!
-->

### gopls version

<!--
Output of `gopls -v version` on the command line
-->
Affects versions >=0.12.0, including current master.

### go env

<!--
Output of `go env` on the command line in your workspace directory
-->
```
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/muchzill4/Library/Caches/go-build"
GOENV="/Users/muchzill4/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/muchzill4/.asdf/installs/golang/1.20.5/packages/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/muchzill4/.asdf/installs/golang/1.20.5/packages"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/muchzill4/.asdf/installs/golang/1.20.5/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/muchzill4/.asdf/installs/golang/1.20.5/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/muchzill4/Dev/vcs/go-tools/gopls/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8b/9q2y2mm51yb6qmjm0pv9sjhm0000gn/T/go-build2972635718=/tmp/go-build -gno-record-gcc-switches -fno-common"
```


### What did you do?

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on go.dev/play is better.
A failing unit test is the best.
-->

When triggering completions in test files, such as:

```go
package main

import "testing"

func T<caret, where completion should be triggered>

func TestSomething(t *testing.T) {
}
```

### What did you expect to see?

`TestXxx` as one of the completions.

### What did you see instead?

`TestMain` and `Test` 

### Editor and settings

<!--
Your editor and any settings you have configured (for example, your VSCode settings.json file)
-->

n/a

### Logs

<!--
If possible please include gopls logs. Instructions for capturing them can be found here:
https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capture-logs
-->

```
[Trace - 11:49:34.710 AM] Sending request 'textDocument/completion - (581)'.
Params: {"textDocument":{"uri":"file:///Users/muchzill4/Dev/my/ttt/foo_test.go"},"position":{"line":4,"character":6},"context":{"triggerKind":1}}
...
[Trace - 11:49:34.711 AM] Received response 'textDocument/completion - (581)' in 1ms.
Result: {"isIncomplete":true,"items":[{"label":"TestMain","kind":3,"documentation":{"kind":"markdown","value":"complete the function name\n"},"preselect":true,"sortText":"00000","filterText":"TestMain","insertTextFormat":2,"textEdit":{"range":{"start":{"line":4,"character":5},"end":{"line":4,"character":6}},"newText":"TestMain"}},{"label":"Test","kind":3,"documentation":{"kind":"markdown","value":"complete the function name\n"},"sortText":"00001","filterText":"Test","insertTextFormat":2,"textEdit":{"range":{"start":{"line":4,"character":5},"end":{"line":4,"character":6}},"newText":"Test"}}]}
```

### Debugging journey

My debugging indicates that this started happening since https://go-review.googlesource.com/c/tools/+/459559. 
It seems that [`t.Closing != t.Opening`](https://github.com/golang/tools/blob/89d9fae1d14029f43a0d3954045b4d4772b9c108/gopls/internal/lsp/source/completion/definition.go#L57) unless the completion is triggered at the end of the file.

To replicate with existing tests:
```patch
diff --git a/gopls/internal/regtest/completion/completion_test.go b/gopls/internal/regtest/completion/completion_test.go
index 117e940e0..e3e62b47e 100644
--- a/gopls/internal/regtest/completion/completion_test.go
+++ b/gopls/internal/regtest/completion/completion_test.go
@@ -729,7 +729,7 @@ package foo
 		env.OpenFile(fname)
 		env.Await(env.DoneWithOpen())
 		for _, test := range tests {
-			env.SetBufferContent(fname, "package foo\n"+test.line)
+			env.SetBufferContent(fname, "package foo\n"+test.line+"\n")
 			loc := env.RegexpSearch(fname, test.pat)
 			loc.Range.Start.Character += uint32(protocol.UTF16Len([]byte(test.pat)))
 			completions := env.Completion(loc)
```