summaryrefslogtreecommitdiff
path: root/lua/clever_f/feedback_service.lua
blob: b915f6a9dd7e97105ee0ceff3b3f26d1fc73f7e9 (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
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

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 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"
  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"
  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
    ),
  }
  return service
end

function FeedbackService.is(value)
  return type(value) == "table" and service_records[value] ~= nil
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")
  return {
    identity = identity,
    window = window,
    group = "CleverFCursor",
    position = position,
  }
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
  local record = service_records[self]
  local removed = record.host:remove_highlight(resource.identity)
  record.transitions:RemoveTemporaryOverlay(resource.identity, resource.window)
  return removed
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 M.new(options)
  return FeedbackService.new(options)
end

setmetatable(M, {
  __call = function(_, options)
    return FeedbackService.new(options)
  end,
})

return M