From 4acd690d9e1c35d62a006761273d06edd5f98344 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 19 Jan 2025 19:35:37 -0800 Subject: [PATCH] Split param and cells into different files --- lua/argonaut/param.lua | 99 +----------------------------- lua/argonaut/param_cursor_cell.lua | 48 +++++++++++++++ lua/argonaut/param_range_cell.lua | 52 ++++++++++++++++ 3 files changed, 102 insertions(+), 97 deletions(-) create mode 100644 lua/argonaut/param_cursor_cell.lua create mode 100644 lua/argonaut/param_range_cell.lua diff --git a/lua/argonaut/param.lua b/lua/argonaut/param.lua index c260966..53e3b31 100644 --- a/lua/argonaut/param.lua +++ b/lua/argonaut/param.lua @@ -1,100 +1,5 @@ -local ParamCursorCell = {} -ParamCursorCell.__index = ParamCursorCell - -function ParamCursorCell.new(cursor) - local cell = {cursor = cursor} - return setmetatable(cell, ParamCursorCell) -end - -function ParamCursorCell:write(builder) - builder:write(self.cursor:get_value()) -end - -function ParamCursorCell:hit_test(cursor) - if self.cursor == cursor then - return {{char = self.cursor:get_value(), type = 'cursor_cell'}} - end -end - -function ParamCursorCell:hit_search(trace, depth) - local frame = trace[depth] - assert(frame.type == 'cursor_cell') - assert(frame.char == self.cursor:get_value()) - assert(depth == #trace) - - return self -end - -function ParamCursorCell:is_before_cursor(cursor) - return self.cursor < cursor -end - -function ParamCursorCell:is_after_cursor(cursor) - return self.cursor > cursor -end - -function ParamCursorCell:get_start_cursor() - return self.cursor -end - -function ParamCursorCell:get_stop_cursor() - return self.cursor -end - -function ParamCursorCell:is_empty() - return not self.cursor:get_value():match('%S') -end - -local ParamRangeCell = {} -ParamRangeCell.__index = ParamRangeCell - -function ParamRangeCell.new(range) - local cell = {range = range} - return setmetatable(cell, ParamRangeCell) -end - -function ParamRangeCell:write(builder, wrapped) - self.range:write(builder, wrapped) -end - -function ParamRangeCell:hit_test(cursor) - local trace = self.range:hit_test(cursor) - if trace then - table.insert(trace, 1, {type = 'range_cell'}) - return trace - end -end - -function ParamRangeCell:is_before_cursor(cursor) - return self.range.start_cursor < cursor -end - -function ParamRangeCell:is_after_cursor(cursor) - return self.range.stop_cursor > cursor -end - -function ParamRangeCell:hit_search(trace, depth) - local frame = trace[depth] - assert(frame.type == 'range_cell') - - if depth == #trace then - return self - else - return self.range:hit_search(trace, depth + 1) - end -end - -function ParamRangeCell:get_start_cursor() - return self.range.start_cursor -end - -function ParamRangeCell:get_stop_cursor() - return self.range.stop_cursor -end - -function ParamRangeCell:is_empty() - return false -end +ParamCursorCell = require('argonaut.param_cursor_cell') +ParamRangeCell = require('argonaut.param_range_cell') local Param = {} Param.__index = Param diff --git a/lua/argonaut/param_cursor_cell.lua b/lua/argonaut/param_cursor_cell.lua new file mode 100644 index 0000000..7fc9c3b --- /dev/null +++ b/lua/argonaut/param_cursor_cell.lua @@ -0,0 +1,48 @@ +local ParamCursorCell = {} +ParamCursorCell.__index = ParamCursorCell + +function ParamCursorCell.new(cursor) + local cell = {cursor = cursor} + return setmetatable(cell, ParamCursorCell) +end + +function ParamCursorCell:write(builder) + builder:write(self.cursor:get_value()) +end + +function ParamCursorCell:hit_test(cursor) + if self.cursor == cursor then + return {{char = self.cursor:get_value(), type = 'cursor_cell'}} + end +end + +function ParamCursorCell:hit_search(trace, depth) + local frame = trace[depth] + assert(frame.type == 'cursor_cell') + assert(frame.char == self.cursor:get_value()) + assert(depth == #trace) + + return self +end + +function ParamCursorCell:is_before_cursor(cursor) + return self.cursor < cursor +end + +function ParamCursorCell:is_after_cursor(cursor) + return self.cursor > cursor +end + +function ParamCursorCell:get_start_cursor() + return self.cursor +end + +function ParamCursorCell:get_stop_cursor() + return self.cursor +end + +function ParamCursorCell:is_empty() + return not self.cursor:get_value():match('%S') +end + +return ParamCursorCell diff --git a/lua/argonaut/param_range_cell.lua b/lua/argonaut/param_range_cell.lua new file mode 100644 index 0000000..2481294 --- /dev/null +++ b/lua/argonaut/param_range_cell.lua @@ -0,0 +1,52 @@ +local ParamRangeCell = {} +ParamRangeCell.__index = ParamRangeCell + +function ParamRangeCell.new(range) + local cell = {range = range} + return setmetatable(cell, ParamRangeCell) +end + +function ParamRangeCell:write(builder, wrapped) + self.range:write(builder, wrapped) +end + +function ParamRangeCell:hit_test(cursor) + local trace = self.range:hit_test(cursor) + if trace then + table.insert(trace, 1, {type = 'range_cell'}) + return trace + end +end + +function ParamRangeCell:is_before_cursor(cursor) + return self.range.start_cursor < cursor +end + +function ParamRangeCell:is_after_cursor(cursor) + return self.range.stop_cursor > cursor +end + +function ParamRangeCell:hit_search(trace, depth) + local frame = trace[depth] + assert(frame.type == 'range_cell') + + if depth == #trace then + return self + else + return self.range:hit_search(trace, depth + 1) + end +end + +function ParamRangeCell:get_start_cursor() + return self.range.start_cursor +end + +function ParamRangeCell:get_stop_cursor() + return self.range.stop_cursor +end + +function ParamRangeCell:is_empty() + return false +end + +return ParamRangeCell