move much of the buffer layout code from BufferPane to BufferView
This commit is contained in:
1 parent
545e73bd07
commit
8e0796decf
3 files changed
+209
-1
No files matched your search
@@ -1,4 +1,6 @@
|
||||
#include "BufferView.h"
|
||||
#include <list>
|
||||
#include <utility>
|
||||
|
||||
BufferView::BufferView(std::shared_ptr<Buffer> buffer,
|
||||
std::shared_ptr<Buffer::Iterator> iterator,
|
||||
@@ -8,6 +10,9 @@ BufferView::BufferView(std::shared_ptr<Buffer> buffer,
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -21,3 +26,123 @@ 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)
|
||||
{
|
||||
if (m_iterator->valid())
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* Internal functions
|
||||
*************************************************************************/
|
||||
|
||||
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();
|
||||
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 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);
|
||||
m_cursor_screen_row = std::max(min_rows_to_leave_above, m_cursor_screen_row);
|
||||
m_cursor_screen_row = std::max(m_height - rows_below - 1, m_cursor_screen_row);
|
||||
m_cursor_screen_row = std::min(rows_above, m_cursor_screen_row);
|
||||
|
||||
/* Determine the first line that is visible in this view. */
|
||||
m_first_line = std::make_shared<Buffer::Iterator>(*m_iterator);
|
||||
m_first_line_row_offset = m_cursor_screen_row - m_cursor_row_offset;
|
||||
if (m_first_line_row_offset <= 0)
|
||||
{
|
||||
m_first_line->go_start_of_line();
|
||||
}
|
||||
else for (auto rows_iterator_pair : backward_lines)
|
||||
{
|
||||
m_first_line_row_offset -= rows_iterator_pair.first;
|
||||
m_first_line = rows_iterator_pair.second;
|
||||
if (m_first_line_row_offset <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int BufferView::calculate_rows_in_line(const 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))
|
||||
{
|
||||
saved_row_offset = row_offset;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return saved_row_offset + 1;
|
||||
}
|
||||
|
||||
int BufferView::calculate_rows_in_cursor_line(const 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)
|
||||
{
|
||||
m_cursor_virtual_column = virtual_column;
|
||||
m_cursor_screen_column = screen_column;
|
||||
m_cursor_row_offset = row_offset;
|
||||
}
|
||||
if ((code_point != '\n') &&
|
||||
(code_point != Buffer::Iterator::INVALID_CODE_POINT))
|
||||
{
|
||||
saved_row_offset = row_offset;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return saved_row_offset + 1;
|
||||
}
|
||||
|
||||
int BufferView::screen_rows_below_line(const Buffer::Iterator & line)
|
||||
{
|
||||
Buffer::Iterator i = line;
|
||||
int rows = 0;
|
||||
while ((rows < m_height) && i.go_next_line())
|
||||
{
|
||||
rows += calculate_rows_in_line(i);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
int BufferView::screen_rows_above_line(
|
||||
const Buffer::Iterator & line,
|
||||
std::list<std::pair<int, std::shared_ptr<Buffer::Iterator>>> & backward_lines)
|
||||
{
|
||||
Buffer::Iterator i = line;
|
||||
int rows = 0;
|
||||
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)));
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
+17
-1
@@ -18,25 +18,41 @@ public:
|
||||
CharacterWidthDeterminer & character_width_determiner);
|
||||
void resize(int width, int height);
|
||||
void set_scroll_offset(int scroll_offset);
|
||||
void iter_lines(std::function<bool(const Buffer::Iterator &)> callback);
|
||||
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)
|
||||
{
|
||||
m_buffer_line_walker->walk_line(start_of_line, callback);
|
||||
}
|
||||
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::Iterator> m_iterator;
|
||||
std::shared_ptr<Buffer::Iterator> m_first_line;
|
||||
int m_first_line_row_offset;
|
||||
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;
|
||||
|
||||
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 screen_rows_above_line(
|
||||
const Buffer::Iterator & line,
|
||||
std::list<std::pair<int, std::shared_ptr<Buffer::Iterator>>> & backward_lines);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user