Skip to content

Commit

Permalink
db_relpath
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaylatkar committed Jul 5, 2024
1 parent 4091dfd commit 5b82eaa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
45 changes: 43 additions & 2 deletions lua/cscope/db.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
local utils = require("cscope_maps.utils")
local log = require("cscope_maps.utils.log")

local M = {}

--- conns = { {file = db_file, rel = db_rel}, ... }
M.conns = {}
M.global_conn = nil

---Get all db connections
---If global connection is declared then use that
---@return table
M.all_conns = function()
M.update_global_conn()
return M.global_conn or M.conns
end

---Get primary db connection
---If global connection is declared then use that
---@return table
M.primary_conn = function()
M.update_global_conn()
if M.global_conn then
Expand All @@ -18,12 +26,15 @@ M.primary_conn = function()
return M.conns[1]
end

---Update primary db connection
---@param file string
---@param rel string
M.update_primary_conn = function(file, rel)
M.conns[1].file = vim.fs.normalize(file)

M.conns[1].rel = vim.fs.normalize(rel)
end

---Update global db connection
M.update_global_conn = function()
if vim.g.cscope_maps_db_file then
local file, rel = M.sp_file_rel(vim.g.cscope_maps_db_file)
Expand All @@ -33,16 +44,27 @@ M.update_global_conn = function()
end
end

---Split input to ":Cs db add" into file and rel
---@param path string
---@return string
---@return string|nil
M.sp_file_rel = function(path)
local sp = vim.split(path, ":")
local file = vim.fs.normalize(sp[1])

---@type string|nil
local rel = sp[2]

-- use parent as rel if its "@"
if rel and rel == "@" then
rel = utils.get_path_parent(file)
end

-- make it nil if its empty
if rel and rel == "" then
rel = nil
end

-- if rel exists, normalize it
if rel then
rel = vim.fs.normalize(rel)
Expand All @@ -51,23 +73,32 @@ M.sp_file_rel = function(path)
return file, rel
end

---Find index of db in all connections
---@param file string
---@param rel string|nil
---@return integer
M.find = function(file, rel)
for i, cons in ipairs(M.conns) do
if cons.file == file and cons.rel == rel then
if cons.file == file and ((rel and cons.rel == rel) or cons.rel == nil) then
return i
end
end

return -1
end

---Add db in db connections
---@param path string
M.add = function(path)
local file, rel = M.sp_file_rel(path)
if M.find(file, rel) == -1 then
table.insert(M.conns, { file = file, rel = rel })
end
end

---Remove db from db connections
---Primary db connection will not be removed
---@param path string
M.remove = function(path)
local file, rel = M.sp_file_rel(path)
local loc = M.find(file, rel)
Expand All @@ -77,4 +108,14 @@ M.remove = function(path)
end
end

M.print_conns = function()
if not M.conns then
log.warn("No connections")
end

for _, conn in ipairs(M.conns) do
log.warn(string.format("db=%s relpath=%s", conn.file, conn.rel))
end
end

return M
5 changes: 1 addition & 4 deletions lua/cscope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,6 @@ M.db_update = function(op, files)
db.remove(f)
end
end
-- TODO: add proper print
log.warn("updateed DB list: " .. vim.inspect(db.all_conns()))
end

M.run = function(args)
Expand Down Expand Up @@ -388,8 +386,7 @@ M.run = function(args)

M.db_update(op, files)
elseif op == "s" then
-- TODO: use proper print
log.warn("current DB list: " .. vim.inspect(db.all_conns()))
db.print_conns()
else
log.warn("invalid operation")
end
Expand Down
2 changes: 1 addition & 1 deletion lua/cscope/pickers/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local entry_maker = function(entry)
end
end,
ordinal = entry["filename"] .. entry["text"],
path = entry["filename"],
path = cs_utils.get_abs_path(entry["filename"]),
lnum = tonumber(entry["lnum"]),
}
end
Expand Down
40 changes: 6 additions & 34 deletions lua/cscope_maps/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,47 +55,19 @@ M.get_abs_path = function(path)
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
local abs_path = vim.fs.joinpath(vim.fn.getcwd(), path)

--- 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
return vim.fs.normalize(abs_path)
end

--- Get parent of given path
---@param path string
---@return string
---@return string|nil
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 = "/"
for parent in vim.fs.parents(path) do
return parent
end

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

return M

0 comments on commit 5b82eaa

Please sign in to comment.