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()); offsets.push_back(all.length());
} }
Vector<BufferCoord> insert_pos;
auto& selections = context.selections(); auto& selections = context.selections();
{ {
ScopedEdition edition(context); ScopedEdition edition(context);
selections.insert(all, effective_mode, true); selections.insert(all, effective_mode, &insert_pos);
} }
const Buffer& buffer = context.buffer(); const Buffer& buffer = context.buffer();
Vector<Selection> result; Vector<Selection> result;
for (auto& selection : selections) for (auto& ins_pos : insert_pos)
{ {
ByteCount pos = 0; ByteCount pos = 0;
for (auto offset : offsets) for (auto offset : offsets)
{ {
result.push_back({ buffer.advance(selection.min(), pos), result.push_back({ buffer.advance(ins_pos, pos),
buffer.advance(selection.min(), offset-1) }); buffer.advance(ins_pos, offset-1) });
pos = offset; 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, void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted) Vector<BufferCoord>* out_insert_pos)
{ {
if (strings.empty()) if (strings.empty())
return; return;
@ -525,43 +525,39 @@ void SelectionList::insert(ConstArrayView<String> strings, InsertMode mode,
auto& sel = m_selections[index]; auto& sel = m_selections[index];
sel.anchor() = changes_tracker.get_new_coord(sel.anchor()); 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()); sel.cursor() = changes_tracker.get_new_coord(sel.cursor());
kak_assert(m_buffer->is_valid(sel.cursor())); kak_assert(m_buffer->is_valid(sel.anchor()) and
m_buffer->is_valid(sel.cursor()));
auto pos = prepare_insert(*m_buffer, sel, mode);
changes_tracker.update(*m_buffer, m_timestamp);
const String& str = strings[std::min(index, strings.size()-1)]; const String& str = strings[std::min(index, strings.size()-1)];
if (mode == InsertMode::Replace) const auto pos = (mode == InsertMode::Replace) ?
pos = replace(*m_buffer, sel, str); replace(*m_buffer, sel, str)
else : m_buffer->insert(prepare_insert(*m_buffer, sel, mode), str);
pos = m_buffer->insert(pos, 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); changes_tracker.update(*m_buffer, m_timestamp);
m_timestamp = m_buffer->timestamp(); m_timestamp = m_buffer->timestamp();
if (select_inserted or mode == InsertMode::Replace) if (mode == InsertMode::Replace)
{ {
if (str.empty()) if (str.empty())
{
sel.anchor() = sel.cursor() = m_buffer->clamp(pos); sel.anchor() = sel.cursor() = m_buffer->clamp(pos);
continue; else
} {
// we want min and max from *before* we do any change // we want min and max from *before* we do any change
auto& min = sel.min(); auto& min = sel.min();
auto& max = sel.max(); auto& max = sel.max();
auto& change = m_buffer->changes_since(0).back();
min = change.begin; min = change.begin;
max = m_buffer->char_prev(change.end); max = m_buffer->char_prev(change.end);
} }
else }
else if (not str.empty())
{ {
if (str.empty()) auto& change = m_buffer->changes_since(0).back();
continue;
sel.anchor() = m_buffer->clamp(update_insert(sel.anchor(), change.begin, change.end)); 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)); 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 update_timestamp() { m_timestamp = m_buffer->timestamp(); }
void insert(ConstArrayView<String> strings, InsertMode mode, void insert(ConstArrayView<String> strings, InsertMode mode,
bool select_inserted = false); Vector<BufferCoord>* out_insert_pos = nullptr);
void erase(); void erase();
private: private: