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

Version 2.2.2 #33

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ It does
1. Asynchronously formatting on save
2. Sequentially formatting with all attached LSP server
3. Add commands for disabling formatting (globally or per filetype)
4. Makes it easier to send format options to the LSP
4. Make it easier to send format options to the LSP
5. Allow you to exclude specific LSP servers from formatting.

It does not

Expand Down
5 changes: 4 additions & 1 deletion doc/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Author: Lukas Reineke <lukas@reineke.jp>
Version: 2.2.1
Version: 2.2.2

==============================================================================
CONTENTS *lsp-format*
Expand Down Expand Up @@ -96,6 +96,9 @@ the `order` list. (same logic as |vim.lsp.buf.formatting_seq_sync()|)
==============================================================================
4. CHANGELOG *lsp-format-changelog*

2.2.2
Don't overwrite the global format handler

2.2.1
Fix `exclude` command argument when calling with more than one value

Expand Down
94 changes: 47 additions & 47 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,6 @@ M.setup = function(format_options)
vim.cmd [[command! -nargs=? -complete=filetype -bar FormatToggle lua require'lsp-format'.toggle(<q-args>)]]
vim.cmd [[command! -nargs=? -complete=filetype -bar FormatDisable lua require'lsp-format'.disable(<q-args>)]]
vim.cmd [[command! -nargs=? -complete=filetype -bar -bang FormatEnable lua require'lsp-format'.enable(<q-args>, "<bang>" == "!")]]

vim.lsp.handlers["textDocument/formatting"] = function(err, result, ctx)
if err ~= nil then
local client = vim.lsp.get_client_by_id(ctx.client_id)
local client_name = client and client.name or string.format("client_id=%d", ctx.client_id)
vim.api.nvim_err_write(string.format("%s: %d: %s", client_name, err.code, err.message))
return
end
if
result == nil
or vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick") ~= vim.api.nvim_buf_get_var(
ctx.bufnr,
"changedtick"
)
or vim.startswith(vim.api.nvim_get_mode().mode, "i")
then
return
end

local view = vim.fn.winsaveview()
vim.lsp.util.apply_text_edits(result, ctx.bufnr, "utf-16")
vim.fn.winrestview(view)
if ctx.bufnr == vim.api.nvim_get_current_buf() then
vim.b.format_saving = true
vim.cmd [[update]]
vim.b.format_saving = false
end
M._next()
end
end

M.format = function(format_options_string)
Expand Down Expand Up @@ -79,24 +50,6 @@ M.format = function(format_options_string)
M._next()
end

M._format = function(bufnr, client, format_options)
vim.b.format_changedtick = vim.b.changedtick
local params = vim.lsp.util.make_formatting_params(format_options)
client.request("textDocument/formatting", params, nil, bufnr)
end

M._next = function()
local next = M.queue[1]
if not next or #next.clients == 0 then
return
end
local next_client = table.remove(next.clients, 1)
M._format(next.bufnr, next_client, next.format_options)
if #next.clients == 0 then
table.remove(M.queue, 1)
end
end

M.disable = function(filetype)
if filetype == "" then
M.disabled = true
Expand Down Expand Up @@ -135,4 +88,51 @@ M.on_attach = function(client)
end
end

M._handler = function(err, result, ctx)
if err ~= nil then
local client = vim.lsp.get_client_by_id(ctx.client_id)
local client_name = client and client.name or string.format("client_id=%d", ctx.client_id)
vim.api.nvim_err_write(string.format("%s: %d: %s", client_name, err.code, err.message))
return
end
if
result == nil
or vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick") ~= vim.api.nvim_buf_get_var(
ctx.bufnr,
"changedtick"
)
or vim.startswith(vim.api.nvim_get_mode().mode, "i")
then
return
end

local view = vim.fn.winsaveview()
vim.lsp.util.apply_text_edits(result, ctx.bufnr, "utf-16")
vim.fn.winrestview(view)
if ctx.bufnr == vim.api.nvim_get_current_buf() then
vim.b.format_saving = true
vim.cmd [[update]]
vim.b.format_saving = false
end
M._next()
end

M._format = function(bufnr, client, format_options)
vim.b.format_changedtick = vim.b.changedtick
local params = vim.lsp.util.make_formatting_params(format_options)
client.request("textDocument/formatting", params, M._handler, bufnr)
end

M._next = function()
local next = M.queue[1]
if not next or #next.clients == 0 then
return
end
local next_client = table.remove(next.clients, 1)
M._format(next.bufnr, next_client, next.format_options)
if #next.clients == 0 then
table.remove(M.queue, 1)
end
end

return M