Refactor SelectionList::insert a bit

This commit is contained in:
Maxime Coste 2017-01-01 17:31:47 +00:00
parent 69789d4793
commit cdb2c766a5
3 changed files with 26 additions and 29 deletions

View File

@ -594,21 +594,22 @@ void paste_all(Context& context, NormalParams params)
offsets.push_back(all.length());
}
Vector<BufferCoord> insert_pos;
auto& selections = context.selections();
{
ScopedEdition edition(context);
selections.insert(all, effective_mode, true);
selections.insert(all, effective_mode, &insert_pos);
}
const Buffer& buffer = context.buffer();
Vector<Selection> result;
for (auto& selection : selections)
for (auto& ins_pos : insert_pos)
{
ByteCount pos = 0;
for (auto offset : offsets)
{
result.push_back({ buffer.advance(selection.min(), pos),
buffer.advance(selection.min(), offset-1) });
result.push_back({ buffer.advance(ins_pos, pos),
buffer.advance(ins_pos, offset-1) });
pos = offset;
}
}

View File

@ -513,7 +513,7 @@ BufferCoord prepare_insert(Buffer& buffer, const Selection& sel, InsertMode mode
}
void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted)
Vector<BufferCoord>* out_insert_pos)
{
if (strings.empty())
return;
@ -525,43 +525,39 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
auto& sel = m_selections[index];
sel.anchor() = changes_tracker.get_new_coord(sel.anchor());
kak_assert(m_buffer->is_valid(sel.anchor()));
sel.cursor() = changes_tracker.get_new_coord(sel.cursor());
kak_assert(m_buffer->is_valid(sel.cursor()));
auto pos = prepare_insert(*m_buffer, sel, mode);
changes_tracker.update(*m_buffer, m_timestamp);
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)];
if (mode == InsertMode::Replace)
pos = replace(*m_buffer, sel, str);
else
pos = m_buffer->insert(pos, str);
const auto pos = (mode == InsertMode::Replace) ?
replace(*m_buffer, sel, str)
: m_buffer->insert(prepare_insert(*m_buffer, sel, mode), str);
if (out_insert_pos)
out_insert_pos->push_back(pos);
auto& change = m_buffer->changes_since(m_timestamp).back();
changes_tracker.update(*m_buffer, m_timestamp);
m_timestamp = m_buffer->timestamp();
if (select_inserted or mode == InsertMode::Replace)
if (mode == InsertMode::Replace)
{
if (str.empty())
{
sel.anchor() = sel.cursor() = m_buffer->clamp(pos);
continue;
else
{
// we want min and max from *before* we do any change
auto& min = sel.min();
auto& max = sel.max();
auto& change = m_buffer->changes_since(0).back();
min = change.begin;
max = m_buffer->char_prev(change.end);
}
// we want min and max from *before* we do any change
auto& min = sel.min();
auto& max = sel.max();
min = change.begin;
max = m_buffer->char_prev(change.end);
}
else
else if (not str.empty())
{
if (str.empty())
continue;
auto& change = m_buffer->changes_since(0).back();
sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), change.begin, change.end));
sel.cursor() = m_buffer->clamp(update_insert(sel.cursor(), change.begin, change.end));
}

View File

@ -135,7 +135,7 @@ struct SelectionList
void update_timestamp() { m_timestamp = m_buffer->timestamp(); }
void insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted = false);
Vector<BufferCoord>* out_insert_pos = nullptr);
void erase();
private: