Skip to content

Commit 87331b7

Browse files
committed
feat: sql formatting plugin with sqlparse
1 parent d072dcb commit 87331b7

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

autoload/sqlformat.vim

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,24 @@
33
" Description: Autoloaded functions for SQL formatting
44

55
function! sqlformat#Format(startline, endline) abort
6-
" Ensure Python3 is available
76
if !has('python3')
87
echohl ErrorMsg
98
echomsg 'SQLFormat requires Neovim with +python3 support'
109
echohl None
1110
return
1211
endif
1312

14-
" Get the selected lines
1513
let l:lines = getline(a:startline, a:endline)
1614
let l:input = join(l:lines, '\n')
1715

18-
" Call Python script to format SQL
1916
python3 << EOF
2017
import vim
2118
from sqlformat import format_sql
2219
input_sql = vim.eval('l:input')
23-
indent = int(vim.eval('g:sqlformat_indent'))
24-
keyword_case = vim.eval('g:sqlformat_keyword_case')
25-
formatted = format_sql(input_sql, indent, keyword_case)
20+
formatted = format_sql(input_sql)
2621
vim.command('let l:formatted = ' + repr(formatted))
2722
EOF
2823

29-
" Replace buffer content with formatted SQL
3024
let l:formatted_lines = split(l:formatted, '\n')
3125
call setline(a:startline, l:formatted_lines)
3226
endfunction

lua/sqlformat.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Author: anvndev
2+
-- File: lua/sqlformat.lua
3+
return {
4+
setup = function()
5+
-- Plugin is loaded via plugin/sqlformat.vim, so no additional Lua setup is needed
6+
end,
7+
}

plugin/sqlformat.vim

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
" Author: Anvndev
2-
" Description: SQL formatting plugin for Neovim using sqlparse
2+
" File: plugin/sqlformat.vim
3+
" Description: SQL formatting plugin for Neovim
34

45
if exists('g:loaded_sqlformat')
56
finish
67
endif
78
let g:loaded_sqlformat = 1
89

9-
" Default settings
10-
let g:sqlformat_indent = get(g:, 'sqlformat_indent', 2)
11-
let g:sqlformat_keyword_case = get(g:, 'sqlformat_keyword_case', 'upper')
12-
13-
" Define command
1410
command! -range=% SQLFormat call sqlformat#Format(<line1>, <line2>)
15-
16-
" Keybinding (optional, customizable by user)
1711
nnoremap <silent> <leader>sf :SQLFormat<CR>

python/sqlformat.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@
99
#
1010
# The above copyright notice and this permission notice shall be included in all
1111
# copies or substantial portions of the Software.
12+
# File: python/sqlformat.py
13+
1214
import sqlparse
1315

14-
def format_sql(sql, indent, keyword_case):
16+
17+
def format_sql(sql):
1518
"""
16-
Format SQL code using sqlparse.
19+
Format SQL code with fixed style: 2-space indent, uppercase keywords.
1720
1821
Args:
1922
sql (str): SQL code to format
20-
indent (int): Number of spaces for indentation
21-
keyword_case (str): 'upper', 'lower', or None for keyword case
2223
Returns:
2324
str: Formatted SQL
2425
"""
2526
return sqlparse.format(
2627
sql,
2728
reindent=True,
28-
indent_width=indent,
29-
keyword_case=keyword_case,
29+
indent_width=2,
30+
keyword_case='upper',
3031
wrap_after=80
3132
)

0 commit comments

Comments
 (0)