Skip to content

Commit 9ee2e7d

Browse files
committed
refactor: comment util.path.is_descendant to prepare deperecation in future
We don't deprecate it currently as the suggested replacement (vim.fs.relpath) isn't available on the minimum supported neovim version. Work on #2079.
1 parent 14b5a80 commit 9ee2e7d

File tree

1 file changed

+47
-53
lines changed

1 file changed

+47
-53
lines changed

Diff for: lua/lspconfig/util.lua

+47-53
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local nvim_eleven = vim.fn.has 'nvim-0.11' == 1
55

66
local iswin = vim.loop.os_uname().version:match 'Windows'
77

8-
local M = {}
8+
local M = { path = {} }
99

1010
M.default_config = {
1111
log_level = lsp.protocol.MessageType.Warning,
@@ -95,57 +95,6 @@ function M.create_module_commands(module_name, commands)
9595
end
9696
end
9797

98-
-- Some path utilities
99-
M.path = (function()
100-
--- @param path string
101-
--- @return boolean
102-
local function is_fs_root(path)
103-
if iswin then
104-
return path:match '^%a:$'
105-
else
106-
return path == '/'
107-
end
108-
end
109-
110-
-- Traverse the path calling cb along the way.
111-
local function traverse_parents(path, cb)
112-
path = vim.loop.fs_realpath(path)
113-
local dir = path
114-
-- Just in case our algo is buggy, don't infinite loop.
115-
for _ = 1, 100 do
116-
dir = vim.fs.dirname(dir)
117-
if not dir then
118-
return
119-
end
120-
-- If we can't ascend further, then stop looking.
121-
if cb(dir, path) then
122-
return dir, path
123-
end
124-
if is_fs_root(dir) then
125-
break
126-
end
127-
end
128-
end
129-
130-
local function is_descendant(root, path)
131-
if not path then
132-
return false
133-
end
134-
135-
local function cb(dir, _)
136-
return dir == root
137-
end
138-
139-
local dir, _ = traverse_parents(path, cb)
140-
141-
return dir == root
142-
end
143-
144-
return {
145-
is_descendant = is_descendant,
146-
}
147-
end)()
148-
14998
function M.search_ancestors(startpath, func)
15099
if nvim_eleven then
151100
validate('func', func, 'function')
@@ -294,7 +243,52 @@ function M.strip_archive_subpath(path)
294243
return path
295244
end
296245

297-
--- Functions that can be removed once minimum required neovim version is high enough
246+
--- Public functions that can be deprecated once minimum required neovim version is high enough
247+
248+
local function is_fs_root(path)
249+
if iswin then
250+
return path:match '^%a:$'
251+
else
252+
return path == '/'
253+
end
254+
end
255+
256+
-- Traverse the path calling cb along the way.
257+
local function traverse_parents(path, cb)
258+
path = vim.loop.fs_realpath(path)
259+
local dir = path
260+
-- Just in case our algo is buggy, don't infinite loop.
261+
for _ = 1, 100 do
262+
dir = vim.fs.dirname(dir)
263+
if not dir then
264+
return
265+
end
266+
-- If we can't ascend further, then stop looking.
267+
if cb(dir, path) then
268+
return dir, path
269+
end
270+
if is_fs_root(dir) then
271+
break
272+
end
273+
end
274+
end
275+
276+
--- This can be replaced with `vim.fs.relpath` once minimum neovim version is at least 0.11.
277+
function M.path.is_descendant(root, path)
278+
if not path then
279+
return false
280+
end
281+
282+
local function cb(dir, _)
283+
return dir == root
284+
end
285+
286+
local dir, _ = traverse_parents(path, cb)
287+
288+
return dir == root
289+
end
290+
291+
--- Helper functions that can be removed once minimum required neovim version is high enough
298292

299293
function M.tbl_flatten(t)
300294
--- @diagnostic disable-next-line:deprecated

0 commit comments

Comments
 (0)