|
| 1 | +local M = {} |
| 2 | + |
| 3 | +M.augroup = vim.api.nvim_create_augroup('MinuetLSP', { clear = true }) |
| 4 | + |
| 5 | +function M.get_trigger_characters() |
| 6 | + return { '@', '.', '(', '[', ':', ' ' } |
| 7 | +end |
| 8 | + |
| 9 | +function M.get_capabilities() |
| 10 | + return { |
| 11 | + completionProvider = { |
| 12 | + triggerCharacters = M.get_trigger_characters(), |
| 13 | + }, |
| 14 | + } |
| 15 | +end |
| 16 | + |
| 17 | +function M.generate_request_id() |
| 18 | + return os.time() |
| 19 | +end |
| 20 | + |
| 21 | +M.request_handler = {} |
| 22 | + |
| 23 | +M.request_handler.initialize = function(_, _, callback, _) |
| 24 | + local id = M.generate_request_id() |
| 25 | + vim.schedule(function() |
| 26 | + callback(nil, { capabilities = M.get_capabilities() }) |
| 27 | + end) |
| 28 | + return true, id |
| 29 | +end |
| 30 | + |
| 31 | +M.request_handler['textDocument/completion'] = function(_, params, callback, notify_callback) |
| 32 | + local id = M.generate_request_id() |
| 33 | + vim.schedule(function() |
| 34 | + callback(nil, { isIncomplete = false, items = { { label = 'hello', kind = 20 } } }) |
| 35 | + -- We want to trigger the notification callback to explicitly mark the |
| 36 | + -- operation as complete. This ensures the "complete" event is |
| 37 | + -- dispatched and associated completion requests are no longer |
| 38 | + -- considered pending. |
| 39 | + if notify_callback then |
| 40 | + notify_callback(id) |
| 41 | + end |
| 42 | + end) |
| 43 | + return true, id |
| 44 | +end |
| 45 | + |
| 46 | +M.request_handler.shutdown = function(_, _, callback, _) |
| 47 | + local id = M.generate_request_id() |
| 48 | + vim.schedule(function() |
| 49 | + callback(nil, nil) |
| 50 | + end) |
| 51 | + return true, id |
| 52 | +end |
| 53 | + |
| 54 | +---@param dispatchers vim.lsp.rpc.Dispatchers |
| 55 | +---@return vim.lsp.rpc.PublicClient |
| 56 | +function M.server(dispatchers) |
| 57 | + local closing = false |
| 58 | + return { |
| 59 | + request = function(method, params, callback, notify_callback) |
| 60 | + if M.request_handler[method] then |
| 61 | + local ok, id = M.request_handler[method](method, params, callback, notify_callback) |
| 62 | + return ok, id |
| 63 | + else |
| 64 | + return false, nil |
| 65 | + end |
| 66 | + end, |
| 67 | + notify = function(method, _) |
| 68 | + if method == 'exit' then |
| 69 | + -- code 0 (success), signal 15 (SIGTERM) |
| 70 | + dispatchers.on_exit(0, 15) |
| 71 | + end |
| 72 | + return true |
| 73 | + end, |
| 74 | + is_closing = function() |
| 75 | + return closing |
| 76 | + end, |
| 77 | + terminate = function() |
| 78 | + closing = true |
| 79 | + end, |
| 80 | + } |
| 81 | +end |
| 82 | + |
| 83 | +function M.start_server(args) |
| 84 | + ---@type vim.lsp.ClientConfig |
| 85 | + local config = { |
| 86 | + name = 'minuet', |
| 87 | + cmd = M.server, |
| 88 | + } |
| 89 | + ---@type vim.lsp.start.Opts |
| 90 | + local opts = { |
| 91 | + bufnr = args.buf, |
| 92 | + reuse_client = function(lsp_client, lsp_config) |
| 93 | + return lsp_client.name == lsp_config.name |
| 94 | + end, |
| 95 | + } |
| 96 | + vim.lsp.start(config, opts) |
| 97 | +end |
| 98 | + |
| 99 | +function M.setup() |
| 100 | + vim.api.nvim_create_autocmd('FileType', { |
| 101 | + pattern = '*', |
| 102 | + callback = M.start_server, |
| 103 | + desc = 'Starts the minuet LSP server', |
| 104 | + group = M.augroup, |
| 105 | + }) |
| 106 | +end |
| 107 | + |
| 108 | +return M |
0 commit comments