Buffer: replace insert and erase methods with modify

modify directly takes a Modification as parameter
This commit is contained in:
Maxime Coste 2011-12-07 14:26:40 +00:00
parent 2edddfe009
commit c40eb2b03a
4 changed files with 28 additions and 28 deletions

View File

@ -37,17 +37,6 @@ Buffer::~Buffer()
assert(m_modification_listeners.empty());
}
void Buffer::erase(const BufferIterator& begin, const BufferIterator& end)
{
append_modification(Modification(Modification::Erase, begin,
string(begin, end)));
}
void Buffer::insert(const BufferIterator& position, const BufferString& string)
{
append_modification(Modification(Modification::Insert, position, string));
}
BufferIterator Buffer::iterator_at(const BufferCoord& line_and_column) const
{
if (m_lines.empty())
@ -227,7 +216,7 @@ void Buffer::apply_modification(const Modification& modification)
listener->on_modification(modification);
}
void Buffer::append_modification(Modification&& modification)
void Buffer::modify(Modification&& modification)
{
for (auto filter : m_filters)
filter.second(*this, modification);

View File

@ -86,10 +86,14 @@ struct Modification
BufferString content;
Modification(Type type, BufferIterator position,
BufferString content)
const BufferString& content)
: type(type), position(position), content(content) {}
Modification inverse() const;
static Modification make_erase(BufferIterator begin, BufferIterator end);
static Modification make_insert(BufferIterator position,
const BufferString& content);
};
class ModificationListener
@ -117,15 +121,11 @@ public:
void begin_undo_group();
void end_undo_group();
void modify(Modification&& modification);
bool undo();
bool redo();
void erase(const BufferIterator& begin,
const BufferIterator& end);
void insert(const BufferIterator& position,
const BufferString& string);
BufferString string(const BufferIterator& begin,
const BufferIterator& end) const;
@ -200,8 +200,6 @@ private:
void apply_modification(const Modification& modification);
void revert_modification(const Modification& modification);
void append_modification(Modification&& modification);
std::list<std::unique_ptr<Window>> m_windows;
size_t m_last_save_undo_index;
@ -211,6 +209,18 @@ private:
idvaluemap<std::string, FilterFunc> m_filters;
};
inline Modification Modification::make_erase(BufferIterator begin,
BufferIterator end)
{
return Modification(Erase, begin, begin.buffer().string(begin, end));
}
inline Modification Modification::make_insert(BufferIterator position,
const BufferString& content)
{
return Modification(Insert, position, content);
}
}
#include "buffer_iterator.inl.hh"

View File

@ -21,7 +21,7 @@ static Buffer& get_or_create_debug_buffer()
void write_debug(const std::string& str)
{
Buffer& debug_buffer = get_or_create_debug_buffer();
debug_buffer.insert(debug_buffer.end()-1, str);
debug_buffer.modify(Modification::make_insert(debug_buffer.end()-1, str));
}
}

View File

@ -90,7 +90,7 @@ void Window::erase_noundo()
{
check_invariant();
for (auto& sel : m_selections)
m_buffer.erase(sel.begin(), sel.end());
m_buffer.modify(Modification::make_erase(sel.begin(), sel.end()));
scroll_to_keep_cursor_visible_ifn();
}
@ -126,7 +126,7 @@ void Window::insert(const String& string)
void Window::insert_noundo(const String& string)
{
for (auto& sel : m_selections)
m_buffer.insert(sel.begin(), string);
m_buffer.modify(Modification::make_insert(sel.begin(), string));
scroll_to_keep_cursor_visible_ifn();
}
@ -139,7 +139,7 @@ void Window::append(const String& string)
void Window::append_noundo(const String& string)
{
for (auto& sel : m_selections)
m_buffer.insert(sel.end(), string);
m_buffer.modify(Modification::make_insert(sel.end(), string));
scroll_to_keep_cursor_visible_ifn();
}
@ -394,14 +394,14 @@ IncrementalInserter::IncrementalInserter(Window& window, Mode mode)
case Mode::AppendAtLineEnd:
pos = m_window.m_buffer.iterator_at_line_end(sel.end() - 1) - 1;
if (mode == Mode::OpenLineBelow)
window.m_buffer.insert(pos, "\n");
window.m_buffer.modify(Modification::make_insert(pos, "\n"));
break;
case Mode::OpenLineAbove:
case Mode::InsertAtLineBegin:
pos = m_window.m_buffer.iterator_at_line_begin(sel.begin());
if (mode == Mode::OpenLineAbove)
window.m_buffer.insert(--pos, "\n");
window.m_buffer.modify(Modification::make_insert(--pos, "\n"));
break;
}
sel = Selection(pos, pos, sel.captures());
@ -425,7 +425,8 @@ void IncrementalInserter::insert(const Window::String& string)
void IncrementalInserter::insert_capture(size_t index)
{
for (auto& sel : m_window.m_selections)
m_window.m_buffer.insert(sel.begin(), sel.capture(index));
m_window.m_buffer.modify(Modification::make_insert(sel.begin(),
sel.capture(index)));
m_window.scroll_to_keep_cursor_visible_ifn();
}