Ensure main selection index is correct directly in SelectionList::remove

This commit is contained in:
Maxime Coste 2017-03-03 18:47:11 +00:00
parent 8a1e5d12ac
commit 6759511b9e
3 changed files with 8 additions and 4 deletions

View File

@ -1717,9 +1717,6 @@ void remove_selection(Context& context, NormalParams p)
throw runtime_error{"Cannot remove the last selection"};
selections.remove(index);
size_t main_index = selections.main_index();
if (index < main_index or main_index == selections.size())
selections.set_main_index(main_index - 1);
selections.check_invariant();
}

View File

@ -36,6 +36,13 @@ SelectionList::SelectionList(Buffer& buffer, Vector<Selection> s, size_t timesta
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> s)
: SelectionList(buffer, std::move(s), buffer.timestamp()) {}
void SelectionList::remove(size_t index)
{
m_selections.erase(begin() + index);
if (index < m_main or m_main == m_selections.size())
--m_main;
}
namespace
{

View File

@ -130,7 +130,7 @@ struct SelectionList
const_iterator begin() const { return m_selections.begin(); }
const_iterator end() const { return m_selections.end(); }
void remove(size_t index) { m_selections.erase(begin() + index); }
void remove(size_t index);
size_t size() const { return m_selections.size(); }