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 (108 loc) · 2.96 KB
/
Copy pathlsp.lua
File metadata and controls
120 lines (108 loc) · 2.96 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
---@param ok_to_fail boolean
local function get_client(ok_to_fail)
ok_to_fail = ok_to_fail or true
if #vim.lsp.get_clients({ name = "vectorcode-server" }) > 0 then
CLIENT = vim.lsp.get_clients({ name = "vectorcode-server" })[1]
else
local cmd = { "vectorcode-server" }
local try_root = vim.fs.root(".", ".vectorcode") or vim.fs.root(".", ".git")
if try_root ~= nil then
vim.list_extend(cmd, { "--project_root", try_root })
else
vim.schedule(function()
vim.notify(
"Failed to start vectorcode-server due to failing to resolve the project root.",
vim.log.levels.ERROR,
notify_opts
)
end)
return false
end
local id, err = vim.lsp.start_client({
name = "vectorcode-server",
cmd = cmd,
})
if err ~= nil and (vc_config.get_user_config().notify or not ok_to_fail) then
vim.schedule(function()
vim.notify(
("Failed to start vectorcode-server due to the following error:\n%s"):format(
err
),
vim.log.levels.ERROR,
notify_opts
)
end)
return false
elseif id ~= nil then
local cli = vim.lsp.get_client_by_id(id)
if cli ~= nil then
CLIENT = cli
return true
end
end
end
end
function jobrunner.run(args, timeout_ms, bufnr)
get_client(false)
assert(CLIENT ~= nil)
assert(bufnr ~= nil)
if timeout_ms == nil or timeout_ms < 0 then
timeout_ms = 2 ^ 31 - 1
end
local result, err = CLIENT.request_sync(
vim.lsp.protocol.Methods.workspace_executeCommand,
{ command = "vectorcode", arguments = args },
timeout_ms,
bufnr
)
if result == nil then
return {}, { err }
end
return result.result, result.err
end
function jobrunner.run_async(args, callback, bufnr)
get_client(false)
assert(CLIENT ~= nil)
assert(bufnr ~= nil)
if not CLIENT.attached_buffers[bufnr] then
vim.lsp.buf_attach_client(bufnr, CLIENT.id)
end
local _, id = CLIENT.request(
vim.lsp.protocol.Methods.workspace_executeCommand,
{ command = "vectorcode", arguments = args },
function(err, result, _, _)
if type(callback) == "function" then
vim.schedule_wrap(callback)(result, err)
end
end,
bufnr
)
return id
end
function jobrunner.is_job_running(job_handler)
get_client(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)
get_client(true)
if CLIENT ~= nil then
CLIENT.cancel_request(job_handler)
end
end
return jobrunner