Change BufferView and BufferLineWalker to return iterator objects for iteration instead of using lambdas for callbacks.
This commit is contained in:
1 parent
b89f348602
commit
4b3e1ea7e7
6 files changed
+353
-309
No files matched your search
@@ -2,100 +2,100 @@
|
||||
|
||||
/**
|
||||
* Construct a BufferLineWalker.
|
||||
*
|
||||
* @param buffer The Buffer we're iterating.
|
||||
* @param start_of_line Buffer iterator pointing to the beginning of the buffer line.
|
||||
* @param view_width View width, beyond which characters will wrap to the next line.
|
||||
* @param character_width_determiner Functor to return the width (in columns) of a character.
|
||||
*/
|
||||
BufferLineWalker::BufferLineWalker(
|
||||
std::shared_ptr<Buffer> buffer,
|
||||
std::shared_ptr<Buffer::Iterator> start_of_line,
|
||||
int view_width,
|
||||
CharacterWidthDeterminer & character_width_determiner)
|
||||
: m_buffer(buffer),
|
||||
m_character_width_determiner(character_width_determiner)
|
||||
{
|
||||
m_width = 1;
|
||||
m_view_width = view_width;
|
||||
m_row_offset = 0;
|
||||
m_screen_column = 0;
|
||||
m_virtual_column = 0;
|
||||
m_iterator = std::make_shared<Buffer::Iterator>(*start_of_line);
|
||||
process_new_character();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the view width.
|
||||
*
|
||||
* @param width View width.
|
||||
*/
|
||||
void BufferLineWalker::set_width(int width)
|
||||
void BufferLineWalker::process_new_character()
|
||||
{
|
||||
m_width = std::max(1, width);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the callback for each character in the buffer line, wrapping to the
|
||||
* next screen line and taking into account multi-column characters.
|
||||
*
|
||||
* Callback parameters:
|
||||
* - row_offset
|
||||
* - screen column
|
||||
* - virtual column
|
||||
* - character width
|
||||
* - buffer iterator
|
||||
* If the callback function returns true, iteration continues. Otherwise
|
||||
* iteration is terminated.
|
||||
*
|
||||
* @param start_of_line Buffer iterator pointing to the beginning of the buffer line.
|
||||
* @param callback Function to call for each character in the line.
|
||||
*/
|
||||
void BufferLineWalker::walk_line(
|
||||
const Buffer::Iterator & start_of_line,
|
||||
std::function<bool(int, int, int, int, const Buffer::Iterator &)> callback)
|
||||
{
|
||||
int row_offset = 0;
|
||||
int screen_column = 0;
|
||||
int virtual_column = 0;
|
||||
Buffer::Iterator i = start_of_line;
|
||||
for (;;)
|
||||
if ((!m_iterator) || (!m_iterator->valid()))
|
||||
{
|
||||
uint32_t code_point = *i;
|
||||
if ((code_point == '\n') || (!i.valid()))
|
||||
{
|
||||
callback(row_offset, screen_column, virtual_column, 0, i);
|
||||
break;
|
||||
}
|
||||
int c_width;
|
||||
if (code_point == '\t')
|
||||
{
|
||||
uint8_t tabstop = m_buffer->tabstop();
|
||||
c_width = tabstop - virtual_column % tabstop;
|
||||
}
|
||||
else
|
||||
{
|
||||
c_width = m_character_width_determiner(code_point);
|
||||
if (((screen_column + c_width) > m_width) &&
|
||||
(screen_column > 0))
|
||||
{
|
||||
row_offset++;
|
||||
screen_column = 0;
|
||||
}
|
||||
}
|
||||
if (!callback(row_offset, screen_column, virtual_column, c_width, i))
|
||||
{
|
||||
break;
|
||||
}
|
||||
virtual_column += c_width;
|
||||
if (code_point == '\t')
|
||||
{
|
||||
screen_column += c_width;
|
||||
while (screen_column >= m_width)
|
||||
{
|
||||
screen_column -= m_width;
|
||||
row_offset++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((screen_column + c_width) >= m_width)
|
||||
{
|
||||
row_offset++;
|
||||
screen_column = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
screen_column += c_width;
|
||||
}
|
||||
}
|
||||
i.go_forward();
|
||||
m_code_point = '\n';
|
||||
m_character_width = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
m_code_point = **m_iterator;
|
||||
|
||||
if (m_code_point == '\n')
|
||||
{
|
||||
m_character_width = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_code_point == '\t')
|
||||
{
|
||||
uint8_t tabstop = m_buffer->tabstop();
|
||||
m_character_width = tabstop - m_virtual_column % tabstop;
|
||||
return;
|
||||
}
|
||||
|
||||
m_character_width = m_character_width_determiner(m_code_point);
|
||||
|
||||
if (((m_screen_column + m_character_width) > m_view_width) &&
|
||||
(m_screen_column > 0))
|
||||
{
|
||||
m_row_offset++;
|
||||
m_screen_column = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void BufferLineWalker::operator++(int unused)
|
||||
{
|
||||
if (!m_iterator)
|
||||
return;
|
||||
|
||||
if ((m_code_point == '\n') || (!m_iterator->valid()))
|
||||
{
|
||||
m_code_point = '\n';
|
||||
m_iterator.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
/* Adjust virtual column. */
|
||||
m_virtual_column += m_character_width;
|
||||
|
||||
/* Adjust screen position. */
|
||||
if (m_code_point == '\t')
|
||||
{
|
||||
m_screen_column += m_character_width;
|
||||
while (m_screen_column >= m_view_width)
|
||||
{
|
||||
m_screen_column -= m_view_width;
|
||||
m_row_offset++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((m_screen_column + m_character_width) >= m_view_width)
|
||||
{
|
||||
m_row_offset++;
|
||||
m_screen_column = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_screen_column += m_character_width;
|
||||
}
|
||||
}
|
||||
|
||||
m_iterator->go_forward();
|
||||
process_new_character();
|
||||
}
|
||||
@@ -14,16 +14,42 @@ class BufferLineWalker
|
||||
public:
|
||||
BufferLineWalker(
|
||||
std::shared_ptr<Buffer> buffer,
|
||||
std::shared_ptr<Buffer::Iterator> start_of_line,
|
||||
int view_width,
|
||||
CharacterWidthDeterminer & character_width_determiner);
|
||||
void set_width(int width);
|
||||
void walk_line(
|
||||
const Buffer::Iterator & start_of_line,
|
||||
std::function<bool(int, int, int, int, const Buffer::Iterator &)> callback);
|
||||
void operator++() { return (*this)++; }
|
||||
void operator++(int unused);
|
||||
bool is_eol() const
|
||||
{
|
||||
if (is_valid())
|
||||
{
|
||||
return (m_code_point == '\n');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool is_valid() const
|
||||
{
|
||||
return (bool)m_iterator;
|
||||
}
|
||||
int row_offset() const { return m_row_offset; }
|
||||
int screen_column() const { return m_screen_column; }
|
||||
int virtual_column() const { return m_virtual_column; }
|
||||
int character_width() const { return m_character_width; }
|
||||
uint32_t code_point() const { return m_code_point; }
|
||||
std::shared_ptr<Buffer::Iterator> iterator() const { return m_iterator; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Buffer> m_buffer;
|
||||
std::shared_ptr<Buffer::Iterator> m_iterator;
|
||||
CharacterWidthDeterminer & m_character_width_determiner;
|
||||
int m_width;
|
||||
int m_view_width;
|
||||
int m_row_offset;
|
||||
int m_screen_column;
|
||||
int m_virtual_column;
|
||||
int m_character_width;
|
||||
uint32_t m_code_point;
|
||||
|
||||
void process_new_character();
|
||||
};
|
||||
|
||||
#endif
|
||||
+43
-47
@@ -5,20 +5,22 @@
|
||||
BufferView::BufferView(std::shared_ptr<Buffer> buffer,
|
||||
std::shared_ptr<Buffer::Iterator> iterator,
|
||||
CharacterWidthDeterminer & character_width_determiner)
|
||||
: m_iterator(iterator)
|
||||
: m_buffer(buffer),
|
||||
m_iterator(iterator),
|
||||
m_character_width_determiner(character_width_determiner)
|
||||
{
|
||||
m_width = 1;
|
||||
m_height = 1;
|
||||
m_scroll_offset = 0;
|
||||
m_cursor_screen_row = 0;
|
||||
m_cursor_screen_column = 0;
|
||||
m_cursor_virtual_column = 0;
|
||||
m_cursor_row_offset = 0;
|
||||
m_buffer_line_walker = std::make_shared<BufferLineWalker>(buffer, character_width_determiner);
|
||||
}
|
||||
|
||||
void BufferView::resize(int width, int height)
|
||||
{
|
||||
m_buffer_line_walker->set_width(width);
|
||||
m_width = std::max(1, width);
|
||||
m_height = std::max(1, height);
|
||||
}
|
||||
|
||||
@@ -27,21 +29,12 @@ void BufferView::set_scroll_offset(int scroll_offset)
|
||||
m_scroll_offset = std::max(0, scroll_offset);
|
||||
}
|
||||
|
||||
void BufferView::iter_lines(
|
||||
std::function<bool(int, const Buffer::Iterator &)> callback)
|
||||
BufferView::Iterator BufferView::vert_iter()
|
||||
{
|
||||
if (m_iterator->valid())
|
||||
{
|
||||
/* TODO: user calls update() before vert_iter(). */
|
||||
update();
|
||||
Buffer::Iterator i = *m_first_line;
|
||||
int row_offset = m_first_line_row_offset;
|
||||
while (row_offset < m_height)
|
||||
{
|
||||
callback(row_offset, i);
|
||||
row_offset += calculate_rows_in_line(i);
|
||||
if (!i.go_next_line())
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -49,6 +42,7 @@ void BufferView::iter_lines(
|
||||
m_cursor_screen_column = 0;
|
||||
m_cursor_virtual_column = 0;
|
||||
}
|
||||
return Iterator(*this);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
@@ -58,12 +52,12 @@ void BufferView::iter_lines(
|
||||
void BufferView::update()
|
||||
{
|
||||
std::list<std::pair<int, std::shared_ptr<Buffer::Iterator>>> backward_lines;
|
||||
Buffer::Iterator start_of_line = *m_iterator;
|
||||
start_of_line.go_start_of_line();
|
||||
auto start_of_line = std::make_shared<Buffer::Iterator>(*m_iterator);
|
||||
start_of_line->go_start_of_line();
|
||||
int rows_in_cursor_line = calculate_rows_in_cursor_line(start_of_line);
|
||||
int so = effective_scroll_offset();
|
||||
int rows_above = screen_rows_above_line(*m_iterator, backward_lines) + m_cursor_row_offset;
|
||||
int rows_below = screen_rows_below_line(*m_iterator) + std::max(0, rows_in_cursor_line - m_cursor_row_offset - 1);
|
||||
int rows_above = screen_rows_above_line(start_of_line, backward_lines) + m_cursor_row_offset;
|
||||
int rows_below = screen_rows_below_line(start_of_line) + std::max(0, rows_in_cursor_line - m_cursor_row_offset - 1);
|
||||
int min_rows_to_leave_above = std::min(rows_above, so);
|
||||
int min_rows_to_leave_below = std::min(rows_below, so);
|
||||
m_cursor_screen_row = std::min(m_height - min_rows_to_leave_below - 1, m_cursor_screen_row);
|
||||
@@ -89,49 +83,51 @@ void BufferView::update()
|
||||
}
|
||||
}
|
||||
|
||||
int BufferView::calculate_rows_in_line(const Buffer::Iterator & start_of_line)
|
||||
int BufferView::calculate_rows_in_line(std::shared_ptr<Buffer::Iterator> start_of_line)
|
||||
{
|
||||
int saved_row_offset = 0;
|
||||
walk_line(start_of_line, [&saved_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) -> bool {
|
||||
uint32_t code_point = *i;
|
||||
if ((code_point != '\n') &&
|
||||
(code_point != Buffer::Iterator::INVALID_CODE_POINT))
|
||||
for (auto it = horiz_iter(start_of_line); it.is_valid(); it++)
|
||||
{
|
||||
if (!it.is_eol())
|
||||
{
|
||||
saved_row_offset = row_offset;
|
||||
return true;
|
||||
saved_row_offset = it.row_offset();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return saved_row_offset + 1;
|
||||
}
|
||||
|
||||
int BufferView::calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line)
|
||||
int BufferView::calculate_rows_in_cursor_line(std::shared_ptr<Buffer::Iterator> start_of_line)
|
||||
{
|
||||
int saved_row_offset = 0;
|
||||
walk_line(start_of_line, [this, &saved_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) -> bool {
|
||||
uint32_t code_point = *i;
|
||||
if (i == *m_iterator)
|
||||
for (auto it = horiz_iter(start_of_line); it.is_valid(); it++)
|
||||
{
|
||||
if (*it.iterator() == *m_iterator)
|
||||
{
|
||||
m_cursor_virtual_column = virtual_column;
|
||||
m_cursor_screen_column = screen_column;
|
||||
m_cursor_row_offset = row_offset;
|
||||
m_cursor_virtual_column = it.virtual_column();
|
||||
m_cursor_screen_column = it.screen_column();
|
||||
m_cursor_row_offset = it.row_offset();
|
||||
}
|
||||
if ((code_point != '\n') &&
|
||||
(code_point != Buffer::Iterator::INVALID_CODE_POINT))
|
||||
if (!it.is_eol())
|
||||
{
|
||||
saved_row_offset = row_offset;
|
||||
return true;
|
||||
saved_row_offset = it.row_offset();
|
||||
}
|
||||
return false;
|
||||
});
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return saved_row_offset + 1;
|
||||
}
|
||||
|
||||
int BufferView::screen_rows_below_line(const Buffer::Iterator & line)
|
||||
int BufferView::screen_rows_below_line(std::shared_ptr<Buffer::Iterator> line)
|
||||
{
|
||||
Buffer::Iterator i = line;
|
||||
auto i = std::make_shared<Buffer::Iterator>(*line);
|
||||
int rows = 0;
|
||||
while ((rows < m_height) && i.go_next_line())
|
||||
while ((rows < m_height) && i->go_next_line())
|
||||
{
|
||||
rows += calculate_rows_in_line(i);
|
||||
}
|
||||
@@ -139,16 +135,16 @@ int BufferView::screen_rows_below_line(const Buffer::Iterator & line)
|
||||
}
|
||||
|
||||
int BufferView::screen_rows_above_line(
|
||||
const Buffer::Iterator & line,
|
||||
std::shared_ptr<Buffer::Iterator> line,
|
||||
std::list<std::pair<int, std::shared_ptr<Buffer::Iterator>>> & backward_lines)
|
||||
{
|
||||
Buffer::Iterator i = line;
|
||||
auto i = std::make_shared<Buffer::Iterator>(*line);
|
||||
int rows = 0;
|
||||
while ((rows < m_height) && i.go_previous_line())
|
||||
while ((rows < m_height) && i->go_previous_line())
|
||||
{
|
||||
int rows_in_this_line = calculate_rows_in_line(i);
|
||||
rows += rows_in_this_line;
|
||||
backward_lines.push_back(std::pair<int, std::shared_ptr<Buffer::Iterator>>(rows_in_this_line, std::make_shared<Buffer::Iterator>(i)));
|
||||
backward_lines.push_back(std::pair<int, std::shared_ptr<Buffer::Iterator>>(rows_in_this_line, std::make_shared<Buffer::Iterator>(*i)));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
+55
-11
@@ -13,45 +13,89 @@
|
||||
class BufferView
|
||||
{
|
||||
public:
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
Iterator(BufferView & bv) : m_buffer_view(bv)
|
||||
{
|
||||
if (bv.m_iterator->valid())
|
||||
{
|
||||
m_iterator = std::make_shared<Buffer::Iterator>(*m_buffer_view.m_first_line);
|
||||
m_row_offset = m_buffer_view.m_first_line_row_offset;
|
||||
}
|
||||
}
|
||||
void operator++()
|
||||
{
|
||||
(*this)++;
|
||||
}
|
||||
void operator++(int unused)
|
||||
{
|
||||
if (is_valid())
|
||||
{
|
||||
m_row_offset += m_buffer_view.calculate_rows_in_line(m_iterator);
|
||||
if ((m_row_offset >= m_buffer_view.m_height) || (!m_iterator->go_next_line()))
|
||||
{
|
||||
m_iterator.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
bool is_valid()
|
||||
{
|
||||
return (bool)m_iterator;
|
||||
}
|
||||
int row_offset() const
|
||||
{
|
||||
return m_row_offset;
|
||||
}
|
||||
std::shared_ptr<Buffer::Iterator> iterator() const
|
||||
{
|
||||
return m_iterator;
|
||||
}
|
||||
|
||||
protected:
|
||||
BufferView & m_buffer_view;
|
||||
std::shared_ptr<Buffer::Iterator> m_iterator;
|
||||
int m_row_offset;
|
||||
};
|
||||
|
||||
BufferView(std::shared_ptr<Buffer> buffer,
|
||||
std::shared_ptr<Buffer::Iterator> iterator,
|
||||
CharacterWidthDeterminer & character_width_determiner);
|
||||
void resize(int width, int height);
|
||||
void set_scroll_offset(int scroll_offset);
|
||||
void iter_lines(
|
||||
std::function<bool(int, const Buffer::Iterator &)> callback);
|
||||
void walk_line(
|
||||
const Buffer::Iterator & start_of_line,
|
||||
std::function<bool(int, int, int, int, const Buffer::Iterator &)> callback)
|
||||
Iterator vert_iter();
|
||||
BufferLineWalker horiz_iter(std::shared_ptr<Buffer::Iterator> start_of_line)
|
||||
{
|
||||
m_buffer_line_walker->walk_line(start_of_line, callback);
|
||||
return BufferLineWalker(m_buffer, start_of_line, m_width, m_character_width_determiner);
|
||||
}
|
||||
int cursor_screen_row() const { return m_cursor_screen_row; }
|
||||
int cursor_screen_column() const { return m_cursor_screen_column; }
|
||||
int cursor_virtual_column() const { return m_cursor_virtual_column; }
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Buffer> m_buffer;
|
||||
std::shared_ptr<Buffer::Iterator> m_iterator;
|
||||
std::shared_ptr<Buffer::Iterator> m_first_line;
|
||||
int m_first_line_row_offset;
|
||||
int m_width;
|
||||
int m_height;
|
||||
int m_scroll_offset;
|
||||
int m_cursor_screen_row;
|
||||
int m_cursor_screen_column;
|
||||
int m_cursor_virtual_column;
|
||||
int m_cursor_row_offset;
|
||||
std::shared_ptr<BufferLineWalker> m_buffer_line_walker;
|
||||
CharacterWidthDeterminer & m_character_width_determiner;
|
||||
|
||||
int effective_scroll_offset()
|
||||
{
|
||||
return std::min(m_scroll_offset, (m_height - 1) / 2);
|
||||
}
|
||||
void update();
|
||||
int calculate_rows_in_line(const Buffer::Iterator & start_of_line);
|
||||
int calculate_rows_in_cursor_line(const Buffer::Iterator & start_of_line);
|
||||
int screen_rows_below_line(const Buffer::Iterator & line);
|
||||
int calculate_rows_in_line(std::shared_ptr<Buffer::Iterator> start_of_line);
|
||||
int calculate_rows_in_cursor_line(std::shared_ptr<Buffer::Iterator> start_of_line);
|
||||
int screen_rows_below_line(std::shared_ptr<Buffer::Iterator> line);
|
||||
int screen_rows_above_line(
|
||||
const Buffer::Iterator & line,
|
||||
std::shared_ptr<Buffer::Iterator> line,
|
||||
std::list<std::pair<int, std::shared_ptr<Buffer::Iterator>>> & backward_lines);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user