Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support enable, disable and toggle formatting per buffer #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local M = {
format_options = {},
disabled = false,
disabled_filetypes = {},
disabled_buffers = {},
saving_buffers = {},
queue = {},
buffers = {},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down