Skip to content

Commit

Permalink
refactor: deprecate find_mercurial_ancestor
Browse files Browse the repository at this point in the history
  • Loading branch information
dundargoc committed Nov 28, 2024
1 parent e8f9c46 commit bc01b29
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/ci/run_sanitizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANC
exit 1
fi

SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists)'
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir)'

if git diff --pickaxe-all -U0 -G "${SEARCH_PATTERN}" "${REF_BRANCH}" "${PR_BRANCH}" -- '*.lua' | grep -Ev '\.lua$' | grep -E "^\+.*${SEARCH_PATTERN}" ; then
echo
Expand Down
3 changes: 2 additions & 1 deletion lua/lspconfig/configs/fennel_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ return {
filetypes = { 'fennel' },
root_dir = function(dir)
local has_fls_project_cfg = function(path)
return util.path.is_file(vim.fs.joinpath(path, 'flsproject.fnl'))
local fnlpath = vim.fs.joinpath(path, 'flsproject.fnl')
return (vim.loop.fs_stat(fnlpath) or {}).type == 'file'
end
return util.search_ancestors(dir, has_fls_project_cfg) or vim.fs.root(0, '.git')
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/turtle_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if bin_path == nil then
end
for _, p in ipairs(paths) do
local candidate = util.path.join(p, bin_name)
if util.path.is_file(candidate) then
if (vim.loop.fs_stat(candidate) or {}).type == 'file' then
full_path = candidate
break
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/vdmj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
-- Special case, as vdmj store particular settings under root_dir/.vscode
local function find_vscode_ancestor(startpath)
return util.search_ancestors(startpath, function(path)
if util.path.is_dir(util.path.join(path, '.vscode')) then
if vim.fn.isdirectory(util.path.join(path, '.vscode')) == 1 then
return path
end
end)
Expand Down
52 changes: 24 additions & 28 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,6 @@ M.path = (function()
return path:gsub('([%[%]%?%*])', '\\%1')
end

--- @param filename string
--- @return boolean
local function is_dir(filename)
local stat = uv.fs_stat(filename)
return stat and stat.type == 'directory' or false
end

--- @param filename string
--- @return boolean
local function is_file(filename)
local stat = uv.fs_stat(filename)
return stat and stat.type == 'file' or false
end

--- @param path string
--- @return boolean
local function is_fs_root(path)
Expand Down Expand Up @@ -195,8 +181,6 @@ M.path = (function()

return {
escape_wildcards = escape_wildcards,
is_dir = is_dir,
is_file = is_file,
is_absolute = is_absolute,
join = path_join,
traverse_parents = traverse_parents,
Expand Down Expand Up @@ -256,32 +240,25 @@ end
function M.find_git_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
-- Support git directories and git files (worktrees)
if M.path.is_dir(M.path.join(path, '.git')) or M.path.is_file(M.path.join(path, '.git')) then
return path
end
end)
end

function M.find_mercurial_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
-- Support Mercurial directories
if M.path.is_dir(M.path.join(path, '.hg')) then
local gitpath = M.path.join(path, '.git')
if vim.fn.isdirectory(gitpath) == 1 or (uv.fs_stat(gitpath) or {}).type == 'file' then
return path
end
end)
end

function M.find_node_modules_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
if M.path.is_dir(M.path.join(path, 'node_modules')) then
if vim.fn.isdirectory(M.path.join(path, 'node_modules')) == 1 then
return path
end
end)
end

function M.find_package_json_ancestor(startpath)
return M.search_ancestors(startpath, function(path)
if M.path.is_file(M.path.join(path, 'package.json')) then
local jsonpath = M.path.join(path, 'package.json')
if (uv.fs_stat(jsonpath) or {}).type == 'file' then
return path
end
end)
Expand Down Expand Up @@ -391,6 +368,20 @@ end

--- Deprecated functions

--- @deprecated use `vim.fn.isdirectory(path) == 1` instead
--- @param filename string
--- @return boolean
function M.path.is_dir(filename)
return vim.fn.isdirectory(filename) == 1
end

--- @deprecated use `(vim.loop.fs_stat(path) or {}).type == 'file'` instead
--- @param path string
--- @return boolean
function M.path.is_file(path)
return (vim.loop.fs_stat(path) or {}).type == 'file'
end

--- @deprecated use `vim.fs.dirname` instead
M.path.dirname = vim.fs.dirname

Expand All @@ -405,4 +396,9 @@ function M.path.exists(filename)
return stat and stat.type or false
end

--- @deprecated use `vim.fs.find('.hg', { path = startpath, upward = true })` instead
function M.find_mercurial_ancestor(startpath)
return vim.fs.find('.hg', { path = startpath, upward = true })
end

return M
6 changes: 3 additions & 3 deletions scripts/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ local function make_section(indentlvl, sep, parts)
end

local function readfile(path)
assert(util.path.is_file(path))
assert((uv.fs_stat(path) or {}).type == 'file')
return io.open(path):read '*a'
end

Expand Down Expand Up @@ -181,10 +181,10 @@ local function make_lsp_sections()
function()
local package_json_name = util.path.join(tempdir, config_name .. '.package.json')
if docs.package_json then
if not util.path.is_file(package_json_name) then
if not ((uv.fs_stat(package_json_name) or {}).type == 'file') then
os.execute(string.format('curl -v -L -o %q %q', package_json_name, docs.package_json))
end
if not util.path.is_file(package_json_name) then
if not ((uv.fs_stat(package_json_name) or {}).type == 'file') then
print(string.format('Failed to download package.json for %q at %q', config_name, docs.package_json))
os.exit(1)
return
Expand Down
53 changes: 0 additions & 53 deletions test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,6 @@ describe('lspconfig', function()
end)
end)

describe('is_dir', function()
it('is directory', function()
local lspconfig = require 'lspconfig'
local cwd = vim.fn.getcwd()
assert.is_true(lspconfig.util.path.is_dir(cwd))
end)

it('is not present directory', function()
local lspconfig = require 'lspconfig'
local not_exist_dir = vim.fn.getcwd() .. '/not/exists'
eq(true, not lspconfig.util.path.is_dir(not_exist_dir))
end)

it('is file', function()
local lspconfig = require 'lspconfig'

-- change the working directory to test directory
vim.api.nvim_command 'cd ./test/test_dir/'
local file = vim.fn.getcwd() .. '/root_marker.txt'

eq(true, not lspconfig.util.path.is_dir(file))
end)
end)

describe('is_file', function()
it('is file', function()
local lspconfig = require 'lspconfig'

-- change the working directory to test directory
vim.api.nvim_command 'cd ./test/test_dir/'
local file = vim.fn.getcwd() .. '/root_marker.txt'

eq(true, lspconfig.util.path.is_file(file))
end)

it('is not present file', function()
local lspconfig = require 'lspconfig'

-- change the working directory to test directory
vim.api.nvim_command 'cd ./test/test_dir/'
local file = vim.fn.getcwd() .. '/not_exists.txt'

eq(true, not lspconfig.util.path.is_file(file))
end)

it('is directory', function()
local lspconfig = require 'lspconfig'

local cwd = vim.fn.getcwd()
eq(true, not lspconfig.util.path.is_file(cwd))
end)
end)

describe('is_absolute', function()
it('is absolute', function()
local lspconfig = require 'lspconfig'
Expand Down

0 comments on commit bc01b29

Please sign in to comment.