robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Barrett Ruthrefactor(zip)!: builtin zip.lua plugin #40846e29af9c32f

master
3.7 KiB130 linesraw
1if vim.g.loaded_nvim_net_plugin ~= nil then
2  return
3end
4vim.g.loaded_nvim_net_plugin = true
5
6local augroup = vim.api.nvim_create_augroup('nvim.net.remotefile', {})
7local url_patterns = { 'http://*', 'https://*' }
8
9local archive_patterns = {
10  { suffix = '.tar.gz', kind = 'tar' },
11  { suffix = '.tar.bz2', kind = 'tar' },
12  { suffix = '.tar.xz', kind = 'tar' },
13  { suffix = '.txz', kind = 'tar' },
14  { suffix = '.tar', kind = 'tar' },
15  { suffix = '.zip', kind = 'zip' },
16}
17
18---@param url string
19---@return string
20local function url_path(url)
21  return (url:gsub('[?#].*$', ''))
22end
23
24---@param url string
25---@return 'zip'|'tar'?
26local function archive_type(url)
27  local path = url_path(url)
28  for _, pattern in ipairs(archive_patterns) do
29    if vim.endswith(path, pattern.suffix) then
30      return pattern.kind
31    end
32  end
33end
34
35vim.api.nvim_create_autocmd('BufReadCmd', {
36  group = augroup,
37  pattern = url_patterns,
38  desc = 'Edit remote files (:edit https://example.com)',
39  callback = function(ev)
40    if vim.fn.executable('curl') ~= 1 then
41      vim.notify('vim.net.request: curl not found', vim.log.levels.WARN)
42      return
43    end
44
45    local url = ev.file
46    local archive_kind = archive_type(url)
47
48    vim.notify(('Fetching %s …'):format(url), vim.log.levels.INFO)
49
50    if archive_kind then
51      -- XXX: zipPlugin.vim, tarPlugin.vim don't work with non-file buffers.
52      local tmpfile = vim.fn.tempname()
53
54      vim.net.request(
55        url,
56        { outpath = tmpfile },
57        vim.schedule_wrap(function(err)
58          if err then
59            vim.notify(('Failed to fetch %s: %s'):format(url, err), vim.log.levels.ERROR)
60            return
61          end
62
63          if archive_kind == 'zip' then
64            if vim.fn.exists('#zip') == 1 then
65              vim.fn['zip#Browse'](tmpfile)
66            elseif vim.fn.exists('#nvim.zip') == 1 then
67              require('nvim.zip').browse(ev.buf, tmpfile)
68            end
69          else
70            vim.fn['tar#Browse'](tmpfile)
71          end
72
73          vim.bo[ev.buf].modified = false
74          vim.bo[ev.buf].buftype = 'nofile'
75          vim.notify(('Loaded %s'):format(url), vim.log.levels.INFO)
76        end)
77      )
78      return
79    end
80
81    vim.net.request(
82      url,
83      { outbuf = ev.buf },
84      vim.schedule_wrap(function(err, _)
85        if err then
86          vim.notify(('Failed to fetch %s: %s'):format(url, err), vim.log.levels.ERROR)
87          return
88        end
89
90        vim.api.nvim_exec_autocmds('BufRead', { group = 'filetypedetect', buf = ev.buf })
91        vim.bo[ev.buf].modified = false
92        vim.bo[ev.buf].buftype = 'nofile'
93        vim.notify(('Loaded %s'):format(url), vim.log.levels.INFO)
94      end)
95    )
96  end,
97})
98
99vim.api.nvim_create_autocmd('FileReadCmd', {
100  group = augroup,
101  pattern = url_patterns,
102  desc = 'Read remote files (:read https://example.com)',
103  callback = function(ev)
104    if vim.fn.executable('curl') ~= 1 then
105      vim.notify('vim.net.request: curl not found', vim.log.levels.WARN)
106      return
107    end
108
109    local url = ev.file
110    vim.notify(('Fetching %s …'):format(url), vim.log.levels.INFO)
111
112    vim.net.request(
113      url,
114      {},
115      vim.schedule_wrap(function(err, response)
116        if err or not response then
117          vim.notify(('Failed to fetch %s: %s'):format(url, err), vim.log.levels.ERROR)
118          return
119        end
120
121        -- Start inserting the response at the line number given by read (e.g. :10read).
122        -- FIXME: Doesn't work for :0read as '[ is set to 1. See #7177 for possible solutions.
123        local start = vim.fn.line("'[")
124        local lines = vim.split(response.body or '', '\n', { plain = true })
125        vim.api.nvim_buf_set_lines(ev.buf, start, start, true, lines)
126        vim.notify(('Loaded %s'):format(url), vim.log.levels.INFO)
127      end)
128    )
129  end,
130})