Skip to content

Commit 8a43aba

Browse files
committed
issue #309 range_format
1 parent 3be5241 commit 8a43aba

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lua/navigator/formatting.lua

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
-- https://github.com/wention/dotfiles/blob/master/.config/nvim/lua/config/lsp.lua
21
-- https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/handlers.lua
32

3+
local get_range = function(cmd)
4+
-- if visual selected
5+
local range = {
6+
start = vim.api.nvim_buf_get_mark(0, '['),
7+
['end'] = vim.api.nvim_buf_get_mark(0, ']'),
8+
}
9+
-- if specified range
10+
if cmd.line1 ~= range.start[1] or cmd.line2 ~= range['end'][1] then
11+
-- Supplied range inferred
12+
range = {
13+
start = { cmd.line1, 0 },
14+
['end'] = { cmd.line2, 2147483647 },
15+
}
16+
end
17+
return range
18+
end
419
return {
520
format_hdl = function(err, result, ctx, _) -- FIXME: bufnr is nil
621
if err ~= nil or result == nil then
@@ -18,7 +33,10 @@ return {
1833
vim.defer_fn(function()
1934
log('fmt callback')
2035

21-
if ctx.bufnr == vim.api.nvim_get_current_buf() or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified') then
36+
if
37+
ctx.bufnr == vim.api.nvim_get_current_buf()
38+
or not vim.api.nvim_buf_get_option(ctx.bufnr, 'modified')
39+
then
2240
local view = vim.fn.winsaveview()
2341
vim.lsp.util.apply_text_edits(result, ctx.bufnr, offset_encoding)
2442
vim.fn.winrestview(view)
@@ -34,12 +52,12 @@ return {
3452
end,
3553
range_format = function()
3654
local old_func = vim.go.operatorfunc
37-
_G.op_func_formatting = function()
38-
print('formatting range')
39-
local start = vim.api.nvim_buf_get_mark(0, '[')
40-
local finish = vim.api.nvim_buf_get_mark(0, ']')
41-
print(vim.inspect(start), vim.inspect(finish))
42-
vim.lsp.buf.range_formatting({}, start, finish)
55+
_G.op_func_formatting = function(cmd)
56+
local range = get_range(cmd)
57+
vim.lsp.buf.format({
58+
async = _NgConfigValues.lsp.format_options.async,
59+
range = range,
60+
})
4361
vim.go.operatorfunc = old_func
4462
_G.op_func_formatting = nil
4563
end

lua/navigator/lspclient/mapping.lua

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ local key_maps = {
6767
{ key = '<Space>wa', func = require('navigator.workspace').add_workspace_folder, desc = 'add_workspace_folder' },
6868
{ key = '<Space>wr', func = require('navigator.workspace').remove_workspace_folder, desc = 'remove_workspace_folder' },
6969
{ key = '<Space>ff', func = vim.lsp.buf.format, mode = 'n', desc = 'format' },
70-
{ key = '<Space>ff', func = vim.lsp.buf.range_formatting, mode = 'v', desc = 'range format' },
70+
{ key = '<Space>ff', func = vim.lsp.buf.format, mode = 'v', desc = 'range format', opts = {silent = true} },
7171
{ key = '<Space>gm', func = require('navigator.formatting').range_format, mode = 'n', desc = 'range format operator e.g gmip' },
7272
{ key = '<Space>wl', func = require('navigator.workspace').list_workspace_folders, desc = 'list_workspace_folders' },
7373
{
@@ -221,6 +221,11 @@ local function set_mapping(lsp_attach_info)
221221
opts.desc = value.desc
222222
end
223223
opts.buffer = bufnr
224+
if value.opts then
225+
for k, v in pairs(value.opts) do
226+
opts[k] = v
227+
end
228+
end
224229
vim.keymap.set(value.mode or 'n', value.key, value.func, opts)
225230
if string.find(value.desc, 'range format') and value.mode == 'v' then
226231
rfmtkey = value.key

0 commit comments

Comments
 (0)