diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 8279fdb4..bb26573b 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -32,6 +32,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['python'], \ 'description': 'Fix PEP8 issues with black.', \ }, +\ 'buildifier': { +\ 'function': 'ale#fixers#buildifier#Fix', +\ 'suggested_filetypes': ['bzl'], +\ 'description': 'Format BUILD and .bzl files with buildifier.', +\ }, \ 'deno': { \ 'function': 'ale#fixers#deno#Fix', \ 'suggested_filetypes': ['typescript'], diff --git a/autoload/ale/fixers/buildifier.vim b/autoload/ale/fixers/buildifier.vim new file mode 100644 index 00000000..e3dab5e1 --- /dev/null +++ b/autoload/ale/fixers/buildifier.vim @@ -0,0 +1,37 @@ +" Author: Jon Parise +" Description: Format Bazel BUILD and .bzl files with buildifier. +" +call ale#Set('bazel_buildifier_executable', 'buildifier') +call ale#Set('bazel_buildifier_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('bazel_buildifier_options', '') + +function! ale#fixers#buildifier#GetExecutable(buffer) abort + return ale#node#FindExecutable(a:buffer, 'bazel_buildifier', [ + \ 'buildifier', + \]) +endfunction + +function! ale#fixers#buildifier#Fix(buffer) abort + let l:executable = ale#Escape(ale#fixers#buildifier#GetExecutable(a:buffer)) + let l:options = ale#Var(a:buffer, 'bazel_buildifier_options') + let l:filename = fnamemodify(bufname(a:buffer), ':t') + + let l:command = l:executable . ' -mode fix -lint fix' + + " Attempt to guess the file type based on the filename. buildifier itself + " usually does this based on the filenames provided on the command line, + " but because we're piping our buffer via stdin, we do this manually. + if l:filename =~? 'WORKSPACE' + let l:command .= ' -type workspace' + elseif l:filename =~? 'BUILD' + let l:command .= ' -type build' + elseif l:filename =~? '.bzl$' + let l:command .= ' -type bzl' + endif + + if l:options isnot# '' + let l:command .= ' ' . l:options + endif + + return {'command': l:command . ' -'} +endfunction diff --git a/doc/ale-bazel.txt b/doc/ale-bazel.txt new file mode 100644 index 00000000..e2922aaf --- /dev/null +++ b/doc/ale-bazel.txt @@ -0,0 +1,28 @@ +=============================================================================== +ALE Bazel Integration *ale-bazel-options* + +=============================================================================== +buildifier *ale-bazel-buildifier* + +g:ale_bazel_buildifier_executable *g:ale_bazel_buildifier_executable* + *b:ale_bazel_buildifier_executable* + Type: |String| + Default: `'buildifier'` + + See |ale-integrations-local-executables| + + +g:ale_bazel_buildifier_options *g:ale_bazel_buildifier_options* + *b:ale_bazel_buildifier_options* + Type: |String| + Default: `''` + + This variable can be set to pass extra options to buildifier. + + +g:ale_bazel_buildifier_use_global *g:ale_bazel_buildifier_use_global* + *b:ale_bazel_buildifier_use_global* + Type: |Number| + Default: `get(g:, 'ale_use_global_executables', 0)` + + See |ale-integrations-local-executables| diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 71abb0a1..cb90de4c 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -43,6 +43,8 @@ Notes: * `shfmt` * Bats * `shellcheck` +* Bazel + * `buildifier` * BibTeX * `bibclean` * Bourne Shell diff --git a/doc/ale.txt b/doc/ale.txt index 3031cd1b..113e29c5 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2614,6 +2614,8 @@ documented in additional help files. gawk..................................|ale-awk-gawk| bats....................................|ale-bats-options| shellcheck............................|ale-bats-shellcheck| + bazel...................................|ale-bazel-options| + buildifier............................|ale-bazel-buildifier| bib.....................................|ale-bib-options| bibclean..............................|ale-bib-bibclean| c.......................................|ale-c-options| diff --git a/supported-tools.md b/supported-tools.md index 7e74372d..e9c88e95 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -52,6 +52,8 @@ formatting. * [shfmt](https://github.com/mvdan/sh) * Bats * [shellcheck](https://www.shellcheck.net/) +* Bazel + * [buildifier](https://github.com/bazelbuild/buildtools) * BibTeX * [bibclean](http://ftp.math.utah.edu/pub/bibclean/) * Bourne Shell diff --git a/test/command_callback/bazel_paths/BUILD b/test/command_callback/bazel_paths/BUILD new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/bazel_paths/WORKSPACE b/test/command_callback/bazel_paths/WORKSPACE new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/bazel_paths/defs.bzl b/test/command_callback/bazel_paths/defs.bzl new file mode 100644 index 00000000..e69de29b diff --git a/test/fixers/test_buildifier_fixer_callback.vader b/test/fixers/test_buildifier_fixer_callback.vader new file mode 100644 index 00000000..36de9819 --- /dev/null +++ b/test/fixers/test_buildifier_fixer_callback.vader @@ -0,0 +1,43 @@ +Before: + let g:ale_bazel_buildifier_options = '' + call ale#assert#SetUpFixerTest('bzl', 'buildifier') + +After: + call ale#assert#TearDownFixerTest() + +Execute(The buildifier callback should return the correct default values): + call ale#test#SetFilename('bazel_paths/WORKSPACE') + + AssertFixer + \ { + \ 'command': ale#Escape(g:ale_bazel_buildifier_executable) + \ . ' -mode fix -lint fix -type workspace -' + \ } + +Execute(The buildifier callback should include any additional options): + call ale#test#SetFilename('bazel_paths/WORKSPACE') + let g:ale_bazel_buildifier_options = '--some-option' + + AssertFixer + \ { + \ 'command': ale#Escape(g:ale_bazel_buildifier_executable) + \ . ' -mode fix -lint fix -type workspace --some-option -', + \ } + +Execute(The buildifier callback should recognize BUILD files): + call ale#test#SetFilename('bazel_paths/BUILD') + + AssertFixer + \ { + \ 'command': ale#Escape(g:ale_bazel_buildifier_executable) + \ . ' -mode fix -lint fix -type build -' + \ } + +Execute(The buildifier callback should recognize .bzl files): + call ale#test#SetFilename('bazel_paths/defs.bzl') + + AssertFixer + \ { + \ 'command': ale#Escape(g:ale_bazel_buildifier_executable) + \ . ' -mode fix -lint fix -type bzl -' + \ }