golang.plugin.zsh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. 'run[compile and run Go program]'
  28. 'test[test packages]'
  29. 'tool[run specified go tool]'
  30. 'version[print Go version]'
  31. 'vet[run go tool vet on packages]'
  32. )
  33. if (( CURRENT == 2 )); then
  34. # explain go commands
  35. _values 'go tool commands' ${commands[@]}
  36. return
  37. fi
  38. build_flags=(
  39. '-a[force reinstallation of packages that are already up-to-date]'
  40. '-n[print the commands but do not run them]'
  41. '-p[number of parallel builds]:number'
  42. '-race[enable data race detection]'
  43. '-x[print the commands]'
  44. '-work[print temporary directory name and keep it]'
  45. '-ccflags[flags for 5c/6c/8c]:flags'
  46. '-gcflags[flags for 5g/6g/8g]:flags'
  47. '-ldflags[flags for 5l/6l/8l]:flags'
  48. '-gccgoflags[flags for gccgo]:flags'
  49. '-compiler[name of compiler to use]:name'
  50. '-installsuffix[suffix to add to package directory]:suffix'
  51. '-tags[list of build tags to consider satisfied]:tags'
  52. )
  53. __go_packages() {
  54. local gopaths
  55. declare -a gopaths
  56. gopaths=("${(s/:/)$(go env GOPATH)}")
  57. gopaths+=("$(go env GOROOT)")
  58. for p in $gopaths; do
  59. _path_files -W "$p/src" -/
  60. done
  61. }
  62. __go_identifiers() {
  63. compadd $(godoc -templates $ZSH/plugins/golang/templates ${words[-2]} 2> /dev/null)
  64. }
  65. case ${words[2]} in
  66. doc)
  67. _arguments -s -w \
  68. "-c[symbol matching honors case (paths not affected)]" \
  69. "-cmd[show symbols with package docs even if package is a command]" \
  70. "-u[show unexported symbols as well as exported]" \
  71. "2:importpaths:__go_packages" \
  72. ":next identifiers:__go_identifiers"
  73. ;;
  74. clean)
  75. _arguments -s -w \
  76. "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
  77. "-n[print the remove commands it would execute, but not run them]" \
  78. "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
  79. "-x[print remove commands as it executes them]" \
  80. "*:importpaths:__go_packages"
  81. ;;
  82. fix|fmt|list|vet)
  83. _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
  84. ;;
  85. install)
  86. _arguments -s -w : ${build_flags[@]} \
  87. "-v[show package names]" \
  88. '*:importpaths:__go_packages'
  89. ;;
  90. get)
  91. _arguments -s -w : \
  92. ${build_flags[@]}
  93. ;;
  94. build)
  95. _arguments -s -w : \
  96. ${build_flags[@]} \
  97. "-v[show package names]" \
  98. "-o[output file]:file:_files" \
  99. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  100. ;;
  101. test)
  102. _arguments -s -w : \
  103. ${build_flags[@]} \
  104. "-c[do not run, compile the test binary]" \
  105. "-i[do not run, install dependencies]" \
  106. "-v[print test output]" \
  107. "-x[print the commands]" \
  108. "-short[use short mode]" \
  109. "-parallel[number of parallel tests]:number" \
  110. "-cpu[values of GOMAXPROCS to use]:number list" \
  111. "-run[run tests and examples matching regexp]:regexp" \
  112. "-bench[run benchmarks matching regexp]:regexp" \
  113. "-benchmem[print memory allocation stats]" \
  114. "-benchtime[run each benchmark until taking this long]:duration" \
  115. "-blockprofile[write goroutine blocking profile to file]:file" \
  116. "-blockprofilerate[set sampling rate of goroutine blocking profile]:number" \
  117. "-timeout[kill test after that duration]:duration" \
  118. "-cpuprofile[write CPU profile to file]:file:_files" \
  119. "-memprofile[write heap profile to file]:file:_files" \
  120. "-memprofilerate[set heap profiling rate]:number" \
  121. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  122. ;;
  123. help)
  124. _values "${commands[@]}" \
  125. 'environment[show Go environment variables available]' \
  126. 'gopath[GOPATH environment variable]' \
  127. 'packages[description of package lists]' \
  128. 'remote[remote import path syntax]' \
  129. 'testflag[description of testing flags]' \
  130. 'testfunc[description of testing functions]'
  131. ;;
  132. run)
  133. _arguments -s -w : \
  134. ${build_flags[@]} \
  135. '*:file:_files -g "*.go"'
  136. ;;
  137. tool)
  138. if (( CURRENT == 3 )); then
  139. _values "go tool" $(go tool)
  140. return
  141. fi
  142. case ${words[3]} in
  143. [568]g)
  144. _arguments -s -w : \
  145. '-I[search for packages in DIR]:includes:_path_files -/' \
  146. '-L[show full path in file:line prints]' \
  147. '-S[print the assembly language]' \
  148. '-V[print the compiler version]' \
  149. '-e[no limit on number of errors printed]' \
  150. '-h[panic on an error]' \
  151. '-l[disable inlining]' \
  152. '-m[print optimization decisions]' \
  153. '-o[file specify output file]:file' \
  154. '-p[assumed import path for this code]:importpath' \
  155. '-u[disable package unsafe]' \
  156. "*:file:_files -g '*.go'"
  157. ;;
  158. [568]l)
  159. local O=${words[3]%l}
  160. _arguments -s -w : \
  161. '-o[file specify output file]:file' \
  162. '-L[search for packages in DIR]:includes:_path_files -/' \
  163. "*:file:_files -g '*.[ao$O]'"
  164. ;;
  165. dist)
  166. _values "dist tool" banner bootstrap clean env install version
  167. ;;
  168. *)
  169. # use files by default
  170. _files
  171. ;;
  172. esac
  173. ;;
  174. esac
  175. }
  176. compdef __go_tool_complete go
  177. # aliases: go<~>
  178. alias gob='go build'
  179. alias goc='go clean'
  180. alias god='go doc'
  181. alias gof='go fmt'
  182. alias gofa='go fmt ./...'
  183. alias gog='go get'
  184. alias goi='go install'
  185. alias gol='go list'
  186. alias gop='cd $GOPATH'
  187. alias gopb='cd $GOPATH/bin'
  188. alias gops='cd $GOPATH/src'
  189. alias gor='go run'
  190. alias got='go test'
  191. alias gov='go vet'