1+ local async = require (' guard._async' )
12local M = {}
23local api = vim .api
34local apply = vim .lsp .util .apply_text_edits
45
56--- @param buf number
6- --- @param range table
7+ --- @param range table ?
78--- @param acc string
89--- @return string
910function M .format (buf , range , acc )
10- local co = assert (coroutine.running ())
1111 local clients = vim .lsp .get_clients ({ bufnr = buf , method = ' textDocument/formatting' })
1212 if # clients == 0 then
1313 return acc
@@ -19,30 +19,30 @@ function M.format(buf, range, acc)
1919 api .nvim_buf_set_lines (scratch , 0 , - 1 , false , vim .split (acc , ' \r ?\n ' ))
2020 local line_offset = range and range .start [1 ] - 1 or 0
2121
22- --- @diagnostic disable-next-line : duplicate-set-field
23- vim .lsp .util .apply_text_edits = function (text_edits , _ , offset_encoding )
24- -- the target buffer must be buf, we apply it to our scratch buffer
25- n_edits = n_edits - 1
26- vim .tbl_map (function (edit )
27- edit .range .start .line = edit .range .start .line - line_offset
28- edit .range [' end' ].line = edit .range [' end' ].line - line_offset
29- end , text_edits )
30- apply (text_edits , scratch , offset_encoding )
31- if n_edits == 0 then
32- vim .lsp .util .apply_text_edits = apply
33- local lines = api .nvim_buf_get_lines (scratch , 0 , - 1 , false )
34- api .nvim_command (' silent! bwipe! ' .. scratch )
35- coroutine.resume (co , table.concat (lines , ' \n ' ))
22+ return async .await (1 , function (callback )
23+ --- @diagnostic disable-next-line : duplicate-set-field
24+ vim .lsp .util .apply_text_edits = function (text_edits , _ , offset_encoding )
25+ -- the target buffer must be buf, we apply it to our scratch buffer
26+ n_edits = n_edits - 1
27+ vim .tbl_map (function (edit )
28+ edit .range .start .line = edit .range .start .line - line_offset
29+ edit .range [' end' ].line = edit .range [' end' ].line - line_offset
30+ end , text_edits )
31+ apply (text_edits , scratch , offset_encoding )
32+ if n_edits == 0 then
33+ vim .lsp .util .apply_text_edits = apply
34+ local lines = api .nvim_buf_get_lines (scratch , 0 , - 1 , false )
35+ api .nvim_command (' silent! bwipe! ' .. scratch )
36+ callback (table.concat (lines , ' \n ' ))
37+ end
3638 end
37- end
38-
39- vim .lsp .buf .format ({
40- bufnr = buf ,
41- range = range ,
42- async = true ,
43- })
4439
45- return (coroutine.yield ())
40+ vim .lsp .buf .format ({
41+ bufnr = buf ,
42+ range = range ,
43+ async = true ,
44+ })
45+ end )
4646end
4747
4848return M
0 commit comments