|
1 | 1 | local M = {}
|
2 | 2 |
|
3 |
| ----@alias lsp_join_lines_params { textDocument: lsp_text_document, ranges: lsp_range[] } |
4 |
| - |
5 |
| ----@param visual_mode boolean |
6 |
| ----@return lsp_join_lines_params |
7 |
| -local function get_params(visual_mode) |
8 |
| - local params = visual_mode and vim.lsp.util.make_given_range_params() or vim.lsp.util.make_range_params() |
| 3 | +---@param params { textDocument: lsp_text_document, range: lsp_range } |
| 4 | +---@return { textDocument: lsp_text_document, ranges: lsp_range[] } |
| 5 | +local function modify_params(params) |
9 | 6 | local range = params.range
|
10 |
| - |
11 | 7 | params.range = nil
|
| 8 | + ---@diagnostic disable-next-line: inject-field |
12 | 9 | params.ranges = { range }
|
13 |
| - |
| 10 | + ---@diagnostic disable-next-line: return-type-mismatch |
14 | 11 | return params
|
15 | 12 | end
|
16 | 13 |
|
17 | 14 | local function handler(_, result, ctx)
|
18 | 15 | vim.lsp.util.apply_text_edits(result, ctx.bufnr, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
|
19 | 16 | end
|
20 | 17 |
|
21 |
| -local rl = require('rustaceanvim.rust_analyzer') |
| 18 | +local ra = require('rustaceanvim.rust_analyzer') |
| 19 | + |
| 20 | +--- Sends the request to rust-analyzer to get the TextEdits to join the lines |
| 21 | +--- under the cursor and applies them (for use in visual mode) |
| 22 | +function M.join_lines_visual() |
| 23 | + local clients = ra.get_active_rustaceanvim_clients(0) |
| 24 | + if #clients == 0 then |
| 25 | + return |
| 26 | + end |
| 27 | + local params = modify_params(vim.lsp.util.make_given_range_params(nil, nil, 0, clients[1].offset_encoding)) |
| 28 | + ra.buf_request(0, 'experimental/joinLines', params, handler) |
| 29 | +end |
22 | 30 |
|
23 | 31 | --- Sends the request to rust-analyzer to get the TextEdits to join the lines
|
24 | 32 | --- under the cursor and applies them
|
25 |
| ----@param visual_mode boolean |
26 |
| -function M.join_lines(visual_mode) |
27 |
| - rl.buf_request(0, 'experimental/joinLines', get_params(visual_mode), handler) |
| 33 | +function M.join_lines() |
| 34 | + local clients = ra.get_active_rustaceanvim_clients(0) |
| 35 | + if #clients == 0 then |
| 36 | + return |
| 37 | + end |
| 38 | + local params = modify_params(vim.lsp.util.make_range_params(0, clients[1].offset_encoding)) |
| 39 | + ra.buf_request(0, 'experimental/joinLines', params, handler) |
28 | 40 | end
|
29 | 41 |
|
30 |
| -return M.join_lines |
| 42 | +return M |
0 commit comments