IncrementalInserter preserves selections

This commit is contained in:
Maxime Coste 2012-07-16 21:21:56 +02:00
parent 5a671db12c
commit fe988868e5
2 changed files with 22 additions and 16 deletions

View File

@ -265,7 +265,7 @@ void Editor::end_edition()
} }
IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode) IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
: m_editor(editor), m_edition(editor) : m_editor(editor), m_edition(editor), m_mode(mode)
{ {
m_editor.on_incremental_insertion_begin(); m_editor.on_incremental_insertion_begin();
@ -274,43 +274,48 @@ IncrementalInserter::IncrementalInserter(Editor& editor, Mode mode)
for (auto& sel : m_editor.m_selections.back()) for (auto& sel : m_editor.m_selections.back())
{ {
BufferIterator pos; BufferIterator first, last;
switch (mode) switch (mode)
{ {
case Mode::Insert: pos = sel.begin(); break; case Mode::Insert: first = sel.end()-1; last = sel.begin(); break;
case Mode::Append: pos = sel.end(); break; case Mode::Change: first = sel.end()-1; last = sel.begin(); break;
case Mode::Change: pos = sel.begin(); break; case Mode::Append: first = sel.begin(); last = sel.end(); break;
case Mode::OpenLineBelow: case Mode::OpenLineBelow:
case Mode::AppendAtLineEnd: case Mode::AppendAtLineEnd:
pos = m_editor.m_buffer.iterator_at_line_end(sel.end() - 1) - 1; first = m_editor.m_buffer.iterator_at_line_end(sel.end() - 1) - 1;
last = first;
break; break;
case Mode::OpenLineAbove: case Mode::OpenLineAbove:
case Mode::InsertAtLineBegin: case Mode::InsertAtLineBegin:
pos = m_editor.m_buffer.iterator_at_line_begin(sel.begin()); first = m_editor.m_buffer.iterator_at_line_begin(sel.begin());
if (mode == Mode::OpenLineAbove) if (mode == Mode::OpenLineAbove)
--pos; --first;
else else
{ {
auto first_non_blank = pos; auto first_non_blank = first;
while (*first_non_blank == ' ' or *first_non_blank == '\t') while (*first_non_blank == ' ' or *first_non_blank == '\t')
++first_non_blank; ++first_non_blank;
if (*first_non_blank != '\n') if (*first_non_blank != '\n')
pos = first_non_blank; first = first_non_blank;
} }
last = first;
break; break;
} }
sel = Selection(pos, pos); sel = Selection(first, last);
if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove) if (mode == Mode::OpenLineBelow or mode == Mode::OpenLineAbove)
apply(Modification::make_insert(pos, "\n")); apply(Modification::make_insert(sel.last(), "\n"));
} }
} }
IncrementalInserter::~IncrementalInserter() IncrementalInserter::~IncrementalInserter()
{ {
move_cursors(BufferCoord(0, -1)); if (m_mode == Mode::Append)
for (auto& sel : m_editor.m_selections.back())
sel = Selection(sel.first(), sel.last()-1);
m_editor.on_incremental_insertion_end(); m_editor.on_incremental_insertion_end();
} }
@ -323,7 +328,7 @@ void IncrementalInserter::apply(Modification&& modification) const
void IncrementalInserter::insert(const String& string) void IncrementalInserter::insert(const String& string)
{ {
for (auto& sel : m_editor.selections()) for (auto& sel : m_editor.selections())
apply(Modification::make_insert(sel.begin(), string)); apply(Modification::make_insert(sel.last(), string));
} }
void IncrementalInserter::insert(const memoryview<String>& strings) void IncrementalInserter::insert(const memoryview<String>& strings)
@ -335,8 +340,8 @@ void IncrementalInserter::erase()
{ {
for (auto& sel : m_editor.m_selections.back()) for (auto& sel : m_editor.m_selections.back())
{ {
sel = Selection(sel.first() - 1, sel.last() - 1); BufferIterator pos = sel.last();
apply(Modification::make_erase(sel.begin(), sel.end())); apply(Modification::make_erase(pos-1, pos));
} }
} }

View File

@ -121,6 +121,7 @@ public:
private: private:
void apply(Modification&& modification) const; void apply(Modification&& modification) const;
Mode m_mode;
Editor& m_editor; Editor& m_editor;
scoped_edition m_edition; scoped_edition m_edition;
}; };