This repository is heavily inspired by ckipp01's repo
stylua.nvim is a minimal wrapper around the Lua code formatter, StyLua. It does pretty much what you'd expect it to do, format your Lua file using Stylua.
- Neovim 0.5+
- StyLua
If you have Rust installed, you can install Stylua using cargo
cargo install stylua
MacOS
brew install stylua
You can also download the binary here
Use your faviourie package manager to install stylua.nvim
use 'shoukoo/stylua.nvim'
Plug 'shoukoo/stylua.nvim'
local opts = { noremap=true, silent=true }
buf_set_keymap("n", "<leader>f", "<cmd>lua require("stylua")format_file()<CR>', opts)
You can follow the standalone instruction to install sumneko_lua
local on_attach = function(lsp)
return function(_, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
-- Mappings.
local opts = { noremap = true, silent = true }
-- See `:help vim.lsp.*` for documentation on any of the below functions
if lsp == 'sumneko_lua' then
buf_set_keymap('n', 'gf', [[<Cmd>lua require"stylua".format_file()<CR>]], opts)
else
buf_set_keymap('n', 'gf', '<Cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end
...
end
end
...
nvim_lsp.sumneko_lua.setup({
cmd = { sumneko_binary, '-E', sumneko_root_path .. '/main.lua' },
on_attach = on_attach('sumneko_lua'),
capabilities = capabilities,
...
The code below uses :Format
cmd to format code:
local lsp_config = require("lspconfig")
lsp_config.sumneko_lua.setup({
commands = {
Format = {
function()
require("stylua").format_file()
end,
},
},
...
})
You can get more detail by reading the help docs.