Skip to content

Commit

Permalink
issue #309 range_format
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-x committed Jul 2, 2024
1 parent 3be5241 commit 8a43aba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
34 changes: 26 additions & 8 deletions lua/navigator/formatting.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
-- https://github.com/wention/dotfiles/blob/master/.config/nvim/lua/config/lsp.lua
-- https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/handlers.lua

local get_range = function(cmd)
-- if visual selected
local range = {
start = vim.api.nvim_buf_get_mark(0, '['),
['end'] = vim.api.nvim_buf_get_mark(0, ']'),
}
-- if specified range
if cmd.line1 ~= range.start[1] or cmd.line2 ~= range['end'][1] then
-- Supplied range inferred
range = {
start = { cmd.line1, 0 },
['end'] = { cmd.line2, 2147483647 },
}
end
return range
end
return {
format_hdl = function(err, result, ctx, _) -- FIXME: bufnr is nil
if err ~= nil or result == nil then
Expand All @@ -18,7 +33,10 @@ return {
vim.defer_fn(function()
log('fmt callback')

if ctx.bufnr == vim.api.nvim_get_current_buf() or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
if
ctx.bufnr == vim.api.nvim_get_current_buf()
or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified')
then
local view = vim.fn.winsaveview()
vim.lsp.util.apply_text_edits(result, ctx.bufnr, offset_encoding)
vim.fn.winrestview(view)
Expand All @@ -34,12 +52,12 @@ return {
end,
range_format = function()
local old_func = vim.go.operatorfunc
_G.op_func_formatting = function()
print('formatting range')
local start = vim.api.nvim_buf_get_mark(0, '[')
local finish = vim.api.nvim_buf_get_mark(0, ']')
print(vim.inspect(start), vim.inspect(finish))
vim.lsp.buf.range_formatting({}, start, finish)
_G.op_func_formatting = function(cmd)
local range = get_range(cmd)
vim.lsp.buf.format({
async = _NgConfigValues.lsp.format_options.async,
range = range,
})
vim.go.operatorfunc = old_func
_G.op_func_formatting = nil
end
Expand Down
7 changes: 6 additions & 1 deletion lua/navigator/lspclient/mapping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ local key_maps = {
{ key = '<Space>wa', func = require('navigator.workspace').add_workspace_folder, desc = 'add_workspace_folder' },
{ key = '<Space>wr', func = require('navigator.workspace').remove_workspace_folder, desc = 'remove_workspace_folder' },
{ key = '<Space>ff', func = vim.lsp.buf.format, mode = 'n', desc = 'format' },
{ key = '<Space>ff', func = vim.lsp.buf.range_formatting, mode = 'v', desc = 'range format' },
{ key = '<Space>ff', func = vim.lsp.buf.format, mode = 'v', desc = 'range format', opts = {silent = true} },
{ key = '<Space>gm', func = require('navigator.formatting').range_format, mode = 'n', desc = 'range format operator e.g gmip' },
{ key = '<Space>wl', func = require('navigator.workspace').list_workspace_folders, desc = 'list_workspace_folders' },
{
Expand Down Expand Up @@ -221,6 +221,11 @@ local function set_mapping(lsp_attach_info)
opts.desc = value.desc
end
opts.buffer = bufnr
if value.opts then
for k, v in pairs(value.opts) do
opts[k] = v
end
end
vim.keymap.set(value.mode or 'n', value.key, value.func, opts)
if string.find(value.desc, 'range format') and value.mode == 'v' then
rfmtkey = value.key
Expand Down

0 comments on commit 8a43aba

Please sign in to comment.