Made get_selections a bit more flexible.

This commit is contained in:
Adam Harrison 2021-06-11 18:51:14 -04:00
parent b5cbe3a2fb
commit 0210264552
1 changed files with 24 additions and 29 deletions

View File

@ -173,7 +173,7 @@ end
local function selection_iterator(invariant, idx) local function selection_iterator(invariant, idx)
local target = invariant[3] and (idx*4 - 7) or (idx*4 + 1) local target = invariant[3] and (idx*4 - 7) or (idx*4 + 1)
if target > #invariant[1] or target <= 0 then return end if target > #invariant[1] or target <= 0 or (type(invariant[3]) == "number" and invariant[3] ~= idx - 1) then return end
if invariant[2] then if invariant[2] then
return idx+(invariant[3] and -1 or 1), sort_positions(unpack(invariant[1], target, target+4)) return idx+(invariant[3] and -1 or 1), sort_positions(unpack(invariant[1], target, target+4))
else else
@ -181,8 +181,11 @@ local function selection_iterator(invariant, idx)
end end
end end
function Doc:get_selections(sort_intra, reverse) -- If idx_reverse is true, it'll reverse iterate. If nil, or false, regular iterate.
return selection_iterator, { self.selections, sort_intra, reverse }, reverse and (#self.selections / 4) + 1 or 0 -- If a number, runs for exactly that iteration.
function Doc:get_selections(sort_intra, idx_reverse)
return selection_iterator, { self.selections, sort_intra, idx_reverse },
idx_reverse == true and ((#self.selections / 4) + 1) or ((idx_reverse or -1)+1)
end end
-- End of cursor seciton. -- End of cursor seciton.
@ -366,8 +369,7 @@ end
function Doc:text_input(text, idx) function Doc:text_input(text, idx)
for sidx, line1, col1, line2, col2 in self:get_selections(true) do for sidx, line1, col1, line2, col2 in self:get_selections(true, idx) do
if not idx or idx == sidx then
if line1 ~= line2 or col1 ~= col2 then if line1 ~= line2 or col1 ~= col2 then
self:delete_to_cursor(sidx) self:delete_to_cursor(sidx)
end end
@ -375,7 +377,6 @@ function Doc:text_input(text, idx)
self:move_to_cursor(sidx, #text) self:move_to_cursor(sidx, #text)
end end
end end
end
function Doc:replace(fn) function Doc:replace(fn)
@ -398,8 +399,7 @@ end
function Doc:delete_to_cursor(idx, ...) function Doc:delete_to_cursor(idx, ...)
for sidx, line1, col1, line2, col2 in self:get_selections(true) do for sidx, line1, col1, line2, col2 in self:get_selections(true, idx) do
if not idx or sidx == idx then
if line1 ~= line2 or col1 ~= col2 then if line1 ~= line2 or col1 ~= col2 then
self:remove(line1, col1, line2, col2) self:remove(line1, col1, line2, col2)
else else
@ -409,29 +409,24 @@ function Doc:delete_to_cursor(idx, ...)
end end
self:set_selections(sidx, line1, col1) self:set_selections(sidx, line1, col1)
end end
end
self:merge_cursors(idx) self:merge_cursors(idx)
end end
function Doc:delete_to(...) return self:delete_to(nil, ...) end function Doc:delete_to(...) return self:delete_to(nil, ...) end
function Doc:move_to_cursor(idx, ...) function Doc:move_to_cursor(idx, ...)
for sidx, line, col in self:get_selections() do for sidx, line, col in self:get_selections(false, idx) do
if not idx or sidx == idx then
self:set_selections(sidx, self:position_offset(line, col, ...)) self:set_selections(sidx, self:position_offset(line, col, ...))
end end
end
self:merge_cursors(idx) self:merge_cursors(idx)
end end
function Doc:move_to(...) return self:move_to_cursor(nil, ...) end function Doc:move_to(...) return self:move_to_cursor(nil, ...) end
function Doc:select_to_cursor(idx, ...) function Doc:select_to_cursor(idx, ...)
for sidx, line, col, line2, col2 in self:get_selections() do for sidx, line, col, line2, col2 in self:get_selections(false, idx) do
if not idx or idx == sidx then
line, col = self:position_offset(line, col, ...) line, col = self:position_offset(line, col, ...)
self:set_selections(sidx, line, col, line2, col2) self:set_selections(sidx, line, col, line2, col2)
end end
end
self:merge_cursors(idx) self:merge_cursors(idx)
end end
function Doc:select_to(...) return self:select_to_cursor(nil, ...) end function Doc:select_to(...) return self:select_to_cursor(nil, ...) end