|
| 1 | +local logger = require("copilot.logger") |
| 2 | +local M = {} |
| 3 | + |
| 4 | +M.messages = {} |
| 5 | +M.completion_responses = { |
| 6 | + ["numbers_with_spaces.txt"] = { |
| 7 | + completions = { |
| 8 | + { |
| 9 | + displayText = "8 9", |
| 10 | + docVersion = 7, |
| 11 | + position = { |
| 12 | + character = 2, |
| 13 | + line = 2, |
| 14 | + }, |
| 15 | + range = { |
| 16 | + ["end"] = { |
| 17 | + character = 2, |
| 18 | + line = 2, |
| 19 | + }, |
| 20 | + start = { |
| 21 | + character = 0, |
| 22 | + line = 2, |
| 23 | + }, |
| 24 | + }, |
| 25 | + text = "7 8 9", |
| 26 | + uuid = "b7493391-cd5e-4c10-b0a6-2a844533fbed", |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + ["numbers_as_arrays.txt"] = { |
| 31 | + completions = { |
| 32 | + { |
| 33 | + displayText = " 19,20,21\n 22,23,24\n 25,26,27\n}\n{\n 28,29,30\n 31,32,33\n 34,35,36\n}\n{\n 37,38,39\n 40,41,42\n 43,44,45\n}\n{\n 46,47,48\n 49,50,51\n 52,53,54\n}", |
| 34 | + docVersion = 30, |
| 35 | + position = { |
| 36 | + character = 0, |
| 37 | + line = 11, |
| 38 | + }, |
| 39 | + range = { |
| 40 | + ["end"] = { |
| 41 | + character = 0, |
| 42 | + line = 11, |
| 43 | + }, |
| 44 | + start = { |
| 45 | + character = 0, |
| 46 | + line = 11, |
| 47 | + }, |
| 48 | + }, |
| 49 | + text = " 19,20,21\n 22,23,24\n 25,26,27\n}\n{\n 28,29,30\n 31,32,33\n 34,35,36\n}\n{\n 37,38,39\n 40,41,42\n 43,44,45\n}\n{\n 46,47,48\n 49,50,51\n 52,53,54\n}", |
| 50 | + uuid = "1df58ae9-3e93-4e6a-b514-218a9fe7e816", |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +---@param text_document lsp.VersionedTextDocumentIdentifier |
| 57 | +local function get_lsp_responses(text_document) |
| 58 | + local filename = vim.fs.basename(vim.uri_to_fname(text_document.uri)) |
| 59 | + logger.trace("get_lsp_responses: " .. filename) |
| 60 | + |
| 61 | + return M.completion_responses[filename] |
| 62 | + or { |
| 63 | + ---@type copilot_get_completions_data |
| 64 | + completions = { |
| 65 | + { |
| 66 | + displayText = "89", |
| 67 | + docVersion = 7, |
| 68 | + position = { |
| 69 | + character = 1, |
| 70 | + line = 2, |
| 71 | + }, |
| 72 | + range = { |
| 73 | + ["end"] = { |
| 74 | + character = 1, |
| 75 | + line = 2, |
| 76 | + }, |
| 77 | + start = { |
| 78 | + character = 0, |
| 79 | + line = 2, |
| 80 | + }, |
| 81 | + }, |
| 82 | + text = "789", |
| 83 | + uuid = "63f4ab66-e550-4313-9023-144a33254607", |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | +end |
| 88 | + |
| 89 | +function M.server() |
| 90 | + local closing = false |
| 91 | + local srv = {} |
| 92 | + -- local seen_files = {} |
| 93 | + |
| 94 | + function srv.request(method, params, handler) |
| 95 | + logger.trace("lsp request: " .. method) |
| 96 | + table.insert(M.messages, { method = method, params = params }) |
| 97 | + if method == "initialize" then |
| 98 | + handler(nil, { |
| 99 | + capabilities = {}, |
| 100 | + }) |
| 101 | + elseif method == "shutdown" then |
| 102 | + handler(nil, nil) |
| 103 | + elseif method == "getCompletions" then |
| 104 | + -- elseif method == "textDocument/copilotInlineEdit" then |
| 105 | + -- if not seen_files[params.textDocument.uri] then |
| 106 | + -- seen_files[params.textDocument.uri] = true |
| 107 | + local response = get_lsp_responses(params.textDocument) |
| 108 | + handler(nil, response) |
| 109 | + -- local empty_response = { |
| 110 | + -- edits = {}, |
| 111 | + -- } |
| 112 | + -- handler(nil, empty_response) |
| 113 | + elseif method == "checkStatus" then |
| 114 | + handler(nil, { user = "someUser", status = "OK" }) |
| 115 | + elseif method == "notifyAccepted" or method == "notifyRejected" or method == "notifyShown" then |
| 116 | + handler(nil, {}) |
| 117 | + elseif method == "getPanelCompletions" then |
| 118 | + local params_panel = params or {} |
| 119 | + local panelId = params_panel.panelId or (params_panel.doc and params_panel.doc.panelId) or "test-panel-id" |
| 120 | + |
| 121 | + handler(nil, { |
| 122 | + solutionCountTarget = 10, |
| 123 | + }) |
| 124 | + |
| 125 | + vim.defer_fn(function() |
| 126 | + local handlers = require("copilot.panel.handlers") |
| 127 | + |
| 128 | + handlers.handlers.PanelSolution(nil, { |
| 129 | + completionText = "89\n0\n1", |
| 130 | + displayText = "789\n0\n1", |
| 131 | + panelId = panelId, |
| 132 | + range = { |
| 133 | + ["end"] = { |
| 134 | + character = 1, |
| 135 | + line = 2, |
| 136 | + }, |
| 137 | + start = { |
| 138 | + character = 1, |
| 139 | + line = 2, |
| 140 | + }, |
| 141 | + }, |
| 142 | + score = 0, |
| 143 | + solutionId = "213fc33d8f2dbde3207734e3097ea72a69fb8b009f2878468cdd9e74b70a1e59", |
| 144 | + }) |
| 145 | + |
| 146 | + handlers.handlers.PanelSolutionsDone(nil, { |
| 147 | + panelId = panelId, |
| 148 | + status = "OK", |
| 149 | + }) |
| 150 | + end, 10) |
| 151 | + else |
| 152 | + assert(false, "Unhandled method: " .. method) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + function srv.notify(method, params) |
| 157 | + logger.trace("lsp notify") |
| 158 | + table.insert(M.messages, { method = method, params = params }) |
| 159 | + if method == "exit" then |
| 160 | + closing = true |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + function srv.is_closing() |
| 165 | + logger.trace("lsp closing") |
| 166 | + return closing |
| 167 | + end |
| 168 | + |
| 169 | + function srv.terminate() |
| 170 | + logger.trace("lsp terminate") |
| 171 | + closing = true |
| 172 | + end |
| 173 | + |
| 174 | + return srv |
| 175 | +end |
| 176 | + |
| 177 | +function M.reset() |
| 178 | + M.messages = {} |
| 179 | +end |
| 180 | + |
| 181 | +return M |
0 commit comments