golang.plugin.zsh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # install in /etc/zsh/zshrc or your personal .zshrc
  2. # gc
  3. prefixes=(5 6 8)
  4. for p in $prefixes; do
  5. compctl -g "*.${p}" ${p}l
  6. compctl -g "*.go" ${p}g
  7. done
  8. # standard go tools
  9. compctl -g "*.go" gofmt
  10. # gccgo
  11. compctl -g "*.go" gccgo
  12. # go tool
  13. __go_tool_complete() {
  14. typeset -a commands build_flags
  15. commands+=(
  16. 'build[compile packages and dependencies]'
  17. 'clean[remove object files]'
  18. 'doc[run godoc on package sources]'
  19. 'env[print Go environment information]'
  20. 'fix[run go tool fix on packages]'
  21. 'fmt[run gofmt on package sources]'
  22. 'generate[generate Go files by processing source]'
  23. 'get[download and install packages and dependencies]'
  24. 'help[display help]'
  25. 'install[compile and install packages and dependencies]'
  26. 'list[list packages]'
  27. 'mod[modules maintenance]'
  28. 'run[compile and run Go program]'
  29. 'test[test packages]'
  30. 'tool[run specified go tool]'
  31. 'version[print Go version]'
  32. 'vet[run go tool vet on packages]'
  33. )
  34. if (( CURRENT == 2 )); then
  35. # explain go commands
  36. _values 'go tool commands' ${commands[@]}
  37. return
  38. fi
  39. build_flags=(
  40. '-a[force reinstallation of packages that are already up-to-date]'
  41. '-n[print the commands but do not run them]'
  42. '-p[number of parallel builds]:number'
  43. '-race[enable data race detection]'
  44. '-x[print the commands]'
  45. '-work[print temporary directory name and keep it]'
  46. '-ccflags[flags for 5c/6c/8c]:flags'
  47. '-gcflags[flags for 5g/6g/8g]:flags'
  48. '-ldflags[flags for 5l/6l/8l]:flags'
  49. '-gccgoflags[flags for gccgo]:flags'
  50. '-compiler[name of compiler to use]:name'
  51. '-installsuffix[suffix to add to package directory]:suffix'
  52. '-tags[list of build tags to consider satisfied]:tags'
  53. )
  54. __go_packages() {
  55. local gopaths
  56. declare -a gopaths
  57. gopaths=("${(s/:/)$(go env GOPATH)}")
  58. gopaths+=("$(go env GOROOT)")
  59. for p in $gopaths; do
  60. _path_files -W "$p/src" -/
  61. done
  62. }
  63. __go_identifiers() {
  64. compadd $(godoc -templates $ZSH/plugins/golang/templates ${words[-2]} 2> /dev/null)
  65. }
  66. case ${words[2]} in
  67. doc)
  68. _arguments -s -w \
  69. "-c[symbol matching honors case (paths not affected)]" \
  70. "-cmd[show symbols with package docs even if package is a command]" \
  71. "-u[show unexported symbols as well as exported]" \
  72. "2:importpaths:__go_packages" \
  73. ":next identifiers:__go_identifiers"
  74. ;;
  75. clean)
  76. _arguments -s -w \
  77. "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
  78. "-n[print the remove commands it would execute, but not run them]" \
  79. "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
  80. "-x[print remove commands as it executes them]" \
  81. "*:importpaths:__go_packages"
  82. ;;
  83. fix|fmt|vet)
  84. _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
  85. ;;
  86. install)
  87. _arguments -s -w : ${build_flags[@]} \
  88. "-v[show package names]" \
  89. '*:importpaths:__go_packages'
  90. ;;
  91. get)
  92. _arguments -s -w : \
  93. ${build_flags[@]}
  94. ;;
  95. build)
  96. _arguments -s -w : \
  97. ${build_flags[@]} \
  98. "-v[show package names]" \
  99. "-o[output file]:file:_files" \
  100. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  101. ;;
  102. test)
  103. _arguments -s -w : \
  104. ${build_flags[@]} \
  105. "-c[do not run, compile the test binary]" \
  106. "-i[do not run, install dependencies]" \
  107. "-v[print test output]" \
  108. "-x[print the commands]" \
  109. "-short[use short mode]" \
  110. "-parallel[number of parallel tests]:number" \
  111. "-cpu[values of GOMAXPROCS to use]:number list" \
  112. "-run[run tests and examples matching regexp]:regexp" \
  113. "-bench[run benchmarks matching regexp]:regexp" \
  114. "-benchmem[print memory allocation stats]" \
  115. "-benchtime[run each benchmark until taking this long]:duration" \
  116. "-blockprofile[write goroutine blocking profile to file]:file" \
  117. "-blockprofilerate[set sampling rate of goroutine blocking profile]:number" \
  118. "-timeout[kill test after that duration]:duration" \
  119. "-cpuprofile[write CPU profile to file]:file:_files" \
  120. "-memprofile[write heap profile to file]:file:_files" \
  121. "-memprofilerate[set heap profiling rate]:number" \
  122. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  123. ;;
  124. list)
  125. _arguments -s -w : \
  126. "-f[alternative format for the list]:format" \
  127. "-json[print data in json format]" \
  128. "-compiled[set CompiledGoFiles to the Go source files presented to the compiler]" \
  129. "-deps[iterate over not just the named packages but also all their dependencies]" \
  130. "-e[change the handling of erroneous packages]" \
  131. "-export[set the Export field to the name of a file containing up-to-date export information for the given package]" \
  132. "-find[identify the named packages but not resolve their dependencies]" \
  133. "-test[report not only the named packages but also their test binaries]" \
  134. "-m[list modules instead of packages]" \
  135. "-u[adds information about available upgrades]" \
  136. "-versions[set the Module's Versions field to a list of all known versions of that module]:number" \
  137. "*:importpaths:__go_packages"
  138. ;;
  139. mod)
  140. typeset -a mod_commands
  141. mod_commands+=(
  142. 'download[download modules to local cache]'
  143. 'edit[edit go.mod from tools or scripts]'
  144. 'graph[print module requirement graph]'
  145. 'init[initialize new module in current directory]'
  146. 'tidy[add missing and remove unused modules]'
  147. 'vendor[make vendored copy of dependencies]'
  148. 'verify[verify dependencies have expected content]'
  149. 'why[explain why packages or modules are needed]'
  150. )
  151. if (( CURRENT == 3 )); then
  152. _values 'go mod commands' ${mod_commands[@]} "help[display help]"
  153. return
  154. fi
  155. case ${words[3]} in
  156. help)
  157. _values 'go mod commands' ${mod_commands[@]}
  158. ;;
  159. download)
  160. _arguments -s -w : \
  161. "-json[print a sequence of JSON objects standard output]" \
  162. "*:flags"
  163. ;;
  164. edit)
  165. _arguments -s -w : \
  166. "-fmt[reformat the go.mod file]" \
  167. "-module[change the module's path]" \
  168. "-replace[=old{@v}=new{@v} add a replacement of the given module path and version pair]:name" \
  169. "-dropreplace[=old{@v}=new{@v} drop a replacement of the given module path and version pair]:name" \
  170. "-go[={version} set the expected Go language version]:number" \
  171. "-print[print the final go.mod in its text format]" \
  172. "-json[print the final go.mod file in JSON format]" \
  173. "*:flags"
  174. ;;
  175. graph)
  176. ;;
  177. init)
  178. ;;
  179. tidy)
  180. _arguments -s -w : \
  181. "-v[print information about removed modules]" \
  182. "*:flags"
  183. ;;
  184. vendor)
  185. _arguments -s -w : \
  186. "-v[print the names of vendored]" \
  187. "*:flags"
  188. ;;
  189. verify)
  190. ;;
  191. why)
  192. _arguments -s -w : \
  193. "-m[treats the arguments as a list of modules and finds a path to any package in each of the modules]" \
  194. "-vendor[exclude tests of dependencies]" \
  195. "*:importpaths:__go_packages"
  196. ;;
  197. esac
  198. ;;
  199. help)
  200. _values "${commands[@]}" \
  201. 'environment[show Go environment variables available]' \
  202. 'gopath[GOPATH environment variable]' \
  203. 'packages[description of package lists]' \
  204. 'remote[remote import path syntax]' \
  205. 'testflag[description of testing flags]' \
  206. 'testfunc[description of testing functions]'
  207. ;;
  208. run)
  209. _arguments -s -w : \
  210. ${build_flags[@]} \
  211. '*:file:_files -g "*.go"'
  212. ;;
  213. tool)
  214. if (( CURRENT == 3 )); then
  215. _values "go tool" $(go tool)
  216. return
  217. fi
  218. case ${words[3]} in
  219. [568]g)
  220. _arguments -s -w : \
  221. '-I[search for packages in DIR]:includes:_path_files -/' \
  222. '-L[show full path in file:line prints]' \
  223. '-S[print the assembly language]' \
  224. '-V[print the compiler version]' \
  225. '-e[no limit on number of errors printed]' \
  226. '-h[panic on an error]' \
  227. '-l[disable inlining]' \
  228. '-m[print optimization decisions]' \
  229. '-o[file specify output file]:file' \
  230. '-p[assumed import path for this code]:importpath' \
  231. '-u[disable package unsafe]' \
  232. "*:file:_files -g '*.go'"
  233. ;;
  234. [568]l)
  235. local O=${words[3]%l}
  236. _arguments -s -w : \
  237. '-o[file specify output file]:file' \
  238. '-L[search for packages in DIR]:includes:_path_files -/' \
  239. "*:file:_files -g '*.[ao$O]'"
  240. ;;
  241. dist)
  242. _values "dist tool" banner bootstrap clean env install version
  243. ;;
  244. *)
  245. # use files by default
  246. _files
  247. ;;
  248. esac
  249. ;;
  250. esac
  251. }
  252. compdef __go_tool_complete go
  253. # aliases: go<~>
  254. alias gob='go build'
  255. alias goc='go clean'
  256. alias god='go doc'
  257. alias gof='go fmt'
  258. alias gofa='go fmt ./...'
  259. alias gog='go get'
  260. alias goi='go install'
  261. alias gol='go list'
  262. alias gom='go mod'
  263. alias gop='cd $GOPATH'
  264. alias gopb='cd $GOPATH/bin'
  265. alias gops='cd $GOPATH/src'
  266. alias gor='go run'
  267. alias got='go test'
  268. alias gov='go vet'