Skip to content

Commit

Permalink
feat(nvim): migrate to conform for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sbulav committed Sep 18, 2023
1 parent 3dfcfc4 commit 3d78a82
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 67 deletions.
18 changes: 18 additions & 0 deletions nvim/lua/config/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ vim.api.nvim_create_autocmd({ "BufEnter", "TermOpen" }, {
vim.b.miniindentscope_disable = vim.tbl_contains({ "help", "terminal", "nofile", "prompt" }, vim.bo.buftype)
end,
})

vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
-- FormatDisable! will disable formatting just for this buffer
vim.b.disable_autoformat = true
else
vim.g.disable_autoformat = true
end
end, {
desc = "Disable autoformat-on-save",
bang = true,
})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Re-enable autoformat-on-save",
})
46 changes: 0 additions & 46 deletions nvim/lua/lsp/formatting.lua

This file was deleted.

19 changes: 19 additions & 0 deletions nvim/lua/plugins/conform.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- formatters
return {
"stevearc/conform.nvim",
event = "BufRead",
config = function()
require("conform").setup {
format_on_save = function(bufnr)
-- Disable with a global or buffer-local variable
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { async = true, timeout_ms = 500, lsp_fallback = true }
end,
formatters_by_ft = {
lua = { "stylua" },
},
}
end,
}
21 changes: 0 additions & 21 deletions nvim/lua/plugins/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ return {
require("lsp.utils").on_attach(function(client, buffer)
require("lsp.utils").custom_on_init()
require("lsp.utils").custom_on_attach(client, buffer)
require("lsp.formatting").custom_on_attach(client, buffer)
end)

local servers = opts.servers
Expand All @@ -143,26 +142,6 @@ return {
end,
},

-- -- formatters
{
"jose-elias-alvarez/null-ls.nvim",
event = "BufReadPre",
dependencies = { "mason.nvim" },
opts = function()
local nls = require "null-ls"
return {
sources = {
nls.builtins.diagnostics.flake8,
nls.builtins.formatting.alejandra,
nls.builtins.formatting.gofmt,
nls.builtins.formatting.goimports,
nls.builtins.formatting.stylua,
},
on_attach = require("lsp.utils").custom_on_attach,
}
end,
},

-- cmdline tools and lsp servers
{

Expand Down

0 comments on commit 3d78a82

Please sign in to comment.