Skip to content

Commit

Permalink
Refactor SyntaxErl linter file (dense-analysis#4807)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrivereshchagin authored Jul 12, 2024
1 parent 5c635d5 commit 48d73c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
40 changes: 21 additions & 19 deletions ale_linters/erlang/syntaxerl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,13 @@

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
let l:pattern = '\v\C:(\d+):( warning:)? (.+)'
let l:loclist = []

for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:loclist, {
\ 'lnum': l:match[1] + 0,
\ 'lnum': str2nr(l:match[1]),
\ 'text': l:match[3],
\ 'type': empty(l:match[2]) ? 'E' : 'W',
\})
Expand All @@ -34,9 +18,27 @@ function! ale_linters#erlang#syntaxerl#Handle(buffer, lines) abort
return l:loclist
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', {
\ 'name': 'syntaxerl',
\ 'executable': {b -> ale#Var(b, 'erlang_syntaxerl_executable')},
\ 'command': {b -> ale_linters#erlang#syntaxerl#RunHelpCommand(b)},
\ 'callback': 'ale_linters#erlang#syntaxerl#Handle',
\ 'executable': function('s:GetExecutable'),
\ 'command': function('s:GetCommand'),
\})
8 changes: 4 additions & 4 deletions test/linter/test_erlang_syntaxerl.vader
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Execute (The default commands should be correct):
\]

Execute (The executable should be configurable):
let b:ale_erlang_syntaxerl_executable = 'foobar'
let b:ale_erlang_syntaxerl_executable = '/path/to/syntaxerl'

AssertLinter 'foobar', [
\ ale#Escape('foobar') . ' -h',
\ ale#Escape('foobar') . ' %t',
AssertLinter '/path/to/syntaxerl', [
\ ale#Escape('/path/to/syntaxerl') . ' -h',
\ ale#Escape('/path/to/syntaxerl') . ' %t',
\]

Execute (The -b option should be used when available):
Expand Down

0 comments on commit 48d73c8

Please sign in to comment.