Skip to content

Commit

Permalink
cscope: ability to provide rel-path in db
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaylatkar committed Jun 14, 2024
1 parent 7adf24c commit d95784c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
55 changes: 46 additions & 9 deletions lua/cscope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ end

local cscope_picker = nil
local project_root = nil
local db_cons = {}

M.help = function()
print([[
Expand Down Expand Up @@ -114,8 +115,12 @@ M.parse_line = function(line)
local sp = vim.split(line, "%s+")

t.filename = sp[1]
if M.opts.picker ~= "telescope" then
-- workaround until https://github.com/nvim-telescope/telescope.nvim/pull/3151 is merged
if M.opts.picker == "telescope" then
-- telescope can't handle relative paths, always use abs paths
if vim.startswith(t.filename, "..") then
t.filename = utils.get_abs_path(t.filename)
end
else
t.filename = utils.get_rel_path(vim.fn.getcwd(), t.filename)
end

Expand Down Expand Up @@ -188,23 +193,43 @@ M.cmd_exec = function(cmd)
return output
end

M.get_db_and_rel_path = function(db_item)
local sp_db_item = vim.split(db_item, ":")
local db_file = sp_db_item[1]
local db_rel = sp_db_item[2] or ""

if db_rel == "." or db_rel == "./" then
db_rel = ""
end

return db_file, db_rel
end

M.get_result = function(op_n, op_s, symbol, hide_log)
-- Executes cscope search and return parsed output

local db_file = vim.g.cscope_maps_db_file or M.opts.db_file
local db_item = vim.g.cscope_maps_db_file or M.opts.db_file
local cmd = string.format("%s -dL -%s %s", M.opts.exec, op_n, symbol)
local out = ""

if M.opts.exec == "cscope" then
if type(db_file) == "string" then
if type(db_item) == "string" then
local db_file, db_rel = M.get_db_and_rel_path(db_item)
if vim.loop.fs_stat(db_file) ~= nil then
cmd = string.format("%s -f %s", cmd, db_file)
if db_rel ~= "" then
cmd = cmd .. string.format(" -P %s", db_rel)
end
out = M.cmd_exec(cmd)
end
else -- table
for _, db in ipairs(db_file) do
if vim.loop.fs_stat(db) ~= nil then
local _cmd = string.format("%s -f %s", cmd, db)
for _, db in ipairs(db_item) do
local db_file, db_rel = M.get_db_and_rel_path(db)
if vim.loop.fs_stat(db_file) ~= nil then
local _cmd = string.format("%s -f %s", cmd, db_file)
if db_rel ~= "" then
_cmd = _cmd .. string.format(" -P %s", db_rel)
end
out = string.format("%s%s", out, M.cmd_exec(_cmd))
end
end
Expand Down Expand Up @@ -294,7 +319,11 @@ M.db_build = function()
local stderr = vim.loop.new_pipe(false)
local db_build_cmd_args = vim.tbl_deep_extend("force", M.opts.db_build_cmd_args, {})
local cur_path = vim.fn.expand("%:p:h", true)
local db_file = M.get_db_file()
local db_file, db_rel = M.get_db_and_rel_path(M.get_db_file())

if db_rel ~= "" then
log.warn("db build is not supported for rel-path")
end

if vim.g.cscope_maps_statusline_indicator then
log.warn("db build is already in progress")
Expand Down Expand Up @@ -339,6 +368,14 @@ M.db_build = function()
vim.loop.read_start(stderr, M.db_build_output)
end

M.db_register_con = function (path)
local sp_path = vim.split(path, ":")
local file = sp_path[1]
local rel = sp_path[2]

table.insert(db_cons, {file = file, rel = rel})
end

M.db_update = function(op, files)
if type(M.opts.db_file) == "string" then
M.opts.db_file = { M.opts.db_file }
Expand Down Expand Up @@ -536,7 +573,7 @@ M.setup = function(opts)
vim.g.cscope_maps_statusline_indicator = nil

if M.opts.project_rooter.enable then
local db_file = M.get_db_file()
local db_file, _ = M.get_db_and_rel_path(M.get_db_file())
project_root = M.project_root(db_file)
if project_root ~= nil then
local new_db_path = string.format("%s/%s", project_root, db_file)
Expand Down
62 changes: 60 additions & 2 deletions lua/cscope_maps/utils/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
local M = {}

local non_empty = function(item)
return item and item ~= ""
return item and item ~= "" and item ~= "."
end

--- Check is given path is absolute path
---@param path string
---@return boolean
M.is_path_abs = function(path)
return vim.startswith(path, "/")
end

--- Get relative path
Expand All @@ -11,7 +18,7 @@ end
---@param path string
---@return string
M.get_rel_path = function(rel_to, path)
if not vim.startswith(rel_to, "/") or not vim.startswith(path, "/") then
if not M.is_path_abs(rel_to) or not M.is_path_abs(path) then
return path
end

Expand Down Expand Up @@ -40,4 +47,55 @@ M.get_rel_path = function(rel_to, path)
return rel_path
end

--- Convert given path to absolute path
---@param path string
---@return string
M.get_abs_path = function(path)
if M.is_path_abs(path) then
return path
end

local abs_path = "/"
local cwd = vim.fn.getcwd()
local sp_cwd = vim.tbl_filter(non_empty, vim.split(cwd, "/"))
local sp_path = vim.tbl_filter(non_empty, vim.split(path, "/"))
local len_cwd = #sp_cwd + 1
local len_path = #sp_path + 1
local i = 1

-- get number of "../"
while i < len_path do
if sp_path[i] ~= ".." then
break
end
i = i + 1
end

--- remove trailing parents from "cwd"
abs_path = abs_path .. table.concat(sp_cwd, "/", 1, len_cwd - i)
if abs_path == "/" then
abs_path = ""
end

-- append remaining parents from "path"
abs_path = abs_path .. "/" .. table.concat(sp_path, "/", i)
return abs_path
end

--- Get parent of given path
---@param path string
---@return string
M.get_path_parent = function(path)
local sp_path = vim.tbl_filter(non_empty, vim.split(path, "/"))
local parent = ""

if M.is_path_abs(path) then
parent = "/"
end

parent = parent .. table.concat(sp_path, "/", 1, #sp_path - 1)
print(parent)
return parent
end

return M

0 comments on commit d95784c

Please sign in to comment.