diff --git a/README.md b/README.md index c6bf76e4..55be933a 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ formatting. | FusionScript | [fusion-lint](https://github.com/RyanSquared/fusionscript) | | Git Commit Messages | [gitlint](https://github.com/jorisroovers/gitlint) | | GLSL | [glslang](https://github.com/KhronosGroup/glslang), [glslls](https://github.com/svenstaro/glsl-language-server) | -| Go | [gofmt](https://golang.org/cmd/gofmt/), [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports), [go vet](https://golang.org/cmd/vet/), [golint](https://godoc.org/github.com/golang/lint), [gometalinter](https://github.com/alecthomas/gometalinter) !!, [go build](https://golang.org/cmd/go/) !!, [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) !!, [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) !! | +| Go | [gofmt](https://golang.org/cmd/gofmt/), [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports), [go vet](https://golang.org/cmd/vet/), [golint](https://godoc.org/github.com/golang/lint), [gotype](https://godoc.org/golang.org/x/tools/cmd/gotype), [gometalinter](https://github.com/alecthomas/gometalinter) !!, [go build](https://golang.org/cmd/go/) !!, [gosimple](https://github.com/dominikh/go-tools/tree/master/cmd/gosimple) !!, [staticcheck](https://github.com/dominikh/go-tools/tree/master/cmd/staticcheck) !! | | GraphQL | [eslint](http://eslint.org/), [gqlint](https://github.com/happylinks/gqlint) | | Haml | [haml-lint](https://github.com/brigade/haml-lint) | | Handlebars | [ember-template-lint](https://github.com/rwjblue/ember-template-lint) | diff --git a/ale_linters/go/gotype.vim b/ale_linters/go/gotype.vim new file mode 100644 index 00000000..731f4c92 --- /dev/null +++ b/ale_linters/go/gotype.vim @@ -0,0 +1,23 @@ +" Author: Jelte Fennema +" Description: gotype for Go files + +call ale#linter#Define('go', { +\ 'name': 'gotype', +\ 'output_stream': 'stderr', +\ 'executable': 'gotype', +\ 'command_callback': 'ale_linters#go#gotype#GetCommand', +\ 'callback': 'ale#handlers#unix#HandleAsError', +\}) + +"\ 'command': +function! ale_linters#go#gotype#GetCommand(buffer) abort + let l:cur_file = expand('#' . a:buffer . ':p') + if l:cur_file =~# '_test\.go$' + return + endif + + let l:module_files = globpath(expand('#' . a:buffer . ':p:h'), '*.go', 0, 1) + let l:other_module_files = filter(l:module_files, 'v:val isnot# ' . ale#util#EscapeVim(l:cur_file) . ' && v:val !~# "_test\.go$"') + return 'gotype %t ' . join(map(l:other_module_files, 'ale#Escape(v:val)')) + +endfunction diff --git a/autoload/ale/util.vim b/autoload/ale/util.vim index 1f590adc..b94a11b7 100644 --- a/autoload/ale/util.vim +++ b/autoload/ale/util.vim @@ -236,6 +236,13 @@ function! ale#util#EscapePCRE(unsafe_string) abort return substitute(a:unsafe_string, '\([\-\[\]{}()*+?.^$|]\)', '\\\1', 'g') endfunction +" Escape a string so that it can be used as a literal string inside an evaled +" vim command. +function! ale#util#EscapeVim(unsafe_string) abort + return "'" . substitute(a:unsafe_string, "'", "''", 'g') . "'" +endfunction + + " Given a String or a List of String values, try and decode the string(s) " as a JSON value which can be decoded with json_decode. If the JSON string " is invalid, the default argument value will be returned instead. diff --git a/doc/ale.txt b/doc/ale.txt index 53b19b3e..25c1abe0 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -309,7 +309,7 @@ Notes: * FusionScript: `fusion-lint` * Git Commit Messages: `gitlint` * GLSL: glslang, `glslls` -* Go: `gofmt`, `goimports`, `go vet`, `golint`, `gometalinter`!!, `go build`!!, `gosimple`!!, `staticcheck`!! +* Go: `gofmt`, `goimports`, `go vet`, `golint`, `gotype`, `gometalinter`!!, `go build`!!, `gosimple`!!, `staticcheck`!! * GraphQL: `eslint`, `gqlint` * Haml: `haml-lint` * Handlebars: `ember-template-lint` diff --git a/test/command_callback/test_gotype_command_callback.vader b/test/command_callback/test_gotype_command_callback.vader new file mode 100644 index 00000000..f95e8423 --- /dev/null +++ b/test/command_callback/test_gotype_command_callback.vader @@ -0,0 +1,19 @@ +Before: + runtime ale_linters/go/gotype.vim + call ale#test#SetFilename('../go_files/testfile2.go') + +After: + call ale#linter#Reset() + + +Execute(The gotype callback should include other files from the directory but exclude the file itself): + let dir = expand('#' . bufnr('') . ':p:h') + AssertEqual + \ "gotype %t ". ale#Escape(ale#path#Simplify(dir . "/testfile.go")), + \ ale_linters#go#gotype#GetCommand(bufnr('')) + +Execute(The gotype callback should ignore test files): + call ale#test#SetFilename('bla_test.go') + AssertEqual + \ 0, + \ ale_linters#go#gotype#GetCommand(bufnr('')) diff --git a/test/go_files/testfile2.go b/test/go_files/testfile2.go new file mode 100644 index 00000000..e69de29b