Fix bug in sqlfluff implementation & implement fixer support (#4365)

* Account for no sqlfluff output

Avoid crashes when there isn't any output from sqlfluff.

* Add supplort for sqlfluff as a fixer
This commit is contained in:
Carl Smedstad 2022-11-23 11:58:49 +01:00 committed by GitHub
parent 0b25d712b7
commit 590352304e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -37,7 +37,13 @@ endfunction
function! ale_linters#sql#sqlfluff#Handle(buffer, lines) abort
let l:output = []
let l:json = ale#util#FuzzyJSONDecode(a:lines, {})[0]
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'))

View File

@ -386,6 +386,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['sh'],
\ 'description': 'Fix sh files with shfmt.',
\ },
\ 'sqlfluff': {
\ 'function': 'ale#fixers#sqlfluff#Fix',
\ 'suggested_filetypes': ['sql'],
\ 'description': 'Fix SQL files with sqlfluff.',
\ },
\ 'sqlfmt': {
\ 'function': 'ale#fixers#sqlfmt#Fix',
\ 'suggested_filetypes': ['sql'],

View File

@ -0,0 +1,25 @@
" Author: Carl Smedstad <carl.smedstad at protonmail dot com>
" Description: Fixing SQL files with sqlfluff
call ale#Set('sql_sqlfluff_executable', 'sqlfluff')
function! ale#fixers#sqlfluff#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'sql_sqlfluff_executable')
let l:cmd =
\ ale#Escape(l:executable)
\ . ' fix --force'
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
return {
\ 'command': l:cmd . ' %t > /dev/null',
\ 'read_temporary_file': 1,
\}
endfunction