Editor: refactor InsertMode::Replace

This commit is contained in:
Maxime Coste 2013-01-23 18:58:43 +01:00
parent 410067282a
commit 28cb78bed1

View File

@ -36,8 +36,13 @@ static BufferIterator prepare_insert(Buffer& buffer, const Selection& sel,
switch (mode) switch (mode)
{ {
case InsertMode::Insert: case InsertMode::Insert:
case InsertMode::Replace:
return sel.begin(); return sel.begin();
case InsertMode::Replace:
{
BufferIterator pos = sel.begin();
buffer.erase(sel.begin(), sel.end());
return pos;
}
case InsertMode::Append: case InsertMode::Append:
{ {
// special case for end of lines, append to current line instead // special case for end of lines, append to current line instead
@ -74,13 +79,6 @@ void Editor::insert(const String& string, InsertMode mode)
{ {
scoped_edition edition(*this); scoped_edition edition(*this);
if (mode == InsertMode::Replace)
{
// do not call Editor::erase as we do not want to avoid end of lines
for (auto& sel : m_selections)
m_buffer->erase(sel.begin(), sel.end());
}
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
BufferIterator pos = prepare_insert(*m_buffer, sel, mode); BufferIterator pos = prepare_insert(*m_buffer, sel, mode);
@ -97,12 +95,6 @@ void Editor::insert(const String& string, InsertMode mode)
void Editor::insert(const memoryview<String>& strings, InsertMode mode) void Editor::insert(const memoryview<String>& strings, InsertMode mode)
{ {
scoped_edition edition(*this); scoped_edition edition(*this);
if (mode == InsertMode::Replace)
{
// do not call Editor::erase as we do not want to avoid end of lines
for (auto& sel : m_selections)
m_buffer->erase(sel.begin(), sel.end());
}
if (strings.empty()) if (strings.empty())
return; return;
@ -413,19 +405,18 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
{ {
Buffer& buffer = *editor.m_buffer; Buffer& buffer = *editor.m_buffer;
if (mode == InsertMode::Replace)
{
for (auto& sel : editor.m_selections)
buffer.erase(sel.begin(), sel.end());
}
for (auto& sel : m_editor.m_selections) for (auto& sel : m_editor.m_selections)
{ {
utf8_it first, last; utf8_it first, last;
switch (mode) switch (mode)
{ {
case InsertMode::Insert: first = utf8_it(sel.end()) - 1; last = sel.begin(); break; case InsertMode::Insert: first = utf8_it(sel.end()) - 1; last = sel.begin(); break;
case InsertMode::Replace: first = utf8_it(sel.end()) - 1; last = sel.begin(); break; case InsertMode::Replace:
{
buffer.erase(sel.begin(), sel.end());
first = last = sel.begin();
break;
}
case InsertMode::Append: case InsertMode::Append:
{ {
first = sel.begin(); first = sel.begin();