Skip to content

Commit d072dcb

Browse files
committed
feat: complete functions
0 parents  commit d072dcb

File tree

9 files changed

+359
-0
lines changed

9 files changed

+359
-0
lines changed

.github/FUNDING.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# These are supported funding model platforms
2+
3+
github: anvndev # Replace with your GitHub Sponsors username
4+
open_collective: # Replace with your Open Collective username
5+
ko_fi: # Replace with your Ko-fi username
6+
tidelift: # Replace with your Tidelift platform name/username
7+
community_bridge: # Replace with your Community Bridge project name
8+
liberapay: # Replace with your Liberapay username
9+
issuehunt: # Replace with your IssueHunt username
10+
otechie: # Replace with your Otechie username
11+
custom: # Replace with your custom funding URL

.github/workflows/ci.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: [3.8, 3.9, '3.10', '3.11']
15+
neovim-version: ['stable', 'nightly']
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Install Python dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install sqlparse
29+
pip install vim-vint
30+
pip install pynvim
31+
32+
- name: Install Neovim
33+
run: |
34+
if [ "${{ matrix.neovim-version }}" = "stable" ]; then
35+
sudo add-apt-repository -y ppa:neovim-ppa/stable
36+
else
37+
sudo add-apt-repository -y ppa:neovim-ppa/unstable
38+
fi
39+
sudo apt-get update
40+
sudo apt-get install -y neovim
41+
42+
- name: Check VimScript syntax
43+
run: |
44+
vint plugin/
45+
vint autoload/
46+
47+
- name: Run Python tests
48+
run: |
49+
python -m unittest discover -s tests
50+
51+
- name: Check for Python syntax errors
52+
run: |
53+
python -m py_compile python/*.py
54+
55+
- name: Check for Python type hints
56+
run: |
57+
pip install mypy
58+
mypy python/*.py
59+
60+
lint:
61+
runs-on: ubuntu-latest
62+
steps:
63+
- uses: actions/checkout@v3
64+
65+
- name: Set up Python
66+
uses: actions/setup-python@v4
67+
with:
68+
python-version: '3.11'
69+
70+
- name: Install dependencies
71+
run: |
72+
python -m pip install --upgrade pip
73+
pip install flake8
74+
pip install black
75+
76+
- name: Run flake8
77+
run: |
78+
flake8 python/ --count --select=E9,F63,F7,F82 --show-source --statistics
79+
80+
- name: Run black
81+
run: |
82+
black --check python/

.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
MANIFEST
23+
.pytest_cache/
24+
.coverage
25+
htmlcov/
26+
27+
# Python virtual environment
28+
venv/
29+
env/
30+
ENV/
31+
32+
# IDE
33+
.idea/
34+
.vscode/
35+
*.swp
36+
*.swo
37+
*~
38+
39+
# OS
40+
.DS_Store
41+
.DS_Store?
42+
._*
43+
.Spotlight-V100
44+
.Trashes
45+
ehthumbs.db
46+
Thumbs.db
47+
48+
# Neovim
49+
*.log
50+
*.log.*
51+
*.swp
52+
*.swo
53+
*~
54+
55+
# Project specific
56+
.mypy_cache/

LICENSE

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
MIT License
2+
3+
Copyright (c) 2025 anvndev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
---
24+
25+
⚠️ Security & Respect Notice:
26+
27+
This project may involve sensitive logic, security features, or proprietary ideas. By using or contributing to this project, you agree to:
28+
29+
- Respect the original author's work and intentions.
30+
- Not use this software for malicious, unethical, or harmful purposes.
31+
- Acknowledge the value of transparency, privacy, and responsible development.
32+
33+
Any violation may lead to removal of access or reporting of misuse.

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# nvim-sqlformat
2+
3+
A Neovim plugin for formatting SQL code using [sqlparse](https://github.com/andialbrecht/sqlparse).
4+
5+
## Features
6+
7+
- Format SQL code with customizable indentation
8+
- Support for keyword case customization (upper/lower)
9+
- Simple command and keybinding interface
10+
- Uses the powerful sqlparse library for consistent SQL formatting
11+
12+
## Requirements
13+
14+
- Neovim
15+
- Python 3.x
16+
- sqlparse Python package
17+
18+
## Installation
19+
20+
1. Install the plugin with your preferred plugin manager:
21+
22+
```vim
23+
" vim-plug
24+
Plug 'andevgo/nvim-sqlformat'
25+
26+
" packer.nvim
27+
use 'andevgo/nvim-sqlformat'
28+
29+
" lazy.nvim
30+
{
31+
'andevgo/nvim-sqlformat',
32+
config = function()
33+
-- Optional: Configure plugin settings here
34+
end
35+
}
36+
```
37+
38+
2. Install the required Python package:
39+
```bash
40+
pip install sqlparse
41+
```
42+
43+
## Usage
44+
45+
### Commands
46+
47+
- `:SQLFormat` - Format the entire buffer
48+
- `:'<,'>SQLFormat` - Format selected lines in visual mode
49+
50+
### Keybindings
51+
52+
By default, the plugin provides the following keybinding:
53+
- `<leader>sf` - Format the current buffer
54+
55+
## Configuration
56+
57+
You can customize the plugin behavior by setting these variables in your Neovim configuration:
58+
59+
```vim
60+
" Set indentation size (default: 2)
61+
let g:sqlformat_indent = 4
62+
63+
" Set keyword case (options: 'upper' or 'lower', default: 'upper')
64+
let g:sqlformat_keyword_case = 'lower'
65+
```
66+
67+
## License
68+
69+
This project is licensed under the MIT License - see the LICENSE file for details.
70+
71+
## Contributing
72+
73+
Contributions are welcome! Please feel free to submit a Pull Request.

autoload/sqlformat.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
" Author: Anvndev
2+
" File: autoload/sqlformat.vim
3+
" Description: Autoloaded functions for SQL formatting
4+
5+
function! sqlformat#Format(startline, endline) abort
6+
" Ensure Python3 is available
7+
if !has('python3')
8+
echohl ErrorMsg
9+
echomsg 'SQLFormat requires Neovim with +python3 support'
10+
echohl None
11+
return
12+
endif
13+
14+
" Get the selected lines
15+
let l:lines = getline(a:startline, a:endline)
16+
let l:input = join(l:lines, '\n')
17+
18+
" Call Python script to format SQL
19+
python3 << EOF
20+
import vim
21+
from sqlformat import format_sql
22+
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)
26+
vim.command('let l:formatted = ' + repr(formatted))
27+
EOF
28+
29+
" Replace buffer content with formatted SQL
30+
let l:formatted_lines = split(l:formatted, '\n')
31+
call setline(a:startline, l:formatted_lines)
32+
endfunction

doc/sqlformat.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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'

plugin/sqlformat.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
" Author: Anvndev
2+
" Description: SQL formatting plugin for Neovim using sqlparse
3+
4+
if exists('g:loaded_sqlformat')
5+
finish
6+
endif
7+
let g:loaded_sqlformat = 1
8+
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
14+
command! -range=% SQLFormat call sqlformat#Format(<line1>, <line2>)
15+
16+
" Keybinding (optional, customizable by user)
17+
nnoremap <silent> <leader>sf :SQLFormat<CR>

python/sqlformat.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2025 Anvndev
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
import sqlparse
13+
14+
def format_sql(sql, indent, keyword_case):
15+
"""
16+
Format SQL code using sqlparse.
17+
18+
Args:
19+
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
22+
Returns:
23+
str: Formatted SQL
24+
"""
25+
return sqlparse.format(
26+
sql,
27+
reindent=True,
28+
indent_width=indent,
29+
keyword_case=keyword_case,
30+
wrap_after=80
31+
)

0 commit comments

Comments
 (0)