mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
doc: file-change-detect [ci skip]
This commit is contained in:
committed by
Justin M. Keyes
parent
3e21d49836
commit
c66297452c
@@ -416,20 +416,46 @@ Example: repeating timer
|
|||||||
print('sleeping');
|
print('sleeping');
|
||||||
|
|
||||||
|
|
||||||
|
Example: File-change detection *file-change-detect*
|
||||||
|
1. Save this code to a file.
|
||||||
|
2. Execute it with ":luafile %".
|
||||||
|
3. Use ":Watch %" to watch any file.
|
||||||
|
4. Try editing the file from another text editor.
|
||||||
|
5. Observe that the file reloads in Nvim (because on_change() calls
|
||||||
|
|:checktime|). >
|
||||||
|
|
||||||
|
local w = vim.loop.new_fs_event()
|
||||||
|
local function on_change(err, fname, status)
|
||||||
|
-- Do work...
|
||||||
|
vim.api.nvim_command('checktime')
|
||||||
|
-- Debounce: stop/start.
|
||||||
|
w:stop()
|
||||||
|
watch_file(fname)
|
||||||
|
end
|
||||||
|
function watch_file(fname)
|
||||||
|
local fullpath = vim.api.nvim_call_function(
|
||||||
|
'fnamemodify', {fname, ':p'})
|
||||||
|
w:start(fullpath, {}, vim.schedule_wrap(function(...)
|
||||||
|
on_change(...) end))
|
||||||
|
end
|
||||||
|
vim.api.nvim_command(
|
||||||
|
"command! -nargs=1 Watch call luaeval('watch_file(_A)', expand('<args>'))")
|
||||||
|
|
||||||
|
|
||||||
Example: TCP echo-server *tcp-server*
|
Example: TCP echo-server *tcp-server*
|
||||||
1. Save this code to a file.
|
1. Save this code to a file.
|
||||||
2. Execute it with ":luafile %".
|
2. Execute it with ":luafile %".
|
||||||
3. Note the port number.
|
3. Note the port number.
|
||||||
4. Connect from any TCP client (e.g. "nc 0.0.0.0 36795"): >
|
4. Connect from any TCP client (e.g. "nc 0.0.0.0 36795"): >
|
||||||
|
|
||||||
local function create_server(host, port, on_connection)
|
local function create_server(host, port, on_connect)
|
||||||
local server = vim.loop.new_tcp()
|
local server = vim.loop.new_tcp()
|
||||||
server:bind(host, port)
|
server:bind(host, port)
|
||||||
server:listen(128, function(err)
|
server:listen(128, function(err)
|
||||||
assert(not err, err) -- Check for errors.
|
assert(not err, err) -- Check for errors.
|
||||||
local sock = vim.loop.new_tcp()
|
local sock = vim.loop.new_tcp()
|
||||||
server:accept(sock) -- Accept client connection.
|
server:accept(sock) -- Accept client connection.
|
||||||
on_connection(sock) -- Start reading messages.
|
on_connect(sock) -- Start reading messages.
|
||||||
end)
|
end)
|
||||||
return server
|
return server
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user