Add Cursor::go_up() and go_down()

They currently allow going to EOL
This commit is contained in:
Josh Holtrop 2016-11-06 14:28:56 -05:00
parent 9dee74d18b
commit 8d19b23597
2 changed files with 49 additions and 0 deletions

View File

@ -184,6 +184,35 @@ bool GapBuffer::Cursor::go_right()
return false;
}
bool GapBuffer::Cursor::go_up(size_t target_column)
{
Cursor c2 = *this;
c2.go_start_of_line();
if (!c2.m_iterator.check_go_back())
{
return false;
}
c2.go_start_of_line();
*this = c2;
m_line--;
forward_to_column(target_column);
return true;
}
bool GapBuffer::Cursor::go_down(size_t target_column)
{
Cursor c2 = *this;
c2.go_end_of_line();
if (!c2.m_iterator.check_go_forward())
{
return false;
}
*this = c2;
m_line++;
forward_to_column(target_column);
return true;
}
void GapBuffer::Cursor::init_column()
{
if (*m_iterator == '\t')
@ -206,3 +235,20 @@ void GapBuffer::Cursor::calculate_column()
}
m_column = tmp_cursor.m_column;
}
void GapBuffer::Cursor::forward_to_column(size_t target_column)
{
int32_t diff = abs((int32_t)(target_column - m_column));
if (diff == 0)
return;
while (go_right())
{
int32_t new_diff = abs((int32_t)(target_column - m_column));
if (new_diff > diff)
{
go_left();
return;
}
diff = new_diff;
}
}

View File

@ -70,6 +70,8 @@ public:
bool go_end_of_line();
bool go_left();
bool go_right();
bool go_up(size_t target_column);
bool go_down(size_t target_column);
bool operator<(const Cursor & other)
{
return m_iterator < other.m_iterator;
@ -82,6 +84,7 @@ public:
void init_column();
void calculate_column();
void forward_to_column(size_t target_column);
};
uint8_t tabstop;