Store capture groups with selections in editor, and access them through dynamic registers

This commit is contained in:
Maxime Coste 2012-09-26 20:43:42 +02:00
parent a8d2482eb6
commit 79d8d082bd
3 changed files with 27 additions and 38 deletions

View File

@ -159,9 +159,6 @@ void Editor::select(const Selector& selector, SelectMode mode)
{ {
check_invariant(); check_invariant();
std::array<std::vector<String>, 10> captures;
size_t capture_count = -1;
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
SelectionAndCaptures res = selector(sel.selection); SelectionAndCaptures res = selector(sel.selection);
@ -170,17 +167,8 @@ void Editor::select(const Selector& selector, SelectMode mode)
else else
sel.selection = std::move(res.selection); sel.selection = std::move(res.selection);
assert(capture_count == -1 or capture_count == res.captures.size()); if (not res.captures.empty())
capture_count = res.captures.size(); sel.captures = std::move(res.captures);
for (size_t i = 0; i < res.captures.size(); ++i)
captures[i].push_back(res.captures[i]);
}
if (not captures[0].empty())
{
for (size_t i = 0; i < captures.size(); ++i)
RegisterManager::instance()['0' + i] = std::move(captures[i]);
} }
} }
@ -193,35 +181,23 @@ void Editor::multi_select(const MultiSelector& selector)
{ {
check_invariant(); check_invariant();
std::array<std::vector<String>, 10> captures;
size_t capture_count = -1;
SelectionAndCapturesList new_selections; SelectionAndCapturesList new_selections;
for (auto& sel : m_selections) for (auto& sel : m_selections)
{ {
SelectionAndCapturesList res = selector(sel.selection); 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); // preserve captures when selectors captures nothing.
if (sel_and_cap.captures.empty())
assert(capture_count == -1 or new_selections.emplace_back(sel_and_cap.selection, sel.captures);
capture_count == sel_and_cap.captures.size()); else
capture_count = sel_and_cap.captures.size(); new_selections.push_back(std::move(sel_and_cap));
for (size_t i = 0; i < sel_and_cap.captures.size(); ++i)
captures[i].push_back(sel_and_cap.captures[i]);
} }
} }
if (new_selections.empty()) if (new_selections.empty())
throw nothing_selected(); throw nothing_selected();
m_selections = std::move(new_selections); m_selections = std::move(new_selections);
if (not captures[0].empty())
{
for (size_t i = 0; i < captures.size(); ++i)
RegisterManager::instance()['0' + i] = std::move(captures[i]);
}
} }
class LastModifiedRangeListener : public BufferChangeListener class LastModifiedRangeListener : public BufferChangeListener
@ -265,7 +241,7 @@ bool Editor::undo()
{ {
m_selections.clear(); m_selections.clear();
m_selections.push_back(Selection(listener.first(), m_selections.push_back(Selection(listener.first(),
listener.last())); listener.last()));
} }
return res; return res;
} }
@ -278,7 +254,7 @@ bool Editor::redo()
{ {
m_selections.clear(); m_selections.clear();
m_selections.push_back(Selection(listener.first(), m_selections.push_back(Selection(listener.first(),
listener.last())); listener.last()));
} }
return res; return res;
} }
@ -351,7 +327,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
--first; --first;
if (last.is_end()) if (last.is_end())
--last; --last;
sel = Selection(first, last); sel.selection = Selection(first, last);
} }
if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove) if (mode == InsertMode::OpenLineBelow or mode == InsertMode::OpenLineAbove)
{ {
@ -362,7 +338,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
{ {
// special case, the --first line above did nothing, so we need to compensate now // special case, the --first line above did nothing, so we need to compensate now
if (sel.first() == buffer().begin() + 1) if (sel.first() == buffer().begin() + 1)
sel = Selection(buffer().begin(), buffer().begin()); sel.selection = Selection(buffer().begin(), buffer().begin());
} }
} }
} }

View File

@ -468,8 +468,18 @@ int main(int argc, char* argv[])
[](const String& name, const Context& context) [](const String& name, const Context& context)
{ return RegisterManager::instance()[name[4]].values(context)[0]; }); { return RegisterManager::instance()[name[4]].values(context)[0]; });
register_manager.register_dynamic_register('%', [&](const Context& context) { return std::vector<String>(1, context.buffer().name()); }); register_manager.register_dynamic_register('%', [](const Context& context) { return std::vector<String>(1, context.buffer().name()); });
register_manager.register_dynamic_register('.', [&](const Context& context) { return context.editor().selections_content(); }); register_manager.register_dynamic_register('.', [](const Context& context) { return context.editor().selections_content(); });
for (size_t i = 0; i < 10; ++i)
{
register_manager.register_dynamic_register('0'+i,
[i](const Context& context) {
std::vector<String> result;
for (auto& sel_and_cap : context.editor().selections())
result.emplace_back(i < sel_and_cap.captures.size() ? sel_and_cap.captures[i] : "");
return result;
});
}
register_commands(); register_commands();
register_highlighters(); register_highlighters();

View File

@ -57,8 +57,11 @@ 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(std::move(captures_list)) {} : selection(first, last), captures(std::move(captures_list)) {}
SelectionAndCaptures(const Selection& sel,
CaptureList captures_list)
: selection(sel), captures(std::move(captures_list)) {}
SelectionAndCaptures(const Selection& sel) SelectionAndCaptures(const Selection& sel)
: selection(sel) {} : selection(sel) {}