BufferPane: keep track of cursor screen column and row offset

This commit is contained in:
Josh Holtrop 2017-01-01 21:32:55 -05:00
parent 7a5b58e8a8
commit 0d0e47593b
2 changed files with 18 additions and 19 deletions

View File

@ -87,15 +87,16 @@ int BufferPane::calculate_rows_in_line(const Buffer::Iterator & start_of_line)
return saved_row_offset + 1; return saved_row_offset + 1;
} }
int BufferPane::calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line, const Buffer::Iterator & reference, int * iterator_row_offset) int BufferPane::calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line)
{ {
int saved_row_offset = 0; int saved_row_offset = 0;
walk_line(start_of_line, [this, &saved_row_offset, &reference, &iterator_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) { walk_line(start_of_line, [this, &saved_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) {
uint32_t code_point = *i; uint32_t code_point = *i;
if (i == reference) if (i == *m_iterator)
{ {
m_cursor_virtual_column = virtual_column; m_cursor_virtual_column = virtual_column;
*iterator_row_offset = row_offset; m_cursor_screen_column = screen_column;
m_cursor_row_offset = row_offset;
} }
if ((code_point != '\n') && (code_point != Buffer::Iterator::INVALID_CODE_POINT)) if ((code_point != '\n') && (code_point != Buffer::Iterator::INVALID_CODE_POINT))
{ {
@ -146,22 +147,20 @@ int BufferPane::screen_rows_above_line(const Buffer::Iterator & line, std::list<
return rows; return rows;
} }
int BufferPane::update_cursor_row(std::list<std::pair<int, Buffer::Iterator>> & backward_lines) void BufferPane::update_cursor_row(std::list<std::pair<int, Buffer::Iterator>> & backward_lines)
{ {
Buffer::Iterator start_of_line = *m_iterator; Buffer::Iterator start_of_line = *m_iterator;
start_of_line.go_start_of_line(); start_of_line.go_start_of_line();
int cursor_row_offset; int rows_in_cursor_line = calculate_rows_in_cursor_line(start_of_line);
int rows_in_cursor_line = calculate_rows_in_cursor_line(start_of_line, *m_iterator, &cursor_row_offset);
int so = effective_scroll_offset(); int so = effective_scroll_offset();
int rows_above = screen_rows_above_line(*m_iterator, backward_lines) + cursor_row_offset; int rows_above = screen_rows_above_line(*m_iterator, backward_lines) + m_cursor_row_offset;
int rows_below = screen_rows_below_line(*m_iterator) + std::max(0, rows_in_cursor_line - cursor_row_offset - 1); int rows_below = screen_rows_below_line(*m_iterator) + std::max(0, rows_in_cursor_line - m_cursor_row_offset - 1);
int min_rows_to_leave_above = std::min(rows_above, so); int min_rows_to_leave_above = std::min(rows_above, so);
int min_rows_to_leave_below = std::min(rows_below, so); int min_rows_to_leave_below = std::min(rows_below, so);
m_cursor_screen_row = std::min(m_rows - min_rows_to_leave_below - 1, m_cursor_screen_row); m_cursor_screen_row = std::min(m_rows - min_rows_to_leave_below - 1, m_cursor_screen_row);
m_cursor_screen_row = std::max(min_rows_to_leave_above, m_cursor_screen_row); m_cursor_screen_row = std::max(min_rows_to_leave_above, m_cursor_screen_row);
m_cursor_screen_row = std::max(m_rows - rows_below - 1, m_cursor_screen_row); m_cursor_screen_row = std::max(m_rows - rows_below - 1, m_cursor_screen_row);
m_cursor_screen_row = std::min(rows_above, m_cursor_screen_row); m_cursor_screen_row = std::min(rows_above, m_cursor_screen_row);
return cursor_row_offset;
} }
int BufferPane::determine_new_cursor_screen_row() int BufferPane::determine_new_cursor_screen_row()
@ -180,9 +179,8 @@ int BufferPane::determine_new_cursor_screen_row()
{ {
if (screen_line.second.line() == m_iterator->line()) if (screen_line.second.line() == m_iterator->line())
{ {
int row_offset; calculate_rows_in_cursor_line(screen_line.second);
calculate_rows_in_cursor_line(screen_line.second, *m_iterator, &row_offset); return screen_line.first + m_cursor_row_offset;
return screen_line.first + row_offset;
} }
} }
} }
@ -195,8 +193,8 @@ void BufferPane::draw()
if (m_iterator->valid()) if (m_iterator->valid())
{ {
std::list<std::pair<int, Buffer::Iterator>> backward_lines; std::list<std::pair<int, Buffer::Iterator>> backward_lines;
int cursor_row_offset_in_current_line = update_cursor_row(backward_lines); update_cursor_row(backward_lines);
int screen_row = m_cursor_screen_row - cursor_row_offset_in_current_line; int screen_row = m_cursor_screen_row - m_cursor_row_offset;
Buffer::Iterator i = *m_iterator; Buffer::Iterator i = *m_iterator;
if (screen_row <= 0) if (screen_row <= 0)
{ {
@ -466,10 +464,9 @@ void BufferPane::cursor_move(Window::CursorMovement which)
case Window::CursorMovement::LEFT: case Window::CursorMovement::LEFT:
case Window::CursorMovement::RIGHT: case Window::CursorMovement::RIGHT:
{ {
int cursor_row_offset;
Buffer::Iterator start_of_line = *m_iterator; Buffer::Iterator start_of_line = *m_iterator;
start_of_line.go_start_of_line(); start_of_line.go_start_of_line();
calculate_rows_in_cursor_line(start_of_line, *m_iterator, &cursor_row_offset); calculate_rows_in_cursor_line(start_of_line);
m_target_column = m_cursor_virtual_column; m_target_column = m_cursor_virtual_column;
} }
break; break;

View File

@ -33,11 +33,11 @@ protected:
void draw_buffer_character(int screen_column, int screen_row, uint32_t character); void draw_buffer_character(int screen_column, int screen_row, uint32_t character);
void draw_cursor(int x, int y); void draw_cursor(int x, int y);
int calculate_rows_in_line(const Buffer::Iterator & start_of_line); int calculate_rows_in_line(const Buffer::Iterator & start_of_line);
int calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line, const Buffer::Iterator & reference, int * iterator_row_offset); int calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line);
int calculate_rows_in_line_with_iterator_offset(const Buffer::Iterator & start_of_line, const Buffer::Iterator & reference, int * iterator_row_offset); int calculate_rows_in_line_with_iterator_offset(const Buffer::Iterator & start_of_line, const Buffer::Iterator & reference, int * iterator_row_offset);
int screen_rows_below_line(const Buffer::Iterator & line); int screen_rows_below_line(const Buffer::Iterator & line);
int screen_rows_above_line(const Buffer::Iterator & line, std::list<std::pair<int, Buffer::Iterator>> & backward_lines); int screen_rows_above_line(const Buffer::Iterator & line, std::list<std::pair<int, Buffer::Iterator>> & backward_lines);
int update_cursor_row(std::list<std::pair<int, Buffer::Iterator>> & backward_lines); void update_cursor_row(std::list<std::pair<int, Buffer::Iterator>> & backward_lines);
void walk_line(const Buffer::Iterator & start_of_line, std::function<void(int, int, int, int, const Buffer::Iterator &)> callback); void walk_line(const Buffer::Iterator & start_of_line, std::function<void(int, int, int, int, const Buffer::Iterator &)> callback);
int determine_new_cursor_screen_row(); int determine_new_cursor_screen_row();
int col_x(int col) int col_x(int col)
@ -59,7 +59,9 @@ protected:
int m_columns; int m_columns;
int m_scroll_offset; int m_scroll_offset;
int m_cursor_screen_row; int m_cursor_screen_row;
int m_cursor_screen_column;
int m_cursor_virtual_column; int m_cursor_virtual_column;
int m_cursor_row_offset;
std::shared_ptr<Buffer::Iterator> m_iterator; std::shared_ptr<Buffer::Iterator> m_iterator;
std::list<std::pair<int, Buffer::Iterator>> m_screen_lines; std::list<std::pair<int, Buffer::Iterator>> m_screen_lines;
int m_target_column; int m_target_column;