Skip to content

Commit

Permalink
Merge branch 'fix-exception' of https://github.com/linrongbin16/genta…
Browse files Browse the repository at this point in the history
…gs.nvim into fix-exception
  • Loading branch information
linrongbin16 committed Jan 2, 2024
2 parents bb092ba + e3e3496 commit 0d76acb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 28 deletions.
12 changes: 2 additions & 10 deletions lua/gentags/commons/jsons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ M.encode = function(t)
if t == nil then
return nil
end
if vim.fn.has("nvim-0.9") and vim.json ~= nil then
return vim.json.encode(t)
else
return require("gentags.commons._json").encode(t)
end
return require("gentags.commons._json").encode(t)
end

--- @param j string?
Expand All @@ -19,11 +15,7 @@ M.decode = function(j)
if j == nil then
return nil
end
if vim.fn.has("nvim-0.9") and vim.json ~= nil then
return vim.json.decode(j)
else
return require("gentags.commons._json").decode(j)
end
return require("gentags.commons._json").decode(j)
end

return M
141 changes: 128 additions & 13 deletions lua/gentags/commons/paths.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,67 @@ local M = {}
M.SEPARATOR = IS_WINDOWS and "\\" or "/"

--- @param p string
--- @param opts {double_backslash:boolean?,expand:boolean?}?
--- @return boolean
M.exists = function(p)
assert(type(p) == "string")
local result, _ = require("gentags.commons.uv").fs_lstat(p)
return result ~= nil
end

--- @param p string
--- @return boolean
M.isfile = function(p)
assert(type(p) == "string")
local result, _ = require("gentags.commons.uv").fs_lstat(p)
-- print(
-- string.format(
-- "|paths.isfile| p:%s, result:%s\n",
-- vim.inspect(p),
-- vim.inspect(result)
-- )
-- )
return result ~= nil and result.type == "file"
end

--- @param p string
--- @return boolean
M.isdir = function(p)
assert(type(p) == "string")
local result, _ = require("gentags.commons.uv").fs_lstat(p)
print(
string.format(
"|paths.isdir| p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
return result ~= nil and result.type == "directory"
end

--- @param p string
--- @return boolean
M.islink = function(p)
assert(type(p) == "string")
local result, _ = require("gentags.commons.uv").fs_lstat(p)
print(
string.format(
"|paths.issymlink| p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
return result ~= nil and result.type == "link"
end

--- @param p string
--- @param opts {double_backslash:boolean?}?
--- @return string
M.normalize = function(p, opts)
opts = opts or { double_backslash = false, expand = false }
M._normalize_slash = function(p, opts)
assert(type(p) == "string")
opts = opts or { double_backslash = false }
opts.double_backslash = type(opts.double_backslash) == "boolean"
and opts.double_backslash
or false
opts.expand = type(opts.expand) == "boolean" and opts.expand or false

-- '\\\\' => '\\'
local function _double_backslash(s)
Expand All @@ -29,22 +82,84 @@ M.normalize = function(p, opts)
end
return s
end

local result = p
local result = vim.trim(p)

if opts.double_backslash then
result = _double_backslash(result)
end
result = _single_backslash(result)
return result
end

if opts.expand then
result = vim.fn.expand(vim.trim(result)) --[[@as string]]
if opts.double_backslash then
result = _double_backslash(result)
end
result = _single_backslash(result)
--- @param p string
--- @return string
M.expand = function(p)
assert(type(p) == "string")
if string.len(p) >= 1 and string.sub(p, 1, 1) == "~" then
return require("gentags.commons.uv").os_homedir() .. string.sub(p, 2)
else
result = vim.trim(result)
return p
end
end

--- @param p string
--- @return string
M.resolve = function(p)
assert(type(p) == "string")
if not M.islink(p) then
return p
end
local result, _ = require("gentags.commons.uv").fs_realpath(p)
print(
string.format(
"|paths.resolve|-4 p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
return result ~= nil and result or p
end

--- @param p string
--- @param opts {double_backslash:boolean?,expand:boolean?,resolve:boolean?}?
--- @return string
M.normalize = function(p, opts)
assert(type(p) == "string")
opts = opts or { double_backslash = false, expand = false, resolve = false }
opts.double_backslash = type(opts.double_backslash) == "boolean"
and opts.double_backslash
or false
opts.expand = type(opts.expand) == "boolean" and opts.expand or false
opts.resolve = type(opts.resolve) == "boolean" and opts.resolve or false

local result = M._normalize_slash(p, opts)
print(
string.format(
"|paths.normalize| slash, p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
if opts.expand then
result = M.expand(result)
print(
string.format(
"|paths.normalize| expand, p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
end

if opts.resolve then
result = M.resolve(result)
print(
string.format(
"|paths.normalize| resolve, p:%s, result:%s\n",
vim.inspect(p),
vim.inspect(result)
)
)
end

return result
Expand Down
4 changes: 0 additions & 4 deletions lua/gentags/commons/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ end
--- @param ... any
--- @return any
M.tbl_get = function(t, ...)
if vim.fn.has("nvim-0.10") > 0 and type(vim.tbl_get) == "function" then
return type(t) == "table" and vim.tbl_get(t, ...) or nil
end

local e = t --[[@as table]]
for _, k in ipairs({ ... }) do
if M.tbl_not_empty(e) and e[k] ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion lua/gentags/commons/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.5.0
3.6.0

0 comments on commit 0d76acb

Please sign in to comment.