From 2db5a34c05208b1750b8dd8f1f9be7176f3992c3 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 3 Apr 2024 15:12:26 -0400 Subject: [PATCH 01/66] gopls/internal/golang: RenderPkgDoc: navigational element with an index of all sections in the page, following the design of pkg.go.dev. Change-Id: I41b6719e85d0ace7edb864c5b20ae1f5b8e39d1b Reviewed-on: https://go-review.googlesource.com/c/tools/+/576596 Reviewed-by: Robert Findley Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI --- gopls/internal/golang/pkgdoc.go | 118 ++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 6 deletions(-) diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 852b93e7426..2f597b81e06 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -38,6 +38,7 @@ import ( "html" "log" "path/filepath" + "strings" "golang.org/x/tools/gopls/internal/cache" "golang.org/x/tools/gopls/internal/protocol" @@ -181,6 +182,8 @@ func RenderPackageDoc(pkg *cache.Package, posURL func(filename string, line, col } } + scope := pkg.Types().Scope() + var buf bytes.Buffer buf.WriteString(` @@ -196,6 +199,13 @@ function httpGET(url) { return false; // disable usual behavior } +window.onload = () => { + // Hook up the navigation selector. + document.getElementById('hdr-Selector').onchange = (e) => { + window.location.href = e.target.value; + }; +}; + // Start a GET /hang request. If it ever completes, the server // has disconnected. Show a banner in that case. { @@ -209,9 +219,85 @@ function httpGET(url) { +
Gopls server has terminated. Page is inactive.
+\n") + fmt.Fprintf(&buf, "
\n") + + // -- main element -- + escape := html.EscapeString // sourceLink returns HTML for a link to open a file in the client editor. @@ -355,8 +441,10 @@ function httpGET(url) { return other.Name() } + fmt.Fprintf(&buf, "
\n") + // package name - fmt.Fprintf(&buf, "

Package %s

\n", pkg.Types().Name()) + fmt.Fprintf(&buf, "

Package %s

\n", pkg.Types().Name()) // import path fmt.Fprintf(&buf, "
import %q
\n", pkg.Types().Path()) @@ -369,7 +457,7 @@ function httpGET(url) { fmt.Fprintf(&buf, "
%s
\n", docHTML(docpkg.Doc)) // symbol index - fmt.Fprintf(&buf, "

Index

\n") + fmt.Fprintf(&buf, "

Index

\n") fmt.Fprintf(&buf, "
    \n") if len(docpkg.Consts) > 0 { fmt.Fprintf(&buf, "
  • Constants
  • \n") @@ -377,7 +465,6 @@ function httpGET(url) { if len(docpkg.Vars) > 0 { fmt.Fprintf(&buf, "
  • Variables
  • \n") } - scope := pkg.Types().Scope() for _, fn := range docpkg.Funcs { obj := scope.Lookup(fn.Name).(*types.Func) fmt.Fprintf(&buf, "
  • %s
  • \n", @@ -447,7 +534,7 @@ function httpGET(url) { } // package-level functions - fmt.Fprintf(&buf, "

    Functions

    \n") + fmt.Fprintf(&buf, "

    Functions

    \n") // funcs emits a list of package-level functions, // possibly organized beneath the type they construct. funcs := func(funcs []*doc.Func) { @@ -467,7 +554,7 @@ function httpGET(url) { funcs(docpkg.Funcs) // types and their subelements - fmt.Fprintf(&buf, "

    Types

    \n") + fmt.Fprintf(&buf, "

    Types

    \n") for _, doctype := range docpkg.Types { tname := scope.Lookup(doctype.Name).(*types.TypeName) @@ -506,12 +593,16 @@ function httpGET(url) { } // source files - fmt.Fprintf(&buf, "

    Source files

    \n") + fmt.Fprintf(&buf, "

    Source files

    \n") for _, filename := range docpkg.Filenames { fmt.Fprintf(&buf, "
    %s
    \n", sourceLink(filepath.Base(filename), posURL(filename, 1, 1))) } + fmt.Fprintf(&buf, "
\n") + fmt.Fprintf(&buf, "\n") + fmt.Fprintf(&buf, "\n") + return buf.Bytes(), nil } @@ -621,6 +712,21 @@ a:hover > * { #pkgsite { height: 1.5em; } +header { + position: sticky; + top: 0; + left: 0; + width: 100%; + padding: 0.3em; +} + +#hdr-Selector { + margin-right: 0.3em; + float: right; + min-width: 25em; + padding: 0.3em; +} + #disconnected { position: fixed; top: 1em; From 4794229b8cce278746bf68a3453a5b2cd61c8c06 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 5 Apr 2024 11:18:52 -0400 Subject: [PATCH 02/66] gopls/internal/golang: RenderPkgDoc: add element ...following pkg.go.dev. Change-Id: Ibf55871a4f7b4f160c1f83de5e3bec07b287ad9f Reviewed-on: https://go-review.googlesource.com/c/tools/+/576915 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com> --- gopls/internal/golang/pkgdoc.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gopls/internal/golang/pkgdoc.go b/gopls/internal/golang/pkgdoc.go index 2f597b81e06..4087d28ab77 100644 --- a/gopls/internal/golang/pkgdoc.go +++ b/gopls/internal/golang/pkgdoc.go @@ -183,6 +183,10 @@ func RenderPackageDoc(pkg *cache.Package, posURL func(filename string, line, col } scope := pkg.Types().Scope() + escape := html.EscapeString + + title := fmt.Sprintf("%s package - %s - Gopls packages", + pkg.Types().Name(), escape(pkg.Types().Path())) var buf bytes.Buffer buf.WriteString(`<!DOCTYPE html> @@ -190,6 +194,7 @@ func RenderPackageDoc(pkg *cache.Package, posURL func(filename string, line, col <head> <meta charset="UTF-8"> <style>` + pkgDocStyle + `</style> + <title>` + title + `