From 397e266f59dc95b8c6ef6c7e7ebb5d708f73ea37 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 31 Dec 2024 18:56:10 -0800 Subject: [PATCH] Cleanup --- lua/argonaut/param.lua | 159 +++++++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 69 deletions(-) diff --git a/lua/argonaut/param.lua b/lua/argonaut/param.lua index 8aea334..21919fb 100644 --- a/lua/argonaut/param.lua +++ b/lua/argonaut/param.lua @@ -1,43 +1,66 @@ +local ParamCell = {} +ParamCell.__index = ParamCell + +function ParamCell.new(value, type) + assert(type == 'range' or type == 'cursor') + local param_cell = {value = value, type = type} + return setmetatable(param_cell, ParamCell) +end + +function ParamCell:write(builder, wrapped) + if self.type == 'range' then + self.value:write(builder, wrapped) + elseif self.type == 'cursor' then + builder:write(self.value:get_value()) + end +end + +function ParamCell:hit_test(cursor) + if self.type == 'range' then + return self.value:hit_test(cursor) + elseif self.type == 'cursor' and self.value == cursor then + return {} + end +end + +function ParamCell:hit_search(trace, depth) + if self.type == 'range' then + return self.value:hit_search(trace, depth + 1) + elseif self.type == 'cursor' then + return self.value + end +end + +function ParamCell:get_start_row() + if self.type == 'range' then + return self.value.start_cursor.row + elseif self.type == 'cursor' then + return self.value.row + end +end + +function ParamCell:get_stop_row() + if self.type == 'range' then + return self.value.stop_cursor.row + elseif self.type == 'cursor' then + return self.value.row + end +end + +function ParamCell:is_empty() + return self.type == 'cursor' and not self.value:get_value():match('%S') +end + local Param = {} Param.__index = Param -local function is_empty_cell(cell) - return cell.type == 'cursor' and not cell.value:get_value():match('%S') -end - -local function get_param_start_row(param) - local cell = param.cells[1] - if cell.type == 'range' then - return cell.value.start_cursor.row - elseif cell.type == 'cursor' then - return cell.value.row - end -end - -local function get_param_stop_row(param) - local cell = param.cells[#param.cells] - if cell.type == 'range' then - return cell.value.stop_cursor.row - elseif cell.type == 'cursor' then - return cell.value.row - end -end - function Param.new(range, index, cells) - local param = { - range = range, - index = index, - cells = cells or {}, - } - + local param = {range = range, index = index, cells = cells or {}} return setmetatable(param, Param) end function Param:append(value, type) - table.insert(self.cells, { - value = value, - type = type, - }) + table.insert(self.cells, ParamCell.new(value, type)) end function Param:stripped() @@ -45,25 +68,15 @@ function Param:stripped() for i = self:find_index_begin(), self:find_index_end() do table.insert(cells, self.cells[i]) end + return Param.new(self.range, self.index, cells) end function Param:hit_test(cursor) for i, cell in ipairs(self.cells) do - local trace = nil - if cell.type == 'range' then - local trace_param = cell.value:hit_test(cursor) - if trace_param then - trace = {i} - for _, trace_param_frame in ipairs(trace_param) do - table.insert(trace, trace_param_frame) - end - end - elseif cell.type == 'cursor' and cell.value == cursor then - trace = {i} - end - + local trace = cell:hit_test(cursor) if trace then + table.insert(trace, 1, i) return trace end end @@ -71,43 +84,42 @@ end function Param:hit_search(trace, depth) local index = trace[depth] - local cell = self.cells[index] - if cell.type == 'range' then - return cell.value:hit_search(trace, depth + 1) - elseif cell.type == 'cursor' then - return cell.value - end + return self.cells[index]:hit_search(depth) end function Param:write(builder, wrapped) for i = self:find_index_begin(), self:find_index_end() do - local cell = self.cells[i] - if cell.type == 'range' then - cell.value:write(builder, wrapped) - elseif cell.type == 'cursor' then - builder:write(cell.value:get_value()) - end - end -end - -function Param:is_wrapped() - local previous_param = self:get_previous() - if previous_param then - return get_param_stop_row(previous_param) ~= get_param_start_row(self) - else - return self.range.start_cursor.row ~= get_param_start_row(self) + self.cells[i]:write(builder, wrapped) end end function Param:get_previous() if self.index > 1 then - return self.range.params[self.index-1] + return self.range.params[self.index - 1] + end +end + +function Param:get_next() + if self.index < #self.range.params then + return self.range.params[self.index + 1] + end +end + +function Param:get_start_row() + if #self.cells > 0 then + return self.cells[1]:get_start_row() + end +end + +function Param:get_stop_row() + if #self.cells > 0 then + return self.cells[#self.cells]:get_stop_row() end end function Param:find_index_begin() for i = 1, #self.cells do - if not is_empty_cell(self.cells[i]) then + if not self.cells[i]:is_empty() then return i end end @@ -115,12 +127,21 @@ end function Param:find_index_end() for i = #self.cells, 1, -1 do - if not is_empty_cell(self.cells[i]) then + if not self.cells[i]:is_empty() then return i end end end +function Param:is_wrapped() + local previous_param = self:get_previous() + if previous_param then + return previous_param:get_stop_row() ~= self:get_start_row() + else + return self.range.start_cursor.row ~= self:get_start_row() + end +end + function Param:is_empty() return self:find_index_begin() == nil end