Skip to content

Commit

Permalink
gopls: take control of the flag printing
Browse files Browse the repository at this point in the history
Add a copy of flag.PrintDefaults so that we can make changes.
This CL does not actually modify anything (although it does have
to detect the private stringValue type a slightly different way)

For #41860

Change-Id: I19512041f9c1bfde56226aea48302cd63cf670e2
Reviewed-on: https://go-review.googlesource.com/c/tools/+/367837
Trust: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
ianthehat committed Feb 2, 2022
1 parent afd524f commit 217a9fc
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion internal/lsp/cmd/call_hierarchy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Example:
$ gopls call_hierarchy helper/helper.go:8:6
$ gopls call_hierarchy helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (c *callHierarchy) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Example: show the diagnostic results of this file:
$ gopls check internal/lsp/cmd/check.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs the check on the files specified by args and prints the
Expand Down
49 changes: 48 additions & 1 deletion internal/lsp/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"io/ioutil"
"log"
"os"
"reflect"
"strings"
"sync"
"text/tabwriter"
Expand Down Expand Up @@ -132,7 +133,53 @@ command:
fmt.Fprintf(w, " %s\t%s\n", c.Name(), c.ShortHelp())
}
fmt.Fprint(w, "\nflags:\n")
f.PrintDefaults()
printFlagDefaults(f)
}

// this is a slightly modified version of flag.PrintDefaults to give us control
func printFlagDefaults(s *flag.FlagSet) {
s.VisitAll(func(f *flag.Flag) {
var b strings.Builder
fmt.Fprintf(&b, " -%s", f.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
b.WriteString(" ")
b.WriteString(name)
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if b.Len() <= 4 { // space, space, '-', 'x'.
b.WriteString("\t")
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
b.WriteString("\n \t")
}
b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
if !isZeroValue(f, f.DefValue) {
if reflect.TypeOf(f.Value).Elem().Name() == "stringValue" {
fmt.Fprintf(&b, " (default %q)", f.DefValue)
} else {
fmt.Fprintf(&b, " (default %v)", f.DefValue)
}
}
fmt.Fprint(s.Output(), b.String(), "\n")
})
}

// isZeroValue is copied from the flags package
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String()
}

// Run takes the args after top level flag processing, and invokes the correct
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Example: show the definition of the identifier at syntax at offset %[1]v in this
definition-flags:
`, exampleLine, exampleColumn, exampleOffset)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs the definition query as specified by args and prints the
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/folding_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Example:
$ gopls folding_ranges helper/helper.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (r *foldingRanges) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Example: reformat this file:
format-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs the check on the files specified by args and prints the
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Example:
$ gopls highlight helper/helper.go:8:6
$ gopls highlight helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (r *highlight) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Example:
$ gopls implementation helper/helper.go:8:6
$ gopls implementation helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (i *implementation) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Example: update imports statements in a file:
imports-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs diagnostic checks on the file specified and either;
Expand Down
8 changes: 4 additions & 4 deletions internal/lsp/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (v *version) Usage() string { return "" }
func (v *version) ShortHelp() string { return "print the gopls version information" }
func (v *version) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run prints version information to stdout.
Expand All @@ -50,7 +50,7 @@ func (b *bug) Usage() string { return "" }
func (b *bug) ShortHelp() string { return "report a bug in gopls" }
func (b *bug) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}

const goplsBugPrefix = "x/tools/gopls: <DESCRIBE THE PROBLEM>"
Expand Down Expand Up @@ -98,7 +98,7 @@ func (j *apiJSON) Usage() string { return "" }
func (j *apiJSON) ShortHelp() string { return "print json describing gopls API" }
func (j *apiJSON) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}

func (j *apiJSON) Run(ctx context.Context, args ...string) error {
Expand All @@ -120,7 +120,7 @@ func (l *licenses) Usage() string { return "" }
func (l *licenses) ShortHelp() string { return "print licenses of included software" }
func (l *licenses) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), ``)
f.PrintDefaults()
printFlagDefaults(f)
}

const licensePreamble = `
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Example: list links contained within a file:
links-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run finds all the links within a document
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/prepare_rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Example:
$ gopls prepare_rename helper/helper.go:8:6
$ gopls prepare_rename helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// ErrInvalidRenamePosition is returned when prepareRename is run at a position that
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/references.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Example:
references-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (r *references) Run(ctx context.Context, args ...string) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cmd/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ $ gopls -remote=localhost:8082 remote sessions

func (c *listSessions) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), listSessionsExamples)
f.PrintDefaults()
printFlagDefaults(f)
}

func (c *listSessions) Run(ctx context.Context, args ...string) error {
Expand Down Expand Up @@ -131,7 +131,7 @@ $ gopls -remote=localhost:8082 remote debug localhost:8083

func (c *startDebugging) DetailedHelp(f *flag.FlagSet) {
fmt.Fprint(f.Output(), startDebuggingExamples)
f.PrintDefaults()
printFlagDefaults(f)
}

func (c *startDebugging) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Example:
rename-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run renames the specified identifier and either;
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/semantictokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Example: show the semantic tokens for this file:
$ gopls semtok internal/lsp/cmd/semtok.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs the semtok on the files specified by args and prints the
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ a child of an editor process.
server-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (s *Serve) remoteArgs(network, address string) []string {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Example:
$ gopls signature helper/helper.go:8:6
$ gopls signature helper/helper.go:#53
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (r *signature) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s subcommands) DetailedHelp(f *flag.FlagSet) {
for _, c := range s {
fmt.Fprintf(w, " %s\t%s\n", c.Name(), c.ShortHelp())
}
f.PrintDefaults()
printFlagDefaults(f)
}

func (s subcommands) Usage() string { return "<subcommand> [arg]..." }
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/suggested_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Example: apply suggested fixes for this file
fix-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

// Run performs diagnostic checks on the file specified and either;
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *symbols) DetailedHelp(f *flag.FlagSet) {
Example:
$ gopls symbols helper/helper.go
`)
f.PrintDefaults()
printFlagDefaults(f)
}
func (r *symbols) Run(ctx context.Context, args ...string) error {
if len(args) != 1 {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *generateWorkspaceMod) ShortHelp() string {
}

func (c *generateWorkspaceMod) DetailedHelp(f *flag.FlagSet) {
f.PrintDefaults()
printFlagDefaults(f)
}

func (c *generateWorkspaceMod) Run(ctx context.Context, args ...string) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/cmd/workspace_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Example:
workspace_symbol-flags:
`)
f.PrintDefaults()
printFlagDefaults(f)
}

func (r *workspaceSymbol) Run(ctx context.Context, args ...string) error {
Expand Down

0 comments on commit 217a9fc

Please sign in to comment.