adjust cursor movement to optionally allow moving to EOL
This commit is contained in:
parent
4ebe81c062
commit
261cc9abf8
@ -138,7 +138,7 @@ bool PieceTable::Cursor::go_start_of_line()
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool PieceTable::Cursor::go_end_of_line()
|
||||
bool PieceTable::Cursor::go_end_of_line(bool allow_on_eol)
|
||||
{
|
||||
if (!iterator.valid())
|
||||
return false;
|
||||
@ -148,11 +148,14 @@ bool PieceTable::Cursor::go_end_of_line()
|
||||
(iterator.piece->next != iterator.piece_table->end_piece))
|
||||
{
|
||||
iterator.go_next_piece();
|
||||
rv = true;
|
||||
}
|
||||
uint32_t current_column = column;
|
||||
iterator.go_end_of_piece();
|
||||
calculate_column();
|
||||
if (!allow_on_eol)
|
||||
{
|
||||
go_left(1);
|
||||
}
|
||||
if (column != current_column)
|
||||
rv = true;
|
||||
return rv;
|
||||
@ -214,7 +217,7 @@ bool PieceTable::Cursor::go_left(int n)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PieceTable::Cursor::go_right(int n)
|
||||
bool PieceTable::Cursor::go_right(int n, bool allow_on_eol)
|
||||
{
|
||||
if (!iterator.valid())
|
||||
return false;
|
||||
@ -222,7 +225,13 @@ bool PieceTable::Cursor::go_right(int n)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Iterator iterator_2 = iterator;
|
||||
iterator.go_next_position();
|
||||
if ((!allow_on_eol) && is_end_of_line())
|
||||
{
|
||||
iterator = iterator_2;
|
||||
return false;
|
||||
}
|
||||
uint32_t chr = *iterator;
|
||||
if (chr == '\t')
|
||||
{
|
||||
@ -307,7 +316,7 @@ void PieceTable::Cursor::calculate_column()
|
||||
while ((tmp_cursor.iterator.piece != iterator.piece) ||
|
||||
(tmp_cursor.iterator.offset < iterator.offset))
|
||||
{
|
||||
tmp_cursor.go_right(1);
|
||||
tmp_cursor.go_right(1, true);
|
||||
}
|
||||
column = tmp_cursor.column;
|
||||
}
|
||||
@ -317,7 +326,7 @@ void PieceTable::Cursor::forward_to_column(uint32_t c)
|
||||
int32_t diff = abs((int32_t)(c - column));
|
||||
if (diff == 0)
|
||||
return;
|
||||
while (go_right(1))
|
||||
while (go_right(1, false))
|
||||
{
|
||||
int32_t new_diff = abs((int32_t)(c - column));
|
||||
if (new_diff > diff)
|
||||
|
@ -102,11 +102,11 @@ public:
|
||||
Cursor(PieceTable * pt);
|
||||
|
||||
bool go_start_of_line();
|
||||
bool go_end_of_line();
|
||||
bool go_end_of_line(bool allow_on_eol);
|
||||
bool go_up(int n, uint32_t desired_column);
|
||||
bool go_down(int n, uint32_t desired_column);
|
||||
bool go_left(int n);
|
||||
bool go_right(int n);
|
||||
bool go_right(int n, bool allow_on_eol);
|
||||
bool operator==(const Cursor & c)
|
||||
{
|
||||
return (c.line == line) && (c.column == column);
|
||||
|
@ -300,7 +300,7 @@ void Window::cursor_move(int which)
|
||||
m_target_column = m_cursor->column;
|
||||
break;
|
||||
case CURSOR_RIGHT:
|
||||
moved = m_cursor->go_right(1);
|
||||
moved = m_cursor->go_right(1, false);
|
||||
m_target_column = m_cursor->column;
|
||||
break;
|
||||
case CURSOR_UP:
|
||||
@ -314,7 +314,7 @@ void Window::cursor_move(int which)
|
||||
m_target_column = 0u;
|
||||
break;
|
||||
case CURSOR_EOL:
|
||||
moved = m_cursor->go_end_of_line();
|
||||
moved = m_cursor->go_end_of_line(false);
|
||||
m_target_column = 0x7FFFFFFFu;
|
||||
break;
|
||||
}
|
||||
@ -329,7 +329,7 @@ void Window::cursor_move(int which)
|
||||
case CURSOR_UP:
|
||||
{
|
||||
PieceTable::Cursor c = *m_cursor;
|
||||
c.go_end_of_line();
|
||||
c.go_end_of_line(false);
|
||||
row_offset = m_cursor->column / m_columns - c.column / m_columns - 1 - current_row_offset;
|
||||
}
|
||||
break;
|
||||
@ -337,7 +337,7 @@ void Window::cursor_move(int which)
|
||||
{
|
||||
PieceTable::Cursor c = *m_cursor;
|
||||
c.go_up(1, 0u);
|
||||
c.go_end_of_line();
|
||||
c.go_end_of_line(false);
|
||||
row_offset = c.column / m_columns - current_row_offset + 1 + m_cursor->column / m_columns;
|
||||
}
|
||||
break;
|
||||
@ -347,7 +347,7 @@ void Window::cursor_move(int which)
|
||||
case CURSOR_EOL:
|
||||
{
|
||||
PieceTable::Cursor c = *m_cursor;
|
||||
c.go_end_of_line();
|
||||
c.go_end_of_line(false);
|
||||
row_offset = c.column / m_columns - current_row_offset;
|
||||
}
|
||||
break;
|
||||
@ -373,7 +373,7 @@ std::pair<int, PieceTable::Cursor> Window::calculate_start_position()
|
||||
if (cursor.go_up(1, 0u))
|
||||
{
|
||||
PieceTable::Cursor c = cursor;
|
||||
c.go_end_of_line();
|
||||
c.go_end_of_line(false);
|
||||
row -= c.column / m_columns + 1;
|
||||
}
|
||||
else
|
||||
@ -424,7 +424,7 @@ void Window::draw_buffer()
|
||||
(character != '\t'))
|
||||
draw_buffer_character(screen_column, screen_row, character);
|
||||
}
|
||||
if (!cursor.go_right(1))
|
||||
if (!cursor.go_right(1, false))
|
||||
{
|
||||
bool last_row = !cursor.go_down(1, 0);
|
||||
if (last_vertical_crosshair_row < row)
|
||||
|
@ -30,7 +30,7 @@ TEST(BufferTest, allows_navigating_using_cursors)
|
||||
ASSERT_EQ((uint32_t)'H', **cursor);
|
||||
cursor->go_up(1, cursor->column);
|
||||
ASSERT_EQ((uint32_t)'H', **cursor);
|
||||
cursor->go_right(1);
|
||||
cursor->go_right(1, false);
|
||||
ASSERT_EQ((uint32_t)'e', **cursor);
|
||||
cursor->go_down(1, cursor->column);
|
||||
ASSERT_EQ((uint32_t)'h', **cursor);
|
||||
|
Loading…
x
Reference in New Issue
Block a user