Skip to content

Commit

Permalink
feat(nvim): use conform to format Lua files
Browse files Browse the repository at this point in the history
This is replacing using formatprg to run the same program (stylua), this
has extra benefits.

Conform has the ability to run automatically on save, but this is
currently using `formatexpr` to run on demand using `gq`.
  • Loading branch information
dylanpinn committed Apr 25, 2024
1 parent ce12db7 commit 5e59ac0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
5 changes: 0 additions & 5 deletions nvim/after/ftplugin/lua.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
-- Tabs should have 4 space width.
vim.opt_local.tabstop = 4

-- Use stylua as formatter if it is available.
if vim.fn.executable("stylua") == 1 then
vim.opt_local.formatprg = "stylua --stdin-filepath % --search-parent-directories -"
end

-- Use luacheck as compiler for sh files.
vim.cmd([[compiler luacheck]])
7 changes: 7 additions & 0 deletions nvim/after/plugin/conform.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local options = {
formatters_by_ft = {
lua = { "stylua" },
},
}

require("conform").setup(options)
3 changes: 0 additions & 3 deletions nvim/after/plugin/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,5 @@ vim.api.nvim_create_autocmd("LspAttach", {
vim.keymap.set("n", "<localleader>rn", vim.lsp.buf.rename, opts)
vim.keymap.set({ "n", "v" }, "<localleader>ca", vim.lsp.buf.code_action, opts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
vim.keymap.set("n", "<localleader>f", function()
vim.lsp.buf.format({ async = true })
end, opts)
end,
})
7 changes: 7 additions & 0 deletions nvim/lua/dcp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
vim.g.mapleader = "\\"
vim.g.maplocalleader = " "

-- Leader,a series acts on the whole file.
--
-- Leader,ai indents the whole file.
vim.keymap.set("n", "<Leader>ai", "mzgg=G`z")
-- Leader,af formats the whole file.
vim.keymap.set("n", "<Leader>af", "mzgggqG`z")

-- Leader,b list all open buffers.
vim.keymap.set("n", "<Leader>b", ":buffer <C-d>")

Expand Down

0 comments on commit 5e59ac0

Please sign in to comment.