summaryrefslogtreecommitdiff
path: root/ale_linters/slim/slimlint.vim
blob: bb62c731b2598732feb4137caf0eb60a11ef1fef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
" Author: Markus Doits - https://github.com/doits
" Description: slim-lint for Slim files

function! ale_linters#slim#slimlint#GetCommand(buffer) abort
    let l:command = 'slim-lint %t'

    let l:rubocop_config = ale#path#FindNearestFile(a:buffer, '.rubocop.yml')

    " Set SLIM_LINT_RUBOCOP_CONF variable as it is needed for slim-lint to
    " pick up the rubocop config.
    "
    " See https://github.com/sds/slim-lint/blob/master/lib/slim_lint/linter/README.md#rubocop
    if !empty(l:rubocop_config)
      if ale#Has('win32')
        let l:command = 'set SLIM_LINT_RUBOCOP_CONF=' . ale#Escape(l:rubocop_config) . ' && ' . l:command
      else
        let l:command = 'SLIM_LINT_RUBOCOP_CONF=' . ale#Escape(l:rubocop_config) . ' ' . l:command
      endif
    endif

    return l:command
endfunction

function! ale_linters#slim#slimlint#Handle(buffer, lines) abort
    " Matches patterns like the following:
    " <path>:5 [W] LineLength: Line is too long. [150/120]
    let l:pattern = '\v^.*:(\d+) \[([EW])\] (.+)$'
    let l:output = []

    for l:match in ale#util#GetMatches(a:lines, l:pattern)
        call add(l:output, {
        \   'lnum': l:match[1] + 0,
        \   'type': l:match[2],
        \   'text': l:match[3]
        \})
    endfor

    return l:output
endfunction

call ale#linter#Define('slim', {
\   'name': 'slimlint',
\   'executable': 'slim-lint',
\   'command_callback': 'ale_linters#slim#slimlint#GetCommand',
\   'callback': 'ale_linters#slim#slimlint#Handle'
\})