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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
" Author: Carl Smedstad <carl.smedstad at protonmail dot com>
" Description: sqlfluff for SQL files
let g:ale_sql_sqlfluff_executable =
\ get(g:, 'ale_sql_sqlfluff_executable', 'sqlfluff')
let g:ale_sql_sqlfluff_options =
\ get(g:, 'ale_sql_sqlfluff_options', '')
function! ale_linters#sql#sqlfluff#Executable(buffer) abort
return ale#Var(a:buffer, 'sql_sqlfluff_executable')
endfunction
function! ale_linters#sql#sqlfluff#Command(buffer, version) abort
let l:executable = ale_linters#sql#sqlfluff#Executable(a:buffer)
let l:options = ale#Var(a:buffer, 'sql_sqlfluff_options')
let l:cmd =
\ ale#Escape(l:executable)
\ . ' lint'
let l:config_file = ale#path#FindNearestFile(a:buffer, '.sqlfluff')
if !empty(l:config_file)
let l:cmd .= ' --config ' . ale#Escape(l:config_file)
else
let l:cmd .= ' --dialect ansi'
endif
let l:cmd .=
\ ' --format json '
\ . l:options
\ . ' %t'
return l:cmd
endfunction
function! ale_linters#sql#sqlfluff#Handle(buffer, version, lines) abort
let l:output = []
let l:json_lines = ale#util#FuzzyJSONDecode(a:lines, [])
if empty(l:json_lines)
return l:output
endif
let l:json = l:json_lines[0]
" if there's no warning, 'result' is `null`.
if empty(get(l:json, 'violations'))
return l:output
endif
if ale#semver#GTE(a:version, [3, 0, 0])
for l:violation in get(l:json, 'violations', [])
call add(l:output, {
\ 'filename': l:json.filepath,
\ 'lnum': l:violation.start_line_no,
\ 'end_lnum': l:violation.end_line_no,
\ 'col': l:violation.start_line_pos,
\ 'end_col': l:violation.end_line_pos,
\ 'text': l:violation.description,
\ 'code': l:violation.code,
\ 'type': 'W',
\})
endfor
else
for l:violation in get(l:json, 'violations', [])
call add(l:output, {
\ 'filename': l:json.filepath,
\ 'lnum': l:violation.line_no,
\ 'col': l:violation.line_pos,
\ 'text': l:violation.description,
\ 'code': l:violation.code,
\ 'type': 'W',
\})
endfor
endif
return l:output
endfunction
call ale#linter#Define('sql', {
\ 'name': 'sqlfluff',
\ 'executable': function('ale_linters#sql#sqlfluff#Executable'),
\ 'command': {buffer -> ale#semver#RunWithVersionCheck(
\ buffer,
\ ale_linters#sql#sqlfluff#Executable(buffer),
\ '%e --version',
\ function('ale_linters#sql#sqlfluff#Command'),
\ )},
\ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
\ buffer,
\ ale_linters#sql#sqlfluff#Executable(buffer),
\ '%e --version',
\ {buffer, version -> ale_linters#sql#sqlfluff#Handle(
\ buffer,
\ l:version,
\ lines)},
\ )},
\})
|