Merge remote-tracking branch 'eraserhd/selection-list-cleanup'

This commit is contained in:
Maxime Coste 2019-11-09 08:50:09 +11:00
commit d85259f801
4 changed files with 26 additions and 36 deletions

View File

@ -2337,7 +2337,7 @@ const CommandDesc select_cmd = {
{ {
auto& buffer = context.buffer(); auto& buffer = context.buffer();
const size_t timestamp = parser.get_switch("timestamp").map(str_to_int_ifp).cast<size_t>().value_or(buffer.timestamp()); const size_t timestamp = parser.get_switch("timestamp").map(str_to_int_ifp).cast<size_t>().value_or(buffer.timestamp());
context.selections_write_only() = selection_list_from_string(buffer, parser.positionals_from(0), timestamp); context.selections_write_only() = selection_list_from_strings(buffer, parser.positionals_from(0), timestamp, 0);
} }
}; };

View File

@ -1774,20 +1774,7 @@ SelectionList read_selections_from_register(char reg, Context& context)
const size_t timestamp = str_to_int(desc[1]); const size_t timestamp = str_to_int(desc[1]);
size_t main = str_to_int(desc[2]); size_t main = str_to_int(desc[2]);
if (timestamp > buffer.timestamp()) return selection_list_from_strings(buffer, content | skip(1), timestamp, main);
throw runtime_error{"register '{}' refers to an invalid timestamp"};
auto sels = content | skip(1) | transform(selection_from_string) | gather<Vector<Selection>>();
sort_selections(sels, main);
merge_overlapping_selections(sels, main);
if (timestamp < buffer.timestamp())
update_selections(sels, main, buffer, timestamp);
else
clamp_selections(sels, buffer);
SelectionList res{buffer, std::move(sels)};
res.set_main_index(main);
return res;
} }
enum class CombineOp enum class CombineOp

View File

@ -27,13 +27,6 @@ SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list, size_t time
SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list) SelectionList::SelectionList(Buffer& buffer, Vector<Selection> list)
: SelectionList(buffer, std::move(list), buffer.timestamp()) {} : SelectionList(buffer, std::move(list), buffer.timestamp()) {}
SelectionList::SelectionList(SelectionList::UnsortedTag, Buffer& buffer, Vector<Selection> list, size_t timestamp, size_t main)
: m_selections(std::move(list)), m_buffer(&buffer), m_timestamp(timestamp)
{
sort_and_merge_overlapping();
check_invariant();
}
void SelectionList::remove(size_t index) void SelectionList::remove(size_t index)
{ {
m_selections.erase(begin() + index); m_selections.erase(begin() + index);
@ -518,14 +511,4 @@ Selection selection_from_string(StringView desc)
return Selection{anchor, cursor}; return Selection{anchor, cursor};
} }
SelectionList selection_list_from_string(Buffer& buffer, ConstArrayView<String> descs, size_t timestamp)
{
if (descs.empty())
throw runtime_error{"empty selection description"};
auto sels = descs | transform([&](auto&& d) { auto s = selection_from_string(d); clamp(s, buffer); return s; })
| gather<Vector<Selection>>();
return {SelectionList::UnsortedTag{}, buffer, std::move(sels), timestamp, 0};
}
} }

View File

@ -94,9 +94,6 @@ struct SelectionList
SelectionList(Buffer& buffer, Vector<Selection> s); SelectionList(Buffer& buffer, Vector<Selection> s);
SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp); SelectionList(Buffer& buffer, Vector<Selection> s, size_t timestamp);
struct UnsortedTag {};
SelectionList(UnsortedTag, Buffer& buffer, Vector<Selection> s, size_t timestamp, size_t main);
void update(bool merge = true); void update(bool merge = true);
void check_invariant() const; void check_invariant() const;
@ -163,7 +160,30 @@ Vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp);
String selection_to_string(const Selection& selection); String selection_to_string(const Selection& selection);
String selection_list_to_string(const SelectionList& selection); String selection_list_to_string(const SelectionList& selection);
Selection selection_from_string(StringView desc); Selection selection_from_string(StringView desc);
SelectionList selection_list_from_string(Buffer& buffer, ConstArrayView<String> descs, size_t timestamp);
template<class StringArray>
SelectionList selection_list_from_strings(Buffer& buffer, StringArray&& descs, size_t timestamp, size_t main)
{
if (timestamp > buffer.timestamp())
throw runtime_error{format("invalid timestamp '{}'", timestamp)};
auto sels = descs | transform(selection_from_string) | gather<Vector<Selection>>();
if (sels.empty())
throw runtime_error{"empty selection description"};
if (main >= sels.size())
throw runtime_error{"invalid main selection index"};
sort_selections(sels, main);
merge_overlapping_selections(sels, main);
if (timestamp < buffer.timestamp())
update_selections(sels, main, buffer, timestamp);
else
clamp_selections(sels, buffer);
SelectionList res{buffer, std::move(sels)};
res.set_main_index(main);
return res;
}
} }