Skip to content

Commit 7e3b668

Browse files
committed
fix(feat): plugin files and ci workflows
1 parent 87331b7 commit 7e3b668

File tree

6 files changed

+197
-42
lines changed

6 files changed

+197
-42
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# These are supported funding model platforms
22

3-
github: anvndev # Replace with your GitHub Sponsors username
3+
github: andevgo # Replace with your GitHub Sponsors username
44
open_collective: # Replace with your Open Collective username
55
ko_fi: # Replace with your Ko-fi username
66
tidelift: # Replace with your Tidelift platform name/username

autoload/sqlformat.vim

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,49 @@
33
" Description: Autoloaded functions for SQL formatting
44

55
function! sqlformat#Format(startline, endline) abort
6+
" Ensure Python3 is available
67
if !has('python3')
78
echohl ErrorMsg
89
echomsg 'SQLFormat requires Neovim with +python3 support'
910
echohl None
1011
return
1112
endif
1213

14+
" Get the selected lines
1315
let l:lines = getline(a:startline, a:endline)
1416
let l:input = join(l:lines, '\n')
1517

18+
" Call Python script to format SQL
1619
python3 << EOF
1720
import vim
1821
from sqlformat import format_sql
19-
input_sql = vim.eval('l:input')
20-
formatted = format_sql(input_sql)
21-
vim.command('let l:formatted = ' + repr(formatted))
22+
23+
try:
24+
input_sql = vim.eval('l:input')
25+
indent = int(vim.eval('g:sqlformat_indent'))
26+
keyword_case = vim.eval('g:sqlformat_keyword_case')
27+
line_width = int(vim.eval('g:sqlformat_line_width'))
28+
29+
formatted = format_sql(
30+
input_sql,
31+
indent=indent,
32+
keyword_case=keyword_case,
33+
line_width=line_width
34+
)
35+
vim.command('let l:formatted = ' + repr(formatted))
36+
except Exception as e:
37+
vim.command('let l:error = ' + repr(str(e)))
2238
EOF
2339

40+
" Check for Python errors
41+
if exists('l:error')
42+
echohl ErrorMsg
43+
echomsg 'SQLFormat error: ' . l:error
44+
echohl None
45+
return
46+
endif
47+
48+
" Replace buffer content with formatted SQL
2449
let l:formatted_lines = split(l:formatted, '\n')
2550
call setline(a:startline, l:formatted_lines)
2651
endfunction

doc/sqlformat.txt

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,85 @@
1-
*sqlformat.txt* SQL Formatting Plugin for Neovim
2-
3-
Overview~
4-
SQLFormat is a Neovim plugin that formats SQL code using the sqlparse Python library.
5-
6-
Installation~
7-
1. Install the plugin using your favorite plugin manager, e.g.:
8-
Using vim-plug:
9-
`Plug 'username/nvim-sqlformat'`
10-
2. Ensure Python3 and sqlparse are installed:
11-
`pip install sqlparse`
12-
13-
Usage~
14-
- Format the entire buffer: `:SQLFormat`
15-
- Format a range: `:'<,'>SQLFormat`
16-
- Default keybinding: `<leader>sf` (normal mode)
17-
18-
Configuration~
19-
- `g:sqlformat_indent`: Set indentation width (default: 2)
20-
- `g:sqlformat_keyword_case`: Set keyword case ('upper', 'lower', or '' for none; default: 'upper')
21-
22-
Example~
23-
let g:sqlformat_indent = 4
24-
let g:sqlformat_keyword_case = 'lower'
1+
*sqlformat.txt* SQL formatting plugin for Neovim
2+
3+
==============================================================================
4+
INTRODUCTION *sqlformat-intro*
5+
6+
nvim-sqlformat is a Neovim plugin that provides SQL code formatting using the
7+
sqlparse library. It offers customizable formatting options and integrates
8+
seamlessly with Neovim.
9+
10+
==============================================================================
11+
INSTALLATION *sqlformat-installation*
12+
13+
Using lazy.nvim:
14+
```lua
15+
{
16+
'andevgo/nvim-sqlformat',
17+
config = function()
18+
require('sqlformat').setup({
19+
indent = 2,
20+
keyword_case = 'upper',
21+
line_width = 80,
22+
format_on_save = false,
23+
custom_keymaps = {
24+
format = '<leader>sf'
25+
}
26+
})
27+
end
28+
}
29+
```
30+
31+
==============================================================================
32+
USAGE *sqlformat-usage*
33+
34+
Commands:
35+
:SQLFormat Format the entire buffer
36+
:'<,'>SQLFormat Format selected lines in visual mode
37+
38+
Keybindings:
39+
<leader>sf Format the current buffer (default)
40+
41+
==============================================================================
42+
CONFIGURATION *sqlformat-configuration*
43+
44+
The plugin can be configured through the setup function:
45+
46+
```lua
47+
require('sqlformat').setup({
48+
indent = 2, -- Number of spaces for indentation
49+
keyword_case = 'upper', -- 'upper' or 'lower' for SQL keywords
50+
line_width = 80, -- Maximum line width
51+
format_on_save = false, -- Enable/disable format on save
52+
custom_keymaps = { -- Custom keybindings
53+
format = '<leader>sf'
54+
}
55+
})
56+
```
57+
58+
==============================================================================
59+
REQUIREMENTS *sqlformat-requirements*
60+
61+
- Neovim with Python 3 support
62+
- sqlparse Python package
63+
64+
Install sqlparse:
65+
pip install sqlparse
66+
67+
==============================================================================
68+
TROUBLESHOOTING *sqlformat-troubleshooting*
69+
70+
1. Plugin not working:
71+
- Ensure Neovim has Python 3 support
72+
- Check if sqlparse is installed
73+
- Verify Python path in Neovim
74+
75+
2. Format on save not working:
76+
- Check if format_on_save is enabled
77+
- Ensure file has .sql extension
78+
79+
==============================================================================
80+
LICENSE *sqlformat-license*
81+
82+
This plugin is licensed under the MIT License.
83+
84+
==============================================================================
85+
vim:tw=78:ts=8:ft=help:norl:

