summaryrefslogtreecommitdiff
path: root/lua/clever_f/destination_engine.lua
blob: 53cb654361db5290832a1d9c91a2a4646f48e5bd (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
local domain = require("clever_f.domain")
local text_topology = require("clever_f.text_topology")

local M = {}
local DestinationEngine = {}
M.DestinationEngine = DestinationEngine

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

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

local engine_metatable = {
  __index = DestinationEngine,
  __newindex = function()
    fail("DestinationEngine values are immutable", 2)
  end,
  __tostring = function()
    return "destination-engine"
  end,
  __metatable = "clever_f.destination_engine.DestinationEngine",
}

function DestinationEngine.new()
  local engine = setmetatable({}, engine_metatable)
  engines[engine] = true
  return engine
end

function DestinationEngine.is(value)
  return type(value) == "table" and engines[value] == true
end

local function calculation_inputs(view, origin, plan, count, first_move)
  if not text_topology.TextView.is(view) then
    fail("destination calculation view must be a TextView", 3)
  end

  origin = domain.Position.coerce(origin)
  if not view:is_valid_cursor_position(origin) then
    fail("destination calculation origin must be a valid cursor position", 3)
  end
  if not domain.ResolvedMotionPlan.is(plan) then
    fail("destination calculation plan must be a ResolvedMotionPlan", 3)
  end
  count = domain.Count.new(count)
  if type(first_move) ~= "boolean" then
    fail("destination calculation first_move must be a Boolean", 3)
  end

  local bounds = view:match_start_bounds(plan.search_scope, origin)
  return {
    view = view,
    origin = origin,
    plan = plan,
    count = count,
    first_move = first_move,
    bounds = bounds,
  }
end

local function candidate_starts(request, origin)
  return request.view:iter_strict(
    origin,
    request.plan.descriptor.direction,
    request.bounds
  )
end

local function regular_destination(request, target_position)
  local descriptor = request.plan.descriptor
  if descriptor.family == domain.Family.FIND then
    return target_position
  end
  if descriptor.direction == domain.Direction.FORWARD then
    return request.view:predecessor(target_position)
  end
  return request.view:successor(target_position)
end

local function target_destination(request, target_position)
  local descriptor = request.plan.descriptor
  if request.plan.endpoint_policy == domain.EndpointPolicy.VISUAL_EXCLUSIVE
    and descriptor.direction == domain.Direction.FORWARD
  then
    if descriptor.family == domain.Family.FIND then
      return request.view:successor(target_position)
    end
    return target_position
  end
  return regular_destination(request, target_position)
end

local function strict_destination(descriptor, destination, origin)
  local comparison = domain.Position.compare(destination, origin)
  if descriptor.direction == domain.Direction.FORWARD then
    return comparison > 0
  end
  return comparison < 0
end

local function acceptable_destination(
  request,
  destination,
  origin,
  allow_till_equality
)
  local descriptor = request.plan.descriptor
  if strict_destination(descriptor, destination, origin) then
    return true
  end
  return descriptor.family == domain.Family.TILL
    and allow_till_equality
    and domain.Position.equal(destination, origin)
end

local function next_destination(request, origin, allow_till_equality)
  local candidates = candidate_starts(request, origin)

  while true do
    local target_position, character = candidates()
    if target_position == nil then
      return nil
    end
    if request.plan.target_plan:matches(
      character,
      target_position,
      request.view
    ) then
      local destination = target_destination(request, target_position)
      if destination ~= nil
        and acceptable_destination(
          request,
          destination,
          origin,
          allow_till_equality
        )
      then
        return destination
      end
    end
  end
end

local function till_equality_allowed(request, successful_steps)
  return request.first_move and successful_steps == 0
end

local function boundary_outcome(request, endpoint, successful_steps)
  if successful_steps > 0 then
    return domain.SearchOutcome.boundary_after_partial(
      endpoint,
      successful_steps
    )
  end
  return domain.SearchOutcome.boundary_before_any(request.origin)
end

function DestinationEngine:calculate(view, origin, plan, count, first_move)
  local request = calculation_inputs(view, origin, plan, count, first_move)
  local current_origin = request.origin
  local successful_steps = 0

  while successful_steps < request.count.value do
    local destination = next_destination(
      request,
      current_origin,
      till_equality_allowed(request, successful_steps)
    )
    if destination == nil then
      break
    end
    current_origin = destination
    successful_steps = successful_steps + 1
  end

  if successful_steps == request.count.value then
    return domain.SearchOutcome.complete(current_origin, successful_steps)
  end
  return boundary_outcome(request, current_origin, successful_steps)
end

function M.new()
  return DestinationEngine.new()
end

function M.calculate(view, origin, plan, count, first_move)
  return DestinationEngine.new():calculate(view, origin, plan, count, first_move)
end

M.resolve = M.calculate
M.search = M.calculate

setmetatable(M, {
  __call = function()
    return DestinationEngine.new()
  end,
})

return M