Pass a at_end param to BufferChangeListener::on_{insert,erase}

This commit is contained in:
Maxime Coste 2014-05-12 13:59:21 +01:00
parent ddd8f8d392
commit 67a251ffd5
7 changed files with 41 additions and 35 deletions

View File

@ -76,7 +76,7 @@ void Buffer::reload(std::vector<String> lines, time_t fs_timestamp)
// use back coord to simulate the persistance of the last end of line // use back coord to simulate the persistance of the last end of line
// as buffers are expected to never be empty. // as buffers are expected to never be empty.
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_erase(*this, {0,0}, back_coord()); listener->on_erase(*this, {0,0}, back_coord(), true);
m_changes.push_back({ Change::Erase, {0,0}, back_coord(), true }); m_changes.push_back({ Change::Erase, {0,0}, back_coord(), true });
@ -102,7 +102,7 @@ void Buffer::reload(std::vector<String> lines, time_t fs_timestamp)
m_changes.push_back({ Change::Insert, {0,0}, back_coord(), true }); m_changes.push_back({ Change::Insert, {0,0}, back_coord(), true });
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_insert(*this, {0,0}, back_coord()); listener->on_insert(*this, {0,0}, back_coord(), true);
} }
String Buffer::display_name() const String Buffer::display_name() const
@ -523,7 +523,7 @@ ByteCoord Buffer::do_insert(ByteCoord pos, const String& content)
m_changes.push_back({ Change::Insert, begin, end, at_end }); m_changes.push_back({ Change::Insert, begin, end, at_end });
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_insert(*this, begin, end); listener->on_insert(*this, begin, end, at_end);
return begin; return begin;
} }
@ -554,7 +554,7 @@ ByteCoord Buffer::do_erase(ByteCoord begin, ByteCoord end)
m_changes.push_back({ Change::Erase, begin, end, is_end(begin) }); m_changes.push_back({ Change::Erase, begin, end, is_end(begin) });
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_erase(*this, begin, end); listener->on_erase(*this, begin, end, is_end(begin));
return next; return next;
} }

View File

@ -67,9 +67,9 @@ class BufferChangeListener
{ {
public: public:
virtual void on_insert(const Buffer& buffer, virtual void on_insert(const Buffer& buffer,
ByteCoord begin, ByteCoord end) = 0; ByteCoord begin, ByteCoord end, bool at_end) = 0;
virtual void on_erase(const Buffer& buffer, virtual void on_erase(const Buffer& buffer,
ByteCoord begin, ByteCoord end) = 0; ByteCoord begin, ByteCoord end, bool at_end) = 0;
}; };
// A Buffer is a in-memory representation of a file // A Buffer is a in-memory representation of a file

View File

@ -36,14 +36,14 @@ void DynamicSelectionList::check_invariant() const
#endif #endif
} }
void DynamicSelectionList::on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end) void DynamicSelectionList::on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end)
{ {
update_insert(buffer, begin, end); update_insert(begin, end, at_end);
} }
void DynamicSelectionList::on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end) void DynamicSelectionList::on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end)
{ {
update_erase(buffer, begin, end); update_erase(begin, end, at_end);
} }
} }

View File

@ -19,8 +19,8 @@ public:
void check_invariant() const; void check_invariant() const;
private: private:
void on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end) override; void on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end) override;
void on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end) override; void on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end) override;
}; };
} }

View File

