From 9e97a6914e1f7f5ab4ac80c33150c42f733d3a1b Mon Sep 17 00:00:00 2001 From: Horacio Sanson Date: Wed, 7 Nov 2018 17:33:14 +0900 Subject: [PATCH] Add bibclen fixer support Closes #1910 --- README.md | 1 + ale_linters/bib/bibclean.vim | 74 +++++++++++++++++++ autoload/ale/fix/registry.vim | 5 ++ autoload/ale/fixers/bibclean.vim | 15 ++++ doc/ale-bib.txt | 19 +++++ doc/ale.txt | 3 + test/command_callback/bib_paths/dummy.bib | 0 .../test_bib_bibclean_command_callback.vader | 24 ++++++ .../fixers/test_bibclean_fixer_callback.vader | 30 ++++++++ test/fixers/test_rufo_fixer_callback.vader | 4 +- test/handler/test_bibclean_handler.vader | 35 +++++++++ test/script/check-toc | 1 + 12 files changed, 208 insertions(+), 3 deletions(-) create mode 100644 ale_linters/bib/bibclean.vim create mode 100644 autoload/ale/fixers/bibclean.vim create mode 100644 doc/ale-bib.txt create mode 100644 test/command_callback/bib_paths/dummy.bib create mode 100644 test/command_callback/test_bib_bibclean_command_callback.vader create mode 100644 test/fixers/test_bibclean_fixer_callback.vader create mode 100644 test/handler/test_bibclean_handler.vader diff --git a/README.md b/README.md index ad55e255..7a8196f3 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ formatting. | AsciiDoc | [alex](https://github.com/wooorm/alex) !!, [proselint](http://proselint.com/), [redpen](http://redpen.cc/), [write-good](https://github.com/btford/write-good), [vale](https://github.com/ValeLint/vale) | | Awk | [gawk](https://www.gnu.org/software/gawk/)| | Bash | [language-server](https://github.com/mads-hartmann/bash-language-server), shell [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | +| BibTeX | [bibclean](http://ftp.math.utah.edu/pub/bibclean/) | | Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | | C | [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [clang](http://clang.llvm.org/), [clangd](https://clang.llvm.org/extra/clangd.html), [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [cquery](https://github.com/cquery-project/cquery), [flawfinder](https://www.dwheeler.com/flawfinder/), [gcc](https://gcc.gnu.org/), [uncrustify](https://github.com/uncrustify/uncrustify), [ccls](https://github.com/MaskRay/ccls) | | C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangd](https://clang.llvm.org/extra/clangd.html), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) !!, [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [clazy](https://github.com/KDE/clazy) !!, [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) !!, [cquery](https://github.com/cquery-project/cquery), [flawfinder](https://www.dwheeler.com/flawfinder/), [gcc](https://gcc.gnu.org/), [uncrustify](https://github.com/uncrustify/uncrustify), [ccls](https://github.com/MaskRay/ccls) | diff --git a/ale_linters/bib/bibclean.vim b/ale_linters/bib/bibclean.vim new file mode 100644 index 00000000..6750f22f --- /dev/null +++ b/ale_linters/bib/bibclean.vim @@ -0,0 +1,74 @@ +" Author: Horacio Sanson - https://github.com/hsanson +" Description: Support for bibclean linter for BibTeX files. + +call ale#Set('bib_bibclean_executable', 'bibclean') + +function! ale_linters#bib#bibclean#GetCommand(buffer) abort + let l:executable = ale#Var(a:buffer, 'bib_bibclean_executable') + + return ale#Escape(l:executable) . ' -file-position ' +endfunction + +function! ale_linters#bib#bibclean#get_type(str) abort + if a:str is# '??' + return 'E' + else + return 'W' + endif +endfunction + +function! ale_linters#bib#bibclean#match_msg(line) abort + return matchlist(a:line, '^\(.*\) "stdin", line \(.*\): \(.*\)$') +endfunction + +function! ale_linters#bib#bibclean#match_entry(line) abort + return matchlist(a:line, 'Entry input byte=.* line=\(.*\) column=\(.*\) output .*$') +endfunction + +function! ale_linters#bib#bibclean#match_value(line) abort + return matchlist(a:line, 'Value input byte=.* line=\(.*\) column=\(.*\) output .*$') +endfunction + +function! ale_linters#bib#bibclean#Handle(buffer, lines) abort + let l:output = [] + + let l:type = 'E' + let l:msg = '' + + for l:line in a:lines + if empty(l:msg) + let l:mlist = ale_linters#bib#bibclean#match_msg(l:line) + + if !empty(l:mlist) + let l:msg = l:mlist[3] + let l:type = ale_linters#bib#bibclean#get_type(l:mlist[1]) + endif + else + if l:type is# 'E' + let l:mlist = ale_linters#bib#bibclean#match_entry(l:line) + else + let l:mlist = ale_linters#bib#bibclean#match_value(l:line) + endif + + if !empty(l:mlist) + call add(l:output, { + \ 'lnum': l:mlist[1], + \ 'col': l:mlist[2], + \ 'text': l:msg, + \ 'type': l:type + \}) + let l:msg = '' + endif + endif + endfor + + return l:output +endfunction + +call ale#linter#Define('bib', { +\ 'name': 'bibclean', +\ 'executable_callback': ale#VarFunc('bib_bibclean_executable'), +\ 'command_callback': 'ale_linters#bib#bibclean#GetCommand', +\ 'output_stream': 'stderr', +\ 'callback': 'ale_linters#bib#bibclean#Handle', +\}) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index a54be420..75fd1508 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -17,6 +17,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['python'], \ 'description': 'Fix PEP8 issues with autopep8.', \ }, +\ 'bibclean': { +\ 'function': 'ale#fixers#bibclean#Fix', +\ 'suggested_filetypes': ['bib'], +\ 'description': 'Format bib files using bibclean.', +\ }, \ 'black': { \ 'function': 'ale#fixers#black#Fix', \ 'suggested_filetypes': ['python'], diff --git a/autoload/ale/fixers/bibclean.vim b/autoload/ale/fixers/bibclean.vim new file mode 100644 index 00000000..89cb97ab --- /dev/null +++ b/autoload/ale/fixers/bibclean.vim @@ -0,0 +1,15 @@ +" Author: Horacio Sanson - https://github.com/hsanson +" Description: Support for bibclean fixer for BibTeX files. + +call ale#Set('bib_bibclean_executable', 'bibclean') +call ale#Set('bib_bibclean_options', '-align-equals') + +function! ale#fixers#bibclean#Fix(buffer) abort + let l:options = ale#Var(a:buffer, 'bib_bibclean_options') + let l:executable = ale#Var(a:buffer, 'bib_bibclean_executable') + + return { + \ 'command': ale#Escape(l:executable) + \ . ' ' . (empty(l:options) ? '' : l:options), + \} +endfunction diff --git a/doc/ale-bib.txt b/doc/ale-bib.txt new file mode 100644 index 00000000..35998c30 --- /dev/null +++ b/doc/ale-bib.txt @@ -0,0 +1,19 @@ +=============================================================================== +ALE BibTeX Integration *ale-bib-options* + + +=============================================================================== +bibclean *ale-bib-bibclean* + +g:ale_bib_bibclean_executable *g:ale_bib_bibclean_executable* + + Type: |String| + Default: `'bibclean'` + +g:ale_bib_bibclean_options *g:ale_bib_bibclean_options* + + Type: |String| + Default: `'-align-equals'` + +=============================================================================== +vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale.txt b/doc/ale.txt index 1a37f73f..48d7bc13 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -31,6 +31,8 @@ CONTENTS *ale-contents* gcc.................................|ale-asm-gcc| awk...................................|ale-awk-options| gawk................................|ale-awk-gawk| + bib...................................|ale-bib-options| + bibclean............................|ale-bib-bibclean| c.....................................|ale-c-options| clang...............................|ale-c-clang| clangd..............................|ale-c-clangd| @@ -403,6 +405,7 @@ Notes: * AsciiDoc: `alex`!!, `proselint`, `redpen`, `write-good`, `vale` * Awk: `gawk` * Bash: `language-server`, `shell` (-n flag), `shellcheck`, `shfmt` +* BibTeX: `bibclean` * Bourne Shell: `shell` (-n flag), `shellcheck`, `shfmt` * C: `cppcheck`, `cpplint`!!, `clang`, `clangd`, `clangtidy`!!, `clang-format`, `cquery`, `flawfinder`, `gcc`, `uncrustify`, `ccls` * C++ (filetype cpp): `clang`, `clangd`, `clangcheck`!!, `clangtidy`!!, `clang-format`, `clazy`!!, `cppcheck`, `cpplint`!!, `cquery`, `flawfinder`, `gcc`, `uncrustify`, `ccls` diff --git a/test/command_callback/bib_paths/dummy.bib b/test/command_callback/bib_paths/dummy.bib new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/test_bib_bibclean_command_callback.vader b/test/command_callback/test_bib_bibclean_command_callback.vader new file mode 100644 index 00000000..fa6f7d33 --- /dev/null +++ b/test/command_callback/test_bib_bibclean_command_callback.vader @@ -0,0 +1,24 @@ +Before: + call ale#assert#SetUpLinterTest('bib', 'bibclean') + + let g:ale_ruby_rubocop_executable = 'bibclean' + let g:ale_ruby_rubocop_options = '' + +After: + call ale#assert#TearDownLinterTest() + +Execute(Executable should default to bibclean): + AssertLinter 'bibclean', ale#Escape('bibclean') + \ . ' -file-position ' + +Execute(Should be able to set a custom executable): + let g:ale_bib_bibclean_executable = 'bin/bibclean' + + AssertLinter 'bin/bibclean' , ale#Escape('bin/bibclean') + \ . ' -file-position ' + +Execute(Should not include custom options): + let g:ale_bib_bibclean_options = '-no-prettryprint' + + AssertLinter 'bibclean' , ale#Escape('bibclean') + \ . ' -file-position ' diff --git a/test/fixers/test_bibclean_fixer_callback.vader b/test/fixers/test_bibclean_fixer_callback.vader new file mode 100644 index 00000000..8d3081e3 --- /dev/null +++ b/test/fixers/test_bibclean_fixer_callback.vader @@ -0,0 +1,30 @@ +Before: + Save g:ale_bib_bibclean_executable + Save g:ale_bib_bibclean_options + + let g:ale_bib_bibclean_executable = 'xxxinvalid' + let g:ale_bib_bibclean_options = '-align-equals' + + call ale#test#SetDirectory('/testplugin/test/fixers') + +After: + Restore + call ale#test#RestoreDirectory() + +Execute(The bibclean callback should return the correct default values): + call ale#test#SetFilename('../command_callback/bib_paths/dummy.bib') + + AssertEqual + \ {'command': ale#Escape(g:ale_bib_bibclean_executable) . ' -align-equals'}, + \ ale#fixers#bibclean#Fix(bufnr('')) + +Execute(The bibclean callback should include custom bibclean options): + let g:ale_bib_bibclean_options = '-author -check-values' + call ale#test#SetFilename('../command_callback/bib_paths/dummy.bib') + + AssertEqual + \ { + \ 'command': ale#Escape(g:ale_bib_bibclean_executable) . ' -author -check-values' + \ }, + \ ale#fixers#bibclean#Fix(bufnr('')) + diff --git a/test/fixers/test_rufo_fixer_callback.vader b/test/fixers/test_rufo_fixer_callback.vader index a0828406..98108efc 100644 --- a/test/fixers/test_rufo_fixer_callback.vader +++ b/test/fixers/test_rufo_fixer_callback.vader @@ -4,9 +4,7 @@ Before: " Use an invalid global executable, so we don't match it. let g:ale_ruby_rufo_executable = 'xxxinvalid' - call ale#test#SetDirectory('/testplugin/test/fixers') - silent cd .. - silent cd command_callback + call ale#test#SetDirectory('/testplugin/test/command_callback') let g:dir = getcwd() After: diff --git a/test/handler/test_bibclean_handler.vader b/test/handler/test_bibclean_handler.vader new file mode 100644 index 00000000..6179d7f5 --- /dev/null +++ b/test/handler/test_bibclean_handler.vader @@ -0,0 +1,35 @@ +Before: + runtime ale_linters/bib/bibclean.vim + +After: + call ale#linter#Reset() + +Execute(The bibclean handler should parse lines correctly): + + AssertEqual + \ [ + \ { + \ 'lnum': '60', + \ 'type': 'W', + \ 'text': 'Unexpected value in ``month = "09"''''.', + \ 'col': '17' + \ }, + \ { + \ 'lnum': '63', + \ 'type': 'E', + \ 'text': 'Expected comma after last field ``keywords''''.', + \ 'col': ' 1' + \ } + \ ], + \ ale_linters#bib#bibclean#Handle(255, [ + \ "%% \"stdin\", line 60: Unexpected value in ``month = \"09\"''.", + \ "%% File positions: input [main.bib] output [stdout]", + \ "%% Entry input byte=1681 line=50 column= 1 output byte=1680 line=50 column= 0", + \ "%% Value input byte=2137 line=60 column=17 output byte=2137 line=60 column=17", + \ "%% Current input byte=2139 line=60 column=19 output byte=2137 line=60 column=17", + \ "?? \"stdin\", line 71: Expected comma after last field ``keywords''.", + \ "?? File positions: input [main.bib] output [stdout]", + \ "?? Entry input byte=2145 line=63 column= 1 output byte=2146 line=63 column= 0", + \ "?? Value input byte=2528 line=71 column= 2 output byte=2527 line=70 column=49", + \ "?? Current input byte=2529 line=71 column= 3 output byte=2528 line=70 column=50" + \ ]) diff --git a/test/script/check-toc b/test/script/check-toc index 8e411589..09d794ee 100755 --- a/test/script/check-toc +++ b/test/script/check-toc @@ -36,6 +36,7 @@ doc_files="$(/bin/ls -1v doc | grep ^ale- | sed 's/^/doc\//' | paste -sd ' ' -)" grep -h '\*ale-.*-options\|^[a-z].*\*ale-.*\*$' $doc_files \ | sed 's/^/ /' \ | sed 's/ALE Shell Integration/ALE sh Integration/' \ + | sed 's/ALE BibTeX Integration/ALE bib Integration/' \ | sed 's/ ALE \(.*\) Integration/\1/' \ | sed 's/ *\*\(..*\)\*$/, \1/' \ | tr '[:upper:]' '[:lower:]' \