BufferChangeListener: pass buffer to on_{inser,erase}

This commit is contained in:
Maxime Coste 2013-06-01 00:48:46 +02:00
parent 8ddeb141c8
commit 3453ebbd52
8 changed files with 41 additions and 40 deletions

View File

@ -518,7 +518,7 @@ void Buffer::do_insert(const BufferCoord& pos, const String& content)
} }
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_insert(begin, end); listener->on_insert(*this, begin, end);
} }
void Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end) void Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
@ -543,7 +543,7 @@ void Buffer::do_erase(const BufferCoord& begin, const BufferCoord& end)
m_lines[i].start -= length; m_lines[i].start -= length;
for (auto listener : m_change_listeners) for (auto listener : m_change_listeners)
listener->on_erase(begin, end); listener->on_erase(*this, begin, end);
} }
void Buffer::apply_modification(const Modification& modification) void Buffer::apply_modification(const Modification& modification)

View File

@ -79,8 +79,8 @@ private:
class BufferChangeListener class BufferChangeListener
{ {
public: public:
virtual void on_insert(const BufferCoord& begin, const BufferCoord& end) = 0; virtual void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) = 0;
virtual void on_erase(const BufferCoord& begin, const BufferCoord& end) = 0; virtual void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end) = 0;
}; };
// A Buffer is a in-memory representation of a file // A Buffer is a in-memory representation of a file

View File

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

View File

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

View File

@ -364,26 +364,26 @@ public:
ModifiedRangesListener(Buffer& buffer) ModifiedRangesListener(Buffer& buffer)
: BufferChangeListener_AutoRegister(buffer) {} : BufferChangeListener_AutoRegister(buffer) {}
void on_insert(const BufferCoord& begin, const BufferCoord& end) void on_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{ {
m_ranges.update_insert(begin, end); m_ranges.update_insert(buffer, begin, 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,
[](const BufferCoord& c, const Selection& sel) [](const BufferCoord& c, const Selection& sel)
{ return c < sel.min().coord(); }); { return c < sel.min(); });
m_ranges.emplace(it, registry().iterator_at(begin), m_ranges.emplace(it, registry().iterator_at(begin),
utf8::previous(registry().iterator_at(end))); utf8::previous(registry().iterator_at(end)));
} }
void on_erase(const BufferCoord& begin, const BufferCoord& end) void on_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{ {
m_ranges.update_erase(begin, end); m_ranges.update_erase(buffer, begin, end);
BufferIterator pos{registry(), begin}; BufferIterator pos{registry(), begin};
if (pos >= buffer().end()) if (pos >= buffer.end())
pos = utf8::previous(buffer().end()); pos = utf8::previous(buffer.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,
[](const BufferCoord& c, const Selection& sel) [](const BufferCoord& c, const Selection& sel)
{ return c < sel.min().coord(); }); { return c < sel.min(); });
m_ranges.emplace(it, pos, pos); m_ranges.emplace(it, pos, pos);
} }
SelectionList& ranges() { return m_ranges; } SelectionList& ranges() { return m_ranges; }

View File

