From 96b90b45db5070c964adb14f1a0ac67c61571648 Mon Sep 17 00:00:00 2001 From: Johannes Wienke Date: Sun, 17 Dec 2017 16:49:57 +0100 Subject: [PATCH] Use JSON output with vale Switches all vale instances to JSON output and provides an appropriate handler for that. Without JSON, no end_col is provided and text highlighting only catches the first character of every result. --- ale_linters/mail/vale.vim | 4 +- ale_linters/markdown/vale.vim | 4 +- ale_linters/text/vale.vim | 4 +- autoload/ale/handlers/vale.vim | 36 +++++++++++++++ test/handler/test_vale_handler.vader | 67 ++++++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 autoload/ale/handlers/vale.vim create mode 100644 test/handler/test_vale_handler.vader diff --git a/ale_linters/mail/vale.vim b/ale_linters/mail/vale.vim index 9b30bf6..e6dfd2e 100644 --- a/ale_linters/mail/vale.vim +++ b/ale_linters/mail/vale.vim @@ -4,6 +4,6 @@ call ale#linter#Define('mail', { \ 'name': 'vale', \ 'executable': 'vale', -\ 'command': 'vale --output=line %t', -\ 'callback': 'ale#handlers#unix#HandleAsWarning', +\ 'command': 'vale --output=JSON %t', +\ 'callback': 'ale#handlers#vale#Handle', \}) diff --git a/ale_linters/markdown/vale.vim b/ale_linters/markdown/vale.vim index 43b3d34..838c4db 100644 --- a/ale_linters/markdown/vale.vim +++ b/ale_linters/markdown/vale.vim @@ -4,6 +4,6 @@ call ale#linter#Define('markdown', { \ 'name': 'vale', \ 'executable': 'vale', -\ 'command': 'vale --output=line %t', -\ 'callback': 'ale#handlers#unix#HandleAsWarning', +\ 'command': 'vale --output=JSON %t', +\ 'callback': 'ale#handlers#vale#Handle', \}) diff --git a/ale_linters/text/vale.vim b/ale_linters/text/vale.vim index 60bd799..cf37c2f 100644 --- a/ale_linters/text/vale.vim +++ b/ale_linters/text/vale.vim @@ -4,6 +4,6 @@ call ale#linter#Define('text', { \ 'name': 'vale', \ 'executable': 'vale', -\ 'command': 'vale --output=line %t', -\ 'callback': 'ale#handlers#unix#HandleAsWarning', +\ 'command': 'vale --output=JSON %t', +\ 'callback': 'ale#handlers#vale#Handle', \}) diff --git a/autoload/ale/handlers/vale.vim b/autoload/ale/handlers/vale.vim new file mode 100644 index 0000000..c842057 --- /dev/null +++ b/autoload/ale/handlers/vale.vim @@ -0,0 +1,36 @@ +" Author: Johannes Wienke +" Description: output handler for the vale JSON format + +function! ale#handlers#vale#GetType(severity) abort + if a:severity is? 'warning' + return 'W' + endif + + return 'E' +endfunction + +function! ale#handlers#vale#Handle(buffer, lines) abort + try + let l:errors = json_decode(join(a:lines, '')) + catch + return [] + endtry + + if empty(l:errors) + return [] + endif + + let l:output = [] + for l:error in l:errors[keys(l:errors)[0]] + call add(l:output, { + \ 'lnum': l:error['Line'], + \ 'col': l:error['Span'][0], + \ 'end_col': l:error['Span'][1], + \ 'code': l:error['Check'], + \ 'text': l:error['Message'], + \ 'type': ale#handlers#vale#GetType(l:error['Severity']), + \}) + endfor + + return l:output +endfunction diff --git a/test/handler/test_vale_handler.vader b/test/handler/test_vale_handler.vader new file mode 100644 index 0000000..afc32db --- /dev/null +++ b/test/handler/test_vale_handler.vader @@ -0,0 +1,67 @@ +Execute(The vale handler should handle broken JSON): + AssertEqual + \ [], + \ ale#handlers#vale#Handle(bufnr(''), ["{asdf"]) + +Execute(The vale handler should handle am empty string response): + AssertEqual + \ [], + \ ale#handlers#vale#Handle(bufnr(''), []) + +Execute(The vale handler should handle an empty result): + AssertEqual + \ [], + \ ale#handlers#vale#Handle(bufnr(''), ["{}"]) + +Execute(The vale handler should handle a normal example): + AssertEqual + \ [ + \ { + \ 'lnum': 5, + \ 'col': 195, + \ 'end_col': 201, + \ 'type': 'W', + \ 'text': "Consider removing 'usually'", + \ 'code': 'vale.Hedging', + \ }, + \ { + \ 'lnum': 7, + \ 'col': 1, + \ 'end_col': 27, + \ 'type': 'E', + \ 'text': "'Documentation' is repeated!", + \ 'code': 'vale.Repetition', + \ }, + \ ], + \ ale#handlers#vale#Handle(bufnr(''), [ + \ '{', + \ ' "/home/languitar/src/autosuspend/README.md": [', + \ ' {', + \ ' "Check": "vale.Hedging",', + \ ' "Description": "",', + \ ' "Line": 5,', + \ ' "Link": "",', + \ " \"Message\": \"Consider removing 'usually'\",", + \ ' "Severity": "warning",', + \ ' "Span": [', + \ ' 195,', + \ ' 201', + \ ' ],', + \ ' "Hide": false', + \ ' },', + \ ' {', + \ ' "Check": "vale.Repetition",', + \ ' "Description": "",', + \ ' "Line": 7,', + \ ' "Link": "",', + \ " \"Message\": \"'Documentation' is repeated!\",", + \ ' "Severity": "error",', + \ ' "Span": [', + \ ' 1,', + \ ' 27', + \ ' ],', + \ ' "Hide": false', + \ ' }', + \ ' ]', + \ '}', + \ ])