summaryrefslogtreecommitdiff
path: root/lua/clever_tee/policy.lua
blob: 94bf6df384bb6231ef9b8e37e6a502c28d9259a5 (plain)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
local case_policy = require("clever_tee.case_policy")
local domain = require("clever_tee.domain")

local M = {}
local PolicyService = {}
PolicyService.__index = PolicyService
M.PolicyService = PolicyService

M.ValueType = {
  BOOLEAN = "boolean",
  STRING = "string",
  STRING_LIST = "string_list",
  OPTIONAL_GROUP_NAME = "optional_group_name",
  NONNEGATIVE_INTEGER = "nonnegative_integer",
  PRESENCE = "presence",
}

M.Sampling = {
  LIVE = "live",
  ACTIVATION = "activation",
  LINK_EVALUATION = "link_evaluation",
}

M.DEFAULT_MAP_SUPPRESSION_SENTINEL = "suppress_default_mappings"

local NO_VALUE = {}
local SCHEMA = {
  search_current_line_only = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  ignore_case = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  smart_case = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  use_migemo = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  fix_key_direction = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  show_prompt = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  chars_match_any_signs = {
    value_type = M.ValueType.STRING,
    default = "",
    sampling = M.Sampling.LIVE,
  },
  mark_cursor = {
    value_type = M.ValueType.BOOLEAN,
    default = true,
    sampling = M.Sampling.LIVE,
  },
  mark_cursor_color = {
    value_type = M.ValueType.OPTIONAL_GROUP_NAME,
    default = NO_VALUE,
    default_target = "Cursor",
    feature_setting = "mark_cursor",
    highlight_group = "CleverTeeCursor",
    sampling = M.Sampling.LINK_EVALUATION,
  },
  hide_cursor_on_cmdline = {
    value_type = M.ValueType.BOOLEAN,
    default = true,
    sampling = M.Sampling.LIVE,
  },
  repeat_timeout_ms = {
    value_type = M.ValueType.NONNEGATIVE_INTEGER,
    default = 0,
    sampling = M.Sampling.LIVE,
  },
  mark_char = {
    value_type = M.ValueType.BOOLEAN,
    default = true,
    sampling = M.Sampling.LIVE,
  },
  mark_char_color = {
    value_type = M.ValueType.OPTIONAL_GROUP_NAME,
    default = NO_VALUE,
    default_target = "CleverTeeDefaultLabel",
    feature_setting = "mark_char",
    highlight_group = "CleverTeeChar",
    sampling = M.Sampling.LINK_EVALUATION,
  },
  highlight_timeout_ms = {
    value_type = M.ValueType.NONNEGATIVE_INTEGER,
    default = 0,
    sampling = M.Sampling.LIVE,
  },
  repeat_last_char_inputs = {
    value_type = M.ValueType.STRING_LIST,
    default = { "\r" },
    sampling = M.Sampling.LIVE,
  },
  mark_direct = {
    value_type = M.ValueType.BOOLEAN,
    default = false,
    sampling = M.Sampling.LIVE,
  },
  mark_direct_color = {
    value_type = M.ValueType.OPTIONAL_GROUP_NAME,
    default = NO_VALUE,
    default_target = "CleverTeeDefaultLabel",
    feature_setting = "mark_direct",
    highlight_group = "CleverTeeDirect",
    sampling = M.Sampling.LINK_EVALUATION,
  },
  clean_labels_eagerly = {
    value_type = M.ValueType.BOOLEAN,
    default = true,
    sampling = M.Sampling.ACTIVATION,
  },
  [M.DEFAULT_MAP_SUPPRESSION_SENTINEL] = {
    value_type = M.ValueType.PRESENCE,
    default = false,
    sampling = M.Sampling.ACTIVATION,
  },
}

local COLOR_SETTINGS = {
  "mark_cursor_color",
  "mark_char_color",
  "mark_direct_color",
}

local function fail(message, level)
  error(message, (level or 1) + 1)
end

local function is_integer(value)
  return type(value) == "number"
    and value > -math.huge
    and value < math.huge
    and value == math.floor(value)
end

local function copy_list(values)
  local result = {}
  for index = 1, #values do
    result[index] = values[index]
  end
  return result
end

local function copy_table(value)
  local result = {}
  for key, item in pairs(value) do
    if type(item) == "table" and domain.type_of(item) == nil then
      result[key] = copy_table(item)
    else
      result[key] = item
    end
  end
  return result
end

local function schema_entry(name)
  local entry = SCHEMA[name]
  if entry == nil then
    fail("unknown policy setting '" .. tostring(name) .. "'", 2)
  end
  return entry
end

local function default_value(entry)
  if entry.default == NO_VALUE then
    return nil
  end
  if type(entry.default) == "table" then
    return copy_table(entry.default)
  end
  return entry.default
end

local function validate_boolean(value, name)
  if type(value) ~= "boolean" then
    fail("policy setting '" .. name .. "' must be a Boolean", 3)
  end
  return value
end

local function validate_string(value, name)
  if type(value) ~= "string" then
    fail("policy setting '" .. name .. "' must be a string", 3)
  end
  return value
end

local function validate_string_list(value, name)
  if type(value) ~= "table" then
    fail("policy setting '" .. name .. "' must be a list of strings", 3)
  end

  local length = #value
  local item_count = 0
  for key, item in pairs(value) do
    if not is_integer(key) or key < 1 or key > length then
      fail("policy setting '" .. name .. "' must be a list of strings", 3)
    end
    if type(item) ~= "string" then
      fail("policy setting '" .. name .. "' must be a list of strings", 3)
    end
    item_count = item_count + 1
  end
  if item_count ~= length then
    fail("policy setting '" .. name .. "' must be a list of strings", 3)
  end
  return copy_list(value)
end

local function validate_optional_group_name(value, name)
  if value ~= nil and (type(value) ~= "string" or value == "") then
    fail("policy setting '" .. name .. "' must be an optional group name", 3)
  end
  return value
end

local function validate_nonnegative_integer(value, name)
  if not is_integer(value) or value < 0 then
    fail("policy setting '" .. name .. "' must be a nonnegative integer", 3)
  end
  return value
end

local VALIDATORS = {
  [M.ValueType.BOOLEAN] = validate_boolean,
  [M.ValueType.STRING] = validate_string,
  [M.ValueType.STRING_LIST] = validate_string_list,
  [M.ValueType.OPTIONAL_GROUP_NAME] = validate_optional_group_name,
  [M.ValueType.NONNEGATIVE_INTEGER] = validate_nonnegative_integer,
}

local function require_provider(provider)
  if type(provider) ~= "table" then
    fail("policy configuration provider must be a table", 2)
  end
  local required = {
    "configuration_present",
    "read_configuration",
    "write_configuration",
  }
  for _, method_name in ipairs(required) do
    if type(provider[method_name]) ~= "function" then
      fail("policy configuration provider is missing " .. method_name, 2)
    end
  end
  return provider
end

