Skip to content

Commit

Permalink
refactor!: replace util.path.join with vim.fs.joinpath
Browse files Browse the repository at this point in the history
We should rely on builtin functionality if possible to minimize code duplication.
  • Loading branch information
dundargoc committed Oct 2, 2024
1 parent 1356515 commit a204177
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 55 deletions.
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.fs.joinpath(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 = util.find_node_modules_ancestor(root_dir)
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
return project_root and (vim.fs.joinpath(project_root, 'node_modules', '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 = vim.fs.joinpath(new_root_dir, 'flow.json')
end,
},
docs = {
Expand Down
2 changes: 1 addition & 1 deletion lua/lspconfig/configs/drools_lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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.fs.joinpath(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 = vim.fs.joinpath(new_root_dir, '.pnp.cjs')
local pnp_js = vim.fs.joinpath(new_root_dir, '.pnp.js')
if util.path.exists(pnp_cjs) or util.path.exists(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 util.path.exists(util.path.join(path, 'system', 'controlDict')) then
if util.path.exists(vim.fs.joinpath(path, 'system', 'controlDict')) then
return path
end
end)
Expand Down
8 changes: 4 additions & 4 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.uv.os_homedir(), '.cache/gitlab-ci-ls/')
local cache_dir = vim.fs.joinpath(vim.uv.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 = vim.fs.joinpath(cache_dir, 'log/gitlab-ci-ls.log'),
},
},
docs = {
Expand All @@ -25,8 +25,8 @@ cargo install gitlab-ci-ls
filetypes = { 'yaml.gitlab' },
root_dir = [[util.root_pattern('.gitlab*', '.git')]],
init_options = {
cache_path = [[util.path.join(vim.uv.os_homedir(), '.cache/gitlab-ci-ls/')]],
log_path = [[util.path.join(util.path.join(vim.uv.os_homedir(), '.cache/gitlab-ci-ls/'), 'log/gitlab-ci-ls.log')]],
cache_path = [[vim.fs.joinpath(vim.uv.os_homedir(), '.cache/gitlab-ci-ls/')]],
log_path = [[vim.fs.joinpath(vim.fs.joinpath(vim.uv.os_homedir(), '.cache/gitlab-ci-ls/'), 'log/gitlab-ci-ls.log')]],
},
},
},
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 = util.find_node_modules_ancestor(new_root_dir)
-- Glint should not be installed globally.
local node_bin_path = util.path.join(project_root, 'node_modules', '.bin')
local node_bin_path = vim.fs.joinpath(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 vim.fs.joinpath(env.HOME, '.cache')
end

local function get_jdtls_cache_dir()
return util.path.join(get_cache_dir(), 'jdtls')
return vim.fs.joinpath(get_cache_dir(), 'jdtls')
end

local function get_jdtls_config_dir()
return util.path.join(get_jdtls_cache_dir(), 'config')
return vim.fs.joinpath(get_jdtls_cache_dir(), 'config')
end

local function get_jdtls_workspace_dir()
return util.path.join(get_jdtls_cache_dir(), 'workspace')
return vim.fs.joinpath(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'),
vim.fs.joinpath(root_dir, 'lib'),
vim.fs.joinpath(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 = util.find_node_modules_ancestor(root_dir)
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
return project_root and (vim.fs.joinpath(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 = util.find_node_modules_ancestor(root_dir)
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 = vim.fs.joinpath(project_root, 'node_modules', '.bin')
local compiler_cmd = { vim.fs.joinpath(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 = util.path.sanitize(config.path_to_config)
local path_to_config = util.path.join(root_dir, config.path_to_config)
local path_to_config = vim.fs.joinpath(root_dir, config.path_to_config)
if util.path.exists(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 = util.path.sanitize(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 vim.fs.joinpath(user_home, '.cargo')
local registry = vim.fs.joinpath(cargo_home, 'registry', 'src')
local git_registry = vim.fs.joinpath(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 vim.fs.joinpath(user_home, '.rustup')
local toolchains = vim.fs.joinpath(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'),
vim.fs.joinpath(cargo_crate_dir, 'Cargo.toml'),
}

local result = async.run_command(cmd)
Expand Down
4 changes: 2 additions & 2 deletions lua/lspconfig/configs/turtle_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ if bin_path == nil then
end
end
for _, p in ipairs(paths) do
local candidate = util.path.join(p, bin_name)
local candidate = vim.fs.joinpath(p, bin_name)
if util.path.is_file(candidate) then
full_path = candidate
break
end
end
else
full_path = util.path.join(bin_path, bin_name)
full_path = vim.fs.joinpath(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(vim.fs.joinpath(path, pattern))
if f == '' then
return nil
end
Expand Down
20 changes: 10 additions & 10 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.fs.joinpath(vim.env.HOME, '.m2', 'repository', 'dk', 'au', 'ece', 'vdmj')
if util.path.exists(repo) then
return repo
else
return util.path.join(vim.env.HOME, '.m2', 'repository', 'com', 'fujitsu')
return vim.fs.joinpath(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 vim.fs.joinpath(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 = vim.fs.joinpath(repo, 'lsp')
local sort = vim.fn.sort

local subdirs = function(file)
local stat = vim.uv.fs_stat(util.path.join(path, file))
local stat = vim.uv.fs_stat(vim.fs.joinpath(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 util.path.is_dir(util.path.join(path, '.vscode')) then
if util.path.is_dir(vim.fs.joinpath(path, '.vscode')) then
return path
end
end)
Expand All @@ -48,11 +48,11 @@ return {
return util.find_git_ancestor(fname) 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.fs.joinpath(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.fs.joinpath(vim.fn.stdpath 'cache', 'vdm-lsp.log'),
debugger_port = -1,
high_precision = false,
},
Expand Down Expand Up @@ -88,7 +88,7 @@ by neovim.
annotation_paths = {},
mavenrepo = '$HOME/.m2/repository/dk/au/ece/vdmj',
version = 'The latest version installed in `mavenrepo`',
logfile = "path.join(vim.fn.stdpath 'cache', 'vdm-lsp.log')",
logfile = "vim.fs.joinpath(vim.fn.stdpath 'cache', 'vdm-lsp.log')",
debugger_port = -1,
high_precision = false,
},
Expand All @@ -104,7 +104,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'),
vim.fs.joinpath(root_dir, '.vscode'),
unpack(config.options.annotation_paths),
}, ':')

Expand Down
4 changes: 2 additions & 2 deletions 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 = util.find_node_modules_ancestor(root_dir)
return project_root and (util.path.join(project_root, 'node_modules', 'typescript', 'lib')) or ''
return project_root and (vim.fs.joinpath(project_root, 'node_modules', 'typescript', 'lib')) or ''
end

-- https://github.com/johnsoncodehk/volar/blob/20d713b/packages/shared/src/types.ts
Expand Down Expand Up @@ -90,7 +90,7 @@ local function get_typescript_server_path(root_dir)
-- local global_ts = '/usr/local/lib/node_modules/typescript/lib'
local found_ts = ''
local function check_dir(path)
found_ts = util.path.join(path, 'node_modules', 'typescript', 'lib')
found_ts = vim.fs.joinpath(path, 'node_modules', 'typescript', 'lib')
if util.path.exists(found_ts) then
return path
end
Expand Down
15 changes: 5 additions & 10 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ M.path = (function()
return result
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 = uv.fs_realpath(path)
Expand Down Expand Up @@ -236,7 +232,6 @@ M.path = (function()
is_absolute = is_absolute,
exists = exists,
dirname = dirname,
join = path_join,
sanitize = sanitize,
traverse_parents = traverse_parents,
iterate_parents = iterate_parents,
Expand Down Expand Up @@ -278,7 +273,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(M.path.escape_wildcards(path), pattern), true, true)) do
for _, p in ipairs(vim.fn.glob(vim.fs.joinpath(M.path.escape_wildcards(path), pattern), true, true)) do
if M.path.exists(p) then
return path
end
Expand All @@ -295,7 +290,7 @@ 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
if M.path.is_dir(vim.fs.joinpath(path, '.git')) or M.path.is_file(vim.fs.joinpath(path, '.git')) then
return path
end
end)
Expand All @@ -304,23 +299,23 @@ 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
if M.path.is_dir(vim.fs.joinpath(path, '.hg')) 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 M.path.is_dir(vim.fs.joinpath(path, 'node_modules')) 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
if M.path.is_file(vim.fs.joinpath(path, 'package.json')) then
return path
end
end)
Expand Down
2 changes: 1 addition & 1 deletion scripts/docgen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ local function make_lsp_sections()
end
end,
function()
local package_json_name = util.path.join(tempdir, template_name .. '.package.json')
local package_json_name = vim.fs.joinpath(tempdir, template_name .. '.package.json')
if docs.package_json then
if not util.path.is_file(package_json_name) then
os.execute(string.format('curl -v -L -o %q %q', package_json_name, docs.package_json))
Expand Down
2 changes: 1 addition & 1 deletion test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('lspconfig', function()
describe('join', function()
it('', function()
local lspconfig = require 'lspconfig'
local res = lspconfig.util.path.join('foo', 'bar', 'baz')
local res = lspconfig.vim.fs.joinpath('foo', 'bar', 'baz')
eq('foo/bar/baz', res)
end)
end)
Expand Down

0 comments on commit a204177

Please sign in to comment.