This repository was archived by the owner on Jan 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathrunnables.lua
76 lines (59 loc) · 1.87 KB
/
runnables.lua
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
local rt = require("rust-tools")
local rt_utils = require("rust-tools.utils.utils")
local M = {}
local function get_params()
return {
textDocument = vim.lsp.util.make_text_document_params(0),
position = nil, -- get em all
}
end
local function get_options(result)
local option_strings = {}
for _, runnable in ipairs(result) do
local str = runnable.label
table.insert(option_strings, str)
end
return option_strings
end
---comment
---@return string build command
---@return string|table args
---@return any cwd
local function getCommand(c, results)
local ret = " "
local args = results[c].args
local dir = args.workspaceRoot
ret = vim.list_extend({}, args.cargoArgs or {})
ret = vim.list_extend(ret, args.cargoExtraArgs or {})
table.insert(ret, "--")
ret = vim.list_extend(ret, args.executableArgs or {})
return "cargo", ret, dir
end
function M.run_command(choice, result)
-- do nothing if choice is too high or too low
if not choice or choice < 1 or choice > #result then
return
end
local opts = rt.config.options.tools
local command, args, cwd = getCommand(choice, result)
opts.executor.execute_command(command, args, cwd)
end
local function handler(_, result)
if result == nil then
return
end
-- get the choice from the user
local options = get_options(result)
vim.ui.select(options, { prompt = "Runnables", kind = "rust-tools/runnables" }, function(_, choice)
M.run_command(choice, result)
rt.cached_commands.set_last_runnable(choice, result)
end)
end
-- Sends the request to rust-analyzer to get the runnables and handles them
-- The opts provided here are forwarded to telescope, other than use_telescope
-- which is used to check whether we want to use telescope or the vanilla vim
-- way for input
function M.runnables()
rt_utils.request(0, "experimental/runnables", get_params(), handler)
end
return M