Skip to content

Commit

Permalink
implement callback for headless instance
Browse files Browse the repository at this point in the history
ref #43
  • Loading branch information
jbyuki committed Aug 1, 2024
1 parent 6254b2f commit 96d6387
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
20 changes: 20 additions & 0 deletions doc/osv.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ Checks whether |osv| is currently running.
True if osv is running.


=========================================================================
osv callbacks *osv-callbacks*

Certain default bevahviours can be overriden using callbacks. All callbacks
are stored in the `on` table. If no function is assigned, the default
bevahiour is executed, otherwise the callback will be called instead. Only one
callback can be assigned to an action.

For example:
>
require"osv".on["action"] = function(...)
end
<
The `start_server` event is called when osv starts the headless instance for
the DAP server. The default behaviour is:
>
require"osv".on["start_server"] = function(args, env)
return vim.fn.jobstart(args, {rpc = true, env = env})
end
=========================================================================
osv logging *osv-logging*

Expand Down
12 changes: 11 additions & 1 deletion lua/osv/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ function M.launch(opts)
vim.api.nvim_echo({{"Server is already running.", "ErrorMsg"}}, true, {})
return
end


vim.validate {
opts = {opts, 't', true}
}
Expand Down Expand Up @@ -149,7 +151,13 @@ function M.launch(opts)
end
end

nvim_server = vim.fn.jobstart(args, {rpc = true, env = env})
if M.on["start_server"] then
nvim_server = M.callback["start_server"](args, env)
assert(nvim_server)

else
nvim_server = vim.fn.jobstart(args, {rpc = true, env = env})
end

local mode = vim.fn.rpcrequest(nvim_server, "nvim_get_mode")
if mode.blocking then
Expand Down Expand Up @@ -1363,6 +1371,8 @@ function M.wait_attach()
end))
end

M.on = {}

function log(str)
if log_filename then
local f = io.open(log_filename, "a")
Expand Down
15 changes: 14 additions & 1 deletion src/launch.lua.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ local nvim_server
@copy_env
@fill_env_with_custom
@fill_args_with_custom
nvim_server = vim.fn.jobstart(args, {rpc = true, env = env})
@if_exists_use_custom_for_launching_server
else
nvim_server = vim.fn.jobstart(args, {rpc = true, env = env})
end

@script_variables+=
local hook_address
Expand Down Expand Up @@ -137,3 +140,13 @@ if M.is_running() then
vim.api.nvim_echo({{"Server is already running.", "ErrorMsg"}}, true, {})
return
end


@implement+=
M.on = {}

@if_exists_use_custom_for_launching_server+=
if M.on["start_server"] then
nvim_server = M.callback["start_server"](args, env)
assert(nvim_server)

0 comments on commit 96d6387

Please sign in to comment.