Window: code cleanup and more intelligent Selection class

This commit is contained in:
Maxime Coste 2011-09-17 14:57:35 +00:00
parent 0513b4de29
commit 85e2a31b31
2 changed files with 40 additions and 21 deletions

View File

@ -22,10 +22,10 @@ void Window::erase()
m_buffer.erase(cursor, cursor+1); m_buffer.erase(cursor, cursor+1);
} }
for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) for (auto& sel : m_selections)
{ {
m_buffer.erase(sel->begin, sel->end); m_buffer.erase(sel.begin(), sel.end());
sel->end = sel->begin; sel = Selection(sel.begin(), sel.begin());
} }
m_buffer.end_undo_group(); m_buffer.end_undo_group();
} }
@ -55,11 +55,10 @@ void Window::insert(const String& string)
move_cursor(measure_string(string)); move_cursor(measure_string(string));
} }
for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) for (auto& sel : m_selections)
{ {
m_buffer.insert(sel->begin, string); m_buffer.insert(sel.begin(), string);
sel->begin += string.length(); sel.offset(string.length());
sel->end += string.length();
} }
m_buffer.end_undo_group(); m_buffer.end_undo_group();
} }
@ -73,9 +72,9 @@ void Window::append(const String& string)
insert(string); insert(string);
} }
for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) for (auto& sel : m_selections)
{ {
m_buffer.insert(sel->end, string); m_buffer.insert(sel.end(), string);
} }
m_buffer.end_undo_group(); m_buffer.end_undo_group();
} }
@ -126,12 +125,12 @@ void Window::select(bool append, const Selector& selector)
} }
else else
{ {
for (auto sel = m_selections.begin(); sel != m_selections.end(); ++sel) for (auto& sel : m_selections)
{ {
sel->end = selector(sel->end).end; sel = Selection(sel.begin(), selector(sel.end()).end());
} }
} }
m_cursor = line_and_column_at(m_selections.back().end); m_cursor = line_and_column_at(m_selections.back().end());
scroll_to_keep_cursor_visible_ifn(); scroll_to_keep_cursor_visible_ifn();
} }
@ -149,27 +148,31 @@ void Window::update_display_buffer()
m_display_buffer.clear(); m_display_buffer.clear();
SelectionList sorted_selections = m_selections; SelectionList sorted_selections = m_selections;
for (auto& sel : sorted_selections)
sel.canonicalize();
std::sort(sorted_selections.begin(), sorted_selections.end(), std::sort(sorted_selections.begin(), sorted_selections.end(),
[](const Selection& lhs, const Selection& rhs) { return lhs.begin < rhs.begin; }); [](const Selection& lhs, const Selection& rhs) { return lhs.begin() < rhs.begin(); });
BufferIterator current_position = m_buffer.iterator_at(m_position); BufferIterator current_position = m_buffer.iterator_at(m_position);
for (Selection& sel : sorted_selections) for (Selection& sel : sorted_selections)
{ {
if (current_position != sel.begin) if (current_position != sel.begin())
{ {
DisplayAtom atom; DisplayAtom atom;
atom.content = m_buffer.string(current_position, sel.begin); atom.content = m_buffer.string(current_position, sel.begin());
m_display_buffer.append(atom); m_display_buffer.append(atom);
} }
if (sel.begin != sel.end) if (sel.begin() != sel.end())
{ {
DisplayAtom atom; DisplayAtom atom;
atom.content = m_buffer.string(sel.begin, sel.end); atom.content = m_buffer.string(sel.begin(), sel.end());
atom.attribute = UNDERLINE; atom.attribute = UNDERLINE;
m_display_buffer.append(atom); m_display_buffer.append(atom);
} }
current_position = sel.end; current_position = sel.end();
} }
if (current_position != m_buffer.end()) if (current_position != m_buffer.end())
{ {

View File

@ -20,10 +20,26 @@ struct WindowCoord : LineAndColumn<WindowCoord>
struct Selection struct Selection
{ {
Selection(const BufferIterator& begin, const BufferIterator& end) Selection(const BufferIterator& begin, const BufferIterator& end)
: begin(begin), end(end) {} : m_begin(begin), m_end(end) {}
BufferIterator begin; const BufferIterator& begin() const { return m_begin; }
BufferIterator end; const BufferIterator& end() const { return m_end; }
void canonicalize()
{
if (m_end < m_begin)
std::swap(m_begin, m_end);
}
void offset(int offset)
{
m_begin += offset;
m_end += offset;
}
private:
BufferIterator m_begin;
BufferIterator m_end;
}; };
typedef std::vector<Selection> SelectionList; typedef std::vector<Selection> SelectionList;