diff --git a/src/core/GapBuffer.cc b/src/core/GapBuffer.cc index 7c55ed9..0d97caf 100644 --- a/src/core/GapBuffer.cc +++ b/src/core/GapBuffer.cc @@ -46,19 +46,34 @@ void GapBuffer::compact() /** * Insert code_point into the gap buffer at position position. * - * @param position Position in the gap buffer to insert the code point at. - * Must be <= size. + * @param insert_cursor Cursor in the gap buffer to insert the code point at. * @param code_point The code point to insert. */ -void GapBuffer::insert(size_t position, uint32_t code_point) +void GapBuffer::insert(Cursor & insert_cursor, uint32_t code_point) { - if (position > m_size) - return; + size_t position = insert_cursor.iterator().offset(); check_grow(); move_gap(position); size_t size = Encoding::encode(code_point, m_encoding, &m_buffer[m_gap_position]); m_gap_position += size; m_size += size; + + /* Now adjust any cursors that were >= position. */ + for (auto cursor : m_cursors) + { + if (cursor->iterator().offset() >= position) + { + cursor->iterator().set_offset(cursor->iterator().offset() + size); + if (cursor->line() == insert_cursor.line()) + { + cursor->calculate_column(); + } + if (code_point == '\n') + { + cursor->set_line(cursor->line() + 1u); + } + } + } } /** diff --git a/src/core/GapBuffer.h b/src/core/GapBuffer.h index 0100b89..1158ddf 100644 --- a/src/core/GapBuffer.h +++ b/src/core/GapBuffer.h @@ -46,6 +46,8 @@ public: { 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; @@ -78,6 +80,9 @@ public: 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; @@ -85,7 +90,6 @@ public: size_t m_column; void init_column(); - void calculate_column(); void forward_to_column(size_t target_column); }; @@ -110,7 +114,7 @@ public: } size_t gap_size() const { return m_buffer_size - m_size; } void compact(); - void insert(size_t position, uint32_t code_point); + void insert(Cursor & insert_cursor, uint32_t code_point); protected: uint8_t * m_buffer;