Editor now stores selections as SelectionAndCapturesList

This commit is contained in:
Maxime Coste 2012-09-26 20:33:47 +02:00
parent a2fd1528e6
commit a8d2482eb6
4 changed files with 27 additions and 20 deletions

View File

@ -23,7 +23,7 @@ void Editor::erase()
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
m_buffer.erase(sel.begin(), sel.end()); m_buffer.erase(sel.begin(), sel.end());
sel.avoid_eol(); sel.selection.avoid_eol();
} }
} }
@ -33,7 +33,8 @@ static void do_insert(Editor& editor, const String& string)
scoped_edition edition(editor); scoped_edition edition(editor);
for (auto& sel : editor.selections()) for (auto& sel : editor.selections())
{ {
BufferIterator pos = append ? sel.end() : sel.begin(); BufferIterator pos = append ? sel.end()
: sel.begin();
editor.buffer().insert(pos, string); editor.buffer().insert(pos, string);
} }
} }
@ -92,7 +93,8 @@ std::vector<String> Editor::selections_content() const
{ {
std::vector<String> contents; std::vector<String> contents;
for (auto& sel : m_selections) for (auto& sel : m_selections)
contents.push_back(m_buffer.string(sel.begin(), sel.end())); contents.push_back(m_buffer.string(sel.begin(),
sel.end()));
return contents; return contents;
} }
@ -103,7 +105,7 @@ void Editor::move_selections(const BufferCoord& offset, SelectMode mode)
{ {
BufferCoord pos = m_buffer.line_and_column_at(sel.last()); BufferCoord pos = m_buffer.line_and_column_at(sel.last());
BufferIterator last = m_buffer.iterator_at(pos + offset, true); BufferIterator last = m_buffer.iterator_at(pos + offset, true);
sel = Selection(mode == SelectMode::Extend ? sel.first() : last, last); sel.selection = Selection(mode == SelectMode::Extend ? sel.first() : last, last);
} }
} }
@ -126,7 +128,7 @@ void Editor::keep_selection(int index)
if (index < m_selections.size()) if (index < m_selections.size())
{ {
Selection sel = m_selections[index]; SelectionAndCaptures sel = std::move(m_selections[index]);
m_selections.clear(); m_selections.clear();
m_selections.push_back(std::move(sel)); m_selections.push_back(std::move(sel));
} }
@ -146,7 +148,7 @@ void Editor::select(const BufferIterator& iterator)
m_selections.push_back(Selection(iterator, iterator)); m_selections.push_back(Selection(iterator, iterator));
} }
void Editor::select(SelectionList selections) void Editor::select(SelectionAndCapturesList selections)
{ {
if (selections.empty()) if (selections.empty())
throw runtime_error("no selections"); throw runtime_error("no selections");
@ -162,11 +164,11 @@ void Editor::select(const Selector& selector, SelectMode mode)
size_t capture_count = -1; size_t capture_count = -1;
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
SelectionAndCaptures res = selector(sel); SelectionAndCaptures res = selector(sel.selection);
if (mode == SelectMode::Extend) if (mode == SelectMode::Extend)
sel.merge_with(res.selection); sel.selection.merge_with(res.selection);
else else
sel = std::move(res.selection); sel.selection = std::move(res.selection);
assert(capture_count == -1 or capture_count == res.captures.size()); assert(capture_count == -1 or capture_count == res.captures.size());
capture_count = res.captures.size(); capture_count = res.captures.size();
@ -194,10 +196,10 @@ void Editor::multi_select(const MultiSelector& selector)
std::array<std::vector<String>, 10> captures; std::array<std::vector<String>, 10> captures;
size_t capture_count = -1; size_t capture_count = -1;
SelectionList new_selections; SelectionAndCapturesList new_selections;
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
SelectionAndCapturesList res = selector(sel); SelectionAndCapturesList res = selector(sel.selection);
for (auto& sel_and_cap : res) for (auto& sel_and_cap : res)
{ {
new_selections.push_back(sel_and_cap.selection); new_selections.push_back(sel_and_cap.selection);
@ -372,7 +374,7 @@ IncrementalInserter::~IncrementalInserter()
{ {
if (m_mode == InsertMode::Append) if (m_mode == InsertMode::Append)
sel = Selection(sel.first(), sel.last()-1); sel = Selection(sel.first(), sel.last()-1);
sel.avoid_eol(); sel.selection.avoid_eol();
} }
m_editor.on_incremental_insertion_end(); m_editor.on_incremental_insertion_end();

View File

@ -55,10 +55,10 @@ public:
void select(const BufferIterator& iterator); void select(const BufferIterator& iterator);
void select(const Selector& selector, void select(const Selector& selector,
SelectMode mode = SelectMode::Replace); SelectMode mode = SelectMode::Replace);
void select(SelectionList selections); void select(SelectionAndCapturesList selections);
void multi_select(const MultiSelector& selector); void multi_select(const MultiSelector& selector);
const SelectionList& selections() const { return m_selections; } const SelectionAndCapturesList& selections() const { return m_selections; }
std::vector<String> selections_content() const; std::vector<String> selections_content() const;
bool undo(); bool undo();
@ -85,7 +85,7 @@ private:
virtual void on_incremental_insertion_end() {} virtual void on_incremental_insertion_end() {}
Buffer& m_buffer; Buffer& m_buffer;
SelectionList m_selections; SelectionAndCapturesList m_selections;
FilterGroup m_filters; FilterGroup m_filters;
}; };

View File

@ -31,12 +31,12 @@ struct Selection : public BufferChangeListener
void merge_with(const Selection& selection); void merge_with(const Selection& selection);
void avoid_eol(); void avoid_eol();
private:
void on_insert(const BufferIterator& begin, void on_insert(const BufferIterator& begin,
const BufferIterator& end) override; const BufferIterator& end) override;
void on_erase(const BufferIterator& begin, void on_erase(const BufferIterator& begin,
const BufferIterator& end) override; const BufferIterator& end) override;
private:
BufferIterator m_first; BufferIterator m_first;
BufferIterator m_last; BufferIterator m_last;
@ -58,11 +58,16 @@ struct SelectionAndCaptures
SelectionAndCaptures(const BufferIterator& first, SelectionAndCaptures(const BufferIterator& first,
const BufferIterator& last, const BufferIterator& last,
CaptureList&& captures_list) CaptureList&& captures_list)
: selection(first, last), captures(captures_list) {} : selection(first, last), captures(std::move(captures_list)) {}
SelectionAndCaptures(const Selection& sel) SelectionAndCaptures(const Selection& sel)
: selection(sel) {} : selection(sel) {}
SelectionAndCaptures(Selection&& sel)
: selection(sel) {} // helper to access the selection
BufferIterator begin() const { return selection.begin(); }
BufferIterator end() const { return selection.end(); }
const BufferIterator& first() const { return selection.first(); }
const BufferIterator& last() const { return selection.last(); }
}; };
typedef std::vector<SelectionAndCaptures> SelectionAndCapturesList; typedef std::vector<SelectionAndCaptures> SelectionAndCapturesList;

View File

@ -145,7 +145,7 @@ String Window::status_line() const
void Window::on_incremental_insertion_end() void Window::on_incremental_insertion_end()
{ {
SelectionList backup(selections()); SelectionAndCapturesList backup(selections());
hook_manager().run_hook("InsertEnd", "", Context(*this)); hook_manager().run_hook("InsertEnd", "", Context(*this));
select(backup); select(backup);
} }