summaryrefslogtreecommitdiff
path: root/lua/clever_f/host_adapter.lua
blob: 747d9b87f2762f184f80e72eb515b9860c3942b6 (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
local domain = require("clever_f.domain")

local M = {}
local HostAdapter = {}
HostAdapter.__index = HostAdapter
M.HostAdapter = HostAdapter

M.ActionEffect = {
  NONE = "none",
  ESCAPE = "escape",
  ERROR = "error",
}

local adapter_records = setmetatable({}, { __mode = "k" })

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

local function current_runtime(options)
  if type(options) == "table" and options.runtime ~= nil then
    return options.runtime
  end
  if options ~= nil and options ~= HostAdapter then
    return options
  end
  return rawget(_G, "vim")
end

local function require_runtime(runtime)
  if type(runtime) ~= "table" or type(runtime.api) ~= "table" then
    fail("HostAdapter requires the Nvim Lua runtime", 3)
  end
  return runtime
end

function HostAdapter.new(options)
  if HostAdapter.is(options) then
    return options
  end
  local adapter = setmetatable({}, HostAdapter)
  adapter_records[adapter] = {
    runtime = require_runtime(current_runtime(options)),
    next_identity = 1,
    highlights = {},
  }
  return adapter
end

function HostAdapter.is(value)
  return type(value) == "table" and adapter_records[value] ~= nil
end

function HostAdapter:runtime()
  return adapter_records[self].runtime
end

local function next_identity(adapter, prefix)
  local record = adapter_records[adapter]
  local identity = prefix .. "-" .. tostring(record.next_identity)
  record.next_identity = record.next_identity + 1
  return identity
end

local function highlight_exists(runtime, name)
  if type(runtime.fn) == "table" and type(runtime.fn.hlexists) == "function" then
    return runtime.fn.hlexists(name) == 1
  end
  local definition = runtime.api.nvim_get_hl(0, {
    name = name,
    link = true,
    create = false,
  })
  return next(definition) ~= nil
end

function HostAdapter:read_highlight_group(name)
  if type(name) ~= "string" or name == "" then
    fail("highlight group name must be a nonempty string", 2)
  end
  local runtime = self:runtime()
  if not highlight_exists(runtime, name) then
    return nil
  end
  return runtime.api.nvim_get_hl(0, {
    name = name,
    link = true,
    create = false,
  })
end

local function native_highlight_definition(definition, options)
  if type(definition) ~= "table" then
    fail("highlight group definition must be a table", 3)
  end
  options = options or {}
  if type(options) ~= "table" then
    fail("highlight group options must be a table", 3)
  end
  local native = {}
  for key, value in pairs(definition) do
    if key ~= "guifg" and key ~= "guibg" and key ~= "gui" then
      native[key] = value
    end
  end
  if definition.guifg ~= nil then
    native.fg = definition.guifg
  end
  if definition.guibg ~= nil then
    native.bg = definition.guibg
  end
  for key, value in pairs(definition.gui or {}) do
    native[key] = value
  end
  if options.default ~= nil then
    native.default = options.default
  end
  if options.force ~= nil then
    native.force = options.force
  end
  return native
end

function HostAdapter:define_highlight_group(name, definition, options)
  if type(name) ~= "string" or name == "" then
    fail("highlight group name must be a nonempty string", 2)
  end
  options = options or {}
  local runtime = self:runtime()
  if options.default and highlight_exists(runtime, name) then
    return false
  end
  runtime.api.nvim_set_hl(
    0,
    name,
    native_highlight_definition(definition, options)
  )
  return true
end

local function overlay_positions(specification)
  local positions = specification.positions
  if positions == nil and specification.position ~= nil then
    positions = { specification.position }
  end
  if type(positions) ~= "table" then
    fail("highlight positions must be a list", 3)
  end
  local native = {}
  for index, position in ipairs(positions) do
    position = domain.Position.coerce(position)
    native[index] = { position.line, position.byte_column }
  end
  if #native == 0 then
    native[1] = { 0 }
  end
  return native
end

local function overlay_priority(value)
  if value == "high" then
    return 100
  end
  if value == "ordinary" or value == nil then
    return 10
  end
  if type(value) == "number" then
    return value
  end
  fail("highlight priority must be high, ordinary, or numeric", 3)
end

function HostAdapter:create_highlight(specification)
  if type(specification) ~= "table" then
    fail("highlight specification must be a table", 2)
  end
  if type(specification.group) ~= "string" or specification.group == "" then
    fail("highlight group must be a nonempty string", 2)
  end
  if specification.window == nil then
    fail("highlight window must identify its Nvim window", 2)
  end
  local record = adapter_records[self]
  local identity = specification.identity or next_identity(self, "highlight")
  if record.highlights[identity] ~= nil then
    fail("highlight identity is already active", 2)
  end
  local match_id = record.runtime.fn.matchaddpos(
    specification.group,
    overlay_positions(specification),
    overlay_priority(specification.priority),
    -1,
    { window = specification.window }
  )
  if type(match_id) ~= "number" or match_id < 0 then
    fail("Nvim could not create the window-local highlight", 2)
  end
  record.highlights[identity] = {
    match_id = match_id,
    window = specification.window,
  }
  return identity
end

function HostAdapter:remove_highlight(identity)
  local record = adapter_records[self]
  local resource = record.highlights[identity]
  if resource == nil then
    return false
  end
  record.highlights[identity] = nil
  record.runtime.fn.matchdelete(resource.match_id, resource.window)
  return true
end

local function escape_key(runtime)
  if type(runtime.keycode) == "function" then
    return runtime.keycode("<Esc>")
  end
  if type(runtime.api.nvim_replace_termcodes) == "function" then
    return runtime.api.nvim_replace_termcodes("<Esc>", true, false, true)
  end
  return string.char(27)
end

function HostAdapter:return_escape()
  local runtime = self:runtime()
  if type(runtime.api.nvim_feedkeys) ~= "function" then
    fail("HostAdapter runtime must provide nvim_feedkeys", 2)
  end
  runtime.api.nvim_feedkeys(escape_key(runtime), "n", false)
end

function HostAdapter:emit_action_error(text)
  local runtime = self:runtime()
  if type(runtime.notify) ~= "function" then
    fail("HostAdapter runtime must provide notify", 2)
  end
  local levels = type(runtime.log) == "table" and runtime.log.levels or nil
  runtime.notify(text, levels and levels.ERROR or nil, { title = "clever-f" })
end

function HostAdapter:translate_action_outcome(outcome)
  if not domain.ActionOutcome.is(outcome) then
    fail("host action translation requires an ActionOutcome", 2)
  end
  if outcome.kind == domain.ActionKind.ESCAPE then
    self:return_escape()
    return M.ActionEffect.ESCAPE
  end
  if outcome.kind == domain.ActionKind.ERROR then
    self:emit_action_error(outcome.diagnostic)
    return M.ActionEffect.ERROR
  end
  return M.ActionEffect.NONE
end

function M.new(options)
  return HostAdapter.new(options)
end

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

return M