Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stylua.nvim

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.

Getting started 🏡

Requirements

  • Neovim 0.5+
  • StyLua

Install 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

Installation 💾

Use your faviourie package manager to install stylua.nvim

Packer

use 'shoukoo/stylua.nvim'

Vim-Plug

Plug 'shoukoo/stylua.nvim'

Setup 🛠️

Assign a dedicated key to format the file:

local opts = { noremap=true, silent=true }
buf_set_keymap("n", "<leader>f", "<cmd>lua require("stylua")format_file()<CR>', opts)

Usage via nvim-lspconfig with sumneko_lua:

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,
...
Create a seperate command to format your code

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.

About

stylua.nvim is a minimal wrapper around the Lua code formatter, Stylua.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages