Refactor insert_output command to avoid intermediate vector

This is like a paste with a different source, so the same logic
should work. This means we now correctly fix overflowing selections.

Fixes #4750
This commit is contained in:
Maxime Coste 2022-10-19 20:15:54 +11:00
parent d1ac4dbff3
commit ae6ee02cb2

View File

@ -764,7 +764,6 @@ void insert_output(Context& context, NormalParams params)
auto& selections = context.selections();
auto& buffer = context.buffer();
const size_t old_main = selections.main_index();
Vector<BufferRange> ins_range;
selections.for_each([&](size_t index, Selection& sel) {
selections.set_main_index(index);
@ -772,16 +771,13 @@ void insert_output(Context& context, NormalParams params)
cmdline, context, content(context.buffer(), sel),
ShellManager::Flags::WaitForStdout);
auto& min = sel.min();
auto& max = sel.max();
auto range = insert(buffer, sel, paste_pos(buffer, sel, mode, false), out);
ins_range.push_back(range);
min = range.begin;
max = range.end > range.begin ? buffer.char_prev(range.end) : range.begin;
});
selections.set(ins_range | transform([&buffer](auto& range) {
if (range.empty())
return Selection{range.begin, range.end};
return Selection{range.begin,
buffer.char_prev(range.end)};
}) | gather<Vector>(), old_main);
selections.set_main_index(old_main);
});
}