diff options
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/gofmt.vim | 18 | ||||
-rw-r--r-- | doc/ale-go.txt | 10 | ||||
-rw-r--r-- | doc/ale.txt | 1 | ||||
-rw-r--r-- | test/fixers/test_gofmt_fixer_callback.vader | 40 | ||||
-rw-r--r-- | test/go_files/testfile.go | 0 |
6 files changed, 74 insertions, 0 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index e87b02f5..9569d215 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -92,6 +92,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['c', 'cpp'], \ 'description': 'Fix C/C++ files with clang-format.', \ }, +\ 'gofmt': { +\ 'function': 'ale#fixers#gofmt#Fix', +\ 'suggested_filetypes': ['go'], +\ 'description': 'Fix Go files with go fmt.', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/gofmt.vim b/autoload/ale/fixers/gofmt.vim new file mode 100644 index 00000000..66b67a9e --- /dev/null +++ b/autoload/ale/fixers/gofmt.vim @@ -0,0 +1,18 @@ +" Author: aliou <code@aliou.me> +" Description: Integration of gofmt with ALE. + +call ale#Set('go_gofmt_executable', 'gofmt') +call ale#Set('go_gofmt_options', '') + +function! ale#fixers#gofmt#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'go_gofmt_executable') + let l:options = ale#Var(a:buffer, 'go_gofmt_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' -l -w' + \ . (empty(l:options) ? '' : ' ' . l:options) + \ . ' %t', + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/doc/ale-go.txt b/doc/ale-go.txt index 935f491f..c5a68878 100644 --- a/doc/ale-go.txt +++ b/doc/ale-go.txt @@ -21,6 +21,16 @@ while ensuring it doesn't run any linters known to be slow or resource intensive. =============================================================================== +gofmt *ale-go-gofmt* + +g:ale_go_gofmt_options *g:ale_go_gofmt_options* + *b:ale_go_gofmt_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the gofmt fixer. + +=============================================================================== gometalinter *ale-go-gometalinter* `gometalinter` is a `lint_file` linter, which only lints files that are diff --git a/doc/ale.txt b/doc/ale.txt index afdc918c..ba08a454 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -62,6 +62,7 @@ CONTENTS *ale-contents* glsl..................................|ale-glsl-options| glslang.............................|ale-glsl-glslang| go....................................|ale-go-options| + gofmt...............................|ale-go-gofmt| gometalinter........................|ale-go-gometalinter| graphql...............................|ale-graphql-options| gqlint..............................|ale-graphql-gqlint| diff --git a/test/fixers/test_gofmt_fixer_callback.vader b/test/fixers/test_gofmt_fixer_callback.vader new file mode 100644 index 00000000..14e6e063 --- /dev/null +++ b/test/fixers/test_gofmt_fixer_callback.vader @@ -0,0 +1,40 @@ +Before: + Save g:ale_go_gofmt_executable + Save g:ale_go_gofmt_options + + " Use an invalid global executable, so we don't match it. + let g:ale_go_gofmt_executable = 'xxxinvalid' + let g:ale_go_gofmt_options = '' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(The gofmt callback should return the correct default values): + call ale#test#SetFilename('../go_files/testfile.go') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape('xxxinvalid') + \ . ' -l -w' + \ . ' %t', + \ }, + \ ale#fixers#gofmt#Fix(bufnr('')) + +Execute(The gofmt callback should include custom gofmt options): + let g:ale_go_gofmt_options = "-r '(a) -> a'" + call ale#test#SetFilename('../go_files/testfile.go') + + AssertEqual + \ { + \ 'read_temporary_file': 1, + \ 'command': ale#Escape('xxxinvalid') + \ . ' -l -w' + \ . ' ' . g:ale_go_gofmt_options + \ . ' %t', + \ }, + \ ale#fixers#gofmt#Fix(bufnr('')) diff --git a/test/go_files/testfile.go b/test/go_files/testfile.go new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/test/go_files/testfile.go |