golang.plugin.zsh 5.3 KB

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