diff options
author | Jon Parise <jon@indelible.org> | 2021-02-21 04:35:26 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-21 21:35:26 +0900 |
commit | 90b9597d500c528d46aa13ccfa678a86ef7ea22b (patch) | |
tree | 2f94224a548ac63f66860eddc7fa4f6f099923aa /autoload | |
parent | 2550f5d952caea4ac607ec0796d8f1fae4d7b9a5 (diff) | |
download | ale-90b9597d500c528d46aa13ccfa678a86ef7ea22b.zip |
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.
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/buildifier.vim | 37 |
2 files changed, 42 insertions, 0 deletions
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 <jon@indelible.org> +" 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 |