Editor refactoring, merge undo and batch management

This commit is contained in:
Maxime Coste 2012-02-07 23:41:10 +00:00
parent 333e87dedd
commit 04a37d8895
5 changed files with 45 additions and 94 deletions

View File

@ -6,24 +6,9 @@
namespace Kakoune
{
namespace
{
struct scoped_undo_group
{
scoped_undo_group(Buffer& buffer)
: m_buffer(buffer) { m_buffer.begin_undo_group(); }
~scoped_undo_group() { m_buffer.end_undo_group(); }
private:
Buffer& m_buffer;
};
}
Editor::Editor(Buffer& buffer)
: m_buffer(buffer),
m_batch_level(0)
m_edition_level(0)
{
m_selections.push_back(SelectionList());
selections().push_back(Selection(buffer.begin(), buffer.begin()));
@ -31,69 +16,30 @@ Editor::Editor(Buffer& buffer)
void Editor::erase()
{
if (not is_in_batch())
{
scoped_undo_group undo_group(m_buffer);
erase_noundo();
}
else
erase_noundo();
}
void Editor::erase_noundo()
{
check_invariant();
scoped_edition edition(*this);
for (auto& sel : selections())
m_buffer.modify(Modification::make_erase(sel.begin(), sel.end()));
}
void Editor::insert(const String& string)
{
if (not is_in_batch())
{
scoped_undo_group undo_group(m_buffer);
insert_noundo(string);
}
else
insert_noundo(string);
}
void Editor::insert_noundo(const String& string)
{
scoped_edition edition(*this);
for (auto& sel : selections())
m_buffer.modify(Modification::make_insert(sel.begin(), string));
}
void Editor::append(const String& string)
{
if (not is_in_batch())
{
scoped_undo_group undo_group(m_buffer);
append_noundo(string);
}
else
append_noundo(string);
}
void Editor::append_noundo(const String& string)
{
scoped_edition edition(*this);
for (auto& sel : selections())
m_buffer.modify(Modification::make_insert(sel.end(), string));
}
void Editor::replace(const std::string& string)
{
if (not is_in_batch())
{
scoped_undo_group undo_group(m_buffer);
erase_noundo();
insert_noundo(string);
}
else
{
erase_noundo();
insert_noundo(string);
}
scoped_edition edition(*this);
erase();
insert(string);
}
void Editor::push_selections()
@ -236,35 +182,30 @@ CandidateList Editor::complete_filterid(const std::string& prefix,
return m_filters.complete_id<str_to_str>(prefix, cursor_pos);
}
void Editor::begin_batch()
void Editor::begin_edition()
{
++m_batch_level;
++m_edition_level;
if (m_batch_level == 1)
{
if (m_edition_level == 1)
m_buffer.begin_undo_group();
on_begin_batch();
}
}
void Editor::end_batch()
void Editor::end_edition()
{
assert(m_batch_level > 0);
if (m_batch_level == 1)
{
on_end_batch();
assert(m_edition_level > 0);
if (m_edition_level == 1)
m_buffer.end_undo_group();
}
--m_batch_level;
--m_edition_level;
}
IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
: m_editor(editor)
: m_editor(editor), m_edition(editor)
{
m_editor.begin_batch();
m_editor.on_incremental_insertion_begin();
if (mode == Mode::Change)
editor.erase_noundo();
editor.erase();
for (auto& sel : m_editor.selections())
{
@ -297,7 +238,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
IncrementalInserter::~IncrementalInserter()
{
move_cursors(BufferCoord(0, -1));
m_editor.end_batch();
m_editor.on_incremental_insertion_end();
}
void IncrementalInserter::apply(Modification&& modification) const

View File

@ -55,31 +55,40 @@ public:
CandidateList complete_filterid(const std::string& prefix,
size_t cursor_pos = std::string::npos);
void begin_batch();
void end_batch();
bool is_in_batch() const { return m_batch_level != 0; }
bool is_editing() const { return m_edition_level!= 0; }
private:
void erase_noundo();
void insert_noundo(const String& string);
void append_noundo(const String& string);
friend class scoped_edition;
void begin_edition();
void end_edition();
int m_edition_level;
SelectionList& selections() { return m_selections.back(); }
void check_invariant() const;
friend class IncrementalInserter;
int m_batch_level;
virtual void on_begin_batch() {}
virtual void on_end_batch() {}
virtual void on_incremental_insertion_begin() {}
virtual void on_incremental_insertion_end() {}
Buffer& m_buffer;
std::vector<SelectionList> m_selections;
idvaluemap<std::string, FilterFunc> m_filters;
};
struct scoped_edition
{
scoped_edition(Editor& editor)
: m_editor(editor)
{ m_editor.begin_edition(); }
~scoped_edition()
{ m_editor.end_edition(); }
private:
Editor& m_editor;
};
// An IncrementalInserter manage insert mode
class IncrementalInserter
{
@ -109,6 +118,7 @@ private:
void apply(Modification&& modification) const;
Editor& m_editor;
scoped_edition m_edition;
};
}

View File

@ -1054,8 +1054,8 @@ void exec_keys(const KeyList& keys,
Editor& editor = context.has_window() ? static_cast<Editor&>(context.window())
: static_cast<Editor&>(batch_editor);
editor.begin_batch();
auto end_batch = on_scope_end([&]() { editor.end_batch(); });
scoped_edition edition(editor);
int count = 0;
while(pos < keys.size())

View File

@ -154,12 +154,12 @@ std::string Window::status_line() const
oss << " [+]";
oss << " -- " << cursor.line+1 << "," << cursor.column+1
<< " -- " << selections().size() << " sel -- ";
if (is_in_batch())
if (is_editing())
oss << "[Insert]";
return oss.str();
}
void Window::on_end_batch()
void Window::on_incremental_insertion_end()
{
push_selections();
hooks_manager().run_hook("InsertEnd", "", Context(*this));

View File

@ -50,7 +50,7 @@ private:
Window(Buffer& buffer);
Window(const Window&) = delete;
void on_end_batch();
void on_incremental_insertion_end();
void scroll_to_keep_cursor_visible_ifn();