Buffer: fix handling of last eol

This commit is contained in:
Maxime Coste 2011-11-24 14:23:41 +00:00
parent bfc8a0c98b
commit 30b4fb6513
2 changed files with 11 additions and 12 deletions

View File

@ -63,10 +63,8 @@ BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
if (m_lines.empty()) if (m_lines.empty())
return begin(); return begin();
BufferPos line = Kakoune::clamp<int>(0, m_lines.size() - 1, line_and_column.line); BufferCoord clamped = clamp(line_and_column);
int col_max = std::max(0, line_length(line) - 1); return BufferIterator(*this, m_lines[clamped.line] + clamped.column);
BufferPos column = Kakoune::clamp<int>(0, col_max, line_and_column.column);
return BufferIterator(*this, m_lines[line] + column);
} }
BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const
@ -82,19 +80,19 @@ BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const
BufferPos Buffer::line_at(const BufferIterator& iterator) const BufferPos Buffer::line_at(const BufferIterator& iterator) const
{ {
for (unsigned i = 0; i < m_lines.size(); ++i) for (unsigned i = 0; i < line_count(); ++i)
{ {
if (m_lines[i] > iterator.m_position) if (m_lines[i] > iterator.m_position)
return i - 1; return i - 1;
} }
return m_lines.size() - 1; return line_count() - 1;
} }
BufferSize Buffer::line_length(BufferPos line) const BufferSize Buffer::line_length(BufferPos line) const
{ {
assert(not m_lines.empty()); assert(not m_lines.empty());
BufferPos end = (line >= m_lines.size() - 1) ? BufferPos end = (line >= line_count() - 1) ?
m_content.size() : m_lines[line + 1] - 1; m_content.size() - 1 : m_lines[line + 1] - 1;
return end - m_lines[line]; return end - m_lines[line];
} }
@ -104,8 +102,9 @@ BufferCoord Buffer::clamp(const BufferCoord& line_and_column) const
return BufferCoord(); return BufferCoord();
BufferCoord result(line_and_column.line, line_and_column.column); BufferCoord result(line_and_column.line, line_and_column.column);
result.line = Kakoune::clamp<int>(0, m_lines.size() - 1, result.line); result.line = Kakoune::clamp<int>(0, line_count() - 1, result.line);
result.column = Kakoune::clamp<int>(0, line_length(result.line), result.column); int max_col = std::max(0, line_length(result.line)-1);
result.column = Kakoune::clamp<int>(0, max_col, result.column);
return result; return result;
} }
@ -113,7 +112,7 @@ void Buffer::compute_lines()
{ {
m_lines.clear(); m_lines.clear();
m_lines.push_back(0); m_lines.push_back(0);
for (BufferPos i = 0; i < m_content.size(); ++i) for (BufferPos i = 0; i < m_content.size()-1; ++i)
{ {
if (m_content[i] == '\n') if (m_content[i] == '\n')
m_lines.push_back(i + 1); m_lines.push_back(i + 1);

View File

@ -283,7 +283,7 @@ void Window::update_display_buffer()
BufferIterator begin = m_buffer.iterator_at(m_position); BufferIterator begin = m_buffer.iterator_at(m_position);
BufferIterator end = m_buffer.iterator_at(m_position + BufferIterator end = m_buffer.iterator_at(m_position +
BufferCoord(m_dimensions.line, m_dimensions.column+1)); BufferCoord(m_dimensions.line, m_dimensions.column))+1;
if (begin == end) if (begin == end)
return; return;