-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathlsp.lua
More file actions
142 lines (132 loc) · 3.85 KB
/
Copy pathlsp.lua
File metadata and controls
142 lines (132 loc) · 3.85 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
if
vim.fn.executable("vectorcode-server") ~= 1
or vim.system({ "vectorcode-server", "--version" }):wait().code ~= 0
then
return nil
end
---@type VectorCode.JobRunner
local jobrunner = {}
---@type vim.lsp.Client
local CLIENT = nil
local vc_config = require("vectorcode.config")
local notify_opts = vc_config.notify_opts
local logger = vc_config.logger
--- Returns the Client ID if applicable, or `nil` if the language server fails to start
---@param ok_to_fail boolean
---@return integer?
function jobrunner.init(ok_to_fail)
local existing_clients = vim.lsp.get_clients({ name = vc_config.lsp_configs().name })
if #existing_clients > 0 then
CLIENT = existing_clients[1]
return CLIENT.id
end
ok_to_fail = ok_to_fail or true
local client_id = vim.lsp.start(vc_config.lsp_configs(), {})
if client_id ~= nil then
-- server started
CLIENT = vim.lsp.get_client_by_id(client_id) --[[@as vim.lsp.Client]]
else
-- failed to start server
if vc_config.get_user_config().notify or not ok_to_fail then
local message = "Failed to start vectorcode-server due some error."
logger.error(message)
vim.schedule(function()
vim.notify(message, vim.log.levels.ERROR, notify_opts)
end)
end
return nil
end
return client_id
end
function jobrunner.run(args, timeout_ms, bufnr)
jobrunner.init(false)
assert(CLIENT ~= nil)
assert(bufnr ~= nil)
if timeout_ms == nil or timeout_ms < 0 then
timeout_ms = 2 ^ 31 - 1
end
args = require("vectorcode.jobrunner").find_root(args, bufnr)
local result, err
jobrunner.run_async(args, function(res, err)
result = res
err = err
end, bufnr)
vim.wait(timeout_ms, function()
return (result ~= nil) or (err ~= nil)
end)
if result == nil then
return {}, err
end
return result, err
end
function jobrunner.run_async(args, callback, bufnr)
assert(jobrunner.init(false))
assert(bufnr ~= nil)
if not CLIENT.attached_buffers[bufnr] then
if vim.lsp.buf_attach_client(bufnr, CLIENT.id) then
local uri = vim.uri_from_bufnr(bufnr)
local text = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
vim.schedule_wrap(CLIENT.notify)(vim.lsp.protocol.Methods.textDocument_didOpen, {
textDocument = {
uri = uri,
text = text,
version = 1,
languageId = vim.bo[bufnr].filetype,
},
})
else
local message = "Failed to attach lsp client"
vim.schedule(function()
vim.notify(message)
end)
logger.warn(message)
end
end
args = require("vectorcode.jobrunner").find_root(args, bufnr)
logger.debug(
("lsp jobrunner for buffer %s args: %s"):format(bufnr, vim.inspect(args))
)
local _, id = CLIENT:request(
vim.lsp.protocol.Methods.workspace_executeCommand,
{ command = "vectorcode", arguments = args },
function(err, result, _, _)
if type(callback) == "function" then
local err_message = {}
if err ~= nil and err.message ~= nil then
err_message = { err.message }
end
vim.schedule_wrap(callback)(result, err_message)
if result then
logger.debug(
"lsp jobrunner result:\n",
vim.tbl_map(function(item)
item.document = nil
item.chunk = nil
return item
end, vim.deepcopy(result))
)
end
if err then
logger.info("lsp jobrunner error:\n", err)
end
end
end,
bufnr
)
return id
end
function jobrunner.is_job_running(job_handler)
jobrunner.init(true)
if CLIENT ~= nil then
local request_data = CLIENT.requests[job_handler]
return request_data ~= nil and request_data.type == "pending"
end
return false
end
function jobrunner.stop_job(job_handler)
jobrunner.init(true)
if CLIENT ~= nil then
CLIENT:cancel_request(job_handler)
end
end
return jobrunner