go.vim 3.6 KB

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