go.vim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. " Check with gofmt first, since `go build` and `go test` might not report
  27. " syntax errors in the current file if another file with syntax error is
  28. " compiled first.
  29. let makeprg = self.makeprgBuild({
  30. \ 'exe': 'gofmt',
  31. \ 'args': '-l',
  32. \ 'tail': '> ' . syntastic#util#DevNull() })
  33. let errorformat =
  34. \ '%f:%l:%c: %m,' .
  35. \ '%-G%.%#'
  36. let errors = SyntasticMake({
  37. \ 'makeprg': makeprg,
  38. \ 'errorformat': errorformat,
  39. \ 'defaults': {'type': 'e'} })
  40. if !empty(errors)
  41. return errors
  42. endif
  43. " Test files, i.e. files with a name ending in `_test.go`, are not
  44. " compiled by `go build`, therefore `go test` must be called for those.
  45. if match(expand('%', 1), '\m_test\.go$') == -1
  46. let cmd = 'build'
  47. let opts = syntastic#util#var('go_go_build_args')
  48. let cleanup = 0
  49. else
  50. let cmd = 'test -c'
  51. let opts = syntastic#util#var('go_go_test_args')
  52. let cleanup = 1
  53. endif
  54. let opt_str = (type(opts) != type('') || opts !=# '') ? join(syntastic#util#argsescape(opts)) : opts
  55. let makeprg = self.getExec() . ' ' . cmd . ' ' . opt_str . ' ' . syntastic#c#NullOutput()
  56. " The first pattern is for warnings from C compilers.
  57. let errorformat =
  58. \ '%W%f:%l: warning: %m,' .
  59. \ '%E%f:%l:%c:%m,' .
  60. \ '%E%f:%l:%m,' .
  61. \ '%C%\s%\+%m,' .
  62. \ '%-G#%.%#'
  63. " The go compiler needs to either be run with an import path as an
  64. " argument or directly from the package directory. Since figuring out
  65. " the proper import path is fickle, just cwd to the package.
  66. let errors = SyntasticMake({
  67. \ 'makeprg': makeprg,
  68. \ 'errorformat': errorformat,
  69. \ 'cwd': expand('%:p:h', 1),
  70. \ 'defaults': {'type': 'e'} })
  71. if cleanup
  72. call delete(expand('%:p:h', 1) . syntastic#util#Slash() . expand('%:p:h:t', 1) . '.test')
  73. endif
  74. return errors
  75. endfunction
  76. call g:SyntasticRegistry.CreateAndRegisterChecker({
  77. \ 'filetype': 'go',
  78. \ 'name': 'go'})
  79. let &cpo = s:save_cpo
  80. unlet s:save_cpo
  81. " vim: set sw=4 sts=4 et fdm=marker: