Buffer::{iterator_at,clamp} takes an optional avoid_eol parameter

when avoid_eol is true, clamping does avoid end-of-line if possible
This commit is contained in:
Maxime Coste 2012-08-15 18:06:59 +02:00
parent 260b30e6d9
commit 390d4ccdfc
3 changed files with 14 additions and 7 deletions

View File

@ -55,9 +55,10 @@ Buffer::~Buffer()
assert(m_change_listeners.empty());
}
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column,
bool avoid_eol) const
{
return BufferIterator(*this, clamp(line_and_column));
return BufferIterator(*this, clamp(line_and_column, avoid_eol));
}
BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const
@ -78,14 +79,15 @@ BufferSize Buffer::line_length(BufferPos line) const
return end - m_lines[line].start;
}
BufferCoord Buffer::clamp(const BufferCoord& line_and_column) const
BufferCoord Buffer::clamp(const BufferCoord& line_and_column,
bool avoid_eol) const
{
if (m_lines.empty())
return BufferCoord();
BufferCoord result(line_and_column.line, line_and_column.column);
result.line = Kakoune::clamp<int>(0, m_lines.size() - 1, result.line);
int max_col = std::max(0, line_length(result.line) - 2);
int max_col = std::max(0, line_length(result.line) - (avoid_eol ? 2 : 1));
result.column = Kakoune::clamp<int>(0, max_col, result.column);
return result;
}

View File

@ -130,11 +130,16 @@ public:
BufferSize character_count() const;
BufferSize line_count() const;
BufferIterator iterator_at(const BufferCoord& line_and_column) const;
// returns an iterator at given coordinates. line_and_column is
// clamped according to avoid_eol.
BufferIterator iterator_at(const BufferCoord& line_and_column,
bool avoid_eol = false) const;
BufferCoord line_and_column_at(const BufferIterator& iterator) const;
// returns nearest valid coordinates from given ones
BufferCoord clamp(const BufferCoord& line_and_column) const;
// if avoid_eol, clamp to character before eol if line is not empty
BufferCoord clamp(const BufferCoord& line_and_column,
bool avoid_eol = false) const;
const String& name() const { return m_name; }

View File

@ -113,7 +113,7 @@ void Editor::move_selections(const BufferCoord& offset, bool append)
for (auto& sel : m_selections.back())
{
BufferCoord pos = m_buffer.line_and_column_at(sel.last());
BufferIterator last = m_buffer.iterator_at(pos + BufferCoord(offset));
BufferIterator last = m_buffer.iterator_at(pos + offset, true);
sel = Selection(append ? sel.first() : last, last);
}
}