Skip to content

Commit 30a9fdd

Browse files
cscope: db: feat: ability to provide custom script to build DB
1 parent 941c59d commit 30a9fdd

File tree

3 files changed

+76
-64
lines changed

3 files changed

+76
-64
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ Heavily inspired by emacs' [xcscope.el](https://github.com/dkogan/xcscope.el).
3535
- `:Cs db add <space sepatated files>` add db file(s) to cscope search.
3636
- `:Cs db rm <space sepatated files>` remove db file(s) from cscope search.
3737
- `:Cs db show` show all db connections.
38-
- `:Cs db build` (re)builds db for primary db.
38+
- `:Cs db build` (re)builds db.
39+
- if `db_build_cmd.script == "default"` then only primary DB will be built using cscope binary.
40+
- Custom script can be provided. This script with predefined args.
41+
e.g. user script will be called as following -
42+
`/user/provided/script -d <db1>::<pre_path1> -d <db2>::<pre_path2> ...`
3943
- `vim.g.cscope_maps_statusline_indicator` can be used in statusline to indicate ongoing db build.
4044
- DB path grammar
4145
- `db_file::db_pre_path` db_pre_path (prefix path) will be appended to cscope results.
@@ -108,8 +112,8 @@ _cscope_maps_ comes with following defaults:
108112
qf_window_pos = "bottom", -- "bottom", "right", "left" or "top"
109113
-- "true" does not open picker for single result, just JUMP
110114
skip_picker_for_single_result = false, -- "false" or "true"
111-
-- these args are directly passed to "cscope -f <db_file> <args>"
112-
db_build_cmd_args = { "-bqkv" },
115+
-- custom script can be used for db build
116+
db_build_cmd = { script = "default", args = { "-bqkv" } },
113117
-- statusline indicator, default is cscope executable
114118
statusline_indicator = nil,
115119
-- try to locate db_file in parent dir(s)
@@ -212,4 +216,3 @@ under cursor
212216
```lua
213217
vim.keymap.set({ "n", "v" }, "<C-c><C-g>", "<cmd>Cs f g<cr>")
214218
```
215-

lua/cscope/db.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,55 @@ M.print_conns = function()
132132
end
133133
end
134134

135+
---Create command to build DB
136+
---1. If script is default then use opt.exec
137+
---2. If custom script is provided then use that with "-d <db>::<pre_path>" args
138+
M.get_build_cmd = function(opts)
139+
local cmd = {}
140+
141+
if opts.db_build_cmd.script == "default" then
142+
if opts.exec == "cscope" then
143+
cmd = { "cscope", "-f", M.primary_conn().file }
144+
else -- "gtags-cscope"
145+
cmd = { "gtags-cscope" }
146+
end
147+
148+
vim.list_extend(cmd, opts.db_build_cmd.args)
149+
return cmd
150+
end
151+
152+
-- custom script
153+
cmd = { opts.db_build_cmd.script }
154+
vim.list_extend(cmd, opts.db_build_cmd.args)
155+
156+
for _, conn in ipairs(M.conns) do
157+
vim.list_extend(cmd, { "-d", string.format("%s::%s", conn.file, conn.pre_path) })
158+
end
159+
160+
return cmd
161+
end
162+
163+
M.build = function(opts)
164+
if vim.g.cscope_maps_statusline_indicator then
165+
log.warn("db build is already in progress")
166+
return
167+
end
168+
169+
local cmd = M.get_build_cmd(opts)
170+
171+
local on_exit = function(obj)
172+
vim.g.cscope_maps_statusline_indicator = nil
173+
if obj.code == 0 then
174+
-- print("cscope: [build] out: " .. obj.stdout)
175+
print("cscope: database built successfully")
176+
else
177+
-- print("cscope: [build] out: " .. obj.stderr)
178+
print("cscope: database build failed")
179+
end
180+
end
181+
182+
vim.g.cscope_maps_statusline_indicator = opts.statusline_indicator or opts.exec
183+
vim.system(cmd, { text = true }, on_exit)
184+
end
185+
135186
return M

