go.vim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "============================================================================
  2. "File: go.vim
  3. "Description: Check go syntax using 'gofmt -l' followed by 'go [build|test]'
  4. "Maintainer: Kamil Kisiel <kamil@kamilkisiel.net>
  5. "License: This program is free software. It comes without any warranty,
  6. " to the extent permitted by applicable law. You can redistribute
  7. " it and/or modify it under the terms of the Do What The Fuck You
  8. " Want To Public License, Version 2, as published by Sam Hocevar.
  9. " See http://sam.zoy.org/wtfpl/COPYING for more details.
  10. "
  11. "============================================================================
  12. "
  13. " This syntax checker does not reformat your source code.
  14. " Use a BufWritePre autocommand to that end:
  15. " autocmd FileType go autocmd BufWritePre <buffer> Fmt
  16. if exists('g:loaded_syntastic_go_go_checker')
  17. finish
  18. endif
  19. let g:loaded_syntastic_go_go_checker = 1
  20. let s:save_cpo = &cpo
  21. set cpo&vim
  22. function! SyntaxCheckers_go_go_IsAvailable() dict
  23. return executable(self.getExec()) && executable('gofmt')
  24. endfunction
  25. function! SyntaxCheckers_go_go_GetLocList() dict
  26. if !exists('s:go_new')
  27. let s:go_new = syntastic#util#versionIsAtLeast(self.getVersion(self.getExecEscaped() . ' version'), [1, 5])
  28. endif
  29. " Check with gofmt first, since `go build` and `go test` might not report
  30. " syntax errors in the current file if another file with syntax error is
  31. " compiled first.
  32. let makeprg = self.makeprgBuild({
  33. \ 'exe': 'gofmt',
  34. \ 'args': '-l',
  35. \ 'tail': '> ' . syntastic#util#DevNull() })
  36. let errorformat =
  37. \ '%f:%l:%c: %m,' .
  38. \ '%-G%.%#'
  39. let errors = SyntasticMake({
  40. \ 'makeprg': makeprg,
  41. \ 'errorformat': errorformat,
  42. \ 'defaults': {'type': 'e'} })
  43. if !empty(errors)
  44. return errors
  45. endif
  46. " Test files, i.e. files with a name ending in `_test.go`, are not
  47. " compiled by `go build`, therefore `go test` must be called for those.
  48. if match(expand('%', 1), '\m_test\.go$') == -1
  49. let cmd = 'build'
  50. let opts = syntastic#util#var('go_go_build_args', s:go_new ? '-buildmode=archive' : '')
  51. let cleanup = 0
  52. else
  53. let cmd = 'test -c'
  54. let opts = syntastic#util#var('go_go_test_args', s:go_new ? '-buildmode=archive' : '')
  55. let cleanup = 1
  56. endif
  57. let opt_str = (type(opts) != type('') || opts !=# '') ? join(syntastic#util#argsescape(opts)) : opts
  58. let makeprg = self.getExecEscaped() . ' ' . cmd . ' ' . opt_str
  59. " The first pattern is for warnings from C compilers.
  60. let errorformat =
  61. \ '%W%f:%l: warning: %m,' .
  62. \ '%E%f:%l:%c:%m,' .
  63. \ '%E%f:%l:%m,' .
  64. \ '%C%\s%\+%m,' .
  65. \ '%+Ecan''t load package: %m,' .
  66. \ '%+Einternal error: %m,' .
  67. \ '%-G#%.%#'
  68. " The go compiler needs to either be run with an import path as an
  69. " argument or directly from the package directory. Since figuring out
  70. " the proper import path is fickle, just cwd to the package.
  71. let errors = SyntasticMake({
  72. \ 'makeprg': makeprg,
  73. \ 'errorformat': errorformat,
  74. \ 'cwd': expand('%:p:h', 1),
  75. \ 'env': {'GOGC': 'off'},
  76. \ 'defaults': {'type': 'e'} })
  77. if cleanup
  78. call delete(expand('%:p:h', 1) . syntastic#util#Slash() . expand('%:p:h:t', 1) . '.test')
  79. endif
  80. return errors
  81. endfunction
  82. call g:SyntasticRegistry.CreateAndRegisterChecker({
  83. \ 'filetype': 'go',
  84. \ 'name': 'go'})
  85. let &cpo = s:save_cpo
  86. unlet s:save_cpo
  87. " vim: set sw=4 sts=4 et fdm=marker: