Skip to content

Commit

Permalink
refactor: deprecate util.path.join
Browse files Browse the repository at this point in the history
Work on #2079.
  • Loading branch information
dundargoc committed Dec 21, 2024
1 parent 040001d commit 45c5095
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 61 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|util\.path\.is_file|util\.path\.is_dir|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor)'
SEARCH_PATTERN='(util\.path\.dirname|util\.path\.sanitize|util\.path\.exists|util\.path\.is_file|util\.path\.is_dir|util\.path\.join|util\.find_mercurial_ancestor|util\.find_node_modules_ancestor|util\.find_package_json_ancestor|util\.find_git_ancestor)'

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
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/apex_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ return {
on_new_config = function(config)
if not config.cmd and config.apex_jar_path then
config.cmd = {
vim.env.JAVA_HOME and util.path.join(vim.env.JAVA_HOME, 'bin', 'java') or 'java',
vim.env.JAVA_HOME and (vim.env.JAVA_HOME .. '/bin/java') or 'java',
'-cp',
config.apex_jar_path,
'-Ddebug.internal.errors=true',
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/astro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'typescript', 'lib')) or ''
return project_root and (project_root .. '/typescript/lib') or ''
end

return {
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/cadence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ return {
return util.root_pattern 'flow.json'(fname) or vim.env.HOME
end,
on_new_config = function(new_config, new_root_dir)
new_config.init_options.configPath = util.path.join(new_root_dir, 'flow.json')
new_config.init_options.configPath = new_root_dir .. '/flow.json'
end,
},
docs = {
Expand Down
4 changes: 1 addition & 3 deletions lua/lspconfig/configs/drools_lsp.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local util = require 'lspconfig.util'

local function get_java_bin(config)
local java_bin = vim.tbl_get(config, 'drools', 'java', 'bin')
if not java_bin then
java_bin = vim.env.JAVA_HOME and util.path.join(vim.env.JAVA_HOME, 'bin', 'java') or 'java'
java_bin = vim.env.JAVA_HOME and (vim.env.JAVA_HOME .. '/bin/java') or 'java'
if vim.fn.has 'win32' == 1 then
java_bin = java_bin .. '.exe'
end
Expand Down
4 changes: 2 additions & 2 deletions lua/lspconfig/configs/eslint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ return {
end

-- Support Yarn2 (PnP) projects
local pnp_cjs = util.path.join(new_root_dir, '.pnp.cjs')
local pnp_js = util.path.join(new_root_dir, '.pnp.js')
local pnp_cjs = new_root_dir .. '/.pnp.cjs'
local pnp_js = new_root_dir .. '/.pnp.js'
if vim.loop.fs_stat(pnp_cjs) or vim.loop.fs_stat(pnp_js) then
config.cmd = vim.list_extend({ 'yarn', 'exec' }, config.cmd)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/foam_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return {
filetypes = { 'foam', 'OpenFOAM' },
root_dir = function(fname)
return util.search_ancestors(fname, function(path)
if vim.loop.fs_stat(util.path.join(path, 'system', 'controlDict')) then
if vim.loop.fs_stat(path .. '/system/controlDict') then
return path
end
end)
Expand Down
4 changes: 2 additions & 2 deletions lua/lspconfig/configs/gitlab_ci_ls.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local util = require 'lspconfig.util'

local cache_dir = util.path.join(vim.loop.os_homedir(), '.cache/gitlab-ci-ls/')
local cache_dir = vim.loop.os_homedir() .. '/.cache/gitlab-ci-ls/'
return {
default_config = {
cmd = { 'gitlab-ci-ls' },
filetypes = { 'yaml.gitlab' },
root_dir = util.root_pattern('.gitlab*', '.git'),
init_options = {
cache_path = cache_dir,
log_path = util.path.join(cache_dir, 'log/gitlab-ci-ls.log'),
log_path = cache_dir .. '/log/gitlab-ci-ls.log',
},
},
docs = {
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/glint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ return {
on_new_config = function(config, new_root_dir)
local project_root = vim.fs.find('node_modules', { path = new_root_dir, upward = true })[1]
-- Glint should not be installed globally.
local node_bin_path = util.path.join(project_root, 'node_modules', '.bin')
local node_bin_path = project_root .. '/node_modules/.bin'
local path = node_bin_path .. util.path.path_separator .. vim.env.PATH
if config.cmd_env then
config.cmd_env.PATH = path
Expand Down
8 changes: 4 additions & 4 deletions lua/lspconfig/configs/jdtls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ local env = {
}

local function get_cache_dir()
return env.XDG_CACHE_HOME and env.XDG_CACHE_HOME or util.path.join(env.HOME, '.cache')
return env.XDG_CACHE_HOME and env.XDG_CACHE_HOME or env.HOME .. '/.cache'
end

local function get_jdtls_cache_dir()
return util.path.join(get_cache_dir(), 'jdtls')
return get_cache_dir() .. '/jdtls'
end

local function get_jdtls_config_dir()
return util.path.join(get_jdtls_cache_dir(), 'config')
return get_jdtls_cache_dir() .. '/config'
end

local function get_jdtls_workspace_dir()
return util.path.join(get_jdtls_cache_dir(), 'workspace')
return get_jdtls_cache_dir() .. '/workspace'
end

local function get_jdtls_jvm_args()
Expand Down
4 changes: 2 additions & 2 deletions lua/lspconfig/configs/jsonnet_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ local util = require 'lspconfig.util'
-- common jsonnet library paths
local function jsonnet_path(root_dir)
local paths = {
util.path.join(root_dir, 'lib'),
util.path.join(root_dir, 'vendor'),
root_dir .. '/lib',
root_dir .. '/vendor',
}
return table.concat(paths, ':')
end
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/mdx_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
return project_root and (project_root .. '/node_modules/typescript/lib') or ''
end

return {
Expand Down
6 changes: 3 additions & 3 deletions lua/lspconfig/configs/relay_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ return {
root_dir = util.root_pattern('relay.config.*', 'package.json'),
on_new_config = function(config, root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
local node_bin_path = util.path.join(project_root, 'node_modules', '.bin')
local compiler_cmd = { util.path.join(node_bin_path, 'relay-compiler'), '--watch' }
local node_bin_path = project_root .. '/node_modules/.bin'
local compiler_cmd = { node_bin_path .. '/relay-compiler', '--watch' }
local path = node_bin_path .. util.path.path_separator .. vim.env.PATH
if config.cmd_env then
config.cmd_env.PATH = path
Expand All @@ -35,7 +35,7 @@ return {

if config.path_to_config then
config.path_to_config = vim.fs.normalize(config.path_to_config)
local path_to_config = util.path.join(root_dir, config.path_to_config)
local path_to_config = table.concat({ root_dir, config.path_to_config }, '/')
if vim.loop.fs_stat(path_to_config) then
vim.list_extend(config.cmd, { config.path_to_config })
vim.list_extend(compiler_cmd, { config.path_to_config })
Expand Down
12 changes: 6 additions & 6 deletions lua/lspconfig/configs/rust_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ end

local function is_library(fname)
local user_home = vim.fs.normalize(vim.env.HOME)
local cargo_home = os.getenv 'CARGO_HOME' or util.path.join(user_home, '.cargo')
local registry = util.path.join(cargo_home, 'registry', 'src')
local git_registry = util.path.join(cargo_home, 'git', 'checkouts')
local cargo_home = os.getenv 'CARGO_HOME' or user_home .. '/.cargo'
local registry = cargo_home .. '/registry/src'
local git_registry = cargo_home .. '/git/checkouts'

local rustup_home = os.getenv 'RUSTUP_HOME' or util.path.join(user_home, '.rustup')
local toolchains = util.path.join(rustup_home, 'toolchains')
local rustup_home = os.getenv 'RUSTUP_HOME' or user_home .. '/.rustup'
local toolchains = rustup_home .. '/toolchains'

for _, item in ipairs { toolchains, registry, git_registry } do
if util.path.is_descendant(item, fname) then
Expand Down Expand Up @@ -54,7 +54,7 @@ return {
'--format-version',
'1',
'--manifest-path',
util.path.join(cargo_crate_dir, 'Cargo.toml'),
cargo_crate_dir .. '/Cargo.toml',
}

local result = async.run_command(cmd)
Expand Down
6 changes: 2 additions & 4 deletions lua/lspconfig/configs/turtle_ls.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local util = require 'lspconfig.util'

local bin_name = 'turtle-language-server'
local bin_path = os.getenv 'NVM_BIN'
local full_path
Expand All @@ -16,14 +14,14 @@ if bin_path == nil then
end
end
for _, p in ipairs(paths) do
local candidate = util.path.join(p, bin_name)
local candidate = table.concat({ p, bin_name }, '/')
if (vim.loop.fs_stat(candidate) or {}).type == 'file' then
full_path = candidate
break
end
end
else
full_path = util.path.join(bin_path, bin_name)
full_path = table.concat({ bin_path, bin_name }, '/')
end

return {
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/vala_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require 'lspconfig.util'

local meson_matcher = function(path)
local pattern = 'meson.build'
local f = vim.fn.glob(util.path.join(path, pattern))
local f = vim.fn.glob(table.concat({ path, pattern }, '/'))
if f == '' then
return nil
end
Expand Down
18 changes: 9 additions & 9 deletions lua/lspconfig/configs/vdmj.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
local util = require 'lspconfig.util'

local function get_default_mavenrepo()
local repo = util.path.join(vim.env.HOME, '.m2', 'repository', 'dk', 'au', 'ece', 'vdmj')
local repo = vim.env.HOME .. '/.m2/repository/dk/au/ece/vdmj'
if vim.loop.fs_stat(repo) then
return repo
else
return util.path.join(vim.env.HOME, '.m2', 'repository', 'com', 'fujitsu')
return vim.env.HOME .. '/.m2/repository/com/fujitsu'
end
end

local function get_jar_path(config, package, version)
return util.path.join(config.options.mavenrepo, package, version, package .. '-' .. version .. '.jar')
return table.concat({ config.options.mavenrepo, package, version, package .. '-' .. version .. '.jar' }, '/')
end

local function with_precision(version, is_high_precision)
return is_high_precision and version:gsub('([%d.]+)', '%1-P') or version
end

local function get_latest_installed_version(repo)
local path = util.path.join(repo, 'lsp')
local path = repo .. '/lsp'
local sort = vim.fn.sort

local subdirs = function(file)
local stat = vim.loop.fs_stat(util.path.join(path, file))
local stat = vim.loop.fs_stat(table.concat({ path, file }, '/'))
return stat.type == 'directory' and 1 or 0
end

Expand All @@ -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 vim.fn.isdirectory(util.path.join(path, '.vscode')) == 1 then
if vim.fn.isdirectory(path .. '/.vscode') == 1 then
return path
end
end)
Expand All @@ -48,11 +48,11 @@ return {
return vim.fs.dirname(vim.fs.find('.git', { path = fname, upward = true })[1]) or find_vscode_ancestor(fname)
end,
options = {
java = vim.env.JAVA_HOME and util.path.join(vim.env.JAVA_HOME, 'bin', 'java') or 'java',
java = vim.env.JAVA_HOME and (vim.env.JAVA_HOME .. '/bin/java') or 'java',
java_opts = { '-Xmx3000m', '-Xss1m' },
annotation_paths = {},
mavenrepo = get_default_mavenrepo(),
logfile = util.path.join(vim.fn.stdpath 'cache', 'vdm-lsp.log'),
logfile = vim.fn.stdpath('cache') .. '/vdm-lsp.log',
debugger_port = -1,
high_precision = false,
},
Expand Down Expand Up @@ -90,7 +90,7 @@ by neovim.
get_jar_path(config, 'vdmj', version),
get_jar_path(config, 'annotations', version),
get_jar_path(config, 'lsp', version),
util.path.join(root_dir, '.vscode'),
root_dir .. '/.vscode',
unpack(config.options.annotation_paths),
}, ':')

Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/volar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local util = require 'lspconfig.util'

local function get_typescript_server_path(root_dir)
local project_root = vim.fs.find('node_modules', { path = root_dir, upward = true })[1]
return project_root and (util.path.join(project_root, 'typescript', 'lib')) or ''
return project_root and (project_root .. '/typescript/lib') or ''
end

-- https://github.com/vuejs/language-tools/blob/master/packages/language-server/lib/types.ts
Expand Down
12 changes: 6 additions & 6 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ M.path = (function()
end
end

local function path_join(...)
return table.concat(M.tbl_flatten { ... }, '/')
end

-- Traverse the path calling cb along the way.
local function traverse_parents(path, cb)
path = vim.loop.fs_realpath(path)
Expand Down Expand Up @@ -165,7 +161,6 @@ M.path = (function()
local path_separator = iswin and ';' or ':'

return {
join = path_join,
traverse_parents = traverse_parents,
iterate_parents = iterate_parents,
is_descendant = is_descendant,
Expand Down Expand Up @@ -204,7 +199,7 @@ function M.root_pattern(...)
startpath = M.strip_archive_subpath(startpath)
for _, pattern in ipairs(patterns) do
local match = M.search_ancestors(startpath, function(path)
for _, p in ipairs(vim.fn.glob(M.path.join(escape_wildcards(path), pattern), true, true)) do
for _, p in ipairs(vim.fn.glob(table.concat({ escape_wildcards(path), pattern }, '/'), true, true)) do
if vim.loop.fs_stat(p) then
return path
end
Expand Down Expand Up @@ -363,6 +358,11 @@ function M.path.exists(filename)
return stat and stat.type or false
end

--- @deprecated use `table.concat({"path1", "path2"})` or regular string concatenation instead
function M.path.join(...)
return table.concat({ ... }, '/')
end

--- @deprecated use `vim.fs.dirname(vim.fs.find('.hg', { path = startpath, upward = true })[1])` instead
function M.find_mercurial_ancestor(startpath)
return vim.fs.dirname(vim.fs.find('.hg', { path = startpath, upward = true })[1])
Expand Down
2 changes: 1 addition & 1 deletion scripts/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ local function make_lsp_sections()
end
end,
function()
local package_json_name = util.path.join(tempdir, config_name .. '.package.json')
local package_json_name = table.concat({ tempdir, config_name .. '.package.json' }, '/')
if docs.package_json then
if not ((vim.loop.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))
Expand Down
10 changes: 0 additions & 10 deletions test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ describe('lspconfig', function()
end)

describe('util', function()
describe('path', function()
describe('join', function()
it('', function()
local lspconfig = require 'lspconfig'
local res = lspconfig.util.path.join('foo', 'bar', 'baz')
eq('foo/bar/baz', res)
end)
end)
end)

describe('root_pattern', function()
it('resolves to a_marker.txt', function()
local lspconfig = require 'lspconfig'
Expand Down

0 comments on commit 45c5095

Please sign in to comment.