forked from sourcegraph/sg.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.lua
More file actions
113 lines (94 loc) · 2.5 KB
/
Copy pathrequest.lua
File metadata and controls
113 lines (94 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local log = require "sg.log"
local lsp = require "sg.vendored.vim-lsp-rpc"
local bin_sg_nvim = require("sg.config").get_nvim_agent()
local M = {}
local notification_handlers = {
["initialize"] = function(data)
if data.endpoint and data.token then
require("sg.auth").set(data.endpoint, data.token, { from_agent = true })
end
end,
["display_text"] = function(data)
print("display_text::", vim.inspect(data))
end,
}
local server_handlers = {}
--- Start the server
---@param opts { force: boolean? }?
---@return VendoredPublicClient?
M.start = function(opts)
if not bin_sg_nvim then
-- Try and check for the bin again
bin_sg_nvim = require("sg.config").get_nvim_agent()
if not bin_sg_nvim then
require("sg.notify").NO_BUILD()
return nil
end
end
opts = opts or {}
if M.client and not opts.force then
return M.client
end
if M.client then
M.client.terminate()
vim.wait(10)
end
-- Verify that the environment is properly configured
M.client = lsp.start(bin_sg_nvim, {}, {
notification = function(method, data)
log.info("got notification", method, data)
if notification_handlers[method] then
notification_handlers[method](data)
else
log.error("[sg-agent] unhandled method:", method)
end
end,
server_request = function(method, params)
local handler = server_handlers[method]
if handler then
return handler(method, params)
else
log.error("[cody-agent] unhandled server request:", method)
end
end,
}, {
env = {
PATH = vim.env.PATH,
SRC_ACCESS_TOKEN = vim.env.SRC_ACCESS_TOKEN,
SRC_ENDPOINT = vim.env.SRC_ENDPOINT,
},
})
if not M.client then
vim.notify "[sg.nvim] failed to start sg.nvim plugin"
return nil
end
-- Schedule getting the auth from neovim, if possible.
vim.schedule(function()
M.request("sourcegraph/auth", {}, function(err, data)
if err then
return
end
if data.endpoint and data.token then
require("sg.auth").set(data.endpoint, data.token)
end
end)
end)
return M.client
end
M.notify = function(...)
local client = M.start()
if not client then
return
end
return client.notify(...)
end
M.request = function(method, params, callback)
local client = M.start()
if not client then
return callback("no available client", nil)
end
return client.request(method, params, function(err, result)
return callback(err, result)
end)
end
return M