]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/sql/sqlfluff.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit '56df844d3c39ec494dacc69eae34272b27db185a' as '.vim/bundle/asyncomplete'
[etc/vim.git] / .vim / bundle / ale / ale_linters / sql / sqlfluff.vim
1 " Author: Carl Smedstad <carl.smedstad at protonmail dot com>
2 " Description: sqlfluff for SQL files
3
4 let g:ale_sql_sqlfluff_executable =
5 \   get(g:, 'ale_sql_sqlfluff_executable', 'sqlfluff')
6
7 let g:ale_sql_sqlfluff_options =
8 \   get(g:, 'ale_sql_sqlfluff_options', '')
9
10 function! ale_linters#sql#sqlfluff#Executable(buffer) abort
11     return ale#Var(a:buffer, 'sql_sqlfluff_executable')
12 endfunction
13
14 function! ale_linters#sql#sqlfluff#Command(buffer, version) abort
15     let l:executable = ale_linters#sql#sqlfluff#Executable(a:buffer)
16     let l:options = ale#Var(a:buffer, 'sql_sqlfluff_options')
17
18     let l:cmd =
19     \    ale#Escape(l:executable)
20     \    . ' lint'
21
22     let l:config_file = ale#path#FindNearestFile(a:buffer, '.sqlfluff')
23
24     if !empty(l:config_file)
25         let l:cmd .= ' --config ' . ale#Escape(l:config_file)
26     else
27         let l:cmd .= ' --dialect ansi'
28     endif
29
30     let l:cmd .=
31     \   ' --format json '
32     \   . l:options
33     \   . ' %t'
34
35     return l:cmd
36 endfunction
37
38 function! ale_linters#sql#sqlfluff#Handle(buffer, version, lines) abort
39     let l:output = []
40     let l:json_lines = ale#util#FuzzyJSONDecode(a:lines, [])
41
42     if empty(l:json_lines)
43         return l:output
44     endif
45
46     let l:json = l:json_lines[0]
47
48     " if there's no warning, 'result' is `null`.
49     if empty(get(l:json, 'violations'))
50         return l:output
51     endif
52
53     if ale#semver#GTE(a:version, [3, 0, 0])
54         for l:violation in get(l:json, 'violations', [])
55             let l:err = {
56             \   'filename': l:json.filepath,
57             \   'lnum': l:violation.start_line_no,
58             \   'col': l:violation.start_line_pos,
59             \   'text': l:violation.description,
60             \   'code': l:violation.code,
61             \   'type': 'W',
62             \}
63
64             if has_key(l:violation, 'end_line_no')
65                 let l:err.end_lnum = l:violation.end_line_no
66             endif
67
68             if has_key(l:violation, 'end_line_pos')
69                 let l:err.end_col = l:violation.end_line_pos
70             endif
71
72             call add(l:output, l:err)
73         endfor
74     else
75         for l:violation in get(l:json, 'violations', [])
76             call add(l:output, {
77             \   'filename': l:json.filepath,
78             \   'lnum': l:violation.line_no,
79             \   'col': l:violation.line_pos,
80             \   'text': l:violation.description,
81             \   'code': l:violation.code,
82             \   'type': 'W',
83             \})
84         endfor
85     endif
86
87     return l:output
88 endfunction
89
90 call ale#linter#Define('sql', {
91 \   'name': 'sqlfluff',
92 \   'executable': function('ale_linters#sql#sqlfluff#Executable'),
93 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
94 \       buffer,
95 \       ale_linters#sql#sqlfluff#Executable(buffer),
96 \       '%e --version',
97 \       function('ale_linters#sql#sqlfluff#Command'),
98 \   )},
99 \   'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
100 \       buffer,
101 \       ale_linters#sql#sqlfluff#Executable(buffer),
102 \       '%e --version',
103 \       {buffer, version -> ale_linters#sql#sqlfluff#Handle(
104 \           buffer,
105 \           l:version,
106 \           lines)},
107 \   )},
108 \})