lua/cscope/init.lua

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local M = {}
1717
---@field qf_window_size? integer
1818
---@field qf_window_pos? string
1919
---@field skip_picker_for_single_result? boolean
20-
---@field db_build_cmd_args? table
20+
---@field db_build_cmd? table
2121
---@field statusline_indicator? string|nil
2222
---@field project_rooter? CsProjectRooterConfig
2323
M.opts = {
@@ -27,7 +27,7 @@ M.opts = {
2727
qf_window_size = 5,
2828
qf_window_pos = "bottom",
2929
skip_picker_for_single_result = false,
30-
db_build_cmd_args = { "-bqkv" },
30+
db_build_cmd = { script = "default", args = { "-bqkv" } },
3131
statusline_indicator = nil,
3232
project_rooter = {
3333
enable = false,
@@ -273,63 +273,6 @@ M.cstag = function(symbol)
273273
return RC.NO_RESULTS
274274
end
275275

276-
M.db_build_output = function(err, data)
277-
if err then
278-
print("cscope: [build] err: " .. err)
279-
end
280-
if data then
281-
print("cscope: [build] out: " .. data)
282-
end
283-
end
284-
285-
M.db_build = function()
286-
local stdout = vim.loop.new_pipe(false)
287-
local stderr = vim.loop.new_pipe(false)
288-
local db_build_cmd_args = vim.tbl_deep_extend("force", M.opts.db_build_cmd_args, {})
289-
local cur_path = vim.fn.getcwd()
290-
local db_conn = db.primary_conn() -- TODO: extend support to all db conns
291-
local db_file = db_conn.file
292-
local db_root = utils.get_path_parent(db_file)
293-
294-
if vim.g.cscope_maps_statusline_indicator then
295-
log.warn("db build is already in progress")
296-
return
297-
end
298-
299-
if M.opts.exec == "cscope" then
300-
table.insert(db_build_cmd_args, "-f")
301-
table.insert(db_build_cmd_args, db_file)
302-
end
303-
304-
vim.cmd("cd " .. db_root)
305-
306-
local handle = nil
307-
vim.g.cscope_maps_statusline_indicator = M.opts.statusline_indicator or M.opts.exec
308-
handle = vim.loop.spawn(
309-
M.opts.exec,
310-
{
311-
args = db_build_cmd_args,
312-
stdio = { nil, stdout, stderr },
313-
},
314-
vim.schedule_wrap(function(code, _) -- on exit
315-
stdout:read_stop()
316-
stderr:read_stop()
317-
stdout:close()
318-
stderr:close()
319-
handle:close()
320-
if code == 0 then
321-
log.info("database built successfully")
322-
else
323-
log.warn("database build failed")
324-
end
325-
vim.g.cscope_maps_statusline_indicator = nil
326-
vim.cmd("cd " .. cur_path)
327-
end)
328-
)
329-
vim.loop.read_start(stdout, M.db_build_output)
330-
vim.loop.read_start(stderr, M.db_build_output)
331-
end
332-
333276
M.db_update = function(op, files)
334277
if op == "a" then
335278
for _, f in ipairs(files) do
@@ -402,7 +345,7 @@ M.run = function(args)
402345

403346
local op = args[2]:sub(1, 1)
404347
if op == "b" then
405-
M.db_build()
348+
db.build(M.opts)
406349
elseif op == "a" or op == "r" then
407350
-- collect all args
408351
local files = {}
@@ -574,6 +517,21 @@ M.setup = function(opts)
574517
end
575518
end
576519

520+
if vim.fn.executable(M.opts.db_build_cmd.script) ~= 1 then
521+
log.warn("db_build script is not found. Using default")
522+
M.opts.db_build_cmd.script = "default"
523+
end
524+
525+
if M.opts.db_build_cmd_args then
526+
M.opts.db_build_cmd.args = M.opts.db_build_cmd_args
527+
log.warn(
528+
string.format(
529+
[[db_build_cmd_args is deprecated. Use 'db_build_cmd = { args = %s }']],
530+
vim.inspect(M.opts.db_build_cmd_args)
531+
)
532+
)
533+
end
534+
577535
-- if project rooter is enabled,
578536
-- 1. get root of project and update primary conn
579537
-- 2. if change_cwd is enabled, change into it (?)

0 commit comments

Comments
 (0)