Cleanups and minor refactoring on Buffer

This commit is contained in:
Maxime Coste 2012-11-22 18:54:37 +01:00
parent 73c446e379
commit 11e885e5a5
5 changed files with 21 additions and 32 deletions

View File

@ -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());

View File

@ -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<const Buffer> 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<Line>
{
public:
Line& operator[](LineCount line)
{ return std::vector<Line>::operator[]((int)line); }

View File

@ -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);
}

View File

@ -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);

View File

@ -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())