Skip to content

Commit 08e0b30

Browse files
committed
internal/lsp: move PrintVersionInfo to the debug package
Also add a new rendering mode, and clean up the plain text one. Then use it to add an info page to to the server in debug mode. Change-Id: Ifa66a75260965d0e46e874200203ebbc4490e424 Reviewed-on: https://go-review.googlesource.com/c/tools/+/179497 Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 2de7f9b commit 08e0b30

File tree

7 files changed

+91
-48
lines changed

7 files changed

+91
-48
lines changed

internal/lsp/cmd/info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"os"
1414
"strings"
1515

16-
"golang.org/x/tools/internal/lsp"
1716
"golang.org/x/tools/internal/lsp/browser"
17+
"golang.org/x/tools/internal/lsp/debug"
1818
)
1919

2020
// version implements the version command.
@@ -36,7 +36,7 @@ func (v *version) DetailedHelp(f *flag.FlagSet) {
3636
// Run collects some basic information and then prepares an issue ready to
3737
// be reported.
3838
func (v *version) Run(ctx context.Context, args ...string) error {
39-
lsp.PrintVersionInfo(os.Stdout, v.app.Verbose, false)
39+
debug.PrintVersionInfo(os.Stdout, v.app.Verbose, debug.PlainText)
4040
return nil
4141
}
4242

@@ -70,7 +70,7 @@ A failing unit test is the best.
7070
func (b *bug) Run(ctx context.Context, args ...string) error {
7171
buf := &bytes.Buffer{}
7272
fmt.Fprint(buf, goplsBugHeader)
73-
lsp.PrintVersionInfo(buf, true, true)
73+
debug.PrintVersionInfo(buf, true, debug.Markdown)
7474
body := buf.String()
7575
title := strings.Join(args, " ")
7676
if !strings.HasPrefix(title, goplsBugPrefix) {

internal/lsp/info.1.11.go renamed to internal/lsp/debug/info.1.11.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
// +build !go1.12
66

7-
package lsp
7+
package debug
88

99
import (
1010
"fmt"
1111
"io"
1212
)
1313

14-
func printBuildInfo(w io.Writer, verbose bool) {
14+
func printBuildInfo(w io.Writer, verbose bool, mode PrintMode) {
1515
fmt.Fprintf(w, "no module information, gopls not built with go 1.11 or earlier\n")
1616
}

internal/lsp/info.go renamed to internal/lsp/debug/info.1.12.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44

55
// +build go1.12
66

7-
package lsp
7+
package debug
88

99
import (
1010
"fmt"
1111
"io"
1212
"runtime/debug"
1313
)
1414

15-
func printBuildInfo(w io.Writer, verbose bool) {
15+
func printBuildInfo(w io.Writer, verbose bool, mode PrintMode) {
1616
if info, ok := debug.ReadBuildInfo(); ok {
1717
fmt.Fprintf(w, "%v\n", info.Path)
18-
printModuleInfo(w, &info.Main)
18+
printModuleInfo(w, &info.Main, mode)
1919
if verbose {
2020
for _, dep := range info.Deps {
21-
printModuleInfo(w, dep)
21+
printModuleInfo(w, dep, mode)
2222
}
2323
}
2424
} else {
2525
fmt.Fprintf(w, "no module information, gopls not built in module mode\n")
2626
}
2727
}
2828

29-
func printModuleInfo(w io.Writer, m *debug.Module) {
29+
func printModuleInfo(w io.Writer, m *debug.Module, mode PrintMode) {
3030
fmt.Fprintf(w, " %s@%s", m.Path, m.Version)
3131
if m.Sum != "" {
3232
fmt.Fprintf(w, " %s", m.Sum)

internal/lsp/debug/info.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package debug
6+
7+
import (
8+
"fmt"
9+
"io"
10+
"os/exec"
11+
"strings"
12+
)
13+
14+
type PrintMode int
15+
16+
const (
17+
PlainText = PrintMode(iota)
18+
Markdown
19+
HTML
20+
)
21+
22+
// This writes the version and environment information to a writer.
23+
func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) {
24+
if !verbose {
25+
printBuildInfo(w, false, mode)
26+
return
27+
}
28+
section(w, mode, "Build info", func() {
29+
printBuildInfo(w, true, mode)
30+
})
31+
fmt.Fprint(w, "\n")
32+
section(w, mode, "Go info", func() {
33+
cmd := exec.Command("go", "version")
34+
cmd.Stdout = w
35+
cmd.Run()
36+
fmt.Fprint(w, "\n")
37+
cmd = exec.Command("go", "env")
38+
cmd.Stdout = w
39+
cmd.Run()
40+
})
41+
}
42+
43+
func section(w io.Writer, mode PrintMode, title string, body func()) {
44+
switch mode {
45+
case PlainText:
46+
fmt.Fprintln(w, title)
47+
fmt.Fprintln(w, strings.Repeat("-", len(title)))
48+
body()
49+
case Markdown:
50+
fmt.Fprintf(w, "#### %s\n\n```\n", title)
51+
body()
52+
fmt.Fprintf(w, "```\n")
53+
case HTML:
54+
fmt.Fprintf(w, "<h3>%s</h3>\n<pre>\n", title)
55+
body()
56+
fmt.Fprint(w, "</pre>\n")
57+
}
58+
}

internal/lsp/debug/serve.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package debug
66

77
import (
8+
"bytes"
89
"context"
910
"html/template"
1011
"log"
@@ -16,6 +17,13 @@ import (
1617
func init() {
1718
http.HandleFunc("/", Render(mainTmpl, nil))
1819
http.HandleFunc("/debug/", Render(debugTmpl, nil))
20+
http.HandleFunc("/info", Render(infoTmpl, getInfo))
21+
}
22+
23+
func getInfo(r *http.Request) interface{} {
24+
buf := &bytes.Buffer{}
25+
PrintVersionInfo(buf, true, HTML)
26+
return template.HTML(buf.String())
1927
}
2028

2129
// Serve starts and runs a debug server in the background.
@@ -64,9 +72,12 @@ var BaseTemplate = template.Must(template.New("").Parse(`
6472
</style>
6573
</head>
6674
<body>
67-
{{template "title"}}
68-
<br>
69-
{{block "body" .Data}}
75+
<a href="/">Main</a>
76+
<a href="/info">Info</a>
77+
<a href="/debug/">Debug</a>
78+
<hr>
79+
<h1>{{template "title" .}}</h1>
80+
{{block "body" .}}
7081
Unknown page
7182
{{end}}
7283
</body>
@@ -76,7 +87,13 @@ Unknown page
7687
var mainTmpl = template.Must(template.Must(BaseTemplate.Clone()).Parse(`
7788
{{define "title"}}GoPls server information{{end}}
7889
{{define "body"}}
79-
<A href="/debug/">Debug</A>
90+
{{end}}
91+
`))
92+
93+
var infoTmpl = template.Must(template.Must(BaseTemplate.Clone()).Parse(`
94+
{{define "title"}}GoPls version information{{end}}
95+
{{define "body"}}
96+
{{.}}
8097
{{end}}
8198
`))
8299

internal/lsp/general.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path"
1313

1414
"golang.org/x/tools/internal/jsonrpc2"
15+
"golang.org/x/tools/internal/lsp/debug"
1516
"golang.org/x/tools/internal/lsp/protocol"
1617
"golang.org/x/tools/internal/lsp/source"
1718
"golang.org/x/tools/internal/span"
@@ -140,7 +141,7 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
140141
}
141142
}
142143
buf := &bytes.Buffer{}
143-
PrintVersionInfo(buf, true, false)
144+
debug.PrintVersionInfo(buf, true, debug.PlainText)
144145
s.session.Logger().Infof(ctx, "%s", buf)
145146
return nil
146147
}

internal/lsp/util.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,12 @@ package lsp
77
import (
88
"context"
99
"fmt"
10-
"io"
11-
"os/exec"
1210

1311
"golang.org/x/tools/internal/lsp/protocol"
1412
"golang.org/x/tools/internal/lsp/source"
1513
"golang.org/x/tools/internal/span"
1614
)
1715

18-
// This writes the version and environment information to a writer.
19-
func PrintVersionInfo(w io.Writer, verbose bool, markdown bool) {
20-
if !verbose {
21-
printBuildInfo(w, false)
22-
return
23-
}
24-
fmt.Fprint(w, "#### Build info\n\n")
25-
if markdown {
26-
fmt.Fprint(w, "```\n")
27-
}
28-
printBuildInfo(w, true)
29-
fmt.Fprint(w, "\n")
30-
if markdown {
31-
fmt.Fprint(w, "```\n")
32-
}
33-
fmt.Fprint(w, "\n#### Go info\n\n")
34-
if markdown {
35-
fmt.Fprint(w, "```\n")
36-
}
37-
cmd := exec.Command("go", "version")
38-
cmd.Stdout = w
39-
cmd.Run()
40-
fmt.Fprint(w, "\n")
41-
cmd = exec.Command("go", "env")
42-
cmd.Stdout = w
43-
cmd.Run()
44-
if markdown {
45-
fmt.Fprint(w, "```\n")
46-
}
47-
}
48-
4916
func getSourceFile(ctx context.Context, v source.View, uri span.URI) (source.File, *protocol.ColumnMapper, error) {
5017
f, err := v.GetFile(ctx, uri)
5118
if err != nil {

0 commit comments

Comments
 (0)