lua/sqlformat.lua

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
-- Author: anvndev
22
-- 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,
3+
local M = {}
4+
5+
local defaults = {
6+
indent = 2,
7+
keyword_case = 'upper',
8+
line_width = 80,
9+
format_on_save = false,
10+
custom_keymaps = {
11+
format = '<leader>sf'
12+
}
713
}
14+
15+
M.setup = function(opts)
16+
opts = vim.tbl_deep_extend('force', defaults, opts or {})
17+
18+
-- Set global variables for VimScript
19+
vim.g.sqlformat_indent = opts.indent
20+
vim.g.sqlformat_keyword_case = opts.keyword_case
21+
vim.g.sqlformat_line_width = opts.line_width
22+
vim.g.sqlformat_format_on_save = opts.format_on_save
23+
24+
-- Set up custom keymaps
25+
if opts.custom_keymaps then
26+
vim.keymap.set('n', opts.custom_keymaps.format, ':SQLFormat<CR>', {
27+
silent = true,
28+
desc = 'Format SQL code'
29+
})
30+
end
31+
end
32+
33+
return M

plugin/sqlformat.vim

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,24 @@ if exists('g:loaded_sqlformat')
77
endif
88
let g:loaded_sqlformat = 1
99

10+
" Default settings if not set by Lua
11+
let g:sqlformat_indent = get(g:, 'sqlformat_indent', 2)
12+
let g:sqlformat_keyword_case = get(g:, 'sqlformat_keyword_case', 'upper')
13+
let g:sqlformat_line_width = get(g:, 'sqlformat_line_width', 80)
14+
let g:sqlformat_format_on_save = get(g:, 'sqlformat_format_on_save', 0)
15+
16+
" Define command
1017
command! -range=% SQLFormat call sqlformat#Format(<line1>, <line2>)
11-
nnoremap <silent> <leader>sf :SQLFormat<CR>
18+
19+
" Set up format on save if enabled
20+
if g:sqlformat_format_on_save
21+
augroup SQLFormat
22+
autocmd!
23+
autocmd BufWritePre *.sql :SQLFormat
24+
augroup END
25+
endif
26+
27+
" Set up default keybinding if not set by Lua
28+
if !exists('g:sqlformat_custom_keymaps')
29+
nnoremap <silent> <leader>sf :SQLFormat<CR>
30+
endif

python/sqlformat.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,45 @@
1212
# File: python/sqlformat.py
1313

1414
import sqlparse
15+
import vim
1516

1617

17-
def format_sql(sql):
18+
def format_sql(sql, indent, keyword_case, line_width=80):
1819
"""
19-
Format SQL code with fixed style: 2-space indent, uppercase keywords.
20+
Format SQL code using sqlparse.
2021
2122
Args:
2223
sql (str): SQL code to format
24+
indent (int): Number of spaces for indentation
25+
keyword_case (str): 'upper', 'lower', or None for keyword case
26+
line_width (int): Maximum line width for wrapping
2327
Returns:
2428
str: Formatted SQL
2529
"""
26-
return sqlparse.format(
27-
sql,
28-
reindent=True,
29-
indent_width=2,
30-
keyword_case='upper',
31-
wrap_after=80
32-
)
30+
try:
31+
vim.command('echo "Formatting SQL..."')
32+
33+
# Validate inputs
34+
if not isinstance(indent, int) or indent < 0:
35+
raise ValueError("Indent must be a positive integer")
36+
if keyword_case not in ['upper', 'lower', None]:
37+
raise ValueError("Keyword case must be 'upper', 'lower', or None")
38+
if not isinstance(line_width, int) or line_width < 0:
39+
raise ValueError("Line width must be a positive integer")
40+
41+
# Format the SQL
42+
formatted = sqlparse.format(
43+
sql,
44+
reindent=True,
45+
indent_width=indent,
46+
keyword_case=keyword_case,
47+
wrap_after=line_width
48+
)
49+
50+
vim.command('echo "SQL formatting complete"')
51+
return formatted
52+
53+
except Exception as e:
54+
error_msg = f"SQL formatting failed: {str(e)}"
55+
vim.command(f'echoerr "{error_msg}"')
56+
return sql # Return original SQL on error

0 commit comments

Comments
 (0)