diff --git a/home/.config/nvim/init.lua b/home/.config/nvim/init.lua index e5aaac2..b014083 100644 --- a/home/.config/nvim/init.lua +++ b/home/.config/nvim/init.lua @@ -28,6 +28,9 @@ require("terminal") -- Initialize the custom window management functionality require("window_management").setup() +-- Initializes custom commands and keybindings for handling code formatting +require("format_handler").setup() + -- Set configs for servers and enable them in the Neovim LSP Client require("lsp/setup") diff --git a/home/.config/nvim/lua/format_handler.lua b/home/.config/nvim/lua/format_handler.lua new file mode 100644 index 0000000..367c77f --- /dev/null +++ b/home/.config/nvim/lua/format_handler.lua @@ -0,0 +1,44 @@ +local utils = require("utils") +local M = {} +M.format_on_save = true + +function M.format(force) + local do_force = force or false + if M.format_on_save or do_force then + vim.lsp.buf.format() + end +end + +function M.format_enable() + M.format_on_save = true +end + +function M.format_disable() + M.format_on_save = false +end + +function M.setup() + local user_commands = { + { "FormatEnable", "lua require('format_handler').format_enable()" }, + { "FormatDisable", "lua require('format_handler').format_disable()" }, + } + for _, cmd in ipairs(user_commands) do + vim.api.nvim_command("command! " .. cmd[1] .. " " .. cmd[2]) + end + + utils.add_keymaps({ + n = { + ["ff"] = { + cmd = function() M.format(true) end + }, + ["fe"] = { + cmd = M.format_enable, + }, + ["fd"] = { + cmd = M.format_disable, + }, + }, + }) +end + +return M diff --git a/home/.config/nvim/lua/lsp/setup.lua b/home/.config/nvim/lua/lsp/setup.lua index 0a96867..68d7467 100644 --- a/home/.config/nvim/lua/lsp/setup.lua +++ b/home/.config/nvim/lua/lsp/setup.lua @@ -1,5 +1,6 @@ local utils = require("utils") local inlay_hints_handler = require("inlay_hints_handler") +local format_handler = require("format_handler") local function chain_on_attach(...) local funcs = { ... } @@ -18,7 +19,7 @@ local function global_on_attach(client, bufnr) vim.api.nvim_create_autocmd("BufWritePre", { buffer = bufnr, callback = function() - vim.lsp.buf.format() + format_handler.format() end, }) end diff --git a/home/.config/nvim/lua/plugs/rustaceanvim.lua b/home/.config/nvim/lua/plugs/rustaceanvim.lua index 82c32ea..c3de664 100644 --- a/home/.config/nvim/lua/plugs/rustaceanvim.lua +++ b/home/.config/nvim/lua/plugs/rustaceanvim.lua @@ -1,5 +1,6 @@ local utils = require("utils") local inlay_hints_handler = require("inlay_hints_handler") +local format_handler = require("format_handler") return { "mrcjkb/rustaceanvim", @@ -24,7 +25,7 @@ return { vim.api.nvim_create_autocmd("BufWritePre", { buffer = bufnr, callback = function() - vim.lsp.buf.format() + format_handler.format() end, }) end