Implement Cursor go_start_of_line()/go_end_of_line()

This commit is contained in:
Josh Holtrop 2016-11-03 19:50:00 -04:00
parent 37ad814b53
commit 1db3853dab
2 changed files with 44 additions and 3 deletions

View File

@ -103,12 +103,40 @@ bool GapBuffer::Cursor::is_start_of_line()
bool GapBuffer::Cursor::go_start_of_line() bool GapBuffer::Cursor::go_start_of_line()
{ {
/* TODO */ bool moved = false;
Iterator i2 = m_iterator;
for (;;)
{
i2.back();
if (i2.valid() && (*i2 != '\n'))
{
moved = true;
m_iterator = i2;
}
else
{
break;
}
}
if (moved)
{
init_column();
}
return moved;
} }
bool GapBuffer::Cursor::go_end_of_line() bool GapBuffer::Cursor::go_end_of_line()
{ {
/* TODO */ bool moved = false;
if (go_right())
{
moved = true;
}
while (go_right())
{
;
}
return moved;
} }
bool GapBuffer::Cursor::go_left() bool GapBuffer::Cursor::go_left()
@ -156,6 +184,18 @@ bool GapBuffer::Cursor::go_right()
return false; return false;
} }
void GapBuffer::Cursor::init_column()
{
if (*m_iterator == '\t')
{
m_column = m_iterator.gap_buffer()->tabstop - 1u;
}
else
{
m_column = 0u;
}
}
void GapBuffer::Cursor::calculate_column() void GapBuffer::Cursor::calculate_column()
{ {
Cursor tmp_cursor = *this; Cursor tmp_cursor = *this;

View File

@ -59,7 +59,7 @@ public:
: m_iterator(gap_buffer) : m_iterator(gap_buffer)
{ {
m_line = 0u; m_line = 0u;
m_column = 0u; init_column();
} }
bool is_start_of_line(); bool is_start_of_line();
bool is_end_of_line() bool is_end_of_line()
@ -80,6 +80,7 @@ public:
size_t m_line; size_t m_line;
size_t m_column; size_t m_column;
void init_column();
void calculate_column(); void calculate_column();
}; };