From a9b2f24d46de742d72b3458fcfc83968a7fc7be0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 1 May 2024 21:50:58 -0700 Subject: [PATCH] Cleanup --- lua/argonaut/types.lua | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/lua/argonaut/types.lua b/lua/argonaut/types.lua index e7467f2..0e9dbe8 100644 --- a/lua/argonaut/types.lua +++ b/lua/argonaut/types.lua @@ -225,18 +225,21 @@ Param.__index = Param function Param.new(pair) local param = { - cursors = {}, - offset = nil, pair = pair, text = '', + cursors = {}, + offset = nil, } return setmetatable(param, Param) end function Param:append(char, cursor) + assert(cursor:is_valid()) + self.text = self.text .. char table.insert(self.cursors, cursor) + if cursor == Cursor.get_current() then self.offset = #self.text end @@ -246,18 +249,31 @@ function Param:is_active() return self.offset ~= nil end -function Param:flush() - self.text = self.text:match('^(.-)%s*$') - if self.offset then - self.offset = math.min(self.offset, #self.text) +function Param:slice(start, stop) + assert(#self.text == #self.cursors) + + local text = '' + local cursors = {} + + for i = start,stop do + text = text .. self.text:sub(i, i) + table.insert(cursors, self.cursors[i]) end - local length_prev = #self.text - self.text = self.text:match('^%s*(.-)$') + self.text = text + self.cursors = cursors + if self.offset then - local length_diff = length_prev - #self.text - self.offset = math.max(self.offset - length_diff, 1) + self.offset = math.min(self.offset, stop) + self.offset = math.max(self.offset - start + 1, 1) end +end + +function Param:strip() + assert(#self.text == #self.cursors) + + self:slice(1, #self.text - #self.text:match('%s*$')) + self:slice(1 + #self.text:match('^%s*'), #self.text) return #self.text > 0 end @@ -281,7 +297,7 @@ end function ParamList:flush() if self.current then - if self.current:flush() then + if self.current:strip() then table.insert(self.parsed, self.current) end self.current = nil