1

range checking

This commit is contained in:
Alex Yatskov 2025-01-19 10:35:27 -08:00
parent 56e61a6e7d
commit fd0db3b11b
2 changed files with 35 additions and 11 deletions

View File

@ -119,22 +119,35 @@ function Param:append(cell)
table.insert(self.cells, cell) table.insert(self.cells, cell)
end end
function Param:validate()
local index_begin = self:find_index_begin()
local index_end = self:find_index_end()
for i = index_begin, index_end - 1 do
local cell = self.cells[i]
local next_cell = self.cells[i + 1]
if cell:get_stop_row() ~= next_cell:get_start_row() then
return false
end
end
return true
end
function Param:hit_test(cursor) function Param:hit_test(cursor)
local index_begin = self:find_index_begin() local index_begin = self:find_index_begin()
local index_end = self:find_index_end() local index_end = self:find_index_end()
for i = 1, index_begin - 1, 1 do for i = 1, index_begin - 1, 1 do
local cell = self.cells[i] local cell = self.cells[i]
local trace = cell:hit_test(cursor) if cell:hit_test(cursor) then
if trace then
cursor = cursor:get_next() cursor = cursor:get_next()
end end
end end
for i = #self.cells, index_end + 1, -1 do for i = #self.cells, index_end + 1, -1 do
local cell = self.cells[i] local cell = self.cells[i]
local trace = cell:hit_test(cursor) if cell:hit_test(cursor) then
if trace then
cursor = cursor:get_previous() cursor = cursor:get_previous()
end end
end end

View File

@ -28,9 +28,7 @@ local function find_closest()
if #ranges > 0 then if #ranges > 0 then
table.sort(ranges) table.sort(ranges)
local range = ranges[1] return ranges[1]
range:parse()
return range
end end
end end
@ -51,9 +49,7 @@ local function find_at_cursor(start_cursor, parent_range)
return return
end end
local range = Range.new(start_cursor, stop_cursor, pairing) return Range.new(start_cursor, stop_cursor, pairing)
range:parse()
return range
end end
end end
@ -65,7 +61,11 @@ function Range.new(start_cursor, stop_cursor, pairing)
params = {}, params = {},
} }
return setmetatable(range, Range) setmetatable(range, Range)
if range:parse() then
return range
end
end end
function Range:parse() function Range:parse()
@ -109,6 +109,17 @@ function Range:parse()
end end
append_param() append_param()
return self:validate()
end
function Range:validate()
for _, param in ipairs(self.params) do
if not param:validate() then
return false
end
end
return true
end end
function Range:hit_test(cursor) function Range:hit_test(cursor)