@ -1239,18 +1239,18 @@ public:
ModifiedRangesListener(Buffer& buffer) ModifiedRangesListener(Buffer& buffer)
: BufferChangeListener_AutoRegister(buffer) {} : BufferChangeListener_AutoRegister(buffer) {}
void on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end) void on_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end)
{ {
m_ranges.update_insert(buffer, begin, end); m_ranges.update_insert(begin, end, at_end);
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin, auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), begin,
[](ByteCoord c, const Selection& sel) [](ByteCoord c, const Selection& sel)
{ return c < sel.min(); }); { return c < sel.min(); });
m_ranges.insert(it, Selection{ begin, buffer.char_prev(end) }); m_ranges.insert(it, Selection{ begin, buffer.char_prev(end) });
} }
void on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end) void on_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end, bool at_end)
{ {
m_ranges.update_erase(buffer, begin, end); m_ranges.update_erase(begin, end, at_end);
auto pos = std::min(begin, buffer.back_coord()); auto pos = std::min(begin, buffer.back_coord());
auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), pos, auto it = std::upper_bound(m_ranges.begin(), m_ranges.end(), pos,
[](ByteCoord c, const Selection& sel) [](ByteCoord c, const Selection& sel)

View File

@ -18,8 +18,8 @@ namespace
{ {
template<template <bool, bool> class UpdateFunc> template<template <bool, bool> class UpdateFunc>
void on_buffer_change(const Buffer& buffer, SelectionList& sels, void on_buffer_change(SelectionList& sels,
ByteCoord begin, ByteCoord end, LineCount end_line) ByteCoord begin, ByteCoord end, bool at_end, LineCount end_line)
{ {
auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin, auto update_beg = std::lower_bound(sels.begin(), sels.end(), begin,
[](const Selection& s, ByteCoord c) [](const Selection& s, ByteCoord c)
@ -31,20 +31,20 @@ void on_buffer_change(const Buffer& buffer, SelectionList& sels,
if (update_beg != update_only_line_beg) if (update_beg != update_only_line_beg)
{ {
// for the first one, we are not sure if min < begin // for the first one, we are not sure if min < begin
UpdateFunc<false, false>{}(buffer, update_beg->anchor(), begin, end); UpdateFunc<false, false>{}(update_beg->anchor(), begin, end, at_end);
UpdateFunc<false, false>{}(buffer, update_beg->cursor(), begin, end); UpdateFunc<false, false>{}(update_beg->cursor(), begin, end, at_end);
} }
for (auto it = update_beg+1; it < update_only_line_beg; ++it) for (auto it = update_beg+1; it < update_only_line_beg; ++it)
{ {
UpdateFunc<false, true>{}(buffer, it->anchor(), begin, end); UpdateFunc<false, true>{}(it->anchor(), begin, end, at_end);
UpdateFunc<false, true>{}(buffer, it->cursor(), begin, end); UpdateFunc<false, true>{}(it->cursor(), begin, end, at_end);
} }
if (end.line > begin.line) if (end.line > begin.line)
{ {
for (auto it = update_only_line_beg; it != sels.end(); ++it) for (auto it = update_only_line_beg; it != sels.end(); ++it)
{ {
UpdateFunc<true, true>{}(buffer, it->anchor(), begin, end); UpdateFunc<true, true>{}(it->anchor(), begin, end, at_end);
UpdateFunc<true, true>{}(buffer, it->cursor(), begin, end); UpdateFunc<true, true>{}(it->cursor(), begin, end, at_end);
} }
} }
} }
@ -52,8 +52,8 @@ void on_buffer_change(const Buffer& buffer, SelectionList& sels,
template<bool assume_different_line, bool assume_greater_than_begin> template<bool assume_different_line, bool assume_greater_than_begin>
struct UpdateInsert struct UpdateInsert
{ {
void operator()(const Buffer& buffer, ByteCoord& coord, void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
ByteCoord begin, ByteCoord end) const bool at_end) const
{ {
if (assume_different_line) if (assume_different_line)
kak_assert(begin.line < coord.line); kak_assert(begin.line < coord.line);
@ -69,15 +69,21 @@ struct UpdateInsert
template<bool assume_different_line, bool assume_greater_than_begin> template<bool assume_different_line, bool assume_greater_than_begin>
struct UpdateErase struct UpdateErase
{ {
void operator()(const Buffer& buffer, ByteCoord& coord, void operator()(ByteCoord& coord, ByteCoord begin, ByteCoord end,
ByteCoord begin, ByteCoord end) const bool at_end) const
{ {
if (not assume_greater_than_begin and coord < begin) if (not assume_greater_than_begin and coord < begin)
return; return;
if (assume_different_line) if (assume_different_line)
kak_assert(end.line < coord.line); kak_assert(end.line < coord.line);
if (not assume_different_line and coord <= end) if (not assume_different_line and coord <= end)
coord = buffer.clamp(begin); {
if (not at_end)
coord = begin;
else
coord = begin.column ? ByteCoord{begin.line, begin.column-1}
: ByteCoord{begin.line - 1};
}
else else
{ {
if (not assume_different_line and end.line == coord.line) if (not assume_different_line and end.line == coord.line)
@ -93,14 +99,14 @@ struct UpdateErase
} }
void SelectionList::update_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end) void SelectionList::update_insert(ByteCoord begin, ByteCoord end, bool at_end)
{ {
on_buffer_change<UpdateInsert>(buffer, *this, begin, end, begin.line); on_buffer_change<UpdateInsert>(*this, begin, end, at_end, begin.line);
} }
void SelectionList::update_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end) void SelectionList::update_erase(ByteCoord begin, ByteCoord end, bool at_end)
{ {
on_buffer_change<UpdateErase>(buffer, *this, begin, end, end.line); on_buffer_change<UpdateErase>(*this, begin, end, at_end, end.line);
} }
void SelectionList::check_invariant() const void SelectionList::check_invariant() const

View File

@ -61,8 +61,8 @@ struct SelectionList
SelectionList(ByteCoord c) : m_selections{Selection{c,c}} {} SelectionList(ByteCoord c) : m_selections{Selection{c,c}} {}
SelectionList(Selection s) : m_selections{s} {} SelectionList(Selection s) : m_selections{s} {}
void update_insert(const Buffer& buffer, ByteCoord begin, ByteCoord end); void update_insert(ByteCoord begin, ByteCoord end, bool at_end);
void update_erase(const Buffer& buffer, ByteCoord begin, ByteCoord end); void update_erase(ByteCoord begin, ByteCoord end, bool at_end);
void check_invariant() const; void check_invariant() const;