dart.lua (2077B)
1 local get_visual = function() 2 local vs_start = vim.fn.getcurpos()[2] 3 local vs_end = vim.fn.line('v') 4 if vs_start > vs_end then 5 return vs_end - 1, vs_start - 1 6 else 7 return vs_start - 1, vs_end - 1 8 end 9 end 10 11 local extract = function() 12 local buf = vim.api.nvim_get_current_buf() 13 local buf_name = vim.api.nvim_buf_get_name(buf) 14 local start, stop = get_visual() 15 local start_offset = vim.api.nvim_buf_get_offset(buf, start) 16 local end_offset = vim.api.nvim_buf_get_offset(buf, stop + 1) 17 local length = end_offset - start_offset 18 vim.ui.input({}, function(input) 19 if input and #input ~= 0 then 20 vim.lsp.buf_request(buf, 'workspace/executeCommand', { 21 command = 'refactor.perform', 22 arguments = { 'EXTRACT_METHOD', buf_name, nil, start_offset, length, { name = input } }, 23 }) 24 end 25 end) 26 end 27 28 local make_code_action_params = function() 29 local params = vim.lsp.util.make_range_params() 30 params.context = { diagnostics = vim.lsp.diagnostic.get_line_diagnostics() } 31 return params 32 end 33 34 local list_code_action_kinds = function() 35 local params = make_code_action_params() 36 local result = vim.lsp.buf_request_sync(0, 'textDocument/codeAction', params) 37 for _, res in pairs(result or {}) do 38 print('---', 'CODE ACTIONS') 39 for _, r in pairs(res.result or {}) do 40 -- vim.pretty_print(r) 41 print(r.kind) 42 end 43 print('---') 44 end 45 end 46 47 local execute_code_action = function(kind) 48 if not kind then 49 return 50 end 51 local params = make_code_action_params() 52 params.context.only = { kind } 53 local result = vim.lsp.buf_request_sync(0, 'textDocument/codeAction', params) 54 for _, res in pairs(result or {}) do 55 for _, r in pairs(res.result or {}) do 56 if r.edit then 57 vim.lsp.util.apply_workspace_edit(r.edit, 'utf-8') 58 else 59 vim.lsp.buf.execute_command(r.command) 60 end 61 end 62 end 63 end 64 65 vim.keymap.set('n', ',e', function() 66 execute_code_action('refactor.remove.typeAnnotation') 67 end) 68 vim.keymap.set('n', ',l', list_code_action_kinds) 69 70 print('loaded') 71 vim.cmd.messages('clear')