1
- -- https://github.com/wention/dotfiles/blob/master/.config/nvim/lua/config/lsp.lua
2
1
-- https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/handlers.lua
3
2
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
4
19
return {
5
20
format_hdl = function (err , result , ctx , _ ) -- FIXME: bufnr is nil
6
21
if err ~= nil or result == nil then
@@ -18,7 +33,10 @@ return {
18
33
vim .defer_fn (function ()
19
34
log (' fmt callback' )
20
35
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
22
40
local view = vim .fn .winsaveview ()
23
41
vim .lsp .util .apply_text_edits (result , ctx .bufnr , offset_encoding )
24
42
vim .fn .winrestview (view )
@@ -34,12 +52,12 @@ return {
34
52
end ,
35
53
range_format = function ()
36
54
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
+ } )
43
61
vim .go .operatorfunc = old_func
44
62
_G .op_func_formatting = nil
45
63
end
0 commit comments