From 4ed790632d3a8a0901145df99bac5e0a9afa22d4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 15 Jun 2017 18:12:21 +0100 Subject: [PATCH] Fix some other uses of invalid buffer coordinates in display code --- src/display_buffer.cc | 19 ++++++++++++++----- src/display_buffer.hh | 4 ++++ src/highlighters.cc | 9 --------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/display_buffer.cc b/src/display_buffer.cc index 4b025fc9..d9771327 100644 --- a/src/display_buffer.cc +++ b/src/display_buffer.cc @@ -10,6 +10,14 @@ namespace Kakoune { +BufferIterator get_iterator(const Buffer& buffer, BufferCoord coord) +{ + // Correct one past the end of line as next line + if (not buffer.is_end(coord) and coord.column == buffer[coord.line].length()) + coord = coord.line+1; + return buffer.iterator_at(coord); +} + StringView DisplayAtom::content() const { switch (m_type) @@ -36,7 +44,8 @@ ColumnCount DisplayAtom::length() const switch (m_type) { case Range: - return column_length(*m_buffer, m_range.begin, m_range.end); + return utf8::column_distance(get_iterator(*m_buffer, m_range.begin), + get_iterator(*m_buffer, m_range.end)); case Text: case ReplacedRange: return m_text.column_length(); @@ -48,8 +57,8 @@ ColumnCount DisplayAtom::length() const void DisplayAtom::trim_begin(ColumnCount count) { if (m_type == Range) - m_range.begin = utf8::advance(m_buffer->iterator_at(m_range.begin), - m_buffer->iterator_at(m_range.end), + m_range.begin = utf8::advance(get_iterator(*m_buffer, m_range.begin), + get_iterator(*m_buffer, m_range.end), count).coord(); else m_text = m_text.substr(count).str(); @@ -58,8 +67,8 @@ void DisplayAtom::trim_begin(ColumnCount count) void DisplayAtom::trim_end(ColumnCount count) { if (m_type == Range) - m_range.end = utf8::advance(m_buffer->iterator_at(m_range.end), - m_buffer->iterator_at(m_range.begin), + m_range.end = utf8::advance(get_iterator(*m_buffer, m_range.end), + get_iterator(*m_buffer, m_range.begin), -count).coord(); else m_text = m_text.substr(0, m_text.column_length() - count).str(); diff --git a/src/display_buffer.hh b/src/display_buffer.hh index e3c58516..1215d76d 100644 --- a/src/display_buffer.hh +++ b/src/display_buffer.hh @@ -25,6 +25,10 @@ size_t hash_value(const BufferRange& range) return hash_values(range.begin, range.end); } +class BufferIterator; +// Return a buffer iterator to the coord, tolerating one past end of line coords +BufferIterator get_iterator(const Buffer& buffer, BufferCoord coord); + struct DisplayAtom : public UseMemoryDomain { public: diff --git a/src/highlighters.cc b/src/highlighters.cc index 9a72e71a..6e5859fd 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -25,15 +25,6 @@ namespace Kakoune { -BufferIterator get_iterator(const Buffer& buffer, BufferCoord coord) -{ - // Correct one past the end of line as next line - if (not buffer.is_end(coord) and coord.column == buffer[coord.line].length()) - coord = coord.line+1; - return buffer.iterator_at(coord); -} - - template std::unique_ptr make_highlighter(Func func, HighlightPass pass = HighlightPass::Colorize) {