mirror of
https://github.com/neovim/neovim.git
synced 2025-12-15 19:05:40 +00:00
feat(net): vim.net.request(), :edit [url] #34140
Problem:
Nvim depends on netrw to download/request URL contents.
Solution:
- Add `vim.net.request()` as a thin curl wrapper:
- Basic GET with --silent, --show-error, --fail, --location, --retry
- Optional `opts.outpath` to save to a file
- Operates asynchronously. Pass an `on_response` handler to get the result.
- Add integ tests (requires NVIM_TEST_INTEG to be set) to test success
and 404 failure.
- Health check for missing `curl`.
- Handle `:edit https://…` using `vim.net.request()`.
API Usage:
1. Asynchronous request:
vim.net.request('https://httpbingo.org/get', { retry = 2 }, function(err, response)
if err then
print('Fetch failed:', err)
else
print('Got body of length:', #response.body)
end
end)
2. Download to file:
vim.net.request('https://httpbingo.org/get', { outpath = 'out_async.txt' }, function(err)
if err then print('Error:', err) end
end)
3. Remote :edit integration (in runtime/plugin/net.lua) fetches into buffer:
:edit https://httpbingo.org/get
This commit is contained in:
43
runtime/plugin/nvim/net.lua
Normal file
43
runtime/plugin/nvim/net.lua
Normal file
@@ -0,0 +1,43 @@
|
||||
vim.g.loaded_remote_file_loader = true
|
||||
|
||||
--- Callback for BufReadCmd on remote URLs.
|
||||
--- @param args { buf: integer }
|
||||
local function on_remote_read(args)
|
||||
if vim.fn.executable('curl') ~= 1 then
|
||||
vim.api.nvim_echo({
|
||||
{ 'Warning: `curl` not found; remote URL loading disabled.', 'WarningMsg' },
|
||||
}, true, {})
|
||||
return true
|
||||
end
|
||||
|
||||
local bufnr = args.buf
|
||||
local url = vim.api.nvim_buf_get_name(bufnr)
|
||||
local view = vim.fn.winsaveview()
|
||||
|
||||
vim.api.nvim_echo({ { 'Fetching ' .. url .. ' …', 'MoreMsg' } }, true, {})
|
||||
|
||||
vim.net.request(
|
||||
url,
|
||||
{ retry = 3 },
|
||||
vim.schedule_wrap(function(err, content)
|
||||
if err then
|
||||
vim.notify('Failed to fetch ' .. url .. ': ' .. tostring(err), vim.log.levels.ERROR)
|
||||
vim.fn.winrestview(view)
|
||||
return
|
||||
end
|
||||
|
||||
local lines = vim.split(content.body, '\n', { plain = true })
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
|
||||
|
||||
vim.fn.winrestview(view)
|
||||
vim.api.nvim_echo({ { 'Loaded ' .. url, 'Normal' } }, true, {})
|
||||
end)
|
||||
)
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd('BufReadCmd', {
|
||||
group = vim.api.nvim_create_augroup('nvim.net.remotefile', {}),
|
||||
pattern = { 'http://*', 'https://*' },
|
||||
desc = 'Edit remote files (:edit https://example.com)',
|
||||
callback = on_remote_read,
|
||||
})
|
||||
Reference in New Issue
Block a user