Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow attaching to paths inside archives #1687

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ M.default_config = {
M.on_setup = nil

function M.bufname_valid(bufname)
if bufname and bufname ~= '' and (bufname:match '^([a-zA-Z]:).*' or bufname:match '^/') then
return true
else
if not bufname then
return false
end
if bufname:match '^/' or bufname:match '^[a-zA-Z]:' or bufname:match '^zipfile://' or bufname:match '^tarfile:' then
return true
end
return false
end

function M.validate_bufnr(bufnr)
Expand Down Expand Up @@ -341,6 +343,7 @@ function M.root_pattern(...)
end
end
return function(startpath)
startpath = M.strip_archive_subpath(startpath)
return M.search_ancestors(startpath, matcher)
end
end
Expand Down Expand Up @@ -437,4 +440,13 @@ function M.get_managed_clients()
return clients
end

-- For zipfile: or tarfile: virtual paths, returns the path to the archive.
-- Other paths are returned unaltered.
function M.strip_archive_subpath(path)
-- Matches regex from zip.vim / tar.vim
path = vim.fn.substitute(path, 'zipfile://\\(.\\{-}\\)::[^\\\\].*$', '\\1', '')
path = vim.fn.substitute(path, 'tarfile:\\(.\\{-}\\)::.*$', '\\1', '')
return path
end

return M
23 changes: 23 additions & 0 deletions test/lspconfig_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ describe('lspconfig', function()
]])
end)
end)

describe('strip_archive_subpath', function()
it('strips zipfile subpaths', function()
ok(exec_lua [[
local lspconfig = require("lspconfig")
return lspconfig.util.strip_archive_subpath("zipfile:///one/two.zip::three/four") == "/one/two.zip"
]])
end)

it('strips tarfile subpaths', function()
ok(exec_lua [[
local lspconfig = require("lspconfig")
return lspconfig.util.strip_archive_subpath("tarfile:/one/two.tgz::three/four") == "/one/two.tgz"
]])
end)

it('returns non-archive paths as-is', function()
ok(exec_lua [[
local lspconfig = require("lspconfig")
return lspconfig.util.strip_archive_subpath("/one/two.zip") == "/one/two.zip"
]])
end)
end)
end)
describe('config', function()
it('normalizes user, server, and base default configs', function()
Expand Down