From 90b9597d500c528d46aa13ccfa678a86ef7ea22b Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Sun, 21 Feb 2021 04:35:26 -0800 Subject: [PATCH] Add a buildifier fixer for Bazel files (#3499) This fixer enables buildifier's formatting and "lint fix" modes. Additional options can be provided via `bazel_buildifier_options`. It also implements some basic logic for guessing the file's type. buildifier itself usually does this based on the filenames provided on the command line, but because we're piping our buffer via stdin, we need to do this manually. --- autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/buildifier.vim | 37 ++++++++++++++++ doc/ale-bazel.txt | 28 ++++++++++++ doc/ale-supported-languages-and-tools.txt | 2 + doc/ale.txt | 2 + supported-tools.md | 2 + test/command_callback/bazel_paths/BUILD | 0 test/command_callback/bazel_paths/WORKSPACE | 0 test/command_callback/bazel_paths/defs.bzl | 0 .../test_buildifier_fixer_callback.vader | 43 +++++++++++++++++++ 10 files changed, 119 insertions(+) create mode 100644 autoload/ale/fixers/buildifier.vim create mode 100644 doc/ale-bazel.txt create mode 100644 test/command_callback/bazel_paths/BUILD create mode 100644 test/command_callback/bazel_paths/WORKSPACE create mode 100644 test/command_callback/bazel_paths/defs.bzl create mode 100644 test/fixers/test_buildifier_fixer_callback.vader 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 -' + \ }