summaryrefslogtreecommitdiff
path: root/ale_linters/php/tlint.vim
blob: 6bba8defd661f72088944490584a8626f1831c34 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
" Author: Jose Soto <jose@tighten.co>
"
" Description: Tighten Opinionated PHP Linting
" Website: https://github.com/tightenco/tlint

call ale#Set('php_tlint_executable', 'tlint')
call ale#Set('php_tlint_use_global', get(g:, 'ale_use_global_executables', 0))
call ale#Set('php_tlint_options', '')

function! ale_linters#php#tlint#GetProjectRoot(buffer) abort
    let l:composer_path = ale#path#FindNearestFile(a:buffer, 'composer.json')

    if !empty(l:composer_path)
        return fnamemodify(l:composer_path, ':h')
    endif

    let l:git_path = ale#path#FindNearestDirectory(a:buffer, '.git')

    return !empty(l:git_path) ? fnamemodify(l:git_path, ':h:h') : ''
endfunction

function! ale_linters#php#tlint#GetExecutable(buffer) abort
    return ale#node#FindExecutable(a:buffer, 'php_tlint', [
    \   'vendor/bin/tlint',
    \   'tlint',
    \])
endfunction

function! ale_linters#php#tlint#GetCommand(buffer) abort
    let l:executable = ale_linters#php#tlint#GetExecutable(a:buffer)
    let l:options = ale#Var(a:buffer, 'php_tlint_options')

    return ale#node#Executable(a:buffer, l:executable)
    \   . (!empty(l:options) ? ' ' . l:options : '')
    \   . ' lint %s'
endfunction

function! ale_linters#php#tlint#Handle(buffer, lines) abort
    " Matches against lines like the following:
    "
    " ! There should be 1 space around `.` concatenations, and additional lines should always start with a `.`
    " 22 : `        $something = 'a'.'name';`
    "
    let l:loop_count = 0
    let l:messages_pattern = '^\! \(.*\)'
    let l:output = []
    let l:pattern = '^\(\d\+\) \:'
    let l:temp_messages = []

    for l:message in ale#util#GetMatches(a:lines, l:messages_pattern)
        call add(l:temp_messages, l:message)
    endfor

    let l:loop_count = 0

    for l:match in ale#util#GetMatches(a:lines, l:pattern)
        let l:num = l:match[1]
        let l:text = l:temp_messages[l:loop_count]

        call add(l:output, {
        \   'lnum': l:num,
        \   'col': 0,
        \   'text': l:text,
        \   'type': 'W',
        \   'sub_type': 'style',
        \})

        let l:loop_count += 1
    endfor

    return l:output
endfunction

call ale#linter#Define('php', {
\   'name': 'tlint',
\   'executable': function('ale_linters#php#tlint#GetExecutable'),
\   'command': function('ale_linters#php#tlint#GetCommand'),
\   'callback': 'ale_linters#php#tlint#Handle',
\   'project_root': function('ale_linters#php#tlint#GetProjectRoot'),
\})