1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
local domain = require("clever_f.domain")
local policy = require("clever_f.policy")
local state_transitions = require("clever_f.state_transitions")
local M = {}
local FeedbackService = {}
FeedbackService.__index = FeedbackService
M.FeedbackService = FeedbackService
local CursorPresentationLease = {}
M.CursorPresentationLease = CursorPresentationLease
M.DEFAULT_LABEL_GROUP = "CleverFDefaultLabel"
M.Priority = {
HIGH = "high",
ORDINARY = "ordinary",
}
local OVERLAY_PRIORITIES = {
CleverFCursor = M.Priority.HIGH,
CleverFChar = M.Priority.HIGH,
CleverFDirect = M.Priority.ORDINARY,
}
local service_records = setmetatable({}, { __mode = "k" })
local cursor_lease_records = setmetatable({}, { __mode = "k" })
local temporary_release_records = setmetatable({}, { __mode = "k" })
local FEATURE_GROUPS = {
"CleverFCursor",
"CleverFChar",
"CleverFDirect",
}
local DEFAULT_LABEL_DEFINITION = {
guifg = "red",
guibg = "NONE",
gui = {
bold = true,
underline = true,
},
ctermfg = "red",
ctermbg = "NONE",
cterm = {
bold = true,
underline = true,
},
}
local function copy(value)
if type(value) ~= "table" then
return value
end
local result = {}
for key, item in pairs(value) do
result[key] = copy(item)
end
return result
end
function M.default_label_definition()
return copy(DEFAULT_LABEL_DEFINITION)
end
function M.overlay_priority(group)
local priority = OVERLAY_PRIORITIES[group]
if priority == nil then
error("unknown feedback overlay group '" .. tostring(group) .. "'", 2)
end
return priority
end
local function fail(message, level)
error(message, (level or 1) + 1)
end
local function normalize_options(options)
if type(options) ~= "table" then
fail("FeedbackService options must be a table", 3)
end
if options.host == nil then
return { host = options }
end
return options
end
local function require_host(host)
if type(host) ~= "table"
or type(host.read_highlight_group) ~= "function"
or type(host.define_highlight_group) ~= "function"
or type(host.create_highlight) ~= "function"
or type(host.remove_highlight) ~= "function"
or type(host.supports_cursor_presentation) ~= "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
return host
end
local function require_transitions(transitions, state)
transitions = transitions or state_transitions.new(state)
if type(transitions) ~= "table"
or type(transitions.AddTemporaryOverlay) ~= "function"
or type(transitions.RemoveTemporaryOverlay) ~= "function"
then
fail("FeedbackService transitions must manage temporary overlays", 3)
end
return transitions
end
local function require_policy(service, host)
service = service or policy.new(host)
if type(service) ~= "table"
or type(service.evaluate_highlight_links) ~= "function"
or type(service.sample_acquisition) ~= "function"
then
fail("FeedbackService policy must evaluate highlight links", 3)
end
return service
end
function FeedbackService.new(options)
if FeedbackService.is(options) then
return options
end
options = normalize_options(options)
local service = setmetatable({}, FeedbackService)
local host = require_host(options.host)
service_records[service] = {
host = host,
policy = require_policy(options.policy or options.policy_service, host),
transitions = require_transitions(
options.transitions or options.state_transitions,
options.state
),
persistent_requests = {},
}
return service
end
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, suppress)
local lease = setmetatable({}, cursor_lease_metatable)
local identity = suppress and host:suppress_cursor_presentation() or nil
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.active = false
record.host:restore_cursor_presentation(record.identity)
return true
end
function FeedbackService:create_cursor_presentation_lease(enabled)
local record = service_records[self]
if enabled == nil then
enabled = record.policy:sample_acquisition().hide_cursor_on_cmdline
elseif type(enabled) ~= "boolean" then
fail("cursor presentation policy must be a Boolean", 2)
end
local supported = enabled and record.host:supports_cursor_presentation()
return new_cursor_presentation_lease(record.host, supported == true)
end
local function position_list(positions)
if type(positions) ~= "table" then
fail("direct marker positions must be a list", 3)
end
local result = {}
local item_count = 0
for key, position in pairs(positions) do
if type(key) ~= "number"
or key ~= math.floor(key)
or key < 1
or key > #positions
then
fail("direct marker positions must be a list", 3)
end
result[key] = domain.Position.coerce(position)
item_count = item_count + 1
end
if item_count ~= #positions then
fail("direct marker positions must be a list", 3)
end
return result
end
function FeedbackService:create_direct_markers(positions, window)
positions = position_list(positions)
if #positions == 0 then
return nil
end
if window == nil then
fail("direct marker window must identify its host window", 2)
end
local record = service_records[self]
local identity = record.host:create_highlight({
group = "CleverFDirect",
window = window,
positions = positions,
priority = M.overlay_priority("CleverFDirect"),
})
record.transitions:AddTemporaryOverlay(identity, window, "CleverFDirect")
local resource = {
identity = identity,
window = window,
group = "CleverFDirect",
positions = positions,
}
temporary_release_records[resource] = false
return resource
end
function FeedbackService:create_cursor_marker(position, window)
position = domain.Position.coerce(position)
if window == nil then
fail("cursor marker window must identify its host window", 2)
end
local record = service_records[self]
local identity = record.host:create_highlight({
group = "CleverFCursor",
window = window,
position = position,
priority = M.overlay_priority("CleverFCursor"),
})
record.transitions:AddTemporaryOverlay(identity, window, "CleverFCursor")
local resource = {
identity = identity,
window = window,
group = "CleverFCursor",
position = position,
}
temporary_release_records[resource] = false
return resource
end
function FeedbackService:remove_temporary_overlay(resource)
if type(resource) ~= "table" or resource.identity == nil then
fail("temporary overlay resource must identify its highlight", 2)
end
if temporary_release_records[resource] == true then
return false
end
temporary_release_records[resource] = true
local record = service_records[self]
local ok, removed = pcall(
record.host.remove_highlight,
record.host,
resource.identity
)
record.transitions:RemoveTemporaryOverlay(resource.identity, resource.window)
if not ok then
error(removed, 0)
end
return removed
end
function M.persistent_context_eligible(context)
context = domain.ModeContext.from_full_mode(context)
return context.key == "n"
or context.visual_kind ~= nil
or context.select_kind ~= nil
or context.key == "cv"
or context.key == "cvr"
end
function FeedbackService:request_persistent(specification)
if type(specification) ~= "table" then
fail("persistent feedback request must be a table", 2)
end
local context = domain.ModeContext.from_full_mode(specification.context)
if not M.persistent_context_eligible(context) then
fail("persistent feedback request requires an eligible context", 2)
end
if not domain.TargetPlan.is(specification.target_plan) then
fail("persistent feedback request requires a TargetPlan", 2)
end
if not domain.ResolvedMotionPlan.is(specification.motion_plan) then
fail("persistent feedback request requires a ResolvedMotionPlan", 2)
end
local request = {
context = context,
anchor = domain.Position.coerce(specification.anchor),
target_plan = specification.target_plan,
motion_plan = specification.motion_plan,
window = specification.window,
}
local requests = service_records[self].persistent_requests
requests[#requests + 1] = request
return request
end
function FeedbackService:persistent_requests()
local result = {}
for index, request in ipairs(service_records[self].persistent_requests) do
result[index] = request
end
return result
end
function FeedbackService:evaluate_feature_links()
local record = service_records[self]
local rules = record.policy:evaluate_highlight_links()
local results = {}
for _, group in ipairs(FEATURE_GROUPS) do
local rule = rules[group]
if rule.enabled then
if rule.configured_target ~= nil then
record.host:define_highlight_group(
group,
{ link = rule.configured_target },
{ force = true }
)
results[group] = {
group = group,
target = rule.configured_target,
source = "configured",
applied = true,
}
else
local existing = record.host:read_highlight_group(group)
if existing ~= nil then
results[group] = {
group = group,
definition = existing,
source = "colorscheme",
applied = false,
}
else
record.host:define_highlight_group(
group,
{ link = rule.target },
{ default = true }
)
results[group] = {
group = group,
target = rule.target,
source = "fallback",
applied = true,
}
end
end
end
end
return results
end
function FeedbackService:ensure_default_label()
local existing = service_records[self].host:read_highlight_group(
M.DEFAULT_LABEL_GROUP
)
if existing ~= nil then
return {
group = M.DEFAULT_LABEL_GROUP,
definition = existing,
source = "colorscheme",
applied = false,
}
end
local definition = M.default_label_definition()
service_records[self].host:define_highlight_group(
M.DEFAULT_LABEL_GROUP,
definition,
{ default = true }
)
return {
group = M.DEFAULT_LABEL_GROUP,
definition = definition,
source = "fallback",
applied = true,
}
end
function FeedbackService:evaluate_highlights()
return {
default_label = self:ensure_default_label(),
feature_links = self:evaluate_feature_links(),
}
end
function M.new(options)
return FeedbackService.new(options)
end
setmetatable(M, {
__call = function(_, options)
return FeedbackService.new(options)
end,
})
return M
|