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

local M = {}
local active_root
local active_activation

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

local function build_root(options)
  if composition_root.CompositionRoot.is(options) then
    return options
  end
  if host_adapter.HostAdapter.is(options) then
    return composition_root.new({ host = options })
  end
  options = options or {}
  if type(options) ~= "table" then
    fail("clever-f activation options must be a table", 3)
  end
  if options.host == nil then
    local adapter_options = options.runtime ~= nil
        and { runtime = options.runtime }
      or nil
    options = { host = host_adapter.new(adapter_options) }
  end
  return composition_root.new(options)
end

function M.activate(options)
  if active_root == nil then
    active_root = build_root(options)
    active_activation = active_root:activate()
  end
  return active_activation
end

local function root()
  M.activate()
  return active_root
end

function M.root()
  return root()
end

function M.state()
  return root():state()
end

local function invoke(name)
  local instance = root()
  return instance:host():invoke_action(name)
end

function M.StartFindForward()
  return invoke("StartFindForward")
end

function M.StartFindBackward()
  return invoke("StartFindBackward")
end

function M.StartTillForward()
  return invoke("StartTillForward")
end

function M.StartTillBackward()
  return invoke("StartTillBackward")
end

function M.Reset()
  return invoke("Reset")
end

function M.RepeatSameDirection()
  return invoke("RepeatSameDirection")
end

function M.RepeatOppositeDirection()
  return invoke("RepeatOppositeDirection")
end

local function invoke_direct(callback)
  local instance = root()
  local host = instance:host()
  if type(host.invoke_callback) ~= "function" then
    fail("clever-f host must invoke direct action callbacks", 2)
  end
  return host:invoke_callback(function()
    return callback(instance)
  end)
end

function M.invoke_descriptor(value)
  return invoke_direct(function(instance)
    return instance:invoke_descriptor(value)
  end)
end

function M._diagnostic_full_reset()
  return invoke_direct(function(instance)
    return instance:diagnostic_full_reset()
  end)
end

M.start_find_forward = M.StartFindForward
M.start_find_backward = M.StartFindBackward
M.start_till_forward = M.StartTillForward
M.start_till_backward = M.StartTillBackward
M.reset = M.Reset
M.repeat_same_direction = M.RepeatSameDirection
M.repeat_opposite_direction = M.RepeatOppositeDirection
M.free_form = M.invoke_descriptor

return M