lsp.lua (4429B)
1 local M = {} 2 3 local get_visual = function() 4 local curpos = vim.fn.getcurpos() 5 local one = { row = curpos[2] - 1, col = curpos[3] - 1 } 6 local two = { row = vim.fn.line('v') - 1, col = vim.fn.col('v') - 1 } 7 8 if one.row == two.row then 9 if one.col > two.col then 10 local tmp = one 11 one = two 12 two = tmp 13 end 14 elseif one.row > two.row then 15 local tmp = one 16 one = two 17 two = tmp 18 end 19 20 two.col = two.col + 1 21 22 return { start = one, ['end'] = two } 23 end 24 25 local make_code_action_params = function() 26 local params = vim.lsp.util.make_range_params() 27 params.context = { 28 diagnostics = vim.lsp.diagnostic.get_line_diagnostics() 29 } 30 return params 31 end 32 33 local list_code_action_kinds = function() 34 local params = make_code_action_params() 35 local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params) 36 for _, res in pairs(result or {}) do 37 print('---', 'CODE ACTIONS') 38 for _, r in pairs(res.result or {}) do 39 -- vim.pretty_print(r) 40 print(r.kind) 41 end 42 print('---') 43 end 44 end 45 46 local execute_code_action = function(kind) 47 if not kind then return end 48 local params = make_code_action_params() 49 params.context.only = { kind } 50 local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params) 51 for _, res in pairs(result or {}) do 52 for _, r in pairs(res.result or {}) do 53 if r.edit then 54 vim.lsp.util.apply_workspace_edit(r.edit, 'utf-8') 55 else 56 vim.lsp.buf.execute_command(r.command) 57 end 58 end 59 end 60 end 61 62 -- TODO(tms) 22.04.22: possible bug that does not count new lines 63 local get_offest_length = function(buf, mode) 64 local vs = get_visual() 65 D(mode, vs) 66 67 local start_row_offset = vim.api.nvim_buf_get_offset(buf, vs.start.row) 68 local start_col_offset = vim.lsp.util.character_offset(buf, vs.start.row, vs.start.col, 'utf-8') 69 local start_offset = start_row_offset + start_col_offset 70 71 if mode == 'v' then 72 local end_row_offset = vim.api.nvim_buf_get_offset(buf, vs['end'].row) 73 local end_col_offset = vim.lsp.util.character_offset(buf, vs['end'].row, vs['end'].col, 'utf-8') 74 local end_offset = end_row_offset + end_col_offset 75 local visual_length = end_offset - start_offset 76 return start_offset, visual_length, end_offset 77 elseif mode == 'V' then 78 -- move offset to next line to count this one 79 local end_row_offset = vim.api.nvim_buf_get_offset(buf, vs['end'].row + 1) 80 local visual_length = end_row_offset - start_row_offset 81 return start_row_offset, visual_length, end_row_offset 82 else 83 return start_offset, 0 84 end 85 end 86 87 local make_params = function(buf, mode, command) 88 local buf_name = vim.api.nvim_buf_get_name(buf) 89 local offset, length = get_offest_length(buf, mode) 90 return { command, buf_name, nil, offset, length } 91 end 92 93 local execute_simple_command = function(command) 94 local cur_buf = vim.api.nvim_get_current_buf() 95 local cur_buf_name = vim.api.nvim_buf_get_name(cur_buf) 96 local params = { command = command, arguments = { cur_buf_name }, title = '' } 97 vim.lsp.buf_request_sync(cur_buf, 'workspace/executeCommand', params) 98 end 99 100 local execute_command = function(command) 101 local mode = vim.api.nvim_get_mode().mode 102 local buf = vim.api.nvim_get_current_buf() 103 local params = make_params(buf, mode, command) 104 D(params) 105 vim.ui.input({}, function(input) 106 if input and #input ~= 0 then 107 table.insert(params, { name = input }) 108 vim.lsp.buf_request(buf, 'workspace/executeCommand', { command = 'refactor.perform', arguments = params }) 109 end 110 end) 111 end 112 113 local execute_command_no_input = function(command) 114 local mode = vim.api.nvim_get_mode().mode 115 local buf = vim.api.nvim_get_current_buf() 116 local params = make_params(buf, mode, command) 117 table.insert(params, { name = '' }) 118 D(params) 119 vim.lsp.buf_request(buf, 'workspace/executeCommand', { command = 'refactor.perform', arguments = params }) 120 end 121 122 M.organize_imports = function() execute_simple_command('edit.organizeImports') end 123 M.fix_all = function() execute_simple_command('edit.fixAll') end 124 M.extract_method = function() execute_command('EXTRACT_METHOD') end 125 M.extract_local_variable = function() execute_command('EXTRACT_LOCAL_VARIABLE') end 126 M.inline_local_variable = function() execute_command_no_input('INLINE_LOCAL_VARIABLE') end 127 M.list_code_action_kinds = list_code_action_kinds 128 M.execute_code_action = function(kind) execute_code_action(kind) end 129 130 return M