mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-23 01:09:56 +08:00
feat(sql): support sqlformat & fix encoding
This commit is contained in:
parent
dcfc4b0404
commit
e5387c15c8
@ -20,10 +20,16 @@
|
|||||||
"
|
"
|
||||||
" 1. `enabled_formater`: set the default formatter for sql, default is ['sqlfmtorg']
|
" 1. `enabled_formater`: set the default formatter for sql, default is ['sqlfmtorg']
|
||||||
" https://github.com/sql-formatter-org/sql-formatter
|
" https://github.com/sql-formatter-org/sql-formatter
|
||||||
|
" you can also use `sqlformat` which is from https://github.com/andialbrecht/sqlparse
|
||||||
" 2. `sql_formatter_command`: Set the command of sql-formatter.
|
" 2. `sql_formatter_command`: Set the command of sql-formatter.
|
||||||
" 3. `sql_dialect`: set the SQL dialect, default is basic sql.
|
" 3. `sql_dialect`: set the SQL dialect, default is basic sql.
|
||||||
" 4. `sql_formatter_config`: set the path of config path. default is empty
|
" 4. `sql_formatter_config`: set the path of config path. default is empty
|
||||||
" string.
|
" string.
|
||||||
|
" 5. `sqlformat_cmd`: set the command for sqlformat.
|
||||||
|
" 6. `sqlformat_output_encode`: set the output encoding of sqlformat, default
|
||||||
|
" is `utf-8`. If you are using window, maybe need to change this option to
|
||||||
|
" `cp936`.
|
||||||
|
"
|
||||||
"
|
"
|
||||||
|
|
||||||
if exists('s:enabled_formater')
|
if exists('s:enabled_formater')
|
||||||
@ -34,6 +40,8 @@ let s:enabled_formater = ['sqlfmtorg']
|
|||||||
let s:sql_formatter_command = 'sql-formatter'
|
let s:sql_formatter_command = 'sql-formatter'
|
||||||
let s:sql_dialect = 'sql'
|
let s:sql_dialect = 'sql'
|
||||||
let s:sql_formatter_config = ''
|
let s:sql_formatter_config = ''
|
||||||
|
let s:sqlformat_cmd = 'sqlformat'
|
||||||
|
let s:sqlformat_output_encode = 'utf-8'
|
||||||
|
|
||||||
function! SpaceVim#layers#lang#sql#plugins() abort
|
function! SpaceVim#layers#lang#sql#plugins() abort
|
||||||
let plugins = []
|
let plugins = []
|
||||||
@ -46,6 +54,8 @@ function! SpaceVim#layers#lang#sql#set_variable(opt) abort
|
|||||||
let s:sql_formatter_command = get(a:opt, 'sql_formatter_command', s:sql_formatter_command)
|
let s:sql_formatter_command = get(a:opt, 'sql_formatter_command', s:sql_formatter_command)
|
||||||
let s:sql_dialect = get(a:opt, 'sql_dialect', s:sql_dialect)
|
let s:sql_dialect = get(a:opt, 'sql_dialect', s:sql_dialect)
|
||||||
let s:sql_formatter_config = get(a:opt, 'sql_formatter_config', s:sql_formatter_config)
|
let s:sql_formatter_config = get(a:opt, 'sql_formatter_config', s:sql_formatter_config)
|
||||||
|
let s:sqlformat_cmd = get(a:opt, 'sqlformat_cmd', s:sqlformat_cmd)
|
||||||
|
let s:sqlformat_output_encode = get(a:opt, 'sqlformat_output_encode', s:sqlformat_output_encode)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#lang#sql#config() abort
|
function! SpaceVim#layers#lang#sql#config() abort
|
||||||
@ -60,6 +70,12 @@ function! SpaceVim#layers#lang#sql#config() abort
|
|||||||
\ 'args': ['-l', s:sql_dialect,] + argv,
|
\ 'args': ['-l', s:sql_dialect,] + argv,
|
||||||
\ 'stdin': 1,
|
\ 'stdin': 1,
|
||||||
\ }
|
\ }
|
||||||
|
let g:neoformat_sql_sqlformat = {
|
||||||
|
\ 'exe': s:sqlformat_cmd,
|
||||||
|
\ 'args': ['--reindent', '-'],
|
||||||
|
\ 'output_encode': s:sqlformat_output_encode,
|
||||||
|
\ 'stdin': 1,
|
||||||
|
\ }
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#lang#sql#health() abort
|
function! SpaceVim#layers#lang#sql#health() abort
|
||||||
|
@ -130,6 +130,10 @@ function! s:neoformat(bang, user_input, start_line, end_line) abort
|
|||||||
|
|
||||||
call s:deletelines(len(new_buffer), line('$'))
|
call s:deletelines(len(new_buffer), line('$'))
|
||||||
|
|
||||||
|
if cmd.output_encode != 'utf-8'
|
||||||
|
let new_buffer = map(new_buffer, 'iconv(v:val, "cp936", "utf-8")')
|
||||||
|
endif
|
||||||
|
|
||||||
call setline(1, new_buffer)
|
call setline(1, new_buffer)
|
||||||
|
|
||||||
call add(formatters_changed, cmd.name)
|
call add(formatters_changed, cmd.name)
|
||||||
@ -239,6 +243,7 @@ endfunction
|
|||||||
|
|
||||||
function! s:generate_cmd(definition, filetype) abort
|
function! s:generate_cmd(definition, filetype) abort
|
||||||
let executable = get(a:definition, 'exe', '')
|
let executable = get(a:definition, 'exe', '')
|
||||||
|
let output_encode = get(a:definition, 'output_encode', 'utf-8')
|
||||||
if executable == ''
|
if executable == ''
|
||||||
call neoformat#utils#log('no exe field in definition')
|
call neoformat#utils#log('no exe field in definition')
|
||||||
return {}
|
return {}
|
||||||
@ -309,6 +314,7 @@ function! s:generate_cmd(definition, filetype) abort
|
|||||||
return {
|
return {
|
||||||
\ 'exe': fullcmd,
|
\ 'exe': fullcmd,
|
||||||
\ 'stdin': using_stdin,
|
\ 'stdin': using_stdin,
|
||||||
|
\ 'output_encode' : output_encode,
|
||||||
\ 'stderr_log': stderr_log,
|
\ 'stderr_log': stderr_log,
|
||||||
\ 'name': a:definition.exe,
|
\ 'name': a:definition.exe,
|
||||||
\ 'replace': get(a:definition, 'replace', 0),
|
\ 'replace': get(a:definition, 'replace', 0),
|
||||||
|
@ -5186,11 +5186,18 @@ add following snippet to your |SpaceVim-options| file.
|
|||||||
OPTIONS
|
OPTIONS
|
||||||
|
|
||||||
1. `enabled_formater`: set the default formatter for sql, default is
|
1. `enabled_formater`: set the default formatter for sql, default is
|
||||||
['sqlfmtorg'] https://github.com/sql-formatter-org/sql-formatter
|
['sqlfmtorg'] https://github.com/sql-formatter-org/sql-formatter you can
|
||||||
|
also use `sqlformat` which is from
|
||||||
|
https://github.com/andialbrecht/sqlparse
|
||||||
2. `sql_formatter_command`: Set the command of sql-formatter.
|
2. `sql_formatter_command`: Set the command of sql-formatter.
|
||||||
3. `sql_dialect`: set the SQL dialect, default is basic sql.
|
3. `sql_dialect`: set the SQL dialect, default is basic sql.
|
||||||
4. `sql_formatter_config`: set the path of config path. default is empty
|
4. `sql_formatter_config`: set the path of config path. default is empty
|
||||||
string.
|
string.
|
||||||
|
5. `sqlformat_cmd`: set the command for sqlformat.
|
||||||
|
6. `sqlformat_output_encode`: set the output encoding of sqlformat, default
|
||||||
|
is `utf-8`. If you are using window, maybe need to change this option to
|
||||||
|
`cp936`.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
Loading…
x
Reference in New Issue
Block a user