mirror of
https://github.com/neovim/neovim.git
synced 2025-10-21 17:21:49 +00:00
feat: add vim.fs.relpath
This is needed to replace the nvim-lspconfig function is_descendant that some lspconfg configurations still use.
This commit is contained in:
@@ -741,4 +741,37 @@ function M.abspath(path)
|
||||
return M.joinpath(cwd, path)
|
||||
end
|
||||
|
||||
--- Gets `target` path relative to `base`, or `nil` if `base` is not an ancestor.
|
||||
---
|
||||
--- Example:
|
||||
---
|
||||
--- ```lua
|
||||
--- vim.fs.relpath('/var', '/var/lib') -- 'lib'
|
||||
--- vim.fs.relpath('/var', '/usr/bin') -- nil
|
||||
--- ```
|
||||
---
|
||||
--- @param base string
|
||||
--- @param target string
|
||||
--- @param opts table? Reserved for future use
|
||||
--- @return string|nil
|
||||
function M.relpath(base, target, opts)
|
||||
vim.validate('base', base, 'string')
|
||||
vim.validate('target', target, 'string')
|
||||
vim.validate('opts', opts, 'table', true)
|
||||
|
||||
base = vim.fs.normalize(vim.fs.abspath(base))
|
||||
target = vim.fs.normalize(vim.fs.abspath(target))
|
||||
if base == target then
|
||||
return '.'
|
||||
end
|
||||
|
||||
local prefix = ''
|
||||
if iswin then
|
||||
prefix, base = split_windows_path(base)
|
||||
end
|
||||
base = prefix .. base .. (base ~= '/' and '/' or '')
|
||||
|
||||
return vim.startswith(target, base) and target:sub(#base + 1) or nil
|
||||
end
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user