forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.lua
More file actions
124 lines (115 loc) · 3.43 KB
/
Copy pathconfig.lua
File metadata and controls
124 lines (115 loc) · 3.43 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
---@type VectorCode.Opts
local config = {
async_opts = {
debounce = 10,
events = { "BufWritePost", "InsertEnter", "BufReadPost" },
exclude_this = true,
n_query = 1,
notify = false,
query_cb = require("vectorcode.utils").make_surrounding_lines_cb(-1),
run_on_register = false,
single_job = false,
},
async_backend = "default",
exclude_this = true,
n_query = 1,
notify = true,
timeout_ms = 5000,
on_setup = { update = false },
}
local setup_config = vim.deepcopy(config, true)
local notify_opts = { title = "VectorCode" }
---@param opts {notify:boolean}?
local has_cli = function(opts)
opts = opts or { notify = false }
local ok = vim.fn.executable("vectorcode") == 1
if not ok and opts.notify then
vim.notify("VectorCode CLI is not executable!", vim.log.levels.ERROR, notify_opts)
end
return ok
end
---@generic T: function
---@param func T
---@return T
local check_cli_wrap = function(func)
if not has_cli() then
vim.notify("VectorCode CLI is not executable!", vim.log.levels.ERROR, notify_opts)
end
return func
end
return {
get_default_config = function()
return vim.deepcopy(config, true)
end,
setup = check_cli_wrap(
---@param opts VectorCode.Opts?
function(opts)
opts = opts or {}
setup_config = vim.tbl_deep_extend("force", config, opts or {})
for k, v in pairs(setup_config.async_opts) do
if
setup_config[k] ~= nil
and (opts.async_opts == nil or opts.async_opts[k] == nil)
then
-- NOTE: a lot of options are mutual between `setup_config` and `async_opts`.
-- If users do not explicitly set them `async_opts`, copy them from `setup_config`.
setup_config.async_opts = vim.tbl_deep_extend(
"force",
setup_config.async_opts,
{ [k] = setup_config[k] }
)
end
end
if setup_config.on_setup.update then
require("vectorcode").check("config", function(out)
if out.code == 0 then
local path = string.gsub(out.stdout, "^%s*(.-)%s*$", "%1")
if path ~= "" then
require("vectorcode").update(path)
end
end
end)
end
end
),
---@return VectorCode.CacheBackend
get_cacher_backend = function()
if setup_config.async_backend == "lsp" then
local ok, cacher = pcall(require, "vectorcode.cacher.lsp")
if ok and type(cacher) == "table" then
return cacher
else
vim.notify("Falling back to default backend.", vim.log.levels.WARN, notify_opts)
setup_config.async_backend = "default"
end
end
if setup_config.async_backend ~= "default" then
vim.notify(
("Unrecognised vectorcode backend: %s! Falling back to `default`."):format(
setup_config.async_backend
),
vim.log.levels.ERROR,
notify_opts
)
setup_config.async_backend = "default"
end
return require("vectorcode.cacher.default")
end,
---@return VectorCode.Opts
get_user_config = function()
return vim.deepcopy(setup_config, true)
end,
---@return VectorCode.QueryOpts
get_query_opts = function()
return {
exclude_this = setup_config.exclude_this,
n_query = setup_config.n_query,
notify = setup_config.notify,
timeout_ms = setup_config.timeout_ms,
}
end,
notify_opts = notify_opts,
---@return boolean
has_cli = has_cli,
check_cli_wrap = check_cli_wrap,
}