Skip to content

Instantly share code, notes, and snippets.

@icholy
Last active February 17, 2022 19:17
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save icholy/5320428 to your computer and use it in GitHub Desktop.
Save icholy/5320428 to your computer and use it in GitHub Desktop.
godoc completions

Get the godoc template

mkdir ~/.godoc_templates
cp $GOROOT/lib/godoc/* ~/.godoc_templates
cd ~/.godoc_templates
rm package.txt
wget https://gist.github.com/icholy/5320428/raw/34d716d9bc3badddd2f3164c4d2850b8f2c4f418/package.txt

Add the completion

mkdir ~/.zsh/completions
cd ~/.zsh/completions/
wget https://gist.github.com/icholy/5320428/raw/1775b8c3b85b4a0ef95858afbf2e728c42b4ded0/_godoc

Add the following to your .zshrc

# COMPLETION SETTINGS
fpath=(~/.zsh/completions $fpath)
autoload -U compinit
compinit
#compdef godoc
typeset -A opt_args
local context state line
local pkgdir usrpkgdir
pkgdir="$GOROOT/src/pkg"
usrpkgdir="$GOPATH/src"
godoctmpl=~/.godoc_templates
_arguments -s -S \
"-goroot+[Go root directory]::(/usr/lib/go)" \
"-html[print HTML in command-line mode]" \
"-http+[HTTP service address (e.g., :6060)]" \
"-index[enable search index]" \
"-index_files+[glob pattern specifying index files;if not empty, the index is read from these files in sorted order]" \
"-index_throttle+[index throttle value; 0.0 = no time allocated, 1.0 = full throttle]::(0.75 0.0 1.0)" \
"-maxresults+[maximum number of full text search results shown]::(10000)" \
"-path+[additional package directories (colon-separated)]" \
"-q[arguments are considered search queries]" \
"-server+[webserver address for command line searches]" \
"-src[print (exported) source in command-line mode]" \
"-tabwidth+[tab width]::(4)" \
"-templates+[directory containing alternate template files]" \
"-testdir+[Go root subdirectory - for testing only (faster startups)]" \
"-timestamps[show timestamps with directory listings]" \
"-url+[print HTML for named URL]" \
"-v[verbose mode]" \
"-write_index[write index to a file; the file name must be specified with -index_files]" \
"1:package:->pkgs" \
"*:package contents:->pkg_content"
case $state in
(pkgs)
_path_files -W "$pkgdir" -/
_path_files -W "$usrpkgdir" -/
return 0
;;
(pkg_content)
godoc_completions=$(godoc -templates "$godoctmpl" $words[-2] 2> /dev/null)
if [ $? -eq 0 ]; then
compadd "$@" $(echo "$godoc_completions")
fi
return 0
;;
esac
{{with .PDoc}}{{/*
---------------------------------------
*/}}{{with .Consts}}{{range .}}{{range .Names}}{{.}}
{{end}}{{end}}{{end}}{{/*
---------------------------------------
*/}}{{with .Vars}}{{range .}}{{range .Names}}{{.}}
{{end}}{{end}}{{end}}{{/*
---------------------------------------
*/}}{{with .Funcs}}{{range .}}{{.Name}}
{{end}}{{end}}{{/*
---------------------------------------
*/}}{{with .Types}}{{range .}}{{/*
*/}}{{.Name}}
{{/*
*/}}{{range .Methods}}{{.Name}}
{{end}}{{/*
*/}}{{range .Funcs}}{{.Name}}
{{end}}{{/*
*/}}{{range .Consts}}{{range .Names}}{{.}}
{{end}}{{end}}{{/*
*/}}{{range .Vars}}{{range .Names}}{{.}}
{{end}}{{end}}{{/*
*/}}{{end}}{{end}}{{/*
---------------------------------------
*/}}{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment