Make <a-space> throw on invalid index or last selection

This commit is contained in:
Maxime Coste 2017-03-03 17:05:58 +00:00
parent e3cfde6d07
commit 8a1e5d12ac

View File

@ -1710,13 +1710,16 @@ void remove_selection(Context& context, NormalParams p)
{ {
auto& selections = context.selections(); auto& selections = context.selections();
const int index = p.count ? p.count-1 : selections.main_index(); const int index = p.count ? p.count-1 : selections.main_index();
if (selections.size() > 1 and index < selections.size())
{ if (index >= selections.size())
selections.remove(index); throw runtime_error{format("There is not {}th selection", index)};
size_t main_index = selections.main_index(); if (selections.size() == 1)
if (index < main_index or main_index == selections.size()) throw runtime_error{"Cannot remove the last selection"};
selections.set_main_index(main_index - 1);
} 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(); selections.check_invariant();
} }