@ -384,7 +384,7 @@ private:
m_option->get<std::vector<LineAndFlag>>(); m_option->get<std::vector<LineAndFlag>>();
} }
void on_insert(const BufferCoord& begin, const BufferCoord& end) override void on_insert(const Buffer&, const BufferCoord& begin, const BufferCoord& end) override
{ {
LineCount new_lines = end.line - begin.line; LineCount new_lines = end.line - begin.line;
if (new_lines == 0) if (new_lines == 0)
@ -399,7 +399,7 @@ private:
m_option->set(lines); m_option->set(lines);
} }
void on_erase(const BufferCoord& begin, const BufferCoord& end) override void on_erase(const Buffer&, const BufferCoord& begin, const BufferCoord& end) override
{ {
LineCount removed_lines = end.line - begin.line; LineCount removed_lines = end.line - begin.line;
if (removed_lines == 0) if (removed_lines == 0)

View File

@ -18,30 +18,31 @@ namespace
{ {
template<template <bool, bool> class UpdateFunc> template<template <bool, bool> class UpdateFunc>
void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const BufferCoord& end, LineCount end_line) void on_buffer_change(const Buffer& buffer, SelectionList& sels,
const BufferCoord& begin, const BufferCoord& 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, const BufferCoord& c) { return std::max(s.first(), s.last()).coord() < c; }); [](const Selection& s, const BufferCoord& c) { return std::max(s.first().coord(), s.last().coord()) < c; });
auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line, auto update_only_line_beg = std::upper_bound(sels.begin(), sels.end(), end_line,
[](LineCount l, const Selection& s) { return l < std::min(s.first(), s.last()).line(); }); [](LineCount l, const Selection& s) { return l < std::min(s.first().coord(), s.last().coord()).line; });
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>{}(update_beg->first(), begin, end); UpdateFunc<false, false>{}(buffer, update_beg->first(), begin, end);
UpdateFunc<false, false>{}(update_beg->last(), begin, end); UpdateFunc<false, false>{}(buffer, update_beg->last(), begin, 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>{}(it->first(), begin, end); UpdateFunc<false, true>{}(buffer, it->first(), begin, end);
UpdateFunc<false, true>{}(it->last(), begin, end); UpdateFunc<false, true>{}(buffer, it->last(), begin, 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>{}(it->first(), begin, end); UpdateFunc<true, true>{}(buffer, it->first(), begin, end);
UpdateFunc<true, true>{}(it->last(), begin, end); UpdateFunc<true, true>{}(buffer, it->last(), begin, end);
} }
} }
} }
@ -49,10 +50,10 @@ void on_buffer_change(SelectionList& sels, const BufferCoord& begin, const Buffe
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()(BufferIterator& it, void operator()(const Buffer& buffer, BufferIterator& it,
const BufferCoord& begin, const BufferCoord& end) const const BufferCoord& begin, const BufferCoord& end) const
{ {
BufferCoord coord = it.coord(); auto coord = it.coord();
if (assume_different_line) if (assume_different_line)
kak_assert(begin.line < coord.line); kak_assert(begin.line < coord.line);
if (not assume_greater_than_begin and coord < begin) if (not assume_greater_than_begin and coord < begin)
@ -68,16 +69,16 @@ 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()(BufferIterator& it, void operator()(const Buffer& buffer, BufferIterator& it,
const BufferCoord& begin, const BufferCoord& end) const const BufferCoord& begin, const BufferCoord& end) const
{ {
BufferCoord coord = it.coord(); auto coord = it.coord();
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 = it.buffer().clamp(begin); coord = buffer.clamp(begin);
else else
{ {
if (not assume_different_line and end.line == coord.line) if (not assume_different_line and end.line == coord.line)
@ -94,14 +95,14 @@ struct UpdateErase
} }
void SelectionList::update_insert(const BufferCoord& begin, const BufferCoord& end) void SelectionList::update_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{ {
on_buffer_change<UpdateInsert>(*this, begin, end, begin.line); on_buffer_change<UpdateInsert>(buffer, *this, begin, end, begin.line);
} }
void SelectionList::update_erase(const BufferCoord& begin, const BufferCoord& end) void SelectionList::update_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end)
{ {
on_buffer_change<UpdateErase>(*this, begin, end, end.line); on_buffer_change<UpdateErase>(buffer, *this, begin, end, end.line);
} }
void SelectionList::check_invariant() const void SelectionList::check_invariant() const

View File

@ -63,8 +63,8 @@ struct SelectionList : std::vector<Selection>
{ {
using std::vector<Selection>::vector; using std::vector<Selection>::vector;
void update_insert(const BufferCoord& begin, const BufferCoord& end); void update_insert(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end);
void update_erase(const BufferCoord& begin, const BufferCoord& end); void update_erase(const Buffer& buffer, const BufferCoord& begin, const BufferCoord& end);
void check_invariant() const; void check_invariant() const;
}; };