diff options
| author | Jackson Moore <jacksonmoore@tuta.io> | 2026-09-04 11:39:16 +0200 |
|---|---|---|
| committer | Jackson Moore <jacksonmoore@tuta.io> | 2026-09-04 11:39:16 +0200 |
| commit | cd832008fd96082ac36e32dbde26aefb7147b4c9 (patch) | |
| tree | 806cefad5c516e837ee7aa64d09ce2f6a42ffaf7 /lua/clever_f | |
| parent | ce063ff1737aba8a2da2c2644e03517dd1a318c8 (diff) | |
Implement cursor presentation lease
Diffstat (limited to 'lua/clever_f')
| -rw-r--r-- | lua/clever_f/feedback_service.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lua/clever_f/feedback_service.lua b/lua/clever_f/feedback_service.lua index b915f6a..a06f6d8 100644 --- a/lua/clever_f/feedback_service.lua +++ b/lua/clever_f/feedback_service.lua @@ -6,6 +6,8 @@ local M = {} local FeedbackService = {} FeedbackService.__index = FeedbackService M.FeedbackService = FeedbackService +local CursorPresentationLease = {} +M.CursorPresentationLease = CursorPresentationLease M.DEFAULT_LABEL_GROUP = "CleverFDefaultLabel" M.Priority = { @@ -20,6 +22,7 @@ local OVERLAY_PRIORITIES = { } local service_records = setmetatable({}, { __mode = "k" }) +local cursor_lease_records = setmetatable({}, { __mode = "k" }) local FEATURE_GROUPS = { "CleverFCursor", @@ -85,6 +88,8 @@ local function require_host(host) or type(host.define_highlight_group) ~= "function" or type(host.create_highlight) ~= "function" or type(host.remove_highlight) ~= "function" + or type(host.suppress_cursor_presentation) ~= "function" + or type(host.restore_cursor_presentation) ~= "function" then fail("FeedbackService host must provide highlight groups", 3) end @@ -134,6 +139,59 @@ function FeedbackService.is(value) return type(value) == "table" and service_records[value] ~= nil end +local cursor_lease_metatable = { + __index = function(lease, key) + local method = CursorPresentationLease[key] + if method ~= nil then + return method + end + local record = cursor_lease_records[lease] + if key == "identity" then + return record.identity + end + if key == "active" then + return record.active + end + return nil + end, + __newindex = function() + fail("cursor presentation leases are read-only", 2) + end, + __metatable = "clever_f.feedback_service.CursorPresentationLease", +} + +local function new_cursor_presentation_lease(host) + local lease = setmetatable({}, cursor_lease_metatable) + local identity = host:suppress_cursor_presentation() + cursor_lease_records[lease] = { + host = host, + identity = identity, + active = identity ~= nil, + } + return lease +end + +function CursorPresentationLease.is(value) + return type(value) == "table" and cursor_lease_records[value] ~= nil +end + +function CursorPresentationLease:release() + local record = cursor_lease_records[self] + if record == nil then + fail("cursor presentation lease is invalid", 2) + end + if not record.active then + return false + end + record.host:restore_cursor_presentation(record.identity) + record.active = false + return true +end + +function FeedbackService:create_cursor_presentation_lease() + return new_cursor_presentation_lease(service_records[self].host) +end + function FeedbackService:create_cursor_marker(position, window) position = domain.Position.coerce(position) if window == nil then |
