Cleanups and minor refactoring on Buffer
This commit is contained in:
parent
73c446e379
commit
11e885e5a5
|
@ -53,11 +53,6 @@ BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column,
|
||||||
return BufferIterator(*this, clamp(line_and_column, avoid_eol));
|
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
|
ByteCount Buffer::line_length(LineCount line) const
|
||||||
{
|
{
|
||||||
assert(line < line_count());
|
assert(line < line_count());
|
||||||
|
|
|
@ -70,13 +70,11 @@ public:
|
||||||
const BufferCoord& coord() const { return m_coord; }
|
const BufferCoord& coord() const { return m_coord; }
|
||||||
LineCount line() const { return m_coord.line; }
|
LineCount line() const { return m_coord.line; }
|
||||||
ByteCount column() const { return m_coord.column; }
|
ByteCount column() const { return m_coord.column; }
|
||||||
|
|
||||||
private:
|
|
||||||
ByteCount offset() const;
|
ByteCount offset() const;
|
||||||
|
|
||||||
|
private:
|
||||||
safe_ptr<const Buffer> m_buffer;
|
safe_ptr<const Buffer> m_buffer;
|
||||||
BufferCoord m_coord;
|
BufferCoord m_coord;
|
||||||
friend class Buffer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class BufferChangeListener
|
class BufferChangeListener
|
||||||
|
@ -105,7 +103,6 @@ public:
|
||||||
|
|
||||||
Buffer(String name, Flags flags, String initial_content = "\n");
|
Buffer(String name, Flags flags, String initial_content = "\n");
|
||||||
Buffer(const Buffer&) = delete;
|
Buffer(const Buffer&) = delete;
|
||||||
Buffer(Buffer&&) = delete;
|
|
||||||
Buffer& operator= (const Buffer&) = delete;
|
Buffer& operator= (const Buffer&) = delete;
|
||||||
~Buffer();
|
~Buffer();
|
||||||
|
|
||||||
|
@ -130,30 +127,19 @@ public:
|
||||||
ByteCount character_count() const;
|
ByteCount character_count() const;
|
||||||
LineCount line_count() const;
|
LineCount line_count() const;
|
||||||
ByteCount line_length(LineCount line) 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
|
// returns an iterator at given coordinates. line_and_column is
|
||||||
// clamped according to avoid_eol.
|
// clamped according to avoid_eol.
|
||||||
BufferIterator iterator_at(const BufferCoord& line_and_column,
|
BufferIterator iterator_at(const BufferCoord& line_and_column,
|
||||||
bool avoid_eol = false) const;
|
bool avoid_eol = false) const;
|
||||||
BufferCoord line_and_column_at(const BufferIterator& iterator) const;
|
|
||||||
|
|
||||||
// returns nearest valid coordinates from given ones
|
// returns nearest valid coordinates from given ones
|
||||||
// if avoid_eol, clamp to character before eol if line is not empty
|
// if avoid_eol, clamp to character before eol if line is not empty
|
||||||
BufferCoord clamp(const BufferCoord& line_and_column,
|
BufferCoord clamp(const BufferCoord& line_and_column,
|
||||||
bool avoid_eol = false) const;
|
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
|
// returns an iterator pointing to the first character of the line
|
||||||
// iterator is on
|
// iterator is on
|
||||||
BufferIterator iterator_at_line_begin(const BufferIterator& iterator) const;
|
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
|
// the same but taking a line number instead of an iterator
|
||||||
BufferIterator iterator_at_line_end(LineCount line) const;
|
BufferIterator iterator_at_line_end(LineCount line) const;
|
||||||
|
|
||||||
const String& line_content(LineCount line) const
|
const String& name() const { return m_name; }
|
||||||
{ return m_lines[line].content; }
|
|
||||||
|
// 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; }
|
OptionManager& options() { return m_options; }
|
||||||
const OptionManager& options() const { return m_options; }
|
const OptionManager& options() const { return m_options; }
|
||||||
HookManager& hooks() { return m_hooks; }
|
HookManager& hooks() { return m_hooks; }
|
||||||
const HookManager& hooks() const { 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:
|
private:
|
||||||
friend class BufferIterator;
|
friend class BufferIterator;
|
||||||
|
|
||||||
|
@ -189,7 +184,6 @@ private:
|
||||||
};
|
};
|
||||||
struct LineList : std::vector<Line>
|
struct LineList : std::vector<Line>
|
||||||
{
|
{
|
||||||
public:
|
|
||||||
Line& operator[](LineCount line)
|
Line& operator[](LineCount line)
|
||||||
{ return std::vector<Line>::operator[]((int)line); }
|
{ return std::vector<Line>::operator[]((int)line); }
|
||||||
|
|
||||||
|
|
|
@ -136,7 +136,7 @@ void Editor::move_selections(LineCount offset, SelectMode mode)
|
||||||
assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
|
assert(mode == SelectMode::Replace or mode == SelectMode::Extend);
|
||||||
for (auto& sel : m_selections)
|
for (auto& sel : m_selections)
|
||||||
{
|
{
|
||||||
BufferCoord pos = m_buffer->line_and_column_at(sel.last());
|
BufferCoord pos = sel.last().coord();
|
||||||
pos.line += offset;
|
pos.line += offset;
|
||||||
BufferIterator last = utf8::finish(m_buffer->iterator_at(pos, true));
|
BufferIterator last = utf8::finish(m_buffer->iterator_at(pos, true));
|
||||||
sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last);
|
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)
|
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);
|
BufferIterator it = m_editor.m_buffer->iterator_at(pos + offset);
|
||||||
sel = Selection(it, it);
|
sel = Selection(it, it);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,13 +13,13 @@ void test_buffer()
|
||||||
BufferIterator i = buffer.begin();
|
BufferIterator i = buffer.begin();
|
||||||
assert(*i == 'a');
|
assert(*i == 'a');
|
||||||
i += 6;
|
i += 6;
|
||||||
assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6});
|
assert(i.coord() == BufferCoord{0 COMMA 6});
|
||||||
i += 1;
|
i += 1;
|
||||||
assert(buffer.line_and_column_at(i) == BufferCoord{1 COMMA 0});
|
assert(i.coord() == BufferCoord{1 COMMA 0});
|
||||||
--i;
|
--i;
|
||||||
assert(buffer.line_and_column_at(i) == BufferCoord{0 COMMA 6});
|
assert(i.coord() == BufferCoord{0 COMMA 6});
|
||||||
++i;
|
++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");
|
buffer.insert(i, "tchou kanaky\n");
|
||||||
assert(buffer.line_count() == 5);
|
assert(buffer.line_count() == 5);
|
||||||
|
|
||||||
|
|
|
@ -161,7 +161,7 @@ DisplayCoord Window::display_position(const BufferIterator& iterator)
|
||||||
|
|
||||||
String Window::status_line() const
|
String Window::status_line() const
|
||||||
{
|
{
|
||||||
BufferCoord cursor = buffer().line_and_column_at(selections().back().last());
|
BufferCoord cursor = selections().back().last().coord();
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << buffer().name();
|
oss << buffer().name();
|
||||||
if (buffer().is_modified())
|
if (buffer().is_modified())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user