2e0cf4a3b07c027312e0c419e083fe125b5883c5

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

refactor(golang): move completion out of the plugin

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/golang/_golang b/plugins/golang/_golang
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..01b976b129d7018a3abfed55b9179ca77f7509d9
  4--- /dev/null
  5+++ b/plugins/golang/_golang
  6@@ -0,0 +1,249 @@
  7+#compdef go
  8+
  9+__go_packages() {
 10+  local gopaths
 11+  declare -a gopaths
 12+  gopaths=("${(s/:/)$(go env GOPATH)}")
 13+  gopaths+=("$(go env GOROOT)")
 14+  for p in $gopaths; do
 15+    _path_files -W "$p/src" -/
 16+  done
 17+}
 18+
 19+__go_identifiers() {
 20+  local tmpl_path="${functions_source[$0]:h}/templates"
 21+  compadd $(godoc -templates "$tmpl_path" ${words[-2]} 2> /dev/null)
 22+}
 23+
 24+_go() {
 25+  typeset -a commands build_flags
 26+  commands+=(
 27+    'build[compile packages and dependencies]'
 28+    'clean[remove object files]'
 29+    'doc[run godoc on package sources]'
 30+    'env[print Go environment information]'
 31+    'fix[run go tool fix on packages]'
 32+    'fmt[run gofmt on package sources]'
 33+    'generate[generate Go files by processing source]'
 34+    'get[download and install packages and dependencies]'
 35+    'help[display help]'
 36+    'install[compile and install packages and dependencies]'
 37+    'list[list packages]'
 38+    'mod[modules maintenance]'
 39+    'run[compile and run Go program]'
 40+    'test[test packages]'
 41+    'tool[run specified go tool]'
 42+    'version[print Go version]'
 43+    'vet[run go tool vet on packages]'
 44+  )
 45+  if (( CURRENT == 2 )); then
 46+    # explain go commands
 47+    _values 'go tool commands' ${commands[@]}
 48+    return
 49+  fi
 50+  build_flags=(
 51+    '-a[force reinstallation of packages that are already up to date]'
 52+    '-n[print the commands but do not run them]'
 53+    '-p[number of parallel builds]:number'
 54+    '-race[enable data race detection]'
 55+    '-x[print the commands]'
 56+    '-work[print temporary directory name and keep it]'
 57+    '-ccflags[flags for 5c/6c/8c]:flags'
 58+    '-gcflags[flags for 5g/6g/8g]:flags'
 59+    '-ldflags[flags for 5l/6l/8l]:flags'
 60+    '-gccgoflags[flags for gccgo]:flags'
 61+    '-compiler[name of compiler to use]:name'
 62+    '-installsuffix[suffix to add to package directory]:suffix'
 63+    '-tags[list of build tags to consider satisfied]:tags'
 64+  )
 65+
 66+  case ${words[2]} in
 67+  doc)
 68+    _arguments -s -w \
 69+      "-c[symbol matching honors case (paths not affected)]" \
 70+      "-cmd[show symbols with package docs even if package is a command]" \
 71+      "-u[show unexported symbols as well as exported]" \
 72+      "2:importpaths:__go_packages" \
 73+      ":next identifiers:__go_identifiers"
 74+    ;;
 75+  clean)
 76+    _arguments -s -w \
 77+      "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
 78+      "-n[print the remove commands it would execute, but not run them]" \
 79+      "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
 80+      "-x[print remove commands as it executes them]" \
 81+      "*:importpaths:__go_packages"
 82+    ;;
 83+  fix|fmt|vet)
 84+    _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
 85+    ;;
 86+  install)
 87+    _arguments -s -w : ${build_flags[@]} \
 88+      "-v[show package names]" \
 89+      '*:importpaths:__go_packages'
 90+    ;;
 91+  get)
 92+    _arguments -s -w : \
 93+      ${build_flags[@]}
 94+    ;;
 95+  build)
 96+    _arguments -s -w : \
 97+      ${build_flags[@]} \
 98+      "-v[show package names]" \
 99+      "-o[output file]:file:_files" \
