Split up Cursor module into BufferIterator / BufferCursor
This commit is contained in:
parent
6f244e5c8f
commit
b880397b7e
@ -104,12 +104,12 @@ bool Buffer::write_to_file(const char * filename)
|
||||
|
||||
m_gap_buffer->compact();
|
||||
|
||||
Cursor start_of_line(&*m_gap_buffer, m_encoding, 4u);
|
||||
BufferCursor start_of_line(&*m_gap_buffer, m_encoding, 4u);
|
||||
size_t bytes_written = 0u;
|
||||
|
||||
while (start_of_line.valid())
|
||||
{
|
||||
Cursor cursor = start_of_line;
|
||||
BufferCursor cursor = start_of_line;
|
||||
cursor.go_end_of_line(true);
|
||||
size_t len = cursor.address() - start_of_line.address();
|
||||
if (len > 0u)
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "LineEndings.h"
|
||||
#include "Encoding.h"
|
||||
#include "GapBuffer.h"
|
||||
#include "Cursor.h"
|
||||
#include "BufferCursor.h"
|
||||
|
||||
class Buffer
|
||||
{
|
||||
@ -16,9 +16,9 @@ public:
|
||||
Buffer(const uint8_t * data, size_t data_length);
|
||||
bool write_to_file(const char * filename);
|
||||
|
||||
std::shared_ptr<Cursor> add_cursor()
|
||||
std::shared_ptr<BufferCursor> add_cursor()
|
||||
{
|
||||
return std::make_shared<Cursor>(&*m_gap_buffer, m_encoding, 4u);
|
||||
return std::make_shared<BufferCursor>(&*m_gap_buffer, m_encoding, 4u);
|
||||
}
|
||||
auto get_string() { return m_gap_buffer->get_string(); }
|
||||
|
||||
|
@ -1,73 +1,17 @@
|
||||
#include "Cursor.h"
|
||||
#include "BufferCursor.h"
|
||||
|
||||
void Iterator::go_forward()
|
||||
bool BufferCursor::is_start_of_line()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
m_offset += Encoding::num_bytes_in_code_point(m_encoding, address());
|
||||
}
|
||||
}
|
||||
|
||||
bool Iterator::check_go_forward()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
Iterator i2 = *this;
|
||||
i2.go_forward();
|
||||
if (i2.valid())
|
||||
{
|
||||
m_offset = i2.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Iterator::go_back()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
if (m_offset == 0u)
|
||||
{
|
||||
m_offset = 0xFFFFFFFFu;
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint8_t * a = m_gap_buffer->address(m_offset - 1u);
|
||||
const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_encoding, a);
|
||||
m_offset -= ((a - beginning_of_code_point) + 1u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Iterator::check_go_back()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
Iterator i2 = *this;
|
||||
i2.go_back();
|
||||
if (i2.valid())
|
||||
{
|
||||
m_offset = i2.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool Cursor::is_start_of_line()
|
||||
{
|
||||
Iterator i2 = m_iterator;
|
||||
BufferIterator i2 = m_iterator;
|
||||
if (!i2.check_go_back())
|
||||
{
|
||||
/* Iterator cannot go backwards so it is at beginning of buffer. */
|
||||
/* BufferIterator cannot go backwards so it is at beginning of buffer. */
|
||||
return true;
|
||||
}
|
||||
return *i2 == '\n';
|
||||
}
|
||||
|
||||
bool Cursor::is_end_of_line(bool allow_eol)
|
||||
bool BufferCursor::is_end_of_line(bool allow_eol)
|
||||
{
|
||||
if (*m_iterator == '\n')
|
||||
{
|
||||
@ -75,7 +19,7 @@ bool Cursor::is_end_of_line(bool allow_eol)
|
||||
}
|
||||
if (!allow_eol)
|
||||
{
|
||||
Iterator i2 = m_iterator;
|
||||
BufferIterator i2 = m_iterator;
|
||||
i2.go_forward();
|
||||
if (*i2 == '\n')
|
||||
{
|
||||
@ -85,10 +29,10 @@ bool Cursor::is_end_of_line(bool allow_eol)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Cursor::go_start_of_line()
|
||||
bool BufferCursor::go_start_of_line()
|
||||
{
|
||||
bool moved = false;
|
||||
Iterator i2 = m_iterator;
|
||||
BufferIterator i2 = m_iterator;
|
||||
for (;;)
|
||||
{
|
||||
i2.go_back();
|
||||
@ -109,7 +53,7 @@ bool Cursor::go_start_of_line()
|
||||
return moved;
|
||||
}
|
||||
|
||||
bool Cursor::go_end_of_line(bool allow_eol)
|
||||
bool BufferCursor::go_end_of_line(bool allow_eol)
|
||||
{
|
||||
bool moved = false;
|
||||
if (go_right(allow_eol))
|
||||
@ -123,7 +67,7 @@ bool Cursor::go_end_of_line(bool allow_eol)
|
||||
return moved;
|
||||
}
|
||||
|
||||
bool Cursor::go_left()
|
||||
bool BufferCursor::go_left()
|
||||
{
|
||||
if (!is_start_of_line())
|
||||
{
|
||||
@ -145,7 +89,7 @@ bool Cursor::go_left()
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Cursor::go_right(bool allow_eol)
|
||||
bool BufferCursor::go_right(bool allow_eol)
|
||||
{
|
||||
if (!is_end_of_line(allow_eol))
|
||||
{
|
||||
@ -167,9 +111,9 @@ bool Cursor::go_right(bool allow_eol)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Cursor::go_up(size_t target_column)
|
||||
bool BufferCursor::go_up(size_t target_column)
|
||||
{
|
||||
Cursor c2 = *this;
|
||||
BufferCursor c2 = *this;
|
||||
c2.go_start_of_line();
|
||||
if (!c2.m_iterator.check_go_back())
|
||||
{
|
||||
@ -183,9 +127,9 @@ bool Cursor::go_up(size_t target_column)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Cursor::go_down(size_t target_column)
|
||||
bool BufferCursor::go_down(size_t target_column)
|
||||
{
|
||||
Cursor c2 = *this;
|
||||
BufferCursor c2 = *this;
|
||||
c2.go_end_of_line(true);
|
||||
if (!c2.m_iterator.check_go_forward())
|
||||
{
|
||||
@ -198,7 +142,7 @@ bool Cursor::go_down(size_t target_column)
|
||||
return true;
|
||||
}
|
||||
|
||||
void Cursor::init_column()
|
||||
void BufferCursor::init_column()
|
||||
{
|
||||
if (*m_iterator == '\t')
|
||||
{
|
||||
@ -210,9 +154,9 @@ void Cursor::init_column()
|
||||
}
|
||||
}
|
||||
|
||||
void Cursor::calculate_column()
|
||||
void BufferCursor::calculate_column()
|
||||
{
|
||||
Cursor tmp_cursor = *this;
|
||||
BufferCursor tmp_cursor = *this;
|
||||
tmp_cursor.go_start_of_line();
|
||||
while (tmp_cursor < *this)
|
||||
{
|
||||
@ -221,7 +165,7 @@ void Cursor::calculate_column()
|
||||
m_column = tmp_cursor.m_column;
|
||||
}
|
||||
|
||||
void Cursor::forward_to_column(size_t target_column)
|
||||
void BufferCursor::forward_to_column(size_t target_column)
|
||||
{
|
||||
int32_t diff = abs((int32_t)(target_column - m_column));
|
||||
if (diff == 0)
|
51
src/core/BufferCursor.h
Normal file
51
src/core/BufferCursor.h
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef BufferCURSOR_H
|
||||
#define BufferCURSOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "Encoding.h"
|
||||
#include "GapBuffer.h"
|
||||
#include "BufferIterator.h"
|
||||
|
||||
class BufferCursor
|
||||
{
|
||||
public:
|
||||
BufferCursor(GapBuffer * gap_buffer, Encoding::Type encoding, uint8_t tabstop)
|
||||
: m_iterator(gap_buffer, encoding)
|
||||
{
|
||||
m_line = 0u;
|
||||
m_tabstop = tabstop;
|
||||
init_column();
|
||||
}
|
||||
bool is_start_of_line();
|
||||
bool is_end_of_line(bool allow_eol);
|
||||
bool go_start_of_line();
|
||||
bool go_end_of_line(bool allow_eol);
|
||||
bool go_left();
|
||||
bool go_right(bool allow_eol);
|
||||
bool go_up(size_t target_column);
|
||||
bool go_down(size_t target_column);
|
||||
bool operator<(const BufferCursor & other) const
|
||||
{
|
||||
return m_iterator < other.m_iterator;
|
||||
}
|
||||
uint32_t operator*() const { return *m_iterator; }
|
||||
uint8_t * address() const { return m_iterator.address(); }
|
||||
bool valid() const { return m_iterator.valid(); }
|
||||
size_t line() const { return m_line; }
|
||||
size_t column() const { return m_column; }
|
||||
BufferIterator & iterator() { return m_iterator; }
|
||||
void set_line(size_t line) { m_line = line; }
|
||||
void calculate_column();
|
||||
|
||||
protected:
|
||||
BufferIterator m_iterator;
|
||||
size_t m_line;
|
||||
size_t m_column;
|
||||
uint8_t m_tabstop;
|
||||
|
||||
void init_column();
|
||||
void forward_to_column(size_t target_column);
|
||||
};
|
||||
|
||||
#endif
|
56
src/core/BufferIterator.cc
Normal file
56
src/core/BufferIterator.cc
Normal file
@ -0,0 +1,56 @@
|
||||
#include "BufferIterator.h"
|
||||
|
||||
void BufferIterator::go_forward()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
m_offset += Encoding::num_bytes_in_code_point(m_encoding, address());
|
||||
}
|
||||
}
|
||||
|
||||
bool BufferIterator::check_go_forward()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
BufferIterator i2 = *this;
|
||||
i2.go_forward();
|
||||
if (i2.valid())
|
||||
{
|
||||
m_offset = i2.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void BufferIterator::go_back()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
if (m_offset == 0u)
|
||||
{
|
||||
m_offset = 0xFFFFFFFFu;
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint8_t * a = m_gap_buffer->address(m_offset - 1u);
|
||||
const uint8_t * beginning_of_code_point = Encoding::beginning_of_code_point(m_encoding, a);
|
||||
m_offset -= ((a - beginning_of_code_point) + 1u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool BufferIterator::check_go_back()
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
BufferIterator i2 = *this;
|
||||
i2.go_back();
|
||||
if (i2.valid())
|
||||
{
|
||||
m_offset = i2.m_offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
55
src/core/BufferIterator.h
Normal file
55
src/core/BufferIterator.h
Normal file
@ -0,0 +1,55 @@
|
||||
#ifndef BUFFERITERATOR_H
|
||||
#define BUFFERITERATOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "Encoding.h"
|
||||
#include "GapBuffer.h"
|
||||
|
||||
class BufferIterator
|
||||
{
|
||||
public:
|
||||
BufferIterator(GapBuffer * gap_buffer, Encoding::Type encoding)
|
||||
{
|
||||
m_gap_buffer = gap_buffer;
|
||||
m_offset = 0u;
|
||||
m_encoding = encoding;
|
||||
}
|
||||
bool valid() const
|
||||
{
|
||||
return m_offset < m_gap_buffer->size();
|
||||
}
|
||||
void go_forward();
|
||||
bool check_go_forward();
|
||||
void go_back();
|
||||
bool check_go_back();
|
||||
uint8_t * address() const
|
||||
{
|
||||
return m_gap_buffer->address(m_offset);
|
||||
}
|
||||
uint32_t operator*() const
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
return Encoding::decode(m_encoding, address());
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0xFFFFFFFFu;
|
||||
}
|
||||
}
|
||||
GapBuffer * gap_buffer() const { return m_gap_buffer; }
|
||||
bool operator<(const BufferIterator & other) const
|
||||
{
|
||||
return m_offset < other.m_offset;
|
||||
}
|
||||
size_t offset() { return m_offset; }
|
||||
void set_offset(size_t new_offset) { m_offset = new_offset; }
|
||||
|
||||
protected:
|
||||
GapBuffer * m_gap_buffer;
|
||||
size_t m_offset;
|
||||
Encoding::Type m_encoding;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,96 +0,0 @@
|
||||
#ifndef CURSOR_H
|
||||
#define CURSOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "Encoding.h"
|
||||
#include "GapBuffer.h"
|
||||
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
Iterator(GapBuffer * gap_buffer, Encoding::Type encoding)
|
||||
{
|
||||
m_gap_buffer = gap_buffer;
|
||||
m_offset = 0u;
|
||||
m_encoding = encoding;
|
||||
}
|
||||
bool valid() const
|
||||
{
|
||||
return m_offset < m_gap_buffer->size();
|
||||
}
|
||||
void go_forward();
|
||||
bool check_go_forward();
|
||||
void go_back();
|
||||
bool check_go_back();
|
||||
uint8_t * address() const
|
||||
{
|
||||
return m_gap_buffer->address(m_offset);
|
||||
}
|
||||
uint32_t operator*() const
|
||||
{
|
||||
if (valid())
|
||||
{
|
||||
return Encoding::decode(m_encoding, address());
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0xFFFFFFFFu;
|
||||
}
|
||||
}
|
||||
GapBuffer * gap_buffer() const { return m_gap_buffer; }
|
||||
bool operator<(const Iterator & other) const
|
||||
{
|
||||
return m_offset < other.m_offset;
|
||||
}
|
||||
size_t offset() { return m_offset; }
|
||||
void set_offset(size_t new_offset) { m_offset = new_offset; }
|
||||
|
||||
protected:
|
||||
GapBuffer * m_gap_buffer;
|
||||
size_t m_offset;
|
||||
Encoding::Type m_encoding;
|
||||
};
|
||||
|
||||
class Cursor
|
||||
{
|
||||
public:
|
||||
Cursor(GapBuffer * gap_buffer, Encoding::Type encoding, uint8_t tabstop)
|
||||
: m_iterator(gap_buffer, encoding)
|
||||
{
|
||||
m_line = 0u;
|
||||
m_tabstop = tabstop;
|
||||
init_column();
|
||||
}
|
||||
bool is_start_of_line();
|
||||
bool is_end_of_line(bool allow_eol);
|
||||
bool go_start_of_line();
|
||||
bool go_end_of_line(bool allow_eol);
|
||||
bool go_left();
|
||||
bool go_right(bool allow_eol);
|
||||
bool go_up(size_t target_column);
|
||||
bool go_down(size_t target_column);
|
||||
bool operator<(const Cursor & other) const
|
||||
{
|
||||
return m_iterator < other.m_iterator;
|
||||
}
|
||||
uint32_t operator*() const { return *m_iterator; }
|
||||
uint8_t * address() const { return m_iterator.address(); }
|
||||
bool valid() const { return m_iterator.valid(); }
|
||||
size_t line() const { return m_line; }
|
||||
size_t column() const { return m_column; }
|
||||
Iterator & iterator() { return m_iterator; }
|
||||
void set_line(size_t line) { m_line = line; }
|
||||
void calculate_column();
|
||||
|
||||
protected:
|
||||
Iterator m_iterator;
|
||||
size_t m_line;
|
||||
size_t m_column;
|
||||
uint8_t m_tabstop;
|
||||
|
||||
void init_column();
|
||||
void forward_to_column(size_t target_column);
|
||||
};
|
||||
|
||||
#endif
|
@ -29,7 +29,7 @@ void BufferPane::update_cursor_row()
|
||||
|
||||
int BufferPane::screen_rows_below_cursor(int stop_at)
|
||||
{
|
||||
Cursor cursor = *m_cursor;
|
||||
BufferCursor cursor = *m_cursor;
|
||||
size_t column = cursor.column();
|
||||
cursor.go_end_of_line(false);
|
||||
int rows = (cursor.column() / m_columns) - (column / m_columns);
|
||||
@ -43,11 +43,11 @@ int BufferPane::screen_rows_below_cursor(int stop_at)
|
||||
|
||||
int BufferPane::screen_rows_above_cursor(int stop_at)
|
||||
{
|
||||
Cursor cursor = *m_cursor;
|
||||
BufferCursor cursor = *m_cursor;
|
||||
int rows = cursor.column() / m_columns;
|
||||
while ((rows < stop_at) && cursor.go_up(0u))
|
||||
{
|
||||
Cursor cursor2 = cursor;
|
||||
BufferCursor cursor2 = cursor;
|
||||
cursor2.go_end_of_line(false);
|
||||
rows += (cursor2.column() / m_columns) + 1;
|
||||
}
|
||||
@ -58,11 +58,11 @@ void BufferPane::draw()
|
||||
{
|
||||
update_cursor_row();
|
||||
int screen_row = m_cursor_row - m_cursor->column() / m_columns;
|
||||
Cursor iter_cursor = *m_cursor;
|
||||
BufferCursor iter_cursor = *m_cursor;
|
||||
iter_cursor.go_start_of_line();
|
||||
while ((screen_row > 0) && iter_cursor.go_up(0u))
|
||||
{
|
||||
Cursor cursor2 = iter_cursor;
|
||||
BufferCursor cursor2 = iter_cursor;
|
||||
cursor2.go_end_of_line(false);
|
||||
screen_row -= (1 + cursor2.column() / m_columns);
|
||||
}
|
||||
@ -78,9 +78,9 @@ void BufferPane::draw()
|
||||
}
|
||||
}
|
||||
|
||||
void BufferPane::draw_buffer_line(int screen_row, const Cursor & cursor)
|
||||
void BufferPane::draw_buffer_line(int screen_row, const BufferCursor & cursor)
|
||||
{
|
||||
Cursor iter_cursor = cursor;
|
||||
BufferCursor iter_cursor = cursor;
|
||||
while (!iter_cursor.is_end_of_line(true))
|
||||
{
|
||||
int draw_row = screen_row + (iter_cursor.column() / m_columns);
|
||||
|
@ -15,7 +15,7 @@ public:
|
||||
std::shared_ptr<GL> gl);
|
||||
void resize(int width, int height) override;
|
||||
void draw();
|
||||
std::shared_ptr<Cursor> cursor() { return m_cursor; }
|
||||
std::shared_ptr<BufferCursor> cursor() { return m_cursor; }
|
||||
|
||||
protected:
|
||||
int effective_scroll_offset()
|
||||
@ -25,7 +25,7 @@ protected:
|
||||
int screen_rows_below_cursor(int stop_at);
|
||||
int screen_rows_above_cursor(int stop_at);
|
||||
void update_cursor_row();
|
||||
void draw_buffer_line(int screen_row, const Cursor & cursor);
|
||||
void draw_buffer_line(int screen_row, const BufferCursor & cursor);
|
||||
void draw_buffer_character(int screen_column, int screen_row, uint32_t character);
|
||||
void draw_cursor(int screen_column, int screen_row, bool insert_mode);
|
||||
void colrow_to_xy(int col, int row, int * x, int * y);
|
||||
@ -37,7 +37,7 @@ protected:
|
||||
int m_columns;
|
||||
int m_scroll_offset;
|
||||
int m_cursor_row;
|
||||
std::shared_ptr<Cursor> m_cursor;
|
||||
std::shared_ptr<BufferCursor> m_cursor;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user