From adfb712e10e834e19c1541d0657fe4af4489be61 Mon Sep 17 00:00:00 2001 From: Jackson Moore Date: Fri, 4 Sep 2026 12:37:29 +0200 Subject: Compare cursor with input landing --- lua/clever_f/feedback_service.lua | 27 +++++++++++++++++++++++++-- tests/run.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lua/clever_f/feedback_service.lua b/lua/clever_f/feedback_service.lua index 0af482a..b8a503d 100644 --- a/lua/clever_f/feedback_service.lua +++ b/lua/clever_f/feedback_service.lua @@ -1,5 +1,6 @@ local domain = require("clever_f.domain") local policy = require("clever_f.policy") +local sequence_state = require("clever_f.sequence_state") local state_transitions = require("clever_f.state_transitions") local text_topology = require("clever_f.text_topology") @@ -101,6 +102,7 @@ local function require_host(host) or type(host.create_highlight) ~= "function" or type(host.remove_highlight) ~= "function" or type(host.read_buffer) ~= "function" + or type(host.read_cursor) ~= "function" or type(host.register_events) ~= "function" or type(host.remove_event_registration) ~= "function" or type(host.supports_cursor_presentation) ~= "function" @@ -145,12 +147,17 @@ function FeedbackService.new(options) options = normalize_options(options) local service = setmetatable({}, FeedbackService) local host = require_host(options.host) + local state = options.state or sequence_state.get() + if not sequence_state.is(state) then + fail("FeedbackService state must be the plugin-global SequenceState", 2) + end service_records[service] = { host = host, policy = require_policy(options.policy or options.policy_service, host), + state = state, transitions = require_transitions( options.transitions or options.state_transitions, - options.state + state ), persistent_requests = {}, owned_finalizer = nil, @@ -458,7 +465,23 @@ function FeedbackService:remove_character_overlays(window) return remove_target_overlays(service_records[self], window) end -function FeedbackService:handle_finalizer_event() +function FeedbackService:cursor_moved_decision() + local record = service_records[self] + local context = record.state.last_input_context + local expected = context and record.state:get_previous_landing(context) or nil + local actual = record.host:read_cursor() + return { + context = context, + expected = expected, + actual = actual, + equal = expected ~= nil and domain.Position.equal(actual, expected), + } +end + +function FeedbackService:handle_finalizer_event(name) + if name == "CursorMoved" then + return self:cursor_moved_decision() + end return false end diff --git a/tests/run.lua b/tests/run.lua index 062f9ec..902d846 100644 --- a/tests/run.lua +++ b/tests/run.lua @@ -6132,6 +6132,33 @@ test("Persistent feedback owns one finalizer set", function() truthy(registrations[third.finalizers.identity].active) end) +test("CursorMoved compares the actual cursor with the last-input landing", function() + local state, transitions = fresh_sequence_state() + local landing = domain.Position.new(2, 4) + transitions:BeginAcquisition("v", "f") + transitions:CommitAcquiredTarget("v", target("a")) + transitions:CommitVisualSuccess("v", landing) + transitions:CommitCommandSuccess("n", domain.Position.new(1, 2), true) + local host = MemoryHost.new({ cursor = landing }) + local feedback = feedback_service.new({ + host = host, + state = state, + transitions = transitions, + }) + + local equal = feedback:handle_finalizer_event("CursorMoved") + same(domain.ModeContext.from_full_mode("v"), equal.context) + same(landing, equal.expected) + same(landing, equal.actual) + truthy(equal.equal) + + host:set_cursor(domain.Position.new(2, 5)) + local different = feedback:handle_finalizer_event("CursorMoved") + same(landing, different.expected) + same(domain.Position.new(2, 5), different.actual) + falsy(different.equal) +end) + for _, item in ipairs(tests) do local ok, failure = xpcall(item.body, debug.traceback) if not ok then -- cgit v1.2.3