From 015e16de6df5929d6721b03a050d90de222f5085 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 1 Jan 2025 18:43:57 -0800 Subject: [PATCH] blah --- lua/argonaut/init.lua | 3 ++ lua/argonaut/options.lua | 3 -- lua/argonaut/param.lua | 75 ++++++++++++++++++++++++++++++---------- lua/argonaut/range.lua | 46 +++++++++++++++--------- 4 files changed, 90 insertions(+), 37 deletions(-) diff --git a/lua/argonaut/init.lua b/lua/argonaut/init.lua index b15aa18..c57a0f2 100644 --- a/lua/argonaut/init.lua +++ b/lua/argonaut/init.lua @@ -33,7 +33,10 @@ local function reflow() builder:output(range.start_cursor.row, range.stop_cursor.row) local new_range = Range.find_at_cursor(range.start_cursor) + print('-------- begin ----------------') + dump(trace) new_range:hit_search(trace, 1):set_current() + print('-------- end ----------------') return true end diff --git a/lua/argonaut/options.lua b/lua/argonaut/options.lua index a261c5f..ee75b73 100644 --- a/lua/argonaut/options.lua +++ b/lua/argonaut/options.lua @@ -10,9 +10,6 @@ local options_current = { go = { comma_last = true, }, - lua = { - comma_last = true, - }, } local function setup(opt) diff --git a/lua/argonaut/param.lua b/lua/argonaut/param.lua index 64abf16..1d8412b 100644 --- a/lua/argonaut/param.lua +++ b/lua/argonaut/param.lua @@ -12,6 +12,7 @@ end function ParamCursorCell:hit_test(cursor) if self.cursor == cursor then + print('!!! hit cursor') return {} end end @@ -32,6 +33,10 @@ function ParamCursorCell:is_empty() return not self.cursor:get_value():match('%S') end +function ParamCursorCell:get_type() + return 'cursor' +end + local ParamRangeCell = {} ParamRangeCell.__index = ParamRangeCell @@ -45,11 +50,15 @@ function ParamRangeCell:write(builder, wrapped) end function ParamRangeCell:hit_test(cursor) - return self.range:hit_test(cursor) + local trace = self.range:hit_test(cursor) + if trace then + print('!!! hit range') + return trace + end end function ParamRangeCell:hit_search(trace, depth) - return self.range:hit_search(trace, depth + 1) + return self.range:hit_search(trace, depth) end function ParamRangeCell:get_start_row() @@ -64,6 +73,10 @@ function ParamRangeCell:is_empty() return false end +function ParamRangeCell:get_type() + return 'range' +end + local Param = {} Param.__index = Param @@ -84,32 +97,58 @@ function Param:append(cell) table.insert(self.cells, cell) end -function Param:stripped() - local cells = {} - for i = self:find_index_begin(), self:find_index_end() do - table.insert(cells, self.cells[i]) +function Param:hit_test(cursor) + for _, cell in ipairs(self.cells) do + print(cell:get_type()) end - return Param.new(self.range, self.index, cells) -end - -function Param:hit_test(cursor) - for i, cell in ipairs(self.cells) do + for i = 1, #self.cells do + local cell = self.cells[i] local trace = cell:hit_test(cursor) if trace then - table.insert(trace, 1, i) + local start_index = self:find_index_start() + local stop_index = self:find_index_stop() + + local index = math.min(i, stop_index) + index = math.max(1, index - start_index + 1) + print('lol', i, index, start_index, stop_index) + + + print('hit cell ' .. i .. ' ' .. cell:get_type()) + table.insert(trace, 1, { + index = index, + count = stop_index - start_index + 1, + type = cell:get_type() + }) + return trace end end end function Param:hit_search(trace, depth) - local index = trace[depth] - return self.cells[index]:hit_search(depth) + print('*** PARAM @', depth) + dump(trace[depth]) + + local start_index = self:find_index_start() + local stop_index = self:find_index_stop() + local offset = math.max(0, start_index - 1) + + assert(depth <= #trace) + local frame = trace[depth] + + assert(frame.count == stop_index - start_index + 1) + local index = frame.index + offset + + assert(index <= #self.cells) + local cell = self.cells[index] + + assert(cell:get_type() == frame.type) + return cell:hit_search(trace, depth) end function Param:write(builder, wrapped) - for i = self:find_index_begin(), self:find_index_end() do + for i = self:find_index_start(), self:find_index_stop() do self.cells[i]:write(builder, wrapped) end end @@ -138,7 +177,7 @@ function Param:get_stop_row() end end -function Param:find_index_begin() +function Param:find_index_start() for i = 1, #self.cells do if not self.cells[i]:is_empty() then return i @@ -146,7 +185,7 @@ function Param:find_index_begin() end end -function Param:find_index_end() +function Param:find_index_stop() for i = #self.cells, 1, -1 do if not self.cells[i]:is_empty() then return i @@ -164,7 +203,7 @@ function Param:is_wrapped() end function Param:is_empty() - return self:find_index_begin() == nil + return self:find_index_start() == nil end return Param diff --git a/lua/argonaut/range.lua b/lua/argonaut/range.lua index 2e6924b..7b0c6a0 100644 --- a/lua/argonaut/range.lua +++ b/lua/argonaut/range.lua @@ -74,7 +74,7 @@ function Range:parse() local append_param = function() if param and not param:is_empty() then - table.insert(self.params, param:stripped()) + table.insert(self.params, param) end param = nil end @@ -105,32 +105,46 @@ end function Range:hit_test(cursor) if self:contains(cursor) then - local trace = nil for i, param in pairs(self.params) do - local trace_param = param:hit_test(cursor) - if trace_param then - trace = {i} - for _, trace_param_frame in ipairs(trace_param) do - table.insert(trace, trace_param_frame) + local trace = param:hit_test(cursor) + if trace then + print('hit param ' .. i) + + if #trace > 1 then + print('++++++++++++++++++ do it ++++++++++++') end - break + + table.insert(trace, 1, { + index = i, + count = #self.params, + type = 'range' + }) + + return trace end end - if not trace then - trace = {0} - end - - return trace + return {{ + index = 0, + count = #self.params, + type = 'range' + }} end end function Range:hit_search(trace, depth) - local index = trace[depth] - if index == 0 then + print('*** RANGE @', depth) + dump(trace[depth]) + + local frame = trace[depth] + assert(frame.type == 'range') + assert(frame.count == #self.params) + assert(frame.index <= #self.params) + + if self.index == 0 then return self.start_cursor else - return self.params[index]:hit_search(trace, depth + 1) + return self.params[frame.index]:hit_search(trace, depth + 1) end end