Skip to content

Commit

Permalink
feat: support format only for modidications.
Browse files Browse the repository at this point in the history
Signed-off-by: ayamir <[email protected]>
  • Loading branch information
ayamir committed Oct 18, 2023
1 parent 7eae431 commit 2e68ef5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions lua/core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ local settings = {}
---@type boolean
settings["use_ssh"] = true

-- Set it to true if you prefer formatting the modification region instead of the whole file
-- NOTE: format on the whole file will also be executed if partial format is failed
---@type boolean
settings["format_modify"] = true

-- Set it to false if there are no need to format on save.
---@type boolean
settings["format_on_save"] = true
Expand Down
40 changes: 24 additions & 16 deletions lua/modules/configs/completion/formatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local settings = require("core.settings")
local disabled_workspaces = settings.format_disabled_dirs
local format_on_save = settings.format_on_save
local format_notify = settings.format_notify
local format_modify = settings.format_modify
local server_formatting_block_list = settings.server_formatting_block_list

vim.api.nvim_create_user_command("FormatToggle", function()
Expand Down Expand Up @@ -156,23 +157,30 @@ function M.format(opts)
)
return
end
local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
if result and result.result then
vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
if format_notify then
vim.notify(
string.format("[LSP] Format successfully with %s!", client.name),
vim.log.levels.INFO,
{ title = "LSP Format Success" }
)
if format_modify then
local lsp_fmt_modify = require("lsp-format-modifications")
local res = lsp_fmt_modify.format_modifications(client, bufnr)
-- Still try to format the whole file if partial format failed
if res == false then
local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
if result and result.result then
vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
if format_notify then
vim.notify(
string.format("[LSP] Format successfully with %s!", client.name),
vim.log.levels.INFO,
{ title = "LSP Format Success" }
)
end
elseif err then
vim.notify(
string.format("[LSP][%s] %s", client.name, err),
vim.log.levels.ERROR,
{ title = "LSP Format Error" }
)
end
end
elseif err then
vim.notify(
string.format("[LSP][%s] %s", client.name, err),
vim.log.levels.ERROR,
{ title = "LSP Format Error" }
)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lua/modules/plugins/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ completion["jose-elias-alvarez/null-ls.nvim"] = {
"jay-babu/mason-null-ls.nvim",
},
}
completion["joechrisellis/lsp-format-modifications.nvim"] = {
lazy = true,
event = "LspAttach",
}
completion["hrsh7th/nvim-cmp"] = {
lazy = true,
event = "InsertEnter",
Expand Down

0 comments on commit 2e68ef5

Please sign in to comment.