golang.plugin.zsh 5.3 KB

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