internal/godoc/internal/doc: don't trim function call initalizers

When trimming declarations for playable examples, do not modify
a list of variables initialized by a call to multi-return-value
function, like
```
var a, b = f()
```
Even if the example only refers to `a`, keep both `a` and `b`
so they get the right values.

Change-Id: I4b2661580b6654e8851b8e178fd470f3f42abd12
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/287192
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/internal/godoc/internal/doc/example.go b/internal/godoc/internal/doc/example.go
index e02ba83..19f797f 100644
--- a/internal/godoc/internal/doc/example.go
+++ b/internal/godoc/internal/doc/example.go
@@ -394,6 +394,13 @@
 				case *ast.ValueSpec:
 					// A ValueSpec may have multiple names (e.g. "var a, b int").
 					// Keep only the names that were mentioned in the example.
+					// Exception: the multiple names have a single initializer (which
+					// would be a function call with multiple return values). In that
+					// case, keep everything.
+					if len(s.Names) > 1 && len(s.Values) == 1 {
+						specs = append(specs, s)
+						continue
+					}
 					ns := *s
 					ns.Names = nil
 					ns.Values = nil
diff --git a/internal/godoc/internal/doc/example_test.go b/internal/godoc/internal/doc/example_test.go
index 2e83832..e5ab0ea 100644
--- a/internal/godoc/internal/doc/example_test.go
+++ b/internal/godoc/internal/doc/example_test.go
@@ -61,7 +61,7 @@
 				case "Play":
 					got := strings.TrimSpace(formatFile(t, fset, ex.Play))
 					if diff := cmp.Diff(want, got); diff != "" {
-						t.Errorf("mismatch (-want, +got):\n%s", diff)
+						t.Errorf("%s Play: mismatch (-want, +got):\n%s", name, diff)
 					}
 					delete(unseen, name)
 				case "Output":
diff --git a/internal/godoc/internal/doc/testdata/examples/values.go b/internal/godoc/internal/doc/testdata/examples/values.go
new file mode 100644
index 0000000..64b0de4
--- /dev/null
+++ b/internal/godoc/internal/doc/testdata/examples/values.go
@@ -0,0 +1,22 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package foo_test
+
+// Variable declaration with fewer values than names.
+
+func f() (int, int) {
+	return 1, 2
+}
+
+var a, b = f()
+
+// Need two examples to hit playExample.
+
+func ExampleA() {
+	_ = a
+}
+
+func ExampleB() {
+}
diff --git a/internal/godoc/internal/doc/testdata/examples/values.golden b/internal/godoc/internal/doc/testdata/examples/values.golden
new file mode 100644
index 0000000..84a0aed
--- /dev/null
+++ b/internal/godoc/internal/doc/testdata/examples/values.golden
@@ -0,0 +1,21 @@
+---------------- A.Play
+package main
+
+import ()
+
+func f() (int, int) {
+	return 1, 2
+}
+
+var a, b = f()
+
+func main() {
+	_ = a
+}
+---------------- B.Play
+package main
+
+import ()
+
+func main() {
+}