The best way of sharing data between Vim instances is through pipes.
This is what this library does... Nothing more.
- Copy the folder
lua/deps/vim-sockets
to your project's dependencies folder - Change the imports (Lua doesn't support relative imports)
- And require it
local VimSockets = require 'vim-sockets'
How I implemented relative imports
ScriptPath = debug.getinfo(1, 'S').source:sub(2)
package.path = package.path .. ';' .. ScriptPath:match '(.*)/.*/' .. '/deps/?.lua'
ScriptPath
is lua/vplugin/init.lua
,
:match
turns it into lua
.
---@class VimSockets
---@field socket string
---@field dep_path string
---@field sockets string[]
---@field receivers table<string, fun(props: ReceiverProps)>
---@param dep_path string # package.loaded...
---@param vim_events? boolean
function VimSockets:setup(dep_path, vim_events) end
-
dep_path
is the path to access itself in the loaded vim plugin -
vim_events
create or not:PrintLogs
and:PrintSockets
By default the event
VimLeavePre
is setted with:unregister_self()
.
-
:on(event: string, fn: fun(props: ReceiverProps))
Sets a callback for when the said event is received.
-
:emit(event: string, data: any)
Emit the said event passing the said data to all the other instances.
-
:emit_to(socket: string, event: string: data: any)
Emit the said event with the said data to the said socket.
-
:unregister_self()
Unregister/disable/disconnect the current instance.
---@class ReceiverProps
---@field event string
---@field emitter string
---@field data any