|
1 | 1 | local copilot = {} |
2 | 2 |
|
3 | 3 | local showDocument = function(err, result, ctx, _) |
4 | | - local fallback = vim.lsp.handlers['window/showDocument'] |
5 | | - if not fallback or (result.external and vim.g.copilot_browser) then |
6 | | - return vim.fn['copilot#handlers#window_showDocument'](result) |
7 | | - else |
8 | | - return fallback(err, result, ctx, _) |
9 | | - end |
| 4 | + local fallback = vim.lsp.handlers["window/showDocument"] |
| 5 | + if not fallback or (result.external and vim.g.copilot_browser) then |
| 6 | + return vim.fn["copilot#handlers#window_showDocument"](result) |
| 7 | + else |
| 8 | + return fallback(err, result, ctx, _) |
| 9 | + end |
10 | 10 | end |
11 | 11 |
|
12 | 12 | copilot.lsp_start_client = function(cmd, handler_names, opts, settings) |
13 | | - local handlers = {['window/showDocument'] = showDocument} |
14 | | - local id |
15 | | - for _, name in ipairs(handler_names) do |
16 | | - handlers[name] = function(err, result, ctx, _) |
17 | | - if result then |
18 | | - local retval = vim.call('copilot#client#LspHandle', id, { method = name, params = result }) |
19 | | - if type(retval) == 'table' then |
20 | | - return retval.result, retval.error |
21 | | - elseif vim.lsp.handlers[name] then |
22 | | - return vim.lsp.handlers[name](err, result, ctx, _) |
23 | | - end |
24 | | - end |
25 | | - end |
26 | | - end |
27 | | - local workspace_folders = opts.workspaceFolders |
28 | | - if #workspace_folders == 0 then |
29 | | - workspace_folders = nil |
30 | | - end |
31 | | - id = vim.lsp.start_client({ |
32 | | - cmd = cmd, |
33 | | - cmd_cwd = vim.call('copilot#job#Cwd'), |
34 | | - name = 'GitHub Copilot', |
35 | | - init_options = opts.initializationOptions, |
36 | | - workspace_folders = workspace_folders, |
37 | | - settings = settings, |
38 | | - handlers = handlers, |
39 | | - on_init = function(client, initialize_result) |
40 | | - vim.call('copilot#client#LspInit', client.id, initialize_result) |
41 | | - if vim.fn.has('nvim-0.8') == 0 then |
42 | | - client.notify('workspace/didChangeConfiguration', { settings = settings }) |
43 | | - end |
44 | | - end, |
45 | | - on_exit = function(code, signal, client_id) |
46 | | - vim.schedule(function() |
47 | | - vim.call('copilot#client#LspExit', client_id, code, signal) |
48 | | - end) |
49 | | - end, |
50 | | - }) |
51 | | - return id |
| 13 | + local handlers = { ["window/showDocument"] = showDocument } |
| 14 | + local id |
| 15 | + for _, name in ipairs(handler_names) do |
| 16 | + handlers[name] = function(err, result, ctx, _) |
| 17 | + if result then |
| 18 | + local retval = vim.call("copilot#client#LspHandle", id, { method = name, params = result }) |
| 19 | + if type(retval) == "table" then |
| 20 | + return retval.result, retval.error |
| 21 | + elseif vim.lsp.handlers[name] then |
| 22 | + return vim.lsp.handlers[name](err, result, ctx, _) |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + end |
| 27 | + local workspace_folders = opts.workspaceFolders |
| 28 | + if #workspace_folders == 0 then |
| 29 | + workspace_folders = nil |
| 30 | + end |
| 31 | + id = vim.lsp.start({ |
| 32 | + cmd = cmd, |
| 33 | + cmd_cwd = vim.call("copilot#job#Cwd"), |
| 34 | + name = "GitHub Copilot", |
| 35 | + init_options = opts.initializationOptions, |
| 36 | + workspace_folders = workspace_folders, |
| 37 | + settings = settings, |
| 38 | + handlers = handlers, |
| 39 | + on_init = function(client, initialize_result) |
| 40 | + vim.call("copilot#client#LspInit", client.id, initialize_result) |
| 41 | + if vim.fn.has("nvim-0.8") == 0 then |
| 42 | + client.notify("workspace/didChangeConfiguration", { settings = settings }) |
| 43 | + end |
| 44 | + end, |
| 45 | + on_exit = function(code, signal, client_id) |
| 46 | + vim.schedule(function() |
| 47 | + vim.call("copilot#client#LspExit", client_id, code, signal) |
| 48 | + end) |
| 49 | + end, |
| 50 | + }) |
| 51 | + return id |
52 | 52 | end |
53 | 53 |
|
54 | 54 | copilot.lsp_request = function(client_id, method, params, bufnr) |
55 | | - local client = vim.lsp.get_client_by_id(client_id) |
56 | | - if not client then |
57 | | - return |
58 | | - end |
59 | | - if bufnr == vim.NIL then |
60 | | - bufnr = nil |
61 | | - end |
62 | | - local _, id |
63 | | - _, id = client.request(method, params, function(err, result) |
64 | | - vim.call('copilot#client#LspResponse', client_id, { id = id, error = err, result = result }) |
65 | | - end, bufnr) |
66 | | - return id |
| 55 | + local client = vim.lsp.get_client_by_id(client_id) |
| 56 | + if not client then |
| 57 | + return |
| 58 | + end |
| 59 | + if bufnr == vim.NIL then |
| 60 | + bufnr = nil |
| 61 | + end |
| 62 | + local _, id |
| 63 | + _, id = client.request(method, params, function(err, result) |
| 64 | + vim.call("copilot#client#LspResponse", client_id, { id = id, error = err, result = result }) |
| 65 | + end, bufnr) |
| 66 | + return id |
67 | 67 | end |
68 | 68 |
|
69 | 69 | copilot.rpc_request = function(client_id, method, params) |
70 | | - local client = vim.lsp.get_client_by_id(client_id) |
71 | | - if not client then |
72 | | - return |
73 | | - end |
74 | | - local _, id |
75 | | - _, id = client.rpc.request(method, params, function(err, result) |
76 | | - vim.call('copilot#client#LspResponse', client_id, { id = id, error = err, result = result }) |
77 | | - end) |
78 | | - return id |
| 70 | + local client = vim.lsp.get_client_by_id(client_id) |
| 71 | + if not client then |
| 72 | + return |
| 73 | + end |
| 74 | + local _, id |
| 75 | + _, id = client.rpc.request(method, params, function(err, result) |
| 76 | + vim.call("copilot#client#LspResponse", client_id, { id = id, error = err, result = result }) |
| 77 | + end) |
| 78 | + return id |
79 | 79 | end |
80 | 80 |
|
81 | 81 | copilot.rpc_notify = function(client_id, method, params) |
82 | | - local client = vim.lsp.get_client_by_id(client_id) |
83 | | - if not client then |
84 | | - return |
85 | | - end |
86 | | - return client.rpc.notify(method, params) |
| 82 | + local client = vim.lsp.get_client_by_id(client_id) |
| 83 | + if not client then |
| 84 | + return |
| 85 | + end |
| 86 | + return client.rpc.notify(method, params) |
87 | 87 | end |
88 | 88 |
|
89 | 89 | return copilot |
0 commit comments