From 11e885e5a5e31f75cfb18dd02907b86e3c40b8da Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 22 Nov 2012 18:54:37 +0100 Subject: [PATCH] Cleanups and minor refactoring on Buffer --- src/buffer.cc | 5 ----- src/buffer.hh | 34 ++++++++++++++-------------------- src/editor.cc | 4 ++-- src/unit_tests.cc | 8 ++++---- src/window.cc | 2 +- 5 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 0fc1709b..b8de1e37 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -53,11 +53,6 @@ BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column, return BufferIterator(*this, clamp(line_and_column, avoid_eol)); } -BufferCoord Buffer::line_and_column_at(const BufferIterator& iterator) const -{ - return iterator.m_coord; -} - ByteCount Buffer::line_length(LineCount line) const { assert(line < line_count()); diff --git a/src/buffer.hh b/src/buffer.hh index 05776689..e3c72665 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -70,13 +70,11 @@ public: const BufferCoord& coord() const { return m_coord; } LineCount line() const { return m_coord.line; } ByteCount column() const { return m_coord.column; } - -private: ByteCount offset() const; +private: safe_ptr m_buffer; BufferCoord m_coord; - friend class Buffer; }; class BufferChangeListener @@ -105,7 +103,6 @@ public: Buffer(String name, Flags flags, String initial_content = "\n"); Buffer(const Buffer&) = delete; - Buffer(Buffer&&) = delete; Buffer& operator= (const Buffer&) = delete; ~Buffer(); @@ -130,30 +127,19 @@ public: ByteCount character_count() const; LineCount line_count() const; ByteCount line_length(LineCount line) const; + const String& line_content(LineCount line) const + { return m_lines[line].content; } // returns an iterator at given coordinates. line_and_column is // clamped according to avoid_eol. BufferIterator iterator_at(const BufferCoord& line_and_column, bool avoid_eol = false) const; - BufferCoord line_and_column_at(const BufferIterator& iterator) const; // returns nearest valid coordinates from given ones // if avoid_eol, clamp to character before eol if line is not empty BufferCoord clamp(const BufferCoord& line_and_column, bool avoid_eol = false) const; - const String& name() const { return m_name; } - - // returns true if the buffer is in a different state than - // the last time it was saved - bool is_modified() const; - - // notify the buffer that it was saved in the current state - void notify_saved(); - - void add_change_listener(BufferChangeListener& listener) const; - void remove_change_listener(BufferChangeListener& listener) const; - // returns an iterator pointing to the first character of the line // iterator is on BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const; @@ -167,14 +153,23 @@ public: // the same but taking a line number instead of an iterator BufferIterator iterator_at_line_end(LineCount line) const; - const String& line_content(LineCount line) const - { return m_lines[line].content; } + const String& name() const { return m_name; } + + // returns true if the buffer is in a different state than + // the last time it was saved + bool is_modified() const; + + // notify the buffer that it was saved in the current state + void notify_saved(); OptionManager& options() { return m_options; } const OptionManager& options() const { return m_options; } HookManager& hooks() { return m_hooks; } const HookManager& hooks() const { return m_hooks; } + void add_change_listener(BufferChangeListener& listener) const; + void remove_change_listener(BufferChangeListener& listener) const; + private: friend class BufferIterator; @@ -189,7 +184,6 @@ private: }; struct LineList : std::vector { - public: Line& operator[](LineCount line) { return std::vector::operator[]((int)line); } diff --git a/src/editor.cc b/src/editor.cc index d4c6409d..e188ad87 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -136,7 +136,7 @@ void Editor::move_selections(LineCount offset, SelectMode mode) assert(mode == SelectMode::Replace or mode == SelectMode::Extend); for (auto& sel : m_selections) { - BufferCoord pos = m_buffer->line_and_column_at(sel.last()); + BufferCoord pos = sel.last().coord(); pos.line += offset; BufferIterator last = utf8::finish(m_buffer->iterator_at(pos, true)); sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last); @@ -448,7 +448,7 @@ void IncrementalInserter::move_cursors(const BufferCoord& offset) { for (auto& sel : m_editor.m_selections) { - BufferCoord pos = m_editor.m_buffer->line_and_column_at(sel.last()); + BufferCoord pos = sel.last().coord(); BufferIterator it = m_editor.m_buffer->iterator_at(pos + offset); sel = Selection(it, it); } diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 68474363..2936d9de 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -13,13 +13,13 @@ void test_buffer() BufferIterator i = buffer.begin(); assert(*i == 'a'); i += 6; - assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6}); + assert(i.coord() == BufferCoord{0 COMMA 6}); i += 1; - assert(buffer.line_and_column_at(i) == BufferCoord{1 COMMA 0}); + assert(i.coord() == BufferCoord{1 COMMA 0}); --i; - assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6}); + assert(i.coord() == BufferCoord{0 COMMA 6}); ++i; - assert(buffer.line_and_column_at(i) == BufferCoord{1 COMMA 0}); + assert(i.coord() == BufferCoord{1 COMMA 0}); buffer.insert(i, "tchou kanaky\n"); assert(buffer.line_count() == 5); diff --git a/src/window.cc b/src/window.cc index e5b61910..ae3e0ca5 100644 --- a/src/window.cc +++ b/src/window.cc @@ -161,7 +161,7 @@ DisplayCoord Window::display_position(const BufferIterator& iterator) String Window::status_line() const { - BufferCoord cursor = buffer().line_and_column_at(selections().back().last()); + BufferCoord cursor = selections().back().last().coord(); std::ostringstream oss; oss << buffer().name(); if (buffer().is_modified())