Add buf linter and fixer (#4128)

* Add buf lint to linters

* Add buf format to fixers

* Fix test/linter/test_buf_lint.vader

* Fix test/fixers/test_buf_format_fixer_callback.vader

* Simplify test/test-files/proto/testfile.proto

* Add buf-lint alias and rename linter
This commit is contained in:
Alex McKinney 2022-04-06 01:00:59 -07:00 committed by GitHub
parent 1e997580fd
commit 607f33a1b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 129 additions and 2 deletions

View File

@ -0,0 +1,23 @@
" Author: Alex McKinney <alexmckinney01@gmail.com>
" Description: Run buf lint.
call ale#Set('proto_buf_lint_executable', 'buf')
call ale#Set('proto_buf_lint_config', '')
function! ale_linters#proto#buf_lint#GetCommand(buffer) abort
let l:config = ale#Var(a:buffer, 'proto_buf_lint_config')
return '%e lint'
\ . (!empty(l:config) ? ' --config=' . ale#Escape(l:config) : '')
\ . ' %s#include_package_files=true'
endfunction
call ale#linter#Define('proto', {
\ 'name': 'buf_lint',
\ 'aliases': ['buf-lint'],
\ 'lint_file': 1,
\ 'output_stream': 'stdout',
\ 'executable': {b -> ale#Var(b, 'proto_buf_lint_executable')},
\ 'command': function('ale_linters#proto#buf_lint#GetCommand'),
\ 'callback': 'ale#handlers#unix#HandleAsError',
\})

View File

@ -37,6 +37,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['python'],
\ 'description': 'Fix PEP8 issues with black.',
\ },
\ 'buf-format': {
\ 'function': 'ale#fixers#buf_format#Fix',
\ 'suggested_filetypes': ['proto'],
\ 'description': 'Fix .proto files with buf format.',
\ },
\ 'buildifier': {
\ 'function': 'ale#fixers#buildifier#Fix',
\ 'suggested_filetypes': ['bzl'],

View File

@ -0,0 +1,12 @@
" Author: Alex McKinney <alexmckinney01@gmail.com>
" Description: Run buf format.
call ale#Set('proto_buf_format_executable', 'buf')
function! ale#fixers#buf_format#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'proto_buf_format_executable')
return {
\ 'command': ale#Escape(l:executable) . ' format %t',
\}
endfunction

View File

@ -8,13 +8,51 @@ Integration Information
To enable `.proto` file linting, update |g:ale_linters| as appropriate:
>
" Enable linter for .proto files
let g:ale_linters = {'proto': ['protoc-gen-lint', 'protolint']}
let g:ale_linters = {'proto': ['buf-lint', 'protoc-gen-lint', 'protolint']}
To enable `.proto` file fixing, update |g:ale_fixers| as appropriate:
>
" Enable linter for .proto files
let b:ale_fixers = {'proto': ['protolint']}
let b:ale_fixers = {'proto': ['buf-format', 'protolint']}
<
===============================================================================
buf-format *ale-proto-buf-format*
The formatter uses `buf`, a fully-featured Protobuf compiler that doesn't depend
on `protoc`. Make sure the `buf` binary is available in the system path, or
set ale_proto_buf_format_executable.
g:ale_proto_buf_format_executable *g:ale_proto_buf_format_executable*
Type: |String|
Default: 'buf'
This variable can be changed to modify the executable used for buf.
===============================================================================
buf-lint *ale-proto-buf-lint*
The linter uses `buf`, a fully-featured Protobuf compiler that doesn't depend
on `protoc`. Make sure the `buf` binary is available in the system path, or
set ale_proto_buf_lint_executable.
g:ale_proto_buf_lint_executable *g:ale_proto_buf_lint_executable*
Type: |String|
Default: 'buf'
This variable can be changed to modify the executable used for buf.
g:ale_proto_buf_lint_config *g:ale_proto_buf_lint_config*
Type: |String|
Default: `''`
A path to a buf configuration file.
The path to the configuration file can be an absolute path or a relative
path. ALE will search for the relative path in parent directories.
===============================================================================
protoc-gen-lint *ale-proto-protoc-gen-lint*

View File

@ -430,6 +430,8 @@ Notes:
* Prolog
* `swipl`
* proto
* `buf-format`!!
* `buf-lint`!!
* `protoc-gen-lint`!!
* `protolint`!!
* Pug

View File

@ -3060,6 +3060,8 @@ documented in additional help files.
prolog..................................|ale-prolog-options|
swipl.................................|ale-prolog-swipl|
proto...................................|ale-proto-options|
buf-format............................|ale-proto-buf-format|
buf-lint..............................|ale-proto-buf-lint|
protoc-gen-lint.......................|ale-proto-protoc-gen-lint|
protolint.............................|ale-proto-protolint|
pug.....................................|ale-pug-options|

View File

@ -439,6 +439,8 @@ formatting.
* Prolog
* [swipl](https://github.com/SWI-Prolog/swipl-devel)
* proto
* [buf-format](https://github.com/bufbuild/buf) :floppy_disk:
* [buf-lint](https://github.com/bufbuild/buf) :floppy_disk:
* [protoc-gen-lint](https://github.com/ckaznocha/protoc-gen-lint) :floppy_disk:
* [protolint](https://github.com/yoheimuta/protolint) :floppy_disk:
* Pug

View File

@ -0,0 +1,21 @@
Before:
Save g:ale_proto_buf_format_executable
" Use an invalid global executable, so we don't match it.
let g:ale_proto_buf_format_executable = 'xxxinvalid'
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The buf-format callback should return the correct default values):
call ale#test#SetFilename('../test-files/proto/testfile.proto')
AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid') . ' format %t',
\ },
\ ale#fixers#buf_format#Fix(bufnr(''))

View File

@ -0,0 +1,22 @@
Before:
call ale#assert#SetUpLinterTest('proto', 'buf_lint')
call ale#test#SetFilename('test.proto')
After:
call ale#assert#TearDownLinterTest()
Execute(The default command should be correct):
AssertLinter 'buf',
\ ale#Escape('buf')
\ . ' lint'
\ . ' %s#include_package_files=true'
Execute(The callback should include any additional options):
let b:ale_proto_buf_lint_executable = '/tmp/buf'
let b:ale_proto_buf_lint_config = '/tmp/buf.yaml'
AssertLinter '/tmp/buf',
\ ale#Escape('/tmp/buf')
\ . ' lint'
\ . ' --config=' . ale#Escape('/tmp/buf.yaml')
\ . ' %s#include_package_files=true'

View File