diff --git a/src/core/BufferView.cc b/src/core/BufferView.cc index bc49de2..9a9addd 100644 --- a/src/core/BufferView.cc +++ b/src/core/BufferView.cc @@ -31,17 +31,6 @@ void BufferView::set_scroll_offset(int scroll_offset) BufferView::Iterator BufferView::vert_iter() { - if (m_iterator->valid()) - { - /* TODO: user calls update() before vert_iter(). */ - update(); - } - else - { - m_cursor_screen_row = 0; - m_cursor_screen_column = 0; - m_cursor_virtual_column = 0; - } return Iterator(*this); } @@ -94,6 +83,14 @@ void BufferView::update() row_offset += ld.n_rows; line_iterator->go_next_line(); } + + /* Reset some fields if buffer becomes empty. */ + if (!m_iterator->valid()) + { + m_cursor_screen_row = 0; + m_cursor_screen_column = 0; + m_cursor_virtual_column = 0; + } } int BufferView::calculate_rows_in_line(std::shared_ptr start_of_line) diff --git a/src/core/BufferView.h b/src/core/BufferView.h index a48f72f..b39d6d8 100644 --- a/src/core/BufferView.h +++ b/src/core/BufferView.h @@ -56,6 +56,7 @@ public: CharacterWidthDeterminer & character_width_determiner); void resize(int width, int height); void set_scroll_offset(int scroll_offset); + void update(); Iterator vert_iter(); BufferLineWalker horiz_iter(std::shared_ptr start_of_line) { @@ -88,7 +89,6 @@ protected: { return std::min(m_scroll_offset, (m_height - 1) / 2); } - void update(); int calculate_rows_in_line(std::shared_ptr start_of_line); int calculate_rows_in_cursor_line(std::shared_ptr start_of_line); int screen_rows_below_line(std::shared_ptr line); diff --git a/test/src/test_BufferView.cc b/test/src/test_BufferView.cc index c89421a..363562d 100644 --- a/test/src/test_BufferView.cc +++ b/test/src/test_BufferView.cc @@ -63,6 +63,7 @@ TEST(BufferViewTest, does_not_iterate_for_empty_buffer) BufferView bv(b, iterator, Cwd); bv.resize(40, 8); int iter_count = 0; + bv.update(); for (auto it = bv.vert_iter(); it.is_valid(); it++) { iter_count++; @@ -80,6 +81,7 @@ TEST(BufferViewTest, iterates_through_screen_lines_with_no_wrapping) BufferView bv(b, iterator, Cwd); bv.resize(40, 8); int iter_count = 0; + bv.update(); for (auto it = bv.vert_iter(); it.is_valid(); it++) { EXPECT_EQ(iter_count, LineNumber(it.iterator())); @@ -99,6 +101,7 @@ TEST(BufferViewTest, iterates_through_screen_lines_with_wrapping) bv.resize(3, 10); int iter_count = 0; int expected_row_offsets[] = {0, 1, 2, 4, 5, 6, 7}; + bv.update(); for (auto it = bv.vert_iter(); it.is_valid(); it++) { EXPECT_EQ(iter_count, LineNumber(it.iterator())); @@ -122,6 +125,7 @@ TEST(BufferViewTest, fills_view_with_last_lines_of_buffer_when_cursor_warps_to_l bv.resize(40, 3); int iter_count = 0; int expected_row_offsets[] = {0, 1, 2}; + bv.update(); for (auto it = bv.vert_iter(); it.is_valid(); it++) { EXPECT_EQ(10 + iter_count, LineNumber(it.iterator()));