move BufferIterator on_{insert,erase} as DynamicSelectionList implementation detail

This commit is contained in:
Maxime Coste 2013-03-15 14:22:42 +01:00
parent 0c4addb40c
commit 5e88b7fe28
3 changed files with 49 additions and 46 deletions

View File

@ -58,15 +58,14 @@ public:
BufferIterator operator++ (int); BufferIterator operator++ (int);
BufferIterator operator-- (int); BufferIterator operator-- (int);
BufferIterator& operator=(const BufferCoord& coord);
void clamp(bool avoid_eol); void clamp(bool avoid_eol);
bool is_begin() const; bool is_begin() const;
bool is_end() const; bool is_end() const;
bool is_valid() const; bool is_valid() const;
void on_insert(const BufferCoord& begin, const BufferCoord& end);
void on_erase(const BufferCoord& begin, const BufferCoord& end);
const Buffer& buffer() const; const Buffer& buffer() const;
const BufferCoord& coord() const { return m_coord; } const BufferCoord& coord() const { return m_coord; }
LineCount line() const { return m_coord.line; } LineCount line() const { return m_coord.line; }

View File

@ -70,45 +70,6 @@ inline bool BufferIterator::operator>=(const BufferIterator& iterator) const
return (m_coord >= iterator.m_coord); return (m_coord >= iterator.m_coord);
} }
inline void BufferIterator::on_insert(const BufferCoord& begin,
const BufferCoord& end)
{
if (m_coord < begin)
return;
if (begin.line == m_coord.line)
m_coord.column = end.column + m_coord.column - begin.column;
m_coord.line += end.line - begin.line;
assert(is_valid());
}
inline void BufferIterator::on_erase(const BufferCoord& begin,
const BufferCoord& end)
{
if (m_coord < begin)
return;
if (m_coord <= end)
{
m_coord = begin;
if (is_end())
operator--();
}
else
{
if (end.line == m_coord.line)
{
m_coord.line = begin.line;
m_coord.column = begin.column + m_coord.column - end.column;
}
else
m_coord.line -= end.line - begin.line;
}
assert(is_valid());
}
inline char BufferIterator::operator*() const inline char BufferIterator::operator*() const
{ {
return m_buffer->m_lines[m_coord.line].content[m_coord.column]; return m_buffer->m_lines[m_coord.line].content[m_coord.column];
@ -212,6 +173,13 @@ inline BufferIterator BufferIterator::operator--(int)
return save; return save;
} }
inline BufferIterator& BufferIterator::operator=(const BufferCoord& coord)
{
m_coord = coord;
assert(is_valid());
return *this;
}
inline bool BufferIterator::is_begin() const inline bool BufferIterator::is_begin() const
{ {
assert(m_buffer); assert(m_buffer);

View File

@ -72,14 +72,50 @@ void DynamicSelectionList::check_invariant() const
#endif #endif
} }
static void update_insert(BufferIterator& it,
const BufferCoord& begin, const BufferCoord& end)
{
BufferCoord coord = it.coord();
if (coord < begin)
return;
if (begin.line == coord.line)
coord.column = end.column + coord.column - begin.column;
coord.line += end.line - begin.line;
it = coord;
}
static void update_erase(BufferIterator& it,
const BufferCoord& begin, const BufferCoord& end)
{
BufferCoord coord = it.coord();
if (coord < begin)
return;
if (coord <= end)
coord = it.buffer().clamp(begin);
else
{
if (end.line == coord.line)
{
coord.line = begin.line;
coord.column = begin.column + coord.column - end.column;
}
else
coord.line -= end.line - begin.line;
}
it = coord;
}
void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end) void DynamicSelectionList::on_insert(const BufferIterator& begin, const BufferIterator& end)
{ {
const BufferCoord begin_coord{begin.coord()}; const BufferCoord begin_coord{begin.coord()};
const BufferCoord end_coord{end.coord()}; const BufferCoord end_coord{end.coord()};
for (auto& sel : *this) for (auto& sel : *this)
{ {
sel.first().on_insert(begin_coord, end_coord); update_insert(sel.first(), begin_coord, end_coord);
sel.last().on_insert(begin_coord, end_coord); update_insert(sel.last(), begin_coord, end_coord);
} }
} }
@ -89,8 +125,8 @@ void DynamicSelectionList::on_erase(const BufferIterator& begin, const BufferIte
const BufferCoord end_coord{end.coord()}; const BufferCoord end_coord{end.coord()};
for (auto& sel : *this) for (auto& sel : *this)
{ {
sel.first().on_erase(begin_coord, end_coord); update_erase(sel.first(), begin_coord, end_coord);
sel.last().on_erase(begin_coord, end_coord); update_erase(sel.last(), begin_coord, end_coord);
} }
} }