Skip to content

andev0x/sql-formatter.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

sql-formatter.nvim

A lightweight, high-performance SQL formatter plugin for Neovim that leverages sqlparse for optimal formatting results with a Lua fallback for basic formatting.

Images

Features

  • πŸš€ High Performance: Uses external sqlparse formatter for speed
  • πŸ”§ Configurable: Extensive customization options
  • πŸ“ Format on Save: Automatic formatting when saving files
  • ⌨️ Key Bindings: Convenient shortcuts for formatting
  • 🎯 Multi-dialect: Support for PostgreSQL, MySQL, SQLite, and more
  • πŸ”„ Fallback: Pure Lua formatter when external tools unavailable
  • πŸ“‹ Range Formatting: Format selected text only

Requirements

  • Neovim >= 0.8.0
  • Optional: sqlparse for optimal performance (pip install sqlparse)

Installation

Prerequisites

For optimal performance, install sqlparse:

pip install sqlparse

Plugin Installation

lazy.nvim

{
"andev0x/sql-formatter.nvim",
ft = { "sql", "mysql", "plsql", "pgsql" },
config = function()
  vim.g.sqlformat_command = "sqlformat"
  vim.g.sqlformat_options = "-r -k upper"
  vim.g.sqlformat_prog = "sqlformat"
end,
},

packer.nvim

use {
  "andev0x/sql-formatter.nvim",
  ft = { "sql", "mysql", "plsql", "pgsql" },
  config = function()
    require("sql-formatter").setup()
  end,
}

vim-plug

Plug 'andev0x/sql-formatter.nvim'

Configuration

Minimal Setup

require("sql-formatter").setup()

Full Configuration

require("sql-formatter").setup({
  -- Core settings
  format_on_save = true,
  dialect = "postgresql",

  -- Indentation
  indent = "  ",
  tab_width = 2,
  use_tabs = false,

  -- Case formatting
  uppercase = true,
  identifier_case = "lower",
  function_case = "upper",
  datatype_case = "upper",

  -- Layout
  lines_between_queries = 2,
  max_column_length = 80,
  comma_start = false,
  operator_padding = true,

  -- File types
  filetypes = { "sql", "mysql", "plsql", "pgsql" },

  -- Key bindings
  keybindings = {
    format_buffer = "<leader>sf",
    format_selection = "<leader>ss",
    toggle_format_on_save = "<leader>st",
  },

  -- External formatter (sqlparse)
  external_formatter = {
    enabled = true,
    command = "sqlformat",
    args = {
      "--reindent",
      "--keywords", "upper",
      "--identifiers", "lower",
      "--strip-comments",
      "-"
    }
  },

  -- Notifications
  notify = {
    enabled = true,
    level = "info",
    timeout = 2000,
  },
})

Usage

Commands

  • :SQLFormat - Format entire buffer
  • :SQLFormatRange - Format selected lines (visual mode)
  • :SQLFormatToggle - Toggle format-on-save for current buffer
  • :SQLFormatInfo - Show formatter information

Key Bindings (default)

  • <leader>sf - Format buffer
  • <leader>ss - Format selection (visual mode)
  • <leader>st - Toggle format-on-save

Example

Before:

select u.id,u.name,p.title from users u left join posts p on u.id=p.user_id where u.active=1 and p.published=true order by u.created_at desc;

After:

SELECT
    u.id,
    u.name,
    p.title
FROM users u
LEFT JOIN posts p
    ON u.id = p.user_id
WHERE u.active = 1
    AND p.published = TRUE
ORDER BY u.created_at DESC;

Performance

This plugin prioritizes performance by:

  1. External Formatter: Uses sqlparse (Python) for heavy lifting
  2. Lua Fallback: Lightweight Lua formatter when external tools unavailable
  3. Lazy Loading: Only loads for SQL file types
  4. Minimal Dependencies: Pure Lua implementation with optional external tools

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see LICENSE file for details.

Author

anvndev (@andev0x)

Support

If you find this plugin useful, consider sponsoring the development.

About

A Neovim auto plugin for formatting SQL code using sqlparse.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published