summaryrefslogtreecommitdiff
path: root/src/selection.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2021-08-21 16:42:08 +1000
committerMaxime Coste <mawww@kakoune.org>2021-08-30 16:14:26 +1000
commit33a80e644ccf19d1059363ac0555148e7bff6e3d (patch)
tree5a70631697ac1ea7af3199f857e0676115f59311 /src/selection.cc
parentbc11f972c53cbf445972e726e9e908f9e1929892 (diff)
Take a function SelectionList::insert to get string to insert
This makes it unnecessary to allocate Vector<String> to insert and allows to remove the insert_pos pointer hack by passing it to the callback.
Diffstat (limited to 'src/selection.cc')
-rw-r--r--src/selection.cc16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/selection.cc b/src/selection.cc
index 350b3acc..a552526d 100644
--- a/src/selection.cc
+++ b/src/selection.cc
@@ -385,12 +385,18 @@ static void fix_overflowing_selections(Vector<Selection>& selections,
}
}
-void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
- Vector<BufferCoord>* out_insert_pos)
+void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode)
{
if (strings.empty())
return;
+ insert([&](size_t index, BufferCoord) {
+ return String::no_copy(strings[std::min(strings.size()-1, index)]);
+ }, mode);
+}
+
+void SelectionList::insert(ContentFunc get_content, InsertMode mode)
+{
update();
Vector<BufferCoord> insert_pos;
@@ -410,11 +416,11 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
kak_assert(m_buffer->is_valid(sel.anchor()) and
m_buffer->is_valid(sel.cursor()));
- const String& str = strings[std::min(index, strings.size()-1)];
-
const auto pos = (mode == InsertMode::Replace) ?
sel.min() : changes_tracker.get_new_coord(insert_pos[index]);
+ String str = get_content(index, pos);
+
if (mode == InsertMode::Replace)
{
auto range = replace(*m_buffer, sel, str);
@@ -432,8 +438,6 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
}
changes_tracker.update(*m_buffer, m_timestamp);
- if (out_insert_pos)
- out_insert_pos->push_back(pos);
}
// We might just have been deleting text if strings were empty,