diff options
author | Carl Smedstad <carl.smedstad@protonmail.com> | 2022-11-23 11:58:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-23 19:58:49 +0900 |
commit | 590352304e49874c9598fd49b0ae5ff7ddbc854c (patch) | |
tree | 21723d560ccfd7ce1f11298a5fe393da5eac9766 | |
parent | 0b25d712b7978c3b6a3bd968645183377d9f1761 (diff) | |
download | ale-590352304e49874c9598fd49b0ae5ff7ddbc854c.zip |
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
-rw-r--r-- | ale_linters/sql/sqlfluff.vim | 8 | ||||
-rw-r--r-- | autoload/ale/fix/registry.vim | 5 | ||||
-rw-r--r-- | autoload/ale/fixers/sqlfluff.vim | 25 |
3 files changed, 37 insertions, 1 deletions
diff --git a/ale_linters/sql/sqlfluff.vim b/ale_linters/sql/sqlfluff.vim index 91a39908..0ec062bd 100644 --- a/ale_linters/sql/sqlfluff.vim +++ b/ale_linters/sql/sqlfluff.vim @@ -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')) diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 2062f543..b44adb0e 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -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'], diff --git a/autoload/ale/fixers/sqlfluff.vim b/autoload/ale/fixers/sqlfluff.vim new file mode 100644 index 00000000..1dc9f5c1 --- /dev/null +++ b/autoload/ale/fixers/sqlfluff.vim @@ -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 |