Closed9

goplsをコマンドとして使いたい

podhmopodhmo

help

$ gopls --help

gopls is a Go language server.

It is typically used with an editor to provide language features. When no
command is specified, gopls will default to the 'serve' command. The language
features can also be accessed via the gopls command-line interface.

Usage:
  gopls help [<subject>]

Command:

Main                
  serve             run a server for Go code using the Language Server Protocol
  version           print the gopls version information
  bug               report a bug in gopls
  help              print usage information for subcommands
  api-json          print json describing gopls API
  licenses          print licenses of included software
                    
Features            
  call_hierarchy    display selected identifier's call hierarchy
  check             show diagnostic results for the specified file
  definition        show declaration of selected identifier
  folding_ranges    display selected file's folding ranges
  format            format the code according to the go standard
  highlight         display selected identifier's highlights
  implementation    display selected identifier's implementation
  imports           updates import statements
  remote            interact with the gopls daemon
  inspect           interact with the gopls daemon (deprecated: use 'remote')
  links             list links in a file
  prepare_rename    test validity of a rename operation at location
  references        display selected identifier's references
  rename            rename selected identifier
  semtok            show semantic tokens for the specified file
  signature         display selected identifier's signature
  stats             print workspace statistics
  fix               apply suggested fixes
  symbols           display selected file's symbols
  workspace_symbol  search symbols in workspace
  vulncheck         run experimental vulncheck analysis (experimental: under development)

flags:
  -debug=string
    	serve debug information on the supplied address
  -listen=string
    	address on which to listen for remote connections. If prefixed by 'unix;', the subsequent address is assumed to be a unix domain socket. Otherwise, TCP is used.
  -listen.timeout=duration
    	when used with -listen, shut down the server when there are no connected clients for this duration
  -logfile=string
    	filename to log to. if value is "auto", then logging to a default output file is enabled
  -mode=string
    	no effect
  -ocagent=string
    	the address of the ocagent (e.g. http://localhost:55678), or off (default "off")
  -port=int
    	port on which to run gopls for debugging purposes
  -profile.alloc=string
    	write alloc profile to this file
  -profile.cpu=string
    	write CPU profile to this file
  -profile.mem=string
    	write memory profile to this file
  -profile.trace=string
    	write trace log to this file
  -remote=string
    	forward all commands to a remote lsp specified by this flag. With no special prefix, this is assumed to be a TCP address. If prefixed by 'unix;', the subsequent address is assumed to be a unix domain socket. If 'auto', or prefixed by 'auto;', the remote address is automatically resolved based on the executing environment.
  -remote.debug=string
    	when used with -remote=auto, the -debug value used to start the daemon
  -remote.listen.timeout=duration
    	when used with -remote=auto, the -listen.timeout value used to start the daemon (default 1m0s)
  -remote.logfile=string
    	when used with -remote=auto, the -logfile value used to start the daemon
  -rpc.trace
    	print the full rpc trace in lsp inspector format
  -v,-verbose
    	verbose output
  -vv,-veryverbose
    	very verbose output
podhmopodhmo

go doc

https://pkg.go.dev/github.com/podhmo/reflect-shape

$ go doc .
package reflectshape // import "github.com/podhmo/reflect-shape"

var DocTruncationSize = 10
func IsZeroRecursive(rt reflect.Type, rv reflect.Value) bool
type Config struct{ ... }
type Extractor struct{ ... }
type Field struct{ ... }
type FieldList []*Field
type Func struct{ ... }
type ID struct{ ... }
type Interface struct{ ... }
type Named struct{ ... }
type Package struct{ ... }
type Scope struct{ ... }
type Shape struct{ ... }
type Struct struct{ ... }
type Var struct{ ... }
type VarList []*Var
podhmopodhmo

call_hierarchy

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#callHierarchy_incomingCalls

いい感じにその特定のsymbol (e.g. 関数)の呼び出し関係を出力してくれる。渡す形式は <filename>:<line number>:<column number>

help

$ gopls call_hierarchy --help
display selected identifier's call hierarchy

Usage:
  gopls [flags] call_hierarchy <position>

Example:

        $ # 1-indexed location (:line:column or :#offset) of the target identifier
        $ gopls call_hierarchy helper/helper.go:8:6

is_zero.goのIsZero()で試してみる。

callerが呼び出す側、calleeが呼ばれてる側

$ gopls call_hierarchy is_zero.go:7:6
caller[0]: ranges 49:27-42 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero_test.go from/to function TestIsZeroRecursive.func() in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero_test.go:48:18-22
caller[1]: ranges 25:8-23, 34:10-25 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function IsZeroRecursive in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go:7:6-21
identifier: function IsZeroRecursive in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go:7:6-21
callee[0]: ranges 23:13-18 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function Field in /usr/lib/go-1.18/src/reflect/type.go:163:2-7
callee[1]: ranges 25:8-23, 34:10-25 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function IsZeroRecursive in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go:7:6-21
callee[2]: ranges 31:9-14, 38:30-35 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function IsNil in /usr/lib/go-1.18/src/reflect/value.go:1489:16-21
callee[3]: ranges 34:40-44 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function Elem in /usr/lib/go-1.18/src/reflect/value.go:1166:16-20
callee[4]: ranges 38:14-21 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function IsValid in /usr/lib/go-1.18/src/reflect/value.go:1514:16-23
callee[5]: ranges 10:13-19, 12:13-19, 14:13-19, 16:13-19, 18:13-19, 20:13-19 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function IsZero in /usr/lib/go-1.18/src/reflect/value.go:1520:16-22
callee[6]: ranges 22:22-30 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function NumField in /usr/lib/go-1.18/src/reflect/type.go:204:2-10
callee[7]: ranges 34:29-33 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function Elem in /usr/lib/go-1.18/src/reflect/type.go:158:2-6
callee[8]: ranges 8:12-16 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function Kind in /usr/lib/go-1.18/src/reflect/type.go:101:2-6
callee[9]: ranges 24:13-18 in /home/podhmo/ghq/gith
podhmopodhmo

viewerがあると良いかも。

callee[0]: ranges 23:13-18 in /home/podhmo/ghq/github.com/podhmo/reflect-shape/is_zero.go from/to function Field in /usr/lib/go-1.18/src/reflect/type.go:163:2-7

これの意味は、23行目の13~18文字目でreflectパッケージのField (reflect.Type.Field())を呼んでるということ。

このスクラップは4ヶ月前にクローズされました