function M.setting_names()
  local names = {}
  for name in pairs(SCHEMA) do
    names[#names + 1] = name
  end
  table.sort(names)
  return names
end

function M.schema()
  local result = {}
  for name, entry in pairs(SCHEMA) do
    local public_entry = {
      value_type = entry.value_type,
      sampling = entry.sampling,
      has_default = true,
    }
    local value = default_value(entry)
    if value ~= nil then
      public_entry.default = value
    end
    if entry.default_target ~= nil then
      public_entry.default_target = entry.default_target
      public_entry.feature_setting = entry.feature_setting
      public_entry.highlight_group = entry.highlight_group
    end
    result[name] = public_entry
  end
  return result
end

function M.default(name)
  return default_value(schema_entry(name))
end

function M.defaults()
  local result = {}
  for name, entry in pairs(SCHEMA) do
    local value = default_value(entry)
    if value ~= nil then
      result[name] = value
    end
  end
  return result
end

function PolicyService.new(provider)
  return setmetatable({
    _provider = require_provider(provider),
    _activation = nil,
  }, PolicyService)
end

function M.new(provider)
  return PolicyService.new(provider)
end

setmetatable(M, {
  __call = function(_, provider)
    return PolicyService.new(provider)
  end,
})

function PolicyService:get(name)
  local entry = schema_entry(name)
  if entry.value_type == M.ValueType.PRESENCE then
    return self._provider:configuration_present(name)
  end

  local value
  if self._provider:configuration_present(name) then
    value = self._provider:read_configuration(name)
  else
    value = default_value(entry)
  end
  return VALIDATORS[entry.value_type](value, name)
end

function PolicyService:_get_typed(name, expected_type)
  local entry = schema_entry(name)
  if entry.value_type ~= expected_type then
    fail(
      "policy setting '" .. name .. "' does not have type " .. expected_type,
      2
    )
  end
  return self:get(name)
end

function PolicyService:get_boolean(name)
  return self:_get_typed(name, M.ValueType.BOOLEAN)
end

function PolicyService:get_string(name)
  return self:_get_typed(name, M.ValueType.STRING)
end

function PolicyService:get_string_list(name)
  return self:_get_typed(name, M.ValueType.STRING_LIST)
end

function PolicyService:get_optional_group_name(name)
  return self:_get_typed(name, M.ValueType.OPTIONAL_GROUP_NAME)
end

function PolicyService:get_nonnegative_integer(name)
  return self:_get_typed(name, M.ValueType.NONNEGATIVE_INTEGER)
end

function PolicyService:get_presence(name)
  return self:_get_typed(name, M.ValueType.PRESENCE)
end

function PolicyService:default_maps_suppressed()
  return self:get_presence(M.DEFAULT_MAP_SUPPRESSION_SENTINEL)
end

function PolicyService:capture_activation()
  if self._activation == nil then
    self._activation = {
      install_default_mappings = not self:default_maps_suppressed(),
      clean_labels_eagerly = self:get_boolean("clean_labels_eagerly"),
    }
  end
  return copy_table(self._activation)
end

function PolicyService:evaluate_highlight_links()
  local result = {}
  for _, color_setting in ipairs(COLOR_SETTINGS) do
    local entry = SCHEMA[color_setting]
    local configured_target = self:get_optional_group_name(color_setting)
    result[entry.highlight_group] = {
      enabled = self:get_boolean(entry.feature_setting),
      feature_setting = entry.feature_setting,
      color_setting = color_setting,
      configured_target = configured_target,
      target = configured_target or entry.default_target,
    }
  end
  return result
end

function M.resolve_case_mode(target, ignore_case, smart_case)
  return case_policy.resolve_case_mode(target, ignore_case, smart_case)
end

function PolicyService:case_mode(target)
  return M.resolve_case_mode(
    target,
    self:get_boolean("ignore_case"),
    self:get_boolean("smart_case")
  )
end

function PolicyService:sample_search()
  local current_line_only = self:get_boolean("search_current_line_only")
  return {
    search_current_line_only = current_line_only,
    search_scope = current_line_only
        and domain.SearchScope.CURRENT_LINE
      or domain.SearchScope.BUFFER,
  }
end

function PolicyService:sample_match(target)
  local ignore_case = self:get_boolean("ignore_case")
  local smart_case = self:get_boolean("smart_case")
  return {
    ignore_case = ignore_case,
    smart_case = smart_case,
    use_migemo = self:get_boolean("use_migemo"),
    chars_match_any_signs = self:get_string("chars_match_any_signs"),
    case_mode = M.resolve_case_mode(target, ignore_case, smart_case),
  }
end

function PolicyService:sample_direction()
  return {
    fix_key_direction = self:get_boolean("fix_key_direction"),
  }
end

function PolicyService:sample_acquisition()
  return {
    show_prompt = self:get_boolean("show_prompt"),
    mark_cursor = self:get_boolean("mark_cursor"),
    hide_cursor_on_cmdline = self:get_boolean("hide_cursor_on_cmdline"),
    mark_direct = self:get_boolean("mark_direct"),
  }
end

function PolicyService:sample_direct_preview()
  return {
    ignore_case = self:get_boolean("ignore_case"),
    smart_case = self:get_boolean("smart_case"),
  }
end

function PolicyService:sample_markers()
  return {
    mark_cursor = self:get_boolean("mark_cursor"),
    mark_char = self:get_boolean("mark_char"),
    mark_direct = self:get_boolean("mark_direct"),
  }
end

function PolicyService:sample_timeouts()
  return {
    repeat_timeout_ms = self:get_nonnegative_integer("repeat_timeout_ms"),
    highlight_timeout_ms = self:get_nonnegative_integer("highlight_timeout_ms"),
  }
end

function PolicyService:sample_previous_input()
  return {
    repeat_last_char_inputs = self:get_string_list("repeat_last_char_inputs"),
  }
end

function PolicyService:disable_migemo_for_unsupported_encoding()
  self._provider:write_configuration("use_migemo", false)
end

return M