Make optional whether go_right() allows going to EOL

This commit is contained in:
Josh Holtrop 2016-11-06 14:39:34 -05:00
parent 8d19b23597
commit 07eca3268c
2 changed files with 26 additions and 11 deletions

View File

@ -101,6 +101,24 @@ bool GapBuffer::Cursor::is_start_of_line()
return *i2 == '\n'; return *i2 == '\n';
} }
bool GapBuffer::Cursor::is_end_of_line(bool allow_eol)
{
if (*m_iterator == '\n')
{
return true;
}
if (!allow_eol)
{
Iterator i2 = m_iterator;
i2.go_forward();
if (*i2 == '\n')
{
return true;
}
}
return false;
}
bool GapBuffer::Cursor::go_start_of_line() bool GapBuffer::Cursor::go_start_of_line()
{ {
bool moved = false; bool moved = false;
@ -128,11 +146,11 @@ bool GapBuffer::Cursor::go_start_of_line()
bool GapBuffer::Cursor::go_end_of_line() bool GapBuffer::Cursor::go_end_of_line()
{ {
bool moved = false; bool moved = false;
if (go_right()) if (go_right(false))
{ {
moved = true; moved = true;
} }
while (go_right()) while (go_right(false))
{ {
; ;
} }
@ -161,9 +179,9 @@ bool GapBuffer::Cursor::go_left()
return false; return false;
} }
bool GapBuffer::Cursor::go_right() bool GapBuffer::Cursor::go_right(bool allow_eol)
{ {
if (!is_end_of_line()) if (!is_end_of_line(allow_eol))
{ {
if (m_iterator.check_go_forward()) if (m_iterator.check_go_forward())
{ {
@ -231,7 +249,7 @@ void GapBuffer::Cursor::calculate_column()
tmp_cursor.go_start_of_line(); tmp_cursor.go_start_of_line();
while (tmp_cursor < *this) while (tmp_cursor < *this)
{ {
tmp_cursor.go_right(); tmp_cursor.go_right(true);
} }
m_column = tmp_cursor.m_column; m_column = tmp_cursor.m_column;
} }
@ -241,7 +259,7 @@ void GapBuffer::Cursor::forward_to_column(size_t target_column)
int32_t diff = abs((int32_t)(target_column - m_column)); int32_t diff = abs((int32_t)(target_column - m_column));
if (diff == 0) if (diff == 0)
return; return;
while (go_right()) while (go_right(false))
{ {
int32_t new_diff = abs((int32_t)(target_column - m_column)); int32_t new_diff = abs((int32_t)(target_column - m_column));
if (new_diff > diff) if (new_diff > diff)

View File

@ -62,14 +62,11 @@ public:
init_column(); init_column();
} }
bool is_start_of_line(); bool is_start_of_line();
bool is_end_of_line() bool is_end_of_line(bool allow_eol);
{
return *m_iterator == '\n';
}
bool go_start_of_line(); bool go_start_of_line();
bool go_end_of_line(); bool go_end_of_line();
bool go_left(); bool go_left();
bool go_right(); bool go_right(bool allow_eol);
bool go_up(size_t target_column); bool go_up(size_t target_column);
bool go_down(size_t target_column); bool go_down(size_t target_column);
bool operator<(const Cursor & other) bool operator<(const Cursor & other)