Skip to content

Commit

Permalink
Fix 4740 - add hurlfmt linter (#4741)
Browse files Browse the repository at this point in the history
* Fix 4740 - add hurlfmt linter

* Fix 4740 - add hurlfmt fixer

* Fix wrong comments

* Add end_col to qflist

* Fix test
  • Loading branch information
hsanson committed Mar 12, 2024
1 parent 5a8287e commit 8f9197b
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 0 deletions.
69 changes: 69 additions & 0 deletions ale_linters/hurl/hurlfmt.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
" Description: Hurl linter using hurlfmt --check.
" https://hurl.dev/

call ale#Set('hurl_hurlfmt_executable', 'hurlfmt')

function! ale_linters#hurl#hurlfmt#GetCommand(buffer) abort
return '%e'
\ . ' --check --no-color '
endfunction

function! ale_linters#hurl#hurlfmt#HandleOutput(buffer, lines) abort
" Matches patterns:
"
" error: Parsing space
" --> test.hurl:11:48
" |
" 8 | header "Content-Type"= "application/json; charset=utf-8"
" | ^ expecting a space
" |
"
" error: Parsing URL
" --> test.hurl:11:48
" |
" 11 | PUT https://jsonplaceholder.typicode.com/posts/{post_id}}
" | ^ illegal character <{>
" |
"
" Note: hurlfmt seems to report always the first error only so we assume
" there is only one error to make parsing easier.
let l:output = []

if empty(a:lines)
return l:output
endif

let l:pattern = '\v(error|warning): (.+) --\> (.+):(\d+):(\d+) .+ \^ (.+) |'
let l:lines = join(a:lines, ' ')

for l:match in ale#util#GetMatches(l:lines, l:pattern)
call add(l:output, {
\ 'bufnr': a:buffer,
\ 'lnum': match[4] + 0,
\ 'col': match[5] + 0,
\ 'end_col': match[5] + 0,
\ 'text': match[2] . ' : ' . match[6],
\ 'type': (match[1] is# 'error') ? 'E' : 'W'
\})
endfor

return l:output
endfunction

function! ale_linters#hurl#hurlfmt#GetType(severity) abort
if a:severity is? 'convention'
\|| a:severity is? 'warning'
\|| a:severity is? 'refactor'
return 'W'
endif

return 'E'
endfunction

call ale#linter#Define('hurl', {
\ 'name': 'hurlfmt',
\ 'output_stream': 'stderr',
\ 'executable': {b -> ale#Var(b, 'hurl_hurlfmt_executable')},
\ 'command': function('ale_linters#hurl#hurlfmt#GetCommand'),
\ 'callback': 'ale_linters#hurl#hurlfmt#HandleOutput',
\})
5 changes: 5 additions & 0 deletions autoload/ale/fix/registry.vim
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['javascript', 'css', 'html'],
\ 'description': 'Apply fecs format to a file.',
\ },
\ 'hurlfmt': {
\ 'function': 'ale#fixers#hurlfmt#Fix',
\ 'suggested_filetypes': ['hurl'],
\ 'description': 'Fix hurl files with hurlfmt.',
\ },
\ 'tidy': {
\ 'function': 'ale#fixers#tidy#Fix',
\ 'suggested_filetypes': ['html'],
Expand Down
15 changes: 15 additions & 0 deletions autoload/ale/fixers/hurlfmt.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
call ale#Set('hurl_hurlfmt_executable', 'hurlfmt')

function! ale#fixers#hurlfmt#GetCommand(buffer) abort
let l:executable = ale#Var(a:buffer, 'hurl_hurlfmt_executable')

return ale#Escape(l:executable)
\ . ' --out hurl'
endfunction

function! ale#fixers#hurlfmt#Fix(buffer) abort
return {
\ 'command': ale#fixers#hurlfmt#GetCommand(a:buffer)
\}
endfunction

17 changes: 17 additions & 0 deletions doc/ale-hurl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
===============================================================================
ALE Hurl Integration *ale-hurl-options*


===============================================================================
hurlfmt *ale-hurl-hurlfmt*

g:ale_hurl_hurlfmt_executable *g:ale_hurl_hurlfmt_executable*
*b:ale_hurl_hurlfmt_executable*
Type: |String|
Default: `'hurlfmt'`

Override the invoked hurlfmt binary.


===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
2 changes: 2 additions & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ Notes:
* `rustywind`
* `tidy`
* `write-good`
* Hurl
* `hurlfmt`
* Idris
* `idris`
* Ink
Expand Down
2 changes: 2 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3098,6 +3098,8 @@ documented in additional help files.
tidy..................................|ale-html-tidy|
vscodehtml............................|ale-html-vscode|
write-good............................|ale-html-write-good|
hurl....................................|ale-hurl-options|
hurlfmt...............................|ale-hurl-hurlfmt|
idris...................................|ale-idris-options|
idris.................................|ale-idris-idris|
ink.....................................|ale-ink-options|
Expand Down
2 changes: 2 additions & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ formatting.
* [rustywind](https://github.com/avencera/rustywind)
* [tidy](http://www.html-tidy.org/)
* [write-good](https://github.com/btford/write-good)
* Hurl
* [hurlfmt](https://hurl.dev)
* Idris
* [idris](http://www.idris-lang.org/)
* Ink
Expand Down
23 changes: 23 additions & 0 deletions test/fixers/test_hurlfmt_fixer_callback.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Before:
Save g:ale_hurl_hurlfmt_executable

" Use an invalid global executable, so we don't match it.
let g:ale_hurl_hurlfmt_executable = 'xxxinvalid'

call ale#test#SetDirectory('/testplugin/test/fixers')

After:
Restore

call ale#test#RestoreDirectory()

Execute(The hurlfmt callback should return the correct default values):
call ale#test#SetFilename('../test-files/hurl/dummy.hurl')

AssertEqual
\ {
\ 'command': ale#Escape(g:ale_hurl_hurlfmt_executable)
\ . ' --out hurl',
\ },
\ ale#fixers#hurlfmt#Fix(bufnr(''))

29 changes: 29 additions & 0 deletions test/handler/test_hurlfmt_handler.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Before:
runtime ale_linters/hurl/hurlfmt.vim

After:
call ale#linter#Reset()

Execute(The hurlfmt handler should parse lines correctly):
AssertEqual
\ [
\ {
\ 'lnum': 11,
\ 'bufnr': 345,
\ 'col': 48,
\ 'end_col': 48,
\ 'text': 'Parsing space : expecting a space ',
\ 'type': 'E',
\ },
\ ],
\ ale_linters#hurl#hurlfmt#HandleOutput(345, [
\ 'error: Parsing space',
\ '--> test.hurl:11:48',
\ ' |',
\ '8 " | header "Content-Type"= "application/json; charset=utf-8"',
\ ' | ^ expecting a space',
\ ' |',
\ ])

Execute(The rubocop handler should handle empty output):
AssertEqual [], ale_linters#hurl#hurlfmt#HandleOutput(347, [])
19 changes: 19 additions & 0 deletions test/linter/test_hurlfmt.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Before:
call ale#assert#SetUpLinterTest('hurl', 'hurlfmt')
call ale#test#SetFilename('dummy.hurl')

let g:ale_ruby_hurlfmt_executable = 'hurlfmt'
let g:ale_ruby_hurlfmt_options = ''

After:
call ale#assert#TearDownLinterTest()

Execute(Executable should default to hurlfmt):
AssertLinter 'hurlfmt', ale#Escape('hurlfmt')
\ . ' --check --no-color '

Execute(Should be able to set a custom executable):
let g:ale_hurl_hurlfmt_executable = 'bin/hurlfmt'

AssertLinter 'bin/hurlfmt' , ale#Escape('bin/hurlfmt')
\ . ' --check --no-color '

0 comments on commit 8f9197b

Please sign in to comment.