From 8d8b295ef5ed60f1c1a978d241a85d922ffe05b5 Mon Sep 17 00:00:00 2001 From: Sajjad Hashemian Date: Mon, 27 May 2019 09:00:11 +0430 Subject: [PATCH] add pgformatter --- autoload/ale/fix/registry.vim | 5 ++++ autoload/ale/fixers/pgformatter.vim | 12 ++++++++++ doc/ale-sql.txt | 18 ++++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + .../test_pgformatter_fixer_callback.vader | 24 +++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 autoload/ale/fixers/pgformatter.vim create mode 100644 test/fixers/test_pgformatter_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 3a36f367..925181ca 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -305,6 +305,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['tex'], \ 'description' : 'Indent code within environments, commands, after headings and within special code blocks.', \ }, +\ 'pgformatter': { +\ 'function': 'ale#fixers#pgformatter#Fix', +\ 'suggested_filetypes': ['sql'], +\ 'description': 'A PostgreSQL SQL syntax beautifier', +\ }, \} " Reset the function registry to the default entries. diff --git a/autoload/ale/fixers/pgformatter.vim b/autoload/ale/fixers/pgformatter.vim new file mode 100644 index 00000000..9ea08ec6 --- /dev/null +++ b/autoload/ale/fixers/pgformatter.vim @@ -0,0 +1,12 @@ +call ale#Set('sql_pgformatter_executable', 'pg_format') +call ale#Set('sql_pgformatter_options', '') + +function! ale#fixers#pgformatter#Fix(buffer) abort + let l:executable = ale#Var(a:buffer, 'sql_pgformatter_executable') + let l:options = ale#Var(a:buffer, 'sql_pgformatter_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 75d4b0cf..f9bc6ac2 100644 --- a/doc/ale-sql.txt +++ b/doc/ale-sql.txt @@ -2,6 +2,24 @@ ALE SQL Integration *ale-sql-options* +=============================================================================== +pgformatter *ale-sql-pgformatter* + +g:ale_sql_pgformatter_executable *g:ale_sql_pgformatter_executable* + *b:ale_sql_pgformatter_executable* + Type: |String| + Default: `'pg_format'` + + This variable sets executable used for pgformatter. + +g:ale_sql_pgformatter_options *g:ale_sql_pgformatter_options* + *b:ale_sql_pgformatter_options* + Type: |String| + Default: `''` + + This variable can be set to pass additional options to the pgformatter fixer. + + =============================================================================== sqlfmt *ale-sql-sqlfmt* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 16dc10f9..9487829e 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -415,6 +415,7 @@ Notes: * `solhint` * `solium` * SQL + * `pgformatter` * `sqlfmt` * `sqlint` * Stylus diff --git a/doc/ale.txt b/doc/ale.txt index 4bb34947..e10e6fac 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2236,6 +2236,7 @@ documented in additional help files. spec....................................|ale-spec-options| rpmlint...............................|ale-spec-rpmlint| sql.....................................|ale-sql-options| + pgformatter...........................|ale-sql-pgformatter| sqlfmt................................|ale-sql-sqlfmt| stylus..................................|ale-stylus-options| stylelint.............................|ale-stylus-stylelint| diff --git a/supported-tools.md b/supported-tools.md index 1da25849..a34a3f90 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -424,6 +424,7 @@ formatting. * [solhint](https://github.com/protofire/solhint) * [solium](https://github.com/duaraghav8/Solium) * SQL + * [pgformatter](https://github.com/darold/pgFormatter) * [sqlfmt](https://github.com/jackc/sqlfmt) * [sqlint](https://github.com/purcell/sqlint) * Stylus diff --git a/test/fixers/test_pgformatter_fixer_callback.vader b/test/fixers/test_pgformatter_fixer_callback.vader new file mode 100644 index 00000000..5baa6f6f --- /dev/null +++ b/test/fixers/test_pgformatter_fixer_callback.vader @@ -0,0 +1,24 @@ +Before: + Save g:ale_sql_pgformatter_executable + Save g:ale_sql_pgformatter_options + +After: + Restore + +Execute(The pgFormatter callback should return the correct default values): + AssertEqual + \ { + \ 'command': ale#Escape('pg_format') + \ }, + \ ale#fixers#pgformatter#Fix(bufnr('')) + +Execute(The pgFormatter executable and options should be configurable): + let g:ale_sql_pgformatter_executable = '/path/to/pg_format' + let g:ale_sql_pgformatter_options = '-n' + + AssertEqual + \ { + \ 'command': ale#Escape('/path/to/pg_format') + \ . ' -n', + \ }, + \ ale#fixers#pgformatter#Fix(bufnr(''))