Refactor SyntaxErl linter file (#4807)

This commit is contained in:
Dmitri Vereshchagin 2024-07-12 03:40:38 +03:00 committed by GitHub
parent 5c635d5038
commit 48d73c87c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 23 deletions

View File

@ -3,29 +3,13 @@
call ale#Set('erlang_syntaxerl_executable', 'syntaxerl') call ale#Set('erlang_syntaxerl_executable', 'syntaxerl')
function! ale_linters#erlang#syntaxerl#RunHelpCommand(buffer) abort
let l:executable = ale#Var(a:buffer, 'erlang_syntaxerl_executable')
return ale#command#Run(
\ a:buffer,
\ ale#Escape(l:executable) . ' -h',
\ function('ale_linters#erlang#syntaxerl#GetCommand'),
\)
endfunction
function! ale_linters#erlang#syntaxerl#GetCommand(buffer, output, meta) abort
let l:use_b_option = match(a:output, '\C\V-b, --base\>') > -1
return '%e' . (l:use_b_option ? ' -b %s %t' : ' %t')
endfunction
function! ale_linters#erlang#syntaxerl#Handle(buffer, lines) abort function! ale_linters#erlang#syntaxerl#Handle(buffer, lines) abort
let l:pattern = '\v\C:(\d+):( warning:)? (.+)' let l:pattern = '\v\C:(\d+):( warning:)? (.+)'
let l:loclist = [] let l:loclist = []
for l:match in ale#util#GetMatches(a:lines, l:pattern) for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:loclist, { call add(l:loclist, {
\ 'lnum': l:match[1] + 0, \ 'lnum': str2nr(l:match[1]),
\ 'text': l:match[3], \ 'text': l:match[3],
\ 'type': empty(l:match[2]) ? 'E' : 'W', \ 'type': empty(l:match[2]) ? 'E' : 'W',
\}) \})
@ -34,9 +18,27 @@ function! ale_linters#erlang#syntaxerl#Handle(buffer, lines) abort
return l:loclist return l:loclist
endfunction endfunction
function! s:GetExecutable(buffer) abort
return ale#Var(a:buffer, 'erlang_syntaxerl_executable')
endfunction
function! s:GetCommand(buffer) abort
let l:Callback = function('s:GetCommandFromHelpOutput')
return ale#command#Run(a:buffer, '%e -h', l:Callback, {
\ 'executable': s:GetExecutable(a:buffer),
\})
endfunction
function! s:GetCommandFromHelpOutput(buffer, output, metadata) abort
let l:has_b_option = match(a:output, '\V\C-b, --base\>') > -1
return l:has_b_option ? '%e -b %s %t' : '%e %t'
endfunction
call ale#linter#Define('erlang', { call ale#linter#Define('erlang', {
\ 'name': 'syntaxerl', \ 'name': 'syntaxerl',
\ 'executable': {b -> ale#Var(b, 'erlang_syntaxerl_executable')},
\ 'command': {b -> ale_linters#erlang#syntaxerl#RunHelpCommand(b)},
\ 'callback': 'ale_linters#erlang#syntaxerl#Handle', \ 'callback': 'ale_linters#erlang#syntaxerl#Handle',
\ 'executable': function('s:GetExecutable'),
\ 'command': function('s:GetCommand'),
\}) \})

View File

@ -11,11 +11,11 @@ Execute (The default commands should be correct):
\] \]
Execute (The executable should be configurable): Execute (The executable should be configurable):
let b:ale_erlang_syntaxerl_executable = 'foobar' let b:ale_erlang_syntaxerl_executable = '/path/to/syntaxerl'
AssertLinter 'foobar', [ AssertLinter '/path/to/syntaxerl', [
\ ale#Escape('foobar') . ' -h', \ ale#Escape('/path/to/syntaxerl') . ' -h',
\ ale#Escape('foobar') . ' %t', \ ale#Escape('/path/to/syntaxerl') . ' %t',
\] \]
Execute (The -b option should be used when available): Execute (The -b option should be used when available):