-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathlsp.lua
More file actions
132 lines (122 loc) · 3.81 KB
/
Copy pathlsp.lua
File metadata and controls
132 lines (122 loc) · 3.81 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
local vc_config = require("vectorcode.config")
---@type VectorCode.JobRunner
local jobrunner = {}
---@type vim.lsp.Client
local CLIENT = nil
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, "Failed to initialize the LSP server!")
assert(bufnr ~= nil, "Need to pass the buffer number!")
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, code
jobrunner.run_async(args, function(res, e, e_code)
result = res
err = e
code = e_code
end, bufnr)
vim.wait(timeout_ms, function()
return (result ~= nil) or (err ~= nil)
end)
return result or {}, err, code
end
function jobrunner.run_async(args, callback, bufnr)
assert(jobrunner.init(false))
assert(bufnr ~= nil, "Need to pass the buffer number!")
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,
-- NOTE: This is not a hardcoded executable, but rather part of our LSP implementation.
{ 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
local code = 0
if err and err.code then
code = err.code
end
vim.schedule_wrap(callback)(result, err_message, code)
if result then
logger.debug("lsp jobrunner result:\n", 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