From a6c59faa0f4b7f6226859c1a9b5bae83b1aef295 Mon Sep 17 00:00:00 2001 From: Cluas Date: Fri, 13 Sep 2019 05:48:27 +0800 Subject: [PATCH] feat: support sqlformat. (#2702) * feat: support sqlformat. --- autoload/ale/fix/registry.vim | 5 ++++ autoload/ale/fixers/sqlformat.vim | 16 +++++++++++++ doc/ale-sql.txt | 18 ++++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + .../test_sqlformat_fixer_callback.vader | 24 +++++++++++++++++++ 7 files changed, 66 insertions(+) create mode 100644 autoload/ale/fixers/sqlformat.vim create mode 100644 test/fixers/test_sqlformat_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index d21cb227..ace37207 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -260,6 +260,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['sql'], \ 'description': 'Fix SQL files with sqlfmt.', \ }, +\ 'sqlformat': { +\ 'function': 'ale#fixers#sqlformat#Fix', +\ 'suggested_filetypes': ['sql'], +\ 'description': 'Fix SQL files with sqlformat.', +\ }, \ 'google_java_format': { \ 'function': 'ale#fixers#google_java_format#Fix', \ 'suggested_filetypes': ['java'], diff --git a/autoload/ale/fixers/sqlformat.vim b/autoload/ale/fixers/sqlformat.vim new file mode 100644 index 00000000..6319c1ac --- /dev/null +++ b/autoload/ale/fixers/sqlformat.vim @@ -0,0 +1,16 @@ +" Author: Cluas +" Description: Fixing files with sqlformat. + +call ale#Set('sql_sqlformat_executable', 'sqlformat') +call ale#Set('sql_sqlformat_options', '') + +function! ale#fixers#sqlformat#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'sql_sqlformat_executable') + let l:options = ale#Var(a:buffer, 'sql_sqlformat_options') + + return { + \ 'command': ale#Escape(l:executable) + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' -' + \} +endfunction diff --git a/doc/ale-sql.txt b/doc/ale-sql.txt index f9bc6ac2..2807271b 100644 --- a/doc/ale-sql.txt +++ b/doc/ale-sql.txt @@ -39,5 +39,23 @@ g:ale_sql_sqlfmt_options *g:ale_sql_sqlfmt_options* At this time only the -u flag is available to format with upper-case. +=============================================================================== +sqlformat *ale-sql-sqlformat* + +g:ale_sql_sqlformat_executable *g:ale_sql_sqlformat_executable* + *b:ale_sql_sqlformat_executable* + Type: |String| + Default: `'sqlformat'` + + This variable sets executable used for sqlformat. + +g:ale_sql_sqlformat_options *g:ale_sql_sqlformat_options* + *b:ale_sql_sqlformat_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the sqlformat fixer. + + =============================================================================== vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl: diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 1b1202b9..98c6f354 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -426,6 +426,7 @@ Notes: * SQL * `pgformatter` * `sqlfmt` + * `sqlformat` * `sqlint` * Stylus * `stylelint` diff --git a/doc/ale.txt b/doc/ale.txt index aa8dbafe..5a123921 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2377,6 +2377,7 @@ documented in additional help files. sql.....................................|ale-sql-options| pgformatter...........................|ale-sql-pgformatter| sqlfmt................................|ale-sql-sqlfmt| + sqlformat.............................|ale-sql-sqlformat| stylus..................................|ale-stylus-options| stylelint.............................|ale-stylus-stylelint| sugarss.................................|ale-sugarss-options| diff --git a/supported-tools.md b/supported-tools.md index d94da030..96ebaf0f 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -435,6 +435,7 @@ formatting. * SQL * [pgformatter](https://github.com/darold/pgFormatter) * [sqlfmt](https://github.com/jackc/sqlfmt) + * [sqlformat](https://github.com/andialbrecht/sqlparse) * [sqlint](https://github.com/purcell/sqlint) * Stylus * [stylelint](https://github.com/stylelint/stylelint) diff --git a/test/fixers/test_sqlformat_fixer_callback.vader b/test/fixers/test_sqlformat_fixer_callback.vader new file mode 100644 index 00000000..4bace089 --- /dev/null +++ b/test/fixers/test_sqlformat_fixer_callback.vader @@ -0,0 +1,24 @@ +Before: + Save g:ale_sql_sqlformat_executable + Save g:ale_sql_sqlformat_options + +After: + Restore + +Execute(The sqlformat callback should return the correct default values): + AssertEqual + \ { + \ 'command': ale#Escape('sqlformat') . ' -' + \ }, + \ ale#fixers#sqlformat#Fix(bufnr('')) + +Execute(The sqlformat executable and options should be configurable): + let g:ale_sql_sqlformat_executable = '/path/to/sqlformat' + let g:ale_sql_sqlformat_options = '-a' + + AssertEqual + \ { + \ 'command': ale#Escape('/path/to/sqlformat') + \ . ' -a -' + \ }, + \ ale#fixers#sqlformat#Fix(bufnr(''))