Separate PieceTable::Cursor::go_*() into go_*() and check_go_*()
This commit is contained in:
parent
ef717faa16
commit
66105dc88f
@ -148,89 +148,109 @@ PieceTable::Cursor::Cursor(PieceTable * pt) : iterator(pt)
|
|||||||
init_column();
|
init_column();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_start_of_line()
|
void PieceTable::Cursor::go_start_of_line()
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
bool rv = false;
|
|
||||||
while (iterator.valid() &&
|
while (iterator.valid() &&
|
||||||
(!iterator.piece->prev->eol()) &&
|
(!iterator.piece->prev->eol()) &&
|
||||||
(iterator.piece->prev != iterator.piece_table->start_piece))
|
(iterator.piece->prev != iterator.piece_table->start_piece))
|
||||||
{
|
{
|
||||||
iterator.go_prev_piece();
|
iterator.go_prev_piece();
|
||||||
rv = true;
|
|
||||||
}
|
}
|
||||||
if (iterator.offset != 0u)
|
|
||||||
rv = true;
|
|
||||||
iterator.offset = 0u;
|
iterator.offset = 0u;
|
||||||
init_column();
|
init_column();
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_end_of_line(bool allow_on_eol)
|
bool PieceTable::Cursor::check_go_start_of_line()
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_start_of_line();
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PieceTable::Cursor::go_end_of_line(bool allow_on_eol)
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
bool rv = false;
|
|
||||||
while (iterator.valid() &&
|
while (iterator.valid() &&
|
||||||
(!iterator.piece->eol()) &&
|
(!iterator.piece->eol()) &&
|
||||||
(iterator.piece->next != iterator.piece_table->end_piece))
|
(iterator.piece->next != iterator.piece_table->end_piece))
|
||||||
{
|
{
|
||||||
iterator.go_next_piece();
|
iterator.go_next_piece();
|
||||||
}
|
}
|
||||||
uint32_t current_column = column;
|
|
||||||
iterator.go_end_of_piece();
|
iterator.go_end_of_piece();
|
||||||
calculate_column();
|
calculate_column();
|
||||||
if (!allow_on_eol)
|
if (!allow_on_eol)
|
||||||
{
|
{
|
||||||
go_left(1);
|
go_left(1);
|
||||||
}
|
}
|
||||||
if (column != current_column)
|
|
||||||
rv = true;
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_up(int n, uint32_t desired_column)
|
bool PieceTable::Cursor::check_go_end_of_line(bool allow_on_eol)
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_end_of_line(allow_on_eol);
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PieceTable::Cursor::go_up(int n, uint32_t desired_column)
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
Piece * p = prev_line();
|
Piece * p = prev_line();
|
||||||
if (p == nullptr)
|
if (p == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
iterator.piece = p;
|
iterator.piece = p;
|
||||||
iterator.offset = 0u;
|
iterator.offset = 0u;
|
||||||
line--;
|
line--;
|
||||||
init_column();
|
init_column();
|
||||||
forward_to_column(desired_column);
|
forward_to_column(desired_column);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_down(int n, uint32_t desired_column)
|
bool PieceTable::Cursor::check_go_up(int n, uint32_t desired_column)
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_up(n, desired_column);
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PieceTable::Cursor::go_down(int n, uint32_t desired_column)
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
Piece * p = next_line();
|
Piece * p = next_line();
|
||||||
if (p == nullptr)
|
if (p == nullptr)
|
||||||
{
|
{
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
iterator.piece = p;
|
iterator.piece = p;
|
||||||
iterator.offset = 0u;
|
iterator.offset = 0u;
|
||||||
line++;
|
line++;
|
||||||
init_column();
|
init_column();
|
||||||
forward_to_column(desired_column);
|
forward_to_column(desired_column);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_left(int n)
|
bool PieceTable::Cursor::check_go_down(int n, uint32_t desired_column)
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_down(n, desired_column);
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PieceTable::Cursor::go_left(int n)
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
if (is_start_of_line())
|
if (is_start_of_line())
|
||||||
{
|
{
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
uint32_t chr = *iterator;
|
uint32_t chr = *iterator;
|
||||||
iterator.go_prev_position();
|
iterator.go_prev_position();
|
||||||
@ -243,23 +263,30 @@ bool PieceTable::Cursor::go_left(int n)
|
|||||||
/* TODO: handle multi-column characters */
|
/* TODO: handle multi-column characters */
|
||||||
column--;
|
column--;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PieceTable::Cursor::go_right(int n, bool allow_on_eol)
|
bool PieceTable::Cursor::check_go_left(int n)
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_left(n);
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PieceTable::Cursor::go_right(int n, bool allow_on_eol)
|
||||||
{
|
{
|
||||||
if (!iterator.valid())
|
if (!iterator.valid())
|
||||||
return false;
|
return;
|
||||||
if (is_end_of_line())
|
if (is_end_of_line())
|
||||||
{
|
{
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
Iterator iterator_2 = iterator;
|
Iterator iterator_2 = iterator;
|
||||||
iterator.go_next_position();
|
iterator.go_next_position();
|
||||||
if ((!allow_on_eol) && is_end_of_line())
|
if ((!allow_on_eol) && is_end_of_line())
|
||||||
{
|
{
|
||||||
iterator = iterator_2;
|
iterator = iterator_2;
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
uint32_t chr = *iterator;
|
uint32_t chr = *iterator;
|
||||||
if (chr == '\t')
|
if (chr == '\t')
|
||||||
@ -272,7 +299,14 @@ bool PieceTable::Cursor::go_right(int n, bool allow_on_eol)
|
|||||||
/* TODO: handle multi-column characters */
|
/* TODO: handle multi-column characters */
|
||||||
column++;
|
column++;
|
||||||
}
|
}
|
||||||
return true;
|
}
|
||||||
|
|
||||||
|
bool PieceTable::Cursor::check_go_right(int n, bool allow_on_eol)
|
||||||
|
{
|
||||||
|
uint32_t current_line = line;
|
||||||
|
uint32_t current_column = column;
|
||||||
|
go_right(n, allow_on_eol);
|
||||||
|
return (current_line != line) || (current_column != column);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PieceTable::Cursor::warp_to_inserted_piece(Piece * piece)
|
void PieceTable::Cursor::warp_to_inserted_piece(Piece * piece)
|
||||||
@ -355,7 +389,7 @@ void PieceTable::Cursor::forward_to_column(uint32_t c)
|
|||||||
int32_t diff = abs((int32_t)(c - column));
|
int32_t diff = abs((int32_t)(c - column));
|
||||||
if (diff == 0)
|
if (diff == 0)
|
||||||
return;
|
return;
|
||||||
while (go_right(1, false))
|
while (check_go_right(1, false))
|
||||||
{
|
{
|
||||||
int32_t new_diff = abs((int32_t)(c - column));
|
int32_t new_diff = abs((int32_t)(c - column));
|
||||||
if (new_diff > diff)
|
if (new_diff > diff)
|
||||||
|
@ -96,12 +96,20 @@ public:
|
|||||||
|
|
||||||
Cursor(PieceTable * pt);
|
Cursor(PieceTable * pt);
|
||||||
|
|
||||||
bool go_start_of_line();
|
void go_start_of_line();
|
||||||
bool go_end_of_line(bool allow_on_eol);
|
void go_end_of_line(bool allow_on_eol);
|
||||||
bool go_up(int n, uint32_t desired_column);
|
void go_up(int n, uint32_t desired_column);
|
||||||
bool go_down(int n, uint32_t desired_column);
|
void go_down(int n, uint32_t desired_column);
|
||||||
bool go_left(int n);
|
void go_left(int n);
|
||||||
bool go_right(int n, bool allow_on_eol);
|
void go_right(int n, bool allow_on_eol);
|
||||||
|
|
||||||
|
bool check_go_start_of_line();
|
||||||
|
bool check_go_end_of_line(bool allow_on_eol);
|
||||||
|
bool check_go_up(int n, uint32_t desired_column);
|
||||||
|
bool check_go_down(int n, uint32_t desired_column);
|
||||||
|
bool check_go_left(int n);
|
||||||
|
bool check_go_right(int n, bool allow_on_eol);
|
||||||
|
|
||||||
bool operator==(const Cursor & c)
|
bool operator==(const Cursor & c)
|
||||||
{
|
{
|
||||||
return (c.line == line) && (c.column == column);
|
return (c.line == line) && (c.column == column);
|
||||||
|
@ -296,25 +296,25 @@ void Window::cursor_move(int which)
|
|||||||
switch (which)
|
switch (which)
|
||||||
{
|
{
|
||||||
case CURSOR_LEFT:
|
case CURSOR_LEFT:
|
||||||
moved = m_cursor->go_left(1);
|
moved = m_cursor->check_go_left(1);
|
||||||
m_target_column = m_cursor->column;
|
m_target_column = m_cursor->column;
|
||||||
break;
|
break;
|
||||||
case CURSOR_RIGHT:
|
case CURSOR_RIGHT:
|
||||||
moved = m_cursor->go_right(1, false);
|
moved = m_cursor->check_go_right(1, false);
|
||||||
m_target_column = m_cursor->column;
|
m_target_column = m_cursor->column;
|
||||||
break;
|
break;
|
||||||
case CURSOR_UP:
|
case CURSOR_UP:
|
||||||
moved = m_cursor->go_up(1, m_target_column);
|
moved = m_cursor->check_go_up(1, m_target_column);
|
||||||
break;
|
break;
|
||||||
case CURSOR_DOWN:
|
case CURSOR_DOWN:
|
||||||
moved = m_cursor->go_down(1, m_target_column);
|
moved = m_cursor->check_go_down(1, m_target_column);
|
||||||
break;
|
break;
|
||||||
case CURSOR_SOL:
|
case CURSOR_SOL:
|
||||||
moved = m_cursor->go_start_of_line();
|
moved = m_cursor->check_go_start_of_line();
|
||||||
m_target_column = 0u;
|
m_target_column = 0u;
|
||||||
break;
|
break;
|
||||||
case CURSOR_EOL:
|
case CURSOR_EOL:
|
||||||
moved = m_cursor->go_end_of_line(false);
|
moved = m_cursor->check_go_end_of_line(false);
|
||||||
m_target_column = 0x7FFFFFFFu;
|
m_target_column = 0x7FFFFFFFu;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -370,7 +370,7 @@ std::pair<int, PieceTable::Cursor> Window::calculate_start_position()
|
|||||||
cursor.go_start_of_line();
|
cursor.go_start_of_line();
|
||||||
while (row > 0)
|
while (row > 0)
|
||||||
{
|
{
|
||||||
if (cursor.go_up(1, 0u))
|
if (cursor.check_go_up(1, 0u))
|
||||||
{
|
{
|
||||||
PieceTable::Cursor c = cursor;
|
PieceTable::Cursor c = cursor;
|
||||||
c.go_end_of_line(false);
|
c.go_end_of_line(false);
|
||||||
@ -425,9 +425,9 @@ void Window::draw_buffer()
|
|||||||
(character != '\n'))
|
(character != '\n'))
|
||||||
draw_buffer_character(screen_column, screen_row, character);
|
draw_buffer_character(screen_column, screen_row, character);
|
||||||
}
|
}
|
||||||
if (!cursor.go_right(1, false))
|
if (!cursor.check_go_right(1, false))
|
||||||
{
|
{
|
||||||
bool last_row = !cursor.go_down(1, 0);
|
bool last_row = !cursor.check_go_down(1, 0);
|
||||||
if (last_vertical_crosshair_row < row)
|
if (last_vertical_crosshair_row < row)
|
||||||
{
|
{
|
||||||
int crosshair_screen_row = row + crosshair_row_offset;
|
int crosshair_screen_row = row + crosshair_row_offset;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user