go.plugin.zsh 4.7 KB

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