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: add format_in_range function and FormatInRange command #65

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
55 changes: 43 additions & 12 deletions lua/lsp-format/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local log = require "vim.lsp.log"
local lspUtil = require "vim.lsp.util"

local M = {
format_options = {},
Expand All @@ -12,6 +13,7 @@ M.setup = function(format_options)
M.format_options = vim.tbl_deep_extend("force", M.format_options, format_options or {})

vim.api.nvim_create_user_command("Format", M.format, { nargs = "*", bar = true, force = true })
vim.api.nvim_create_user_command("FormatInRange", M.format_in_range, { nargs = "*", bar = true, force = true })
vim.api.nvim_create_user_command(
"FormatToggle",
M.toggle,
Expand Down Expand Up @@ -50,11 +52,7 @@ M._parse_value = function(key, value)
return value
end

M.format = function(options)
if vim.b.format_saving or M.disabled or M.disabled_filetypes[vim.bo.filetype] then
return
end

local function format(clients, options)
local bufnr = vim.api.nvim_get_current_buf()
local format_options = vim.deepcopy(M.format_options[vim.bo.filetype] or {})
for key, option in pairs(format_options) do
Expand All @@ -67,7 +65,6 @@ M.format = function(options)
format_options[key] = M._parse_value(key, value)
end

local clients = vim.tbl_values(vim.lsp.buf_get_clients())
for i = #clients, 1, -1 do
if
vim.tbl_contains(format_options.exclude or {}, clients[i].name)
Expand All @@ -87,11 +84,35 @@ M.format = function(options)
end

if #clients > 0 then
table.insert(M.queue, { bufnr = bufnr, clients = clients, format_options = format_options })
table.insert(M.queue, { bufnr = bufnr, clients = clients, format_options = format_options, options = options })
M._next()
end
end

M.format = function(options)
if vim.b.format_saving or M.disabled or M.disabled_filetypes[vim.bo.filetype] then
return
end

local clients= vim.tbl_values(vim.lsp.buf_get_clients())
format(clients, options or {})
end

M.format_in_range = function(options)
if vim.b.format_saving or M.disabled or M.disabled_filetypes[vim.bo.filetype] then
return
end

options = options or {}
options.in_range = true

local clients= vim.tbl_filter(function(client)
return client.supports_method "textDocument/rangeFormatting"
end, vim.lsp.buf_get_clients())

format(clients, options)
end

M.disable = function(options)
if options.args == "" then
M.disabled = true
Expand Down Expand Up @@ -184,7 +205,8 @@ M._handler = function(err, result, ctx)
return
end

vim.lsp.util.apply_text_edits(result, ctx.bufnr, "utf-16")
local client = vim.lsp.get_client_by_id(ctx.client_id)
lspUtil.apply_text_edits(result, ctx.bufnr, client.offset_encoding or "utf-16")
if ctx.bufnr == vim.api.nvim_get_current_buf() then
vim.b.format_saving = true
vim.cmd [[update]]
Expand All @@ -193,10 +215,19 @@ M._handler = function(err, result, ctx)
M._next()
end

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

if options.in_range then
method = "textDocument/rangeFormatting"
params = lspUtil.make_given_range_params(nil, nil, bufnr, client.offset_encoding or "utf-16")
params.options = lspUtil.make_formatting_params(format_options).options
else
method = "textDocument/formatting"
params = lspUtil.make_formatting_params(format_options)
end

local timeout_ms = 2000
if format_options.sync then
local result = client.request_sync(method, params, timeout_ms, bufnr) or {}
Expand All @@ -212,7 +243,7 @@ M._next = function()
return
end
local next_client = table.remove(next.clients, 1)
M._format(next.bufnr, next_client, next.format_options)
M._format(next.bufnr, next_client, next.format_options, next.options)
if #next.clients == 0 then
table.remove(M.queue, 1)
end
Expand Down