From f8b253ca527472c832548d0c26cd450e6a357504 Mon Sep 17 00:00:00 2001 From: Jan Christoph Ebersbach Date: Mon, 4 Mar 2024 14:39:38 +0100 Subject: [PATCH] feat: support enable, disable and toggle formatting per buffer --- lua/lsp-format/init.lua | 42 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lua/lsp-format/init.lua b/lua/lsp-format/init.lua index 2c0c7b8..4e07438 100644 --- a/lua/lsp-format/init.lua +++ b/lua/lsp-format/init.lua @@ -4,6 +4,7 @@ local M = { format_options = {}, disabled = false, disabled_filetypes = {}, + disabled_buffers = {}, saving_buffers = {}, queue = {}, buffers = {}, @@ -53,6 +54,27 @@ M.setup = function(format_options) M.enable, { nargs = "?", bar = true, complete = "filetype", force = true, bang = true } ) + vim.api.nvim_create_user_command( + "FormatToggleBuffer", + function() + M.toggleBuffer { buf = vim.api.nvim_get_current_buf() } + end, + { nargs = "?", bar = true, complete = "filetype", force = true } + ) + vim.api.nvim_create_user_command( + "FormatDisableBuffer", + function() + M.disableBuffer { buf = vim.api.nvim_get_current_buf() } + end, + { nargs = "?", bar = true, complete = "filetype", force = true } + ) + vim.api.nvim_create_user_command( + "FormatEnableBuffer", + function() + M.enableBuffer { buf = vim.api.nvim_get_current_buf() } + end, + { nargs = "?", bar = true, complete = "filetype", force = true, bang = true } + ) end ---@param key string @@ -82,7 +104,7 @@ end ---@param options table M.format = function(options) local bufnr = options.buf - if M.saving_buffers[bufnr] or M.disabled then + if M.saving_buffers[bufnr] or M.disabled_buffers[bufnr] or M.disabled then return end @@ -165,6 +187,24 @@ M.toggle = function(options) end end +---@param options table +M.disableBuffer = function(options) + local bufnr = options.buf + M.disabled_buffers[bufnr] = true +end + +---@param options table +M.enableBuffer = function(options) + local bufnr = options.buf + M.disabled_buffers[bufnr] = nil +end + +---@param options table +M.toggleBuffer = function(options) + local bufnr = options.buf + M.disabled_buffers[bufnr] = not M.disabled_buffers[bufnr] +end + ---@param client lsp.Client ---@param bufnr? number M.on_attach = function(client, bufnr)