golang.plugin.zsh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. _path_files -W "$(go env GOROOT)/src" -/
  55. _path_files -W "$(go env GOPATH)/src" -/
  56. }
  57. __go_identifiers() {
  58. compadd $(godoc -templates $ZSH/plugins/golang/templates ${words[-2]} 2> /dev/null)
  59. }
  60. case ${words[2]} in
  61. doc)
  62. _arguments -s -w \
  63. "-c[symbol matching honors case (paths not affected)]" \
  64. "-cmd[show symbols with package docs even if package is a command]" \
  65. "-u[show unexported symbols as well as exported]" \
  66. "2:importpaths:__go_packages" \
  67. ":next identifiers:__go_identifiers"
  68. ;;
  69. clean)
  70. _arguments -s -w \
  71. "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
  72. "-n[print the remove commands it would execute, but not run them]" \
  73. "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
  74. "-x[print remove commands as it executes them]" \
  75. "*:importpaths:__go_packages"
  76. ;;
  77. fix|fmt|list|vet)
  78. _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
  79. ;;
  80. install)
  81. _arguments -s -w : ${build_flags[@]} \
  82. "-v[show package names]" \
  83. '*:importpaths:__go_packages'
  84. ;;
  85. get)
  86. _arguments -s -w : \
  87. ${build_flags[@]}
  88. ;;
  89. build)
  90. _arguments -s -w : \
  91. ${build_flags[@]} \
  92. "-v[show package names]" \
  93. "-o[output file]:file:_files" \
  94. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  95. ;;
  96. test)
  97. _arguments -s -w : \
  98. ${build_flags[@]} \
  99. "-c[do not run, compile the test binary]" \
  100. "-i[do not run, install dependencies]" \
  101. "-v[print test output]" \
  102. "-x[print the commands]" \
  103. "-short[use short mode]" \
  104. "-parallel[number of parallel tests]:number" \
  105. "-cpu[values of GOMAXPROCS to use]:number list" \
  106. "-run[run tests and examples matching regexp]:regexp" \
  107. "-bench[run benchmarks matching regexp]:regexp" \
  108. "-benchmem[print memory allocation stats]" \
  109. "-benchtime[run each benchmark until taking this long]:duration" \
  110. "-blockprofile[write goroutine blocking profile to file]:file" \
  111. "-blockprofilerate[set sampling rate of goroutine blocking profile]:number" \
  112. "-timeout[kill test after that duration]:duration" \
  113. "-cpuprofile[write CPU profile to file]:file:_files" \
  114. "-memprofile[write heap profile to file]:file:_files" \
  115. "-memprofilerate[set heap profiling rate]:number" \
  116. "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
  117. ;;
  118. help)
  119. _values "${commands[@]}" \
  120. 'gopath[GOPATH environment variable]' \
  121. 'packages[description of package lists]' \
  122. 'remote[remote import path syntax]' \
  123. 'testflag[description of testing flags]' \
  124. 'testfunc[description of testing functions]'
  125. ;;
  126. run)
  127. _arguments -s -w : \
  128. ${build_flags[@]} \
  129. '*:file:_path_files -g "*.go"'
  130. ;;
  131. tool)
  132. if (( CURRENT == 3 )); then
  133. _values "go tool" $(go tool)
  134. return
  135. fi
  136. case ${words[3]} in
  137. [568]g)
  138. _arguments -s -w : \
  139. '-I[search for packages in DIR]:includes:_path_files -/' \
  140. '-L[show full path in file:line prints]' \
  141. '-S[print the assembly language]' \
  142. '-V[print the compiler version]' \
  143. '-e[no limit on number of errors printed]' \
  144. '-h[panic on an error]' \
  145. '-l[disable inlining]' \
  146. '-m[print optimization decisions]' \
  147. '-o[file specify output file]:file' \
  148. '-p[assumed import path for this code]:importpath' \
  149. '-u[disable package unsafe]' \
  150. "*:file:_files -g '*.go'"
  151. ;;
  152. [568]l)
  153. local O=${words[3]%l}
  154. _arguments -s -w : \
  155. '-o[file specify output file]:file' \
  156. '-L[search for packages in DIR]:includes:_path_files -/' \
  157. "*:file:_files -g '*.[ao$O]'"
  158. ;;
  159. dist)
  160. _values "dist tool" banner bootstrap clean env install version
  161. ;;
  162. *)
  163. # use files by default
  164. _files
  165. ;;
  166. esac
  167. ;;
  168. esac
  169. }
  170. compdef __go_tool_complete go
  171. # aliases
  172. alias gfa='go fmt . ./...'