summaryrefslogtreecommitdiff
path: root/lua/clever_f/host_adapter.lua
diff options
context:
space:
mode:
authorJackson Moore <jacksonmoore@tuta.io>2026-09-04 14:50:32 +0200
committerJackson Moore <jacksonmoore@tuta.io>2026-09-04 14:50:32 +0200
commit03757a7355bbad885a9f3c86115f0374dd2cb854 (patch)
tree393b9500fe95fe83e96776c946dbb87936f693fa /lua/clever_f/host_adapter.lua
parentaa2c42761a91e8eca2d551fe14f1bc1dd3fc8593 (diff)
Complete Nvim adapter contract
Diffstat (limited to 'lua/clever_f/host_adapter.lua')
-rw-r--r--lua/clever_f/host_adapter.lua411
1 files changed, 393 insertions, 18 deletions
diff --git a/lua/clever_f/host_adapter.lua b/lua/clever_f/host_adapter.lua
index a81a44c..724e9da 100644
--- a/lua/clever_f/host_adapter.lua
+++ b/lua/clever_f/host_adapter.lua
@@ -2,6 +2,7 @@ local capabilities = require("clever_f.capabilities")
local domain = require("clever_f.domain")
local M = {}
+local unpack_values = table.unpack or unpack
local HostAdapter = {}
HostAdapter.__index = HostAdapter
M.HostAdapter = HostAdapter
@@ -11,6 +12,24 @@ M.ActionEffect = {
ESCAPE = "escape",
ERROR = "error",
}
+M.CONFIGURATION_PREFIX = "clever_f_"
+M.CONFIGURATION_GLOBALS = {
+ suppress_default_mappings = "clever_f_not_overwrites_standard_mappings",
+}
+
+local BOOLEAN_CONFIGURATION = {
+ search_current_line_only = true,
+ ignore_case = true,
+ smart_case = true,
+ use_migemo = true,
+ fix_key_direction = true,
+ show_prompt = true,
+ mark_cursor = true,
+ hide_cursor_on_cmdline = true,
+ mark_char = true,
+ mark_direct = true,
+ clean_labels_eagerly = true,
+}
local adapter_records = setmetatable({}, { __mode = "k" })
@@ -19,8 +38,14 @@ local function fail(message, level)
end
local function current_runtime(options)
- if type(options) == "table" and options.runtime ~= nil then
- return options.runtime
+ if type(options) == "table" then
+ if options.runtime ~= nil then
+ return options.runtime
+ end
+ if options.api ~= nil then
+ return options
+ end
+ return rawget(_G, "vim")
end
if options ~= nil and options ~= HostAdapter then
return options
@@ -47,9 +72,12 @@ function HostAdapter.new(options)
timers = {},
cursor_presentations = {},
events = {},
+ actions = {},
+ mappings = {},
dot_repeat = nil,
dot_bridge = nil,
event_order = {},
+ action_diagnostics = nil,
augroup = nil,
}
record.event_queue = capabilities.EventQueue.new(function(_, delivery)
@@ -67,6 +95,108 @@ function HostAdapter:runtime()
return adapter_records[self].runtime
end
+function HostAdapter:read_text()
+ local lines = self:runtime().api.nvim_buf_get_lines(0, 0, -1, true)
+ if #lines == 0 then
+ lines = { "" }
+ end
+ return domain.TextSnapshot.new(lines)
+end
+
+function HostAdapter:read_buffer()
+ return self:runtime().api.nvim_get_current_buf()
+end
+
+function HostAdapter:read_window()
+ return self:runtime().api.nvim_get_current_win()
+end
+
+function HostAdapter:read_cursor()
+ local position = self:runtime().api.nvim_win_get_cursor(0)
+ return domain.Position.new(position[1], position[2] + 1)
+end
+
+function HostAdapter:read_mode()
+ return self:runtime().api.nvim_get_mode().mode
+end
+
+local function selection_option(runtime)
+ local value = runtime.api.nvim_get_option_value(
+ "selection",
+ { scope = "global" }
+ )
+ if value == "exclusive" then
+ return domain.SelectionOption.EXCLUSIVE
+ end
+ return domain.SelectionOption.INCLUSIVE
+end
+
+function HostAdapter:read_selection()
+ local runtime = self:runtime()
+ local context = domain.ModeContext.from_full_mode(self:read_mode())
+ local kind = context.visual_kind or context.select_kind
+ local option = selection_option(runtime)
+ if kind == nil then
+ return domain.Selection.inactive(option)
+ end
+ local raw_anchor = runtime.fn.getpos("v")
+ local focus = self:read_cursor()
+ local anchor
+ if type(raw_anchor) == "table"
+ and type(raw_anchor[2]) == "number"
+ and raw_anchor[2] > 0
+ and type(raw_anchor[3]) == "number"
+ and raw_anchor[3] > 0
+ then
+ anchor = domain.Position.new(raw_anchor[2], raw_anchor[3])
+ else
+ anchor = focus
+ end
+ return domain.Selection.active(kind, anchor, focus, option)
+end
+
+function HostAdapter:read_count()
+ local runtime = self:runtime()
+ local count = runtime.v.count1
+ if type(count) ~= "number" or count < 1 then
+ count = 1
+ end
+ return domain.Count.new(count)
+end
+
+local function configuration_global(name)
+ if type(name) ~= "string" or name == "" then
+ fail("configuration name must be a nonempty string", 3)
+ end
+ return M.CONFIGURATION_GLOBALS[name] or M.CONFIGURATION_PREFIX .. name
+end
+
+function M.configuration_global(name)
+ return configuration_global(name)
+end
+
+function HostAdapter:configuration_present(name)
+ local global = configuration_global(name)
+ return self:runtime().fn.exists("g:" .. global) == 1
+end
+
+local function normalize_configuration(name, value)
+ if BOOLEAN_CONFIGURATION[name] and type(value) == "number" then
+ return value ~= 0
+ end
+ return value
+end
+
+function HostAdapter:read_configuration(name)
+ local global = configuration_global(name)
+ return normalize_configuration(name, self:runtime().g[global])
+end
+
+function HostAdapter:write_configuration(name, value)
+ local global = configuration_global(name)
+ self:runtime().g[global] = value
+end
+
function HostAdapter:read_encoding()
local runtime = self:runtime()
return runtime.api.nvim_get_option_value(
@@ -86,6 +216,45 @@ function HostAdapter:lowercase(value)
return result
end
+function HostAdapter:read_macro_state()
+ local register = self:runtime().fn.reg_executing()
+ return domain.MacroState.new(register ~= "" and register or nil)
+end
+
+local function fold_open_policies(value)
+ local result = {}
+ for item in tostring(value):gmatch("[^,]+") do
+ if item == "hor" then
+ item = "horizontal"
+ end
+ result[#result + 1] = item
+ end
+ return result
+end
+
+function HostAdapter:read_fold_state()
+ local runtime = self:runtime()
+ local foldopen = runtime.api.nvim_get_option_value(
+ "foldopen",
+ { scope = "global" }
+ )
+ local line = self:read_cursor().line
+ local closed_levels = runtime.fn.foldclosed(line) == -1 and 0 or 1
+ return domain.FoldState.new(
+ fold_open_policies(foldopen),
+ closed_levels
+ )
+end
+
+function HostAdapter:read_time_ms()
+ local runtime = self:runtime()
+ local uv = runtime.uv or runtime.loop
+ if type(uv) ~= "table" or type(uv.hrtime) ~= "function" then
+ fail("HostAdapter runtime must provide a monotonic clock", 2)
+ end
+ return uv.hrtime() / 1000000
+end
+
function HostAdapter:read_pending_operator()
local operator = self:runtime().v.operator
if operator == nil then
@@ -125,6 +294,101 @@ function HostAdapter:set_operator_inclusive(enabled)
end
end
+local function string_bytes(value)
+ local bytes = {}
+ for index = 1, #value do
+ bytes[index] = string.byte(value, index)
+ end
+ return bytes
+end
+
+function HostAdapter:read_input()
+ local runtime = self:runtime()
+ local value = runtime.fn.getcharstr()
+ if type(value) ~= "string" or value == "" then
+ fail("Nvim target input must be a nonempty string", 2)
+ end
+ local bytes = string_bytes(value)
+ if #bytes == 3
+ and bytes[1] == 0x80
+ and bytes[2] == 0xfd
+ and bytes[3] == 0x60
+ then
+ return domain.InputPacket.raw_bytes(bytes)
+ end
+ if #bytes == 1 and bytes[1] == 27 then
+ return domain.InputPacket.special_key("Escape", bytes)
+ end
+ if bytes[1] == 0x80 then
+ local name = type(runtime.fn.keytrans) == "function"
+ and runtime.fn.keytrans(value)
+ or "Special"
+ return domain.InputPacket.special_key(name, value)
+ end
+ return domain.InputPacket.text(value)
+end
+
+function HostAdapter:open_fold(position)
+ position = position and domain.Position.coerce(position) or self:read_cursor()
+ local runtime = self:runtime()
+ if runtime.fn.foldclosed(position.line) == -1 then
+ return false
+ end
+ runtime.api.nvim_cmd({
+ cmd = "normal",
+ bang = true,
+ args = { "zo" },
+ }, {})
+ return true
+end
+
+function HostAdapter:show_prompt(text)
+ if type(text) ~= "string" then
+ fail("prompt must be a string", 2)
+ end
+ self:runtime().api.nvim_echo({ { text } }, false, {})
+end
+
+function HostAdapter:redraw(kind)
+ if kind == "suppressed" then
+ return false
+ end
+ if kind ~= "screen" and kind ~= "full" then
+ fail("redraw kind must be screen, full, or suppressed", 2)
+ end
+ self:runtime().api.nvim_cmd({
+ cmd = "redraw",
+ bang = kind == "full",
+ }, {})
+ return true
+end
+
+local DIAGNOSTIC_LEVELS = {
+ error = "ERROR",
+ warning = "WARN",
+ info = "INFO",
+}
+
+function HostAdapter:emit_diagnostic(level, text)
+ local level_name = DIAGNOSTIC_LEVELS[level]
+ if level_name == nil then
+ fail("diagnostic level must be error, warning, or info", 2)
+ end
+ if type(text) ~= "string" or text == "" then
+ fail("diagnostic text must be a nonempty string", 2)
+ end
+ local record = adapter_records[self]
+ local runtime = record.runtime
+ if type(runtime.notify) ~= "function" then
+ fail("HostAdapter runtime must provide notify", 2)
+ end
+ local levels = type(runtime.log) == "table" and runtime.log.levels or {}
+ runtime.notify(text, levels[level_name], { title = "clever-f" })
+ if record.action_diagnostics ~= nil then
+ record.action_diagnostics[level .. "\0" .. text] = true
+ end
+end
+
local install_dot_bridge
function HostAdapter:register_dot_repeat(payload, callback)
@@ -356,7 +620,7 @@ function HostAdapter:start_timer(delay_ms, callback)
if resource == nil or not resource.active then
return
end
- resource.active = false
+ record.timers[identity] = nil
callback(identity)
end)
if type(timer_id) ~= "number" or timer_id < 0 then
@@ -375,7 +639,7 @@ function HostAdapter:stop_timer(identity)
if resource == nil or not resource.active then
return false
end
- resource.active = false
+ record.timers[identity] = nil
record.runtime.fn.timer_stop(resource.timer_id)
return true
end
@@ -544,13 +808,9 @@ install_dot_bridge = function(adapter)
if registration == nil or registration.callback == nil then
return
end
- local count = runtime.v.count1
- if type(count) ~= "number" or count < 1 then
- count = 1
- end
local outcome = registration.callback(
registration.payload,
- domain.Count.new(count)
+ adapter:read_count()
)
if domain.ActionOutcome.is(outcome) then
adapter:translate_action_outcome(outcome)
@@ -627,11 +887,16 @@ function HostAdapter:deliver_event(name, payload)
end
function HostAdapter:begin_action_transition()
- return adapter_records[self].event_queue:begin_transition()
+ local record = adapter_records[self]
+ record.action_diagnostics = {}
+ return record.event_queue:begin_transition()
end
function HostAdapter:commit_action_transition(token)
- return adapter_records[self].event_queue:commit_transition(token)
+ local record = adapter_records[self]
+ local result = record.event_queue:commit_transition(token)
+ record.action_diagnostics = nil
+ return result
end
local function terminal_cursor_option(runtime)
@@ -724,12 +989,7 @@ function HostAdapter:return_escape()
end
function HostAdapter:emit_action_error(text)
- local runtime = self:runtime()
- if type(runtime.notify) ~= "function" then
- fail("HostAdapter runtime must provide notify", 2)
- end
- local levels = type(runtime.log) == "table" and runtime.log.levels or nil
- runtime.notify(text, levels and levels.ERROR or nil, { title = "clever-f" })
+ return self:emit_diagnostic("error", text)
end
function HostAdapter:translate_action_outcome(outcome)
@@ -741,12 +1001,127 @@ function HostAdapter:translate_action_outcome(outcome)
return M.ActionEffect.ESCAPE
end
if outcome.kind == domain.ActionKind.ERROR then
- self:emit_action_error(outcome.diagnostic)
+ local diagnostics = adapter_records[self].action_diagnostics
+ local key = "error\0" .. outcome.diagnostic
+ if diagnostics == nil or not diagnostics[key] then
+ self:emit_action_error(outcome.diagnostic)
+ end
return M.ActionEffect.ERROR
end
return M.ActionEffect.NONE
end
+local function packed(...)
+ return { n = select("#", ...), ... }
+end
+
+local function invoke_callback(adapter, callback, ...)
+ local arguments = packed(...)
+ local token = adapter:begin_action_transition()
+ local results = packed(pcall(function()
+ local values = packed(callback(unpack_values(arguments, 1, arguments.n)))
+ if domain.ActionOutcome.is(values[1]) then
+ adapter:translate_action_outcome(values[1])
+ end
+ return unpack_values(values, 1, values.n)
+ end))
+ local commit = packed(pcall(adapter.commit_action_transition, adapter, token))
+ if not results[1] then
+ error(results[2], 0)
+ end
+ if not commit[1] then
+ error(commit[2], 0)
+ end
+ return unpack_values(results, 2, results.n)
+end
+
+function HostAdapter:register_action(name, callback)
+ if type(name) ~= "string" or name == "" then
+ fail("action name must be a nonempty string", 2)
+ end
+ if type(callback) ~= "function" then
+ fail("action callback must be a function", 2)
+ end
+ local actions = adapter_records[self].actions
+ if actions[name] ~= nil then
+ fail("action is already registered", 2)
+ end
+ actions[name] = callback
+ return name
+end
+
+function HostAdapter:invoke_action(name, ...)
+ local callback = adapter_records[self].actions[name]
+ if callback == nil then
+ fail("action is not registered", 2)
+ end
+ return invoke_callback(self, callback, ...)
+end
+
+function HostAdapter:invoke_callback(callback, ...)
+ if type(callback) ~= "function" then
+ fail("action callback must be a function", 2)
+ end
+ return invoke_callback(self, callback, ...)
+end
+
+local function mapping_modes(value)
+ if type(value) == "string" then
+ value = { value }
+ end
+ if type(value) ~= "table" or #value == 0 then
+ fail("mapping modes must be a nonempty list", 3)
+ end
+ local result = {}
+ for index, mode in ipairs(value) do
+ if type(mode) ~= "string" or mode == "" then
+ fail("mapping mode must be a nonempty string", 3)
+ end
+ result[index] = mode
+ end
+ return result
+end
+
+function HostAdapter:register_mapping(modes, lhs, action, options)
+ modes = mapping_modes(modes)
+ if type(lhs) ~= "string" or lhs == "" then
+ fail("mapping lhs must be a nonempty string", 2)
+ end
+ if type(action) ~= "string" and type(action) ~= "function" then
+ fail("mapping action must be an action name or function", 2)
+ end
+ options = options or {}
+ if type(options) ~= "table" then
+ fail("mapping options must be a table", 2)
+ end
+ local callback
+ if type(action) == "string" then
+ callback = function()
+ return self:invoke_action(action)
+ end
+ else
+ callback = function(...)
+ return invoke_callback(self, action, ...)
+ end
+ end
+ local native_options = {
+ silent = options.silent == true,
+ remap = options.remap == true,
+ desc = options.desc
+ or ("clever-f " .. (type(action) == "string" and action or lhs)),
+ }
+ self:runtime().keymap.set(modes, lhs, callback, native_options)
+ local identity = next_identity(self, "mapping")
+ adapter_records[self].mappings[identity] = {
+ modes = modes,
+ lhs = lhs,
+ action = action,
+ options = options,
+ callback = callback,
+ }
+ return identity
+end
+
function M.new(options)
return HostAdapter.new(options)
end