From e376f0ae44f0656021a8c8945212dc27105b34fe Mon Sep 17 00:00:00 2001 From: aliou Date: Tue, 3 Oct 2017 19:54:35 +0200 Subject: [PATCH] gofmt fixer for Go (#970) Add a gofmt fixer for golang. --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/gofmt.vim | 18 ++++++++++ doc/ale-go.txt | 10 ++++++ doc/ale.txt | 1 + test/fixers/test_gofmt_fixer_callback.vader | 40 +++++++++++++++++++++ test/go_files/testfile.go | 0 6 files changed, 74 insertions(+) create mode 100644 autoload/ale/fixers/gofmt.vim create mode 100644 test/fixers/test_gofmt_fixer_callback.vader create mode 100644 test/go_files/testfile.go diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index e87b02f..9569d21 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 0000000..66b67a9 --- /dev/null +++ b/autoload/ale/fixers/gofmt.vim @@ -0,0 +1,18 @@ +" Author: aliou +" 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 935f491..c5a6887 100644 --- a/doc/ale-go.txt +++ b/doc/ale-go.txt @@ -20,6 +20,16 @@ the benefit of running a number of linters, more than ALE would by default, 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* diff --git a/doc/ale.txt b/doc/ale.txt index afdc918..ba08a45 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 0000000..14e6e06 --- /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 0000000..e69de29