golang.plugin.zsh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # gc
  2. for p in 5 6 8; do
  3. compctl -g "*.${p}" ${p}l
  4. compctl -g "*.go" ${p}g
  5. done
  6. unset p
  7. # standard go tools
  8. compctl -g "*.go" gofmt
  9. # gccgo
  10. compctl -g "*.go" gccgo
  11. # go tool
  12. __go_tool_complete() {
  13. typeset -a commands build_flags
  14. commands+=(
  15. 'build[compile packages and dependencies]'
  16. 'clean[remove object files]'
  17. 'doc[run godoc on package sources]'
  18. 'env[print Go environment information]'
  19. 'fix[run go tool fix on packages]'
  20. 'fmt[run gofmt on package sources]'
  21. 'generate[generate Go files by processing source]'
  22. 'get[download and install packages and dependencies]'
  23. 'help[display help]'
  24. 'install[compile and install packages and dependencies]'
  25. 'list[list packages]'
  26. 'mod[modules maintenance]'
  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|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. list)
  124. _arguments -s -w : \
  125. "-f[alternative format for the list]:format" \
  126. "-json[print data in json format]" \
  127. "-compiled[set CompiledGoFiles to the Go source files presented to the compiler]" \
  128. "-deps[iterate over not just the named packages but also all their dependencies]" \
  129. "-e[change the handling of erroneous packages]" \
  130. "-export[set the Export field to the name of a file containing up-to-date export information for the given package]" \
  131. "-find[identify the named packages but not resolve their dependencies]" \
  132. "-test[report not only the named packages but also their test binaries]" \
  133. "-m[list modules instead of packages]" \
  134. "-u[adds information about available upgrades]" \
  135. "-versions[set the Module's Versions field to a list of all known versions of that module]:number" \
  136. "*:importpaths:__go_packages"
  137. ;;
  138. mod)
  139. typeset -a mod_commands
  140. mod_commands+=(
  141. 'download[download modules to local cache]'
  142. 'edit[edit go.mod from tools or scripts]'
  143. 'graph[print module requirement graph]'
  144. 'init[initialize new module in current directory]'
  145. 'tidy[add missing and remove unused modules]'
  146. 'vendor[make vendored copy of dependencies]'
  147. 'verify[verify dependencies have expected content]'
  148. 'why[explain why packages or modules are needed]'
  149. )
  150. if (( CURRENT == 3 )); then
  151. _values 'go mod commands' ${mod_commands[@]} "help[display help]"
  152. return
  153. fi
  154. case ${words[3]} in
  155. help)
  156. _values 'go mod commands' ${mod_commands[@]}
  157. ;;
  158. download)
  159. _arguments -s -w : \
  160. "-json[print a sequence of JSON objects standard output]" \
  161. "*:flags"
  162. ;;
  163. edit)
  164. _arguments -s -w : \
  165. "-fmt[reformat the go.mod file]" \
  166. "-module[change the module's path]" \
  167. "-replace[=old{@v}=new{@v} add a replacement of the given module path and version pair]:name" \
  168. "-dropreplace[=old{@v}=new{@v} drop a replacement of the given module path and version pair]:name" \
  169. "-go[={version} set the expected Go language version]:number" \
  170. "-print[print the final go.mod in its text format]" \
  171. "-json[print the final go.mod file in JSON format]" \
  172. "*:flags"
  173. ;;
  174. graph)
  175. ;;
  176. init)
  177. ;;
  178. tidy)
  179. _arguments -s -w : \
  180. "-v[print information about removed modules]" \
  181. "*:flags"
  182. ;;
  183. vendor)
  184. _arguments -s -w : \
  185. "-v[print the names of vendored]" \
  186. "*:flags"
  187. ;;
  188. verify)
  189. ;;
  190. why)
  191. _arguments -s -w : \
  192. "-m[treats the arguments as a list of modules and finds a path to any package in each of the modules]" \
  193. "-vendor[exclude tests of dependencies]" \
  194. "*:importpaths:__go_packages"
  195. ;;
  196. esac
  197. ;;
  198. help)
  199. _values "${commands[@]}" \
  200. 'environment[show Go environment variables available]' \
  201. 'gopath[GOPATH environment variable]' \
  202. 'packages[description of package lists]' \
  203. 'remote[remote import path syntax]' \
  204. 'testflag[description of testing flags]' \
  205. 'testfunc[description of testing functions]'
  206. ;;
  207. run)
  208. _arguments -s -w : \
  209. ${build_flags[@]} \
  210. '*:file:_files -g "*.go"'
  211. ;;
  212. tool)
  213. if (( CURRENT == 3 )); then
  214. _values "go tool" $(go tool)
  215. return
  216. fi
  217. case ${words[3]} in
  218. [568]g)
  219. _arguments -s -w : \
  220. '-I[search for packages in DIR]:includes:_path_files -/' \
  221. '-L[show full path in file:line prints]' \
  222. '-S[print the assembly language]' \
  223. '-V[print the compiler version]' \
  224. '-e[no limit on number of errors printed]' \
  225. '-h[panic on an error]' \
  226. '-l[disable inlining]' \
  227. '-m[print optimization decisions]' \
  228. '-o[file specify output file]:file' \
  229. '-p[assumed import path for this code]:importpath' \
  230. '-u[disable package unsafe]' \
  231. "*:file:_files -g '*.go'"
  232. ;;
  233. [568]l)
  234. local O=${words[3]%l}
  235. _arguments -s -w : \
  236. '-o[file specify output file]:file' \
  237. '-L[search for packages in DIR]:includes:_path_files -/' \
  238. "*:file:_files -g '*.[ao$O]'"
  239. ;;
  240. dist)
  241. _values "dist tool" banner bootstrap clean env install version
  242. ;;
  243. *)
  244. # use files by default
  245. _files
  246. ;;
  247. esac
  248. ;;
  249. esac
  250. }
  251. compdef __go_tool_complete go
  252. # aliases: go<~>
  253. alias gob='go build'
  254. alias goc='go clean'
  255. alias god='go doc'
  256. alias gof='go fmt'
  257. alias gofa='go fmt ./...'
  258. alias gofx='go fix'
  259. alias gog='go get'
  260. alias goga='go get ./...'
  261. alias goi='go install'
  262. alias gol='go list'
  263. alias gom='go mod'
  264. alias gopa='cd $GOPATH'
  265. alias gopb='cd $GOPATH/bin'
  266. alias gops='cd $GOPATH/src'
  267. alias gor='go run'
  268. alias got='go test'
  269. alias gota='go test ./...'
  270. alias goto='go tool'
  271. alias gotoc='go tool compile'
  272. alias gotod='go tool dist'
  273. alias gotofx='go tool fix'
  274. alias gov='go vet'