From 607f33a1b0f662d9809d54363e8e81a4965862ce Mon Sep 17 00:00:00 2001 From: Alex McKinney Date: Wed, 6 Apr 2022 01:00:59 -0700 Subject: [PATCH] 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 --- ale_linters/proto/buf_lint.vim | 23 ++++++++++ autoload/ale/fix/registry.vim | 5 +++ autoload/ale/fixers/buf_format.vim | 12 ++++++ doc/ale-proto.txt | 42 ++++++++++++++++++- doc/ale-supported-languages-and-tools.txt | 2 + doc/ale.txt | 2 + supported-tools.md | 2 + .../test_buf_format_fixer_callback.vader | 21 ++++++++++ test/linter/test_buf_lint.vader | 22 ++++++++++ test/test-files/proto/testfile.proto | 0 10 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 ale_linters/proto/buf_lint.vim create mode 100644 autoload/ale/fixers/buf_format.vim create mode 100644 test/fixers/test_buf_format_fixer_callback.vader create mode 100644 test/linter/test_buf_lint.vader create mode 100644 test/test-files/proto/testfile.proto diff --git a/ale_linters/proto/buf_lint.vim b/ale_linters/proto/buf_lint.vim new file mode 100644 index 00000000..e68494a7 --- /dev/null +++ b/ale_linters/proto/buf_lint.vim @@ -0,0 +1,23 @@ +" Author: Alex McKinney +" 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', +\}) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index f9b94843..6ac617d8 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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'], diff --git a/autoload/ale/fixers/buf_format.vim b/autoload/ale/fixers/buf_format.vim new file mode 100644 index 00000000..c2c156b7 --- /dev/null +++ b/autoload/ale/fixers/buf_format.vim @@ -0,0 +1,12 @@ +" Author: Alex McKinney +" 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 diff --git a/doc/ale-proto.txt b/doc/ale-proto.txt index 8ab56a14..a1bf1303 100644 --- a/doc/ale-proto.txt +++ b/doc/ale-proto.txt @@ -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* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 67efc6ec..f0ff29a7 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -430,6 +430,8 @@ Notes: * Prolog * `swipl` * proto + * `buf-format`!! + * `buf-lint`!! * `protoc-gen-lint`!! * `protolint`!! * Pug diff --git a/doc/ale.txt b/doc/ale.txt index 0250637c..96ecd5b8 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -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| diff --git a/supported-tools.md b/supported-tools.md index 491e7b96..3e9bc2f8 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -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 diff --git a/test/fixers/test_buf_format_fixer_callback.vader b/test/fixers/test_buf_format_fixer_callback.vader new file mode 100644 index 00000000..ee484820 --- /dev/null +++ b/test/fixers/test_buf_format_fixer_callback.vader @@ -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('')) diff --git a/test/linter/test_buf_lint.vader b/test/linter/test_buf_lint.vader new file mode 100644 index 00000000..7b8bb52c --- /dev/null +++ b/test/linter/test_buf_lint.vader @@ -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' diff --git a/test/test-files/proto/testfile.proto b/test/test-files/proto/testfile.proto new file mode 100644 index 00000000..e69de29b