neovim: add module for enabling/disabling formatting
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
|
local formatting = require("daniil.formatting")
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("ClearQuickfixList", function()
|
vim.api.nvim_create_user_command("ClearQuickfixList", function()
|
||||||
vim.fn.setqflist({})
|
vim.fn.setqflist({})
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("FormattingEnable", formatting.enable, {})
|
||||||
|
vim.api.nvim_create_user_command("FormattingDisable", formatting.disable, {})
|
||||||
|
vim.api.nvim_create_user_command("FormattingToggle", formatting.toggle, {})
|
||||||
|
vim.api.nvim_create_user_command("FormattingStatus", formatting.status, {})
|
||||||
|
|||||||
@@ -4,19 +4,20 @@ local lsp_installer = require("mason-lspconfig")
|
|||||||
local lspconfig = require("lspconfig")
|
local lspconfig = require("lspconfig")
|
||||||
local null_ls = require("null-ls")
|
local null_ls = require("null-ls")
|
||||||
local util = lspconfig.util
|
local util = lspconfig.util
|
||||||
|
local formatting = require("daniil.formatting")
|
||||||
|
|
||||||
local formatting = null_ls.builtins.formatting
|
local null_formatting = null_ls.builtins.formatting
|
||||||
|
|
||||||
mason.setup()
|
mason.setup()
|
||||||
lsp_installer.setup()
|
lsp_installer.setup()
|
||||||
|
|
||||||
null_ls.setup({
|
null_ls.setup({
|
||||||
sources = {
|
sources = {
|
||||||
formatting.prettierd,
|
null_formatting.prettierd,
|
||||||
formatting.stylua,
|
null_formatting.stylua,
|
||||||
formatting.gofmt,
|
null_formatting.gofmt,
|
||||||
formatting.goimports,
|
null_formatting.goimports,
|
||||||
formatting.shfmt,
|
null_formatting.shfmt,
|
||||||
},
|
},
|
||||||
on_attach = function()
|
on_attach = function()
|
||||||
local group = vim.api.nvim_create_augroup("NullLsLspFormatting", { clear = true })
|
local group = vim.api.nvim_create_augroup("NullLsLspFormatting", { clear = true })
|
||||||
@@ -24,6 +25,10 @@ null_ls.setup({
|
|||||||
pattern = "*",
|
pattern = "*",
|
||||||
group = group,
|
group = group,
|
||||||
callback = function()
|
callback = function()
|
||||||
|
if not formatting.is_enabled() then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
vim.lsp.buf.format()
|
vim.lsp.buf.format()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
local formatting = require("daniil.formatting")
|
||||||
|
|
||||||
-- Disable different keys and commands
|
-- Disable different keys and commands
|
||||||
vim.keymap.set({ "n", "i", "v" }, "<PageDown>", "<nop>")
|
vim.keymap.set({ "n", "i", "v" }, "<PageDown>", "<nop>")
|
||||||
vim.keymap.set({ "n", "i", "v" }, "<PageUp>", "<nop>")
|
vim.keymap.set({ "n", "i", "v" }, "<PageUp>", "<nop>")
|
||||||
@@ -35,3 +37,6 @@ vim.keymap.set("n", "<C-q><C-q>", function()
|
|||||||
end)
|
end)
|
||||||
vim.keymap.set("n", "<C-q>n", vim.cmd.cnext)
|
vim.keymap.set("n", "<C-q>n", vim.cmd.cnext)
|
||||||
vim.keymap.set("n", "<C-q>N", vim.cmd.cprevious)
|
vim.keymap.set("n", "<C-q>N", vim.cmd.cprevious)
|
||||||
|
|
||||||
|
-- formatting
|
||||||
|
vim.keymap.set("n", "<C-g>", formatting.toggle)
|
||||||
|
|||||||
35
neovim/.config/nvim/lua/daniil/formatting/init.lua
Normal file
35
neovim/.config/nvim/lua/daniil/formatting/init.lua
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
vim.g.daniil_formatting_enabled = 1
|
||||||
|
|
||||||
|
function M.enable()
|
||||||
|
vim.g.daniil_formatting_enabled = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.disable()
|
||||||
|
vim.g.daniil_formatting_enabled = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.toggle()
|
||||||
|
if M.is_enabled() then
|
||||||
|
M.disable()
|
||||||
|
else
|
||||||
|
M.enable()
|
||||||
|
end
|
||||||
|
|
||||||
|
M.status()
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.is_enabled()
|
||||||
|
return vim.g.daniil_formatting_enabled == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.status()
|
||||||
|
if M.is_enabled() then
|
||||||
|
vim.notify("Formatting enabled")
|
||||||
|
else
|
||||||
|
vim.notify("Formatting disabled")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
Reference in New Issue
Block a user