Skip to content

Commit

Permalink
feat: allow attaching to paths inside archives
Browse files Browse the repository at this point in the history
  • Loading branch information
stephank committed Jul 1, 2022
1 parent 057ca6b commit 23b7df0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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

0 comments on commit 23b7df0

Please sign in to comment.