forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsp.lua
More file actions
120 lines (109 loc) · 3.09 KB
/
Copy pathlsp.lua
File metadata and controls
120 lines (109 loc) · 3.09 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
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
--- 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)
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
vim.schedule(function()
vim.notify(
"Failed to start vectorcode-server due some error.",
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)
jobrunner.init(false)
assert(CLIENT ~= nil)
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
vim.notify("Failed to attach lsp client")
end
end
args = require("vectorcode.jobrunner").find_root(args, bufnr)
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)
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