1
This commit is contained in:
Alex Yatskov 2024-05-01 21:50:58 -07:00
parent 170dccdaa9
commit a9b2f24d46

View File

@ -225,18 +225,21 @@ Param.__index = Param
function Param.new(pair) function Param.new(pair)
local param = { local param = {
cursors = {},
offset = nil,
pair = pair, pair = pair,
text = '', text = '',
cursors = {},
offset = nil,
} }
return setmetatable(param, Param) return setmetatable(param, Param)
end end
function Param:append(char, cursor) function Param:append(char, cursor)
assert(cursor:is_valid())
self.text = self.text .. char self.text = self.text .. char
table.insert(self.cursors, cursor) table.insert(self.cursors, cursor)
if cursor == Cursor.get_current() then if cursor == Cursor.get_current() then
self.offset = #self.text self.offset = #self.text
end end
@ -246,18 +249,31 @@ function Param:is_active()
return self.offset ~= nil return self.offset ~= nil
end end
function Param:flush() function Param:slice(start, stop)
self.text = self.text:match('^(.-)%s*$') assert(#self.text == #self.cursors)
if self.offset then
self.offset = math.min(self.offset, #self.text) local text = ''
local cursors = {}
for i = start,stop do
text = text .. self.text:sub(i, i)
table.insert(cursors, self.cursors[i])
end end
local length_prev = #self.text self.text = text
self.text = self.text:match('^%s*(.-)$') self.cursors = cursors
if self.offset then if self.offset then
local length_diff = length_prev - #self.text self.offset = math.min(self.offset, stop)
self.offset = math.max(self.offset - length_diff, 1) self.offset = math.max(self.offset - start + 1, 1)
end 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 return #self.text > 0
end end
@ -281,7 +297,7 @@ end
function ParamList:flush() function ParamList:flush()
if self.current then if self.current then
if self.current:flush() then if self.current:strip() then
table.insert(self.parsed, self.current) table.insert(self.parsed, self.current)
end end
self.current = nil self.current = nil