request.lua (1716B)
1 local vim = vim 2 local validate = vim.validate 3 local util = require('vim.lsp.util') 4 5 local M = {} 6 7 local function request(method, params, handler) 8 validate({ method = { method, 's' }, handler = { handler, 'f', true } }) 9 return vim.lsp.buf_request(0, method, params, handler) 10 end 11 12 local function pick_type_hierarchy_item(type_hierarchy_items) 13 if not type_hierarchy_items then 14 return 15 end 16 if #type_hierarchy_items == 1 then 17 return type_hierarchy_items[1] 18 end 19 local items = {} 20 for i, item in pairs(type_hierarchy_items) do 21 local entry = item.detail or item.name 22 table.insert(items, string.format('%d. %s', i, entry)) 23 end 24 local choice = vim.fn.inputlist(items) 25 if choice < 1 or choice > #items then 26 return 27 end 28 return choice 29 end 30 31 function M.type_hierarchy(method) 32 vim.validate({ 33 method = { 34 method, 35 function(m) 36 return (m == 'subtypes') or (m == 'supertypes'), 'Method must be one of ["subtypes", "supertypes"]' 37 end, 38 }, 39 }) 40 method = 'typeHierarchy/' .. method 41 local params = util.make_position_params() 42 request('textDocument/prepareTypeHierarchy', params, function(err, result, ctx) 43 if err then 44 vim.notify(err.message, vim.log.levels.WARN) 45 return 46 end 47 48 local type_hierarchy_item = pick_type_hierarchy_item(result) 49 if not type_hierarchy_item then 50 return 51 end 52 53 local client = vim.lsp.get_client_by_id(ctx.client_id) 54 if client then 55 client.request(method, { item = type_hierarchy_item }, nil, ctx.bufnr) 56 else 57 vim.notify(string.format('Client with id=%d disappeared during type hierarchy request', ctx.client_id), 58 vim.log.levels.WARN) 59 end 60 end) 61 end 62 63 return M