Rework PieceTable cursor management

- rename PieceTable::Position -> PieceTable::Cursor
- remove built-in cursor
- add PieceTable::add_cursor()
This commit is contained in:
Josh Holtrop 2016-07-31 22:11:12 -04:00
parent a5bfcdd3c4
commit 8beea3461c
2 changed files with 23 additions and 7 deletions

View File

@ -9,8 +9,6 @@ PieceTable::PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_si
start_piece->next = end_piece;
end_piece->prev = start_piece;
m_piece_index = 2u;
cursor_position.piece = end_piece;
cursor_position.offset = 0u;
}
void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol)
@ -26,8 +24,6 @@ void PieceTable::append_initial_line_piece(uint8_t * start, uint32_t length, uin
end_piece->prev->next = piece;
end_piece->prev = piece;
m_num_lines++;
if (cursor_position.piece == end_piece)
cursor_position.piece = piece;
}
PieceTable::Piece * PieceTable::get_start_of_line(PieceTable::Piece * start) const
@ -41,3 +37,16 @@ PieceTable::Piece * PieceTable::get_start_of_line(PieceTable::Piece * start) con
return piece;
}
std::shared_ptr<PieceTable::Cursor> PieceTable::add_cursor()
{
auto cursor = std::make_shared<Cursor>();
cursor->piece = start_piece->next;
cursor->line_number = 1u;
cursor->offset = 0u;
m_cursors.push_back(cursor);
return cursor;
}

View File

@ -4,6 +4,8 @@
#include <stdint.h>
#include "PagedBuffer.h"
#include <utility>
#include <memory>
#include <list>
class PieceTable
{
@ -49,13 +51,16 @@ public:
void toggle_eol() { n_chars ^= N_CHARS_EOL_FLAG; }
};
struct Position
struct Cursor
{
/** The piece. */
Piece * piece;
/** Byte offset within the piece. */
uint32_t offset;
/** Line number the cursor is on. */
uint32_t line_number;
};
PieceTable(const uint8_t * file_buffer, unsigned long file_buffer_size);
@ -75,12 +80,12 @@ public:
Piece * start_piece;
Piece * end_piece;
Position cursor_position;
void append_initial_line_piece(uint8_t * start, uint32_t length, uint32_t n_chars, bool eol);
Piece * get_start_of_line(Piece * start) const;
std::shared_ptr<Cursor> add_cursor();
protected:
const uint8_t * m_file_buffer;
unsigned long m_file_buffer_size;
@ -91,6 +96,8 @@ protected:
PagedBuffer<uint8_t> m_append_buffer;
PagedBuffer<Piece> m_pieces;
std::list<std::shared_ptr<Cursor>> m_cursors;
};
#endif