100+      "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
101+    ;;
102+  test)
103+    _arguments -s -w : \
104+      ${build_flags[@]} \
105+      "-c[do not run, compile the test binary]" \
106diff --git a/plugins/golang/golang.plugin.zsh b/plugins/golang/golang.plugin.zsh
107index 6d1eb40c2452edda8aeee5bf23fd97f1344d62cb..4dafecd8653d728570eb3cedc81f18e6398049eb 100644
108--- a/plugins/golang/golang.plugin.zsh
109+++ b/plugins/golang/golang.plugin.zsh
110@@ -1,3 +1,7 @@
111+## completion
112+compctl -g "*.go" gofmt # standard go tools
113+compctl -g "*.go" gccgo # gccgo
114+
115 # gc
116 for p in 5 6 8; do
117   compctl -g "*.${p}" ${p}l
118@@ -5,256 +9,7 @@ for p in 5 6 8; do
119 done
120 unset p
121 
122-# standard go tools
123-compctl -g "*.go" gofmt
124-
125-# gccgo
126-compctl -g "*.go" gccgo
127-
128-# go tool
129-__go_tool_complete() {
130-  typeset -a commands build_flags
131-  commands+=(
132-    'build[compile packages and dependencies]'
133-    'clean[remove object files]'
134-    'doc[run godoc on package sources]'
135-    'env[print Go environment information]'
136-    'fix[run go tool fix on packages]'
137-    'fmt[run gofmt on package sources]'
138-    'generate[generate Go files by processing source]'
139-    'get[download and install packages and dependencies]'
140-    'help[display help]'
141-    'install[compile and install packages and dependencies]'
142-    'list[list packages]'
143-    'mod[modules maintenance]'
144-    'run[compile and run Go program]'
145-    'test[test packages]'
146-    'tool[run specified go tool]'
147-    'version[print Go version]'
148-    'vet[run go tool vet on packages]'
149-  )
150-  if (( CURRENT == 2 )); then
151-    # explain go commands
152-    _values 'go tool commands' ${commands[@]}
153-    return
154-  fi
155-  build_flags=(
156-    '-a[force reinstallation of packages that are already up to date]'
157-    '-n[print the commands but do not run them]'
158-    '-p[number of parallel builds]:number'
159-    '-race[enable data race detection]'
160-    '-x[print the commands]'
161-    '-work[print temporary directory name and keep it]'
162-    '-ccflags[flags for 5c/6c/8c]:flags'
163-    '-gcflags[flags for 5g/6g/8g]:flags'
164-    '-ldflags[flags for 5l/6l/8l]:flags'
165-    '-gccgoflags[flags for gccgo]:flags'
166-    '-compiler[name of compiler to use]:name'
167-    '-installsuffix[suffix to add to package directory]:suffix'
168-    '-tags[list of build tags to consider satisfied]:tags'
169-  )
170-  __go_packages() {
171-      local gopaths
172-      declare -a gopaths
173-      gopaths=("${(s/:/)$(go env GOPATH)}")
174-      gopaths+=("$(go env GOROOT)")
175-      for p in $gopaths; do
176-        _path_files -W "$p/src" -/
177-      done
178-  }
179-  __go_identifiers() {
180-      compadd $(godoc -templates $ZSH/plugins/golang/templates ${words[-2]} 2> /dev/null)
181-  }
182-  case ${words[2]} in
183-  doc)
184-    _arguments -s -w \
185-      "-c[symbol matching honors case (paths not affected)]" \
186-      "-cmd[show symbols with package docs even if package is a command]" \
187-      "-u[show unexported symbols as well as exported]" \
188-      "2:importpaths:__go_packages" \
189-      ":next identifiers:__go_identifiers"
190-      ;;
191-  clean)
192-    _arguments -s -w \
193-      "-i[remove the corresponding installed archive or binary (what 'go install' would create)]" \
194-      "-n[print the remove commands it would execute, but not run them]" \
195-      "-r[apply recursively to all the dependencies of the packages named by the import paths]" \
196-      "-x[print remove commands as it executes them]" \
197-      "*:importpaths:__go_packages"
198-      ;;
199-  fix|fmt|vet)
200-      _alternative ':importpaths:__go_packages' ':files:_path_files -g "*.go"'
201-      ;;
202-  install)
203-      _arguments -s -w : ${build_flags[@]} \
204-        "-v[show package names]" \
205-        '*:importpaths:__go_packages'
206-      ;;
207-  get)
208-      _arguments -s -w : \
209-        ${build_flags[@]}