golang.plugin.zsh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 'gopath[GOPATH environment variable]' \
  126. 'packages[description of package lists]' \
  127. 'remote[remote import path syntax]' \
  128. 'testflag[description of testing flags]' \
  129. 'testfunc[description of testing functions]'
  130. ;;
  131. run)
  132. _arguments -s -w : \
  133. ${build_flags[@]} \
  134. '*:file:_path_files -g "*.go"'
  135. ;;
  136. tool)
  137. if (( CURRENT == 3 )); then
  138. _values "go tool" $(go tool)
  139. return
  140. fi
  141. case ${words[3]} in
  142. [568]g)
  143. _arguments -s -w : \
  144. '-I[search for packages in DIR]:includes:_path_files -/' \
  145. '-L[show full path in file:line prints]' \
  146. '-S[print the assembly language]' \
  147. '-V[print the compiler version]' \
  148. '-e[no limit on number of errors printed]' \
  149. '-h[panic on an error]' \
  150. '-l[disable inlining]' \
  151. '-m[print optimization decisions]' \
  152. '-o[file specify output file]:file' \
  153. '-p[assumed import path for this code]:importpath' \
  154. '-u[disable package unsafe]' \
  155. "*:file:_files -g '*.go'"
  156. ;;
  157. [568]l)
  158. local O=${words[3]%l}
  159. _arguments -s -w : \
  160. '-o[file specify output file]:file' \
  161. '-L[search for packages in DIR]:includes:_path_files -/' \
  162. "*:file:_files -g '*.[ao$O]'"
  163. ;;
  164. dist)
  165. _values "dist tool" banner bootstrap clean env install version
  166. ;;
  167. *)
  168. # use files by default
  169. _files
  170. ;;
  171. esac
  172. ;;
  173. esac
  174. }
  175. compdef __go_tool_complete go
  176. # aliases
  177. alias gfa='go fmt . ./...'