diff --git a/syntax_checkers/go.vim b/syntax_checkers/go.vim index b48d07d5..8270915f 100644 --- a/syntax_checkers/go.vim +++ b/syntax_checkers/go.vim @@ -18,5 +18,5 @@ if exists("loaded_go_syntax_checker") endif let loaded_go_syntax_checker = 1 -let s:supported_checkers = ["go", "6g", "gofmt"] +let s:supported_checkers = ["go", "gofmt"] call SyntasticLoadChecker(s:supported_checkers, 'go') diff --git a/syntax_checkers/go/6g.vim b/syntax_checkers/go/6g.vim deleted file mode 100644 index 1a4249ed..00000000 --- a/syntax_checkers/go/6g.vim +++ /dev/null @@ -1,17 +0,0 @@ -"============================================================================ -"File: 6g.vim -"Description: Syntax checking plugin for syntastic.vim -"Maintainer: Sam Nguyen -"License: This program is free software. It comes without any warranty, -" to the extent permitted by applicable law. You can redistribute -" it and/or modify it under the terms of the Do What The Fuck You -" Want To Public License, Version 2, as published by Sam Hocevar. -" See http://sam.zoy.org/wtfpl/COPYING for more details. -" -"============================================================================ -function! SyntaxCheckers_go_GetLocList() - let makeprg = '6g -o /dev/null %' - let errorformat = '%E%f:%l: %m' - - return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) -endfunction diff --git a/syntax_checkers/go/go.vim b/syntax_checkers/go/go.vim index 3cbbc023..ddb32a93 100644 --- a/syntax_checkers/go/go.vim +++ b/syntax_checkers/go/go.vim @@ -1,6 +1,6 @@ "============================================================================ "File: go.vim -"Description: Check go syntax using 'go build' +"Description: Check go syntax using 'gofmt -l' followed by 'go [build|test]' "Maintainer: Kamil Kisiel "License: This program is free software. It comes without any warranty, " to the extent permitted by applicable law. You can redistribute @@ -8,10 +8,44 @@ " Want To Public License, Version 2, as published by Sam Hocevar. " See http://sam.zoy.org/wtfpl/COPYING for more details. " +" This syntax checker does not reformat your source code. +" Use a BufWritePre autocommand to that end: +" autocmd FileType go autocmd BufWritePre Fmt "============================================================================ function! SyntaxCheckers_go_GetLocList() - let makeprg = 'go build -o /dev/null' - let errorformat = '%f:%l:%c:%m,%E%f:%l:%m,%C%m,%-G#%.%#' + " Check with gofmt first, since `go build` and `go test` might not report + " syntax errors in the current file if another file with syntax error is + " compiled first. + let makeprg = 'gofmt -l % 1>/dev/null' + let errorformat = '%f:%l:%c: %m,%-G%.%#' + let errors = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat, 'defaults': {'type': 'e'} }) - return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) + if !empty(errors) + return errors + endif + + " Test files, i.e. files with a name ending in `_test.go`, are not + " compiled by `go build`, therefore `go test` must be called for those. + if match(expand('%'), '_test.go$') == -1 + let makeprg = 'go build -o /dev/null' + else + let makeprg = 'go test -c -o /dev/null' + endif + let errorformat = '%f:%l:%c:%m,%f:%l%m,%-G#%.%#' + + " The go compiler needs to either be run with an import path as an + " argument or directly from the package directory. Since figuring out + " the poper import path is fickle, just pushd/popd to the package. + let popd = getcwd() + let pushd = expand('%:p:h') + " + " pushd + exec 'lcd ' . fnameescape(pushd) + + let errors = SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat }) + + " popd + exec 'lcd ' . fnameescape(popd) + + return errors endfunction diff --git a/syntax_checkers/go/gofmt.vim b/syntax_checkers/go/gofmt.vim index 77abe7e8..982beaa9 100644 --- a/syntax_checkers/go/gofmt.vim +++ b/syntax_checkers/go/gofmt.vim @@ -1,6 +1,6 @@ "============================================================================ "File: gofmt.vim -"Description: Check go syntax using gofmt +"Description: Check go syntax using 'gofmt -l' "Maintainer: Brandon Thomson "License: This program is free software. It comes without any warranty, " to the extent permitted by applicable law. You can redistribute @@ -8,9 +8,12 @@ " Want To Public License, Version 2, as published by Sam Hocevar. " See http://sam.zoy.org/wtfpl/COPYING for more details. " +" This syntax checker does not reformat your source code. +" Use a BufWritePre autocommand to that end: +" autocmd FileType go autocmd BufWritePre Fmt "============================================================================ function! SyntaxCheckers_go_GetLocList() - let makeprg = 'gofmt %' + let makeprg = 'gofmt -l % 1>/dev/null' let errorformat = '%f:%l:%c: %m,%-G%.%#' return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat, 'defaults': {'type': 'e'} }) endfunction