Store capture groups with selections in editor, and access them through dynamic registers
This commit is contained in:
parent
a8d2482eb6
commit
79d8d082bd
|
@ -159,9 +159,6 @@ void Editor::select(const Selector& selector, SelectMode mode)
|
|||
{
|
||||
check_invariant();
|
||||
|
||||
std::array<std::vector<String>, 10> captures;
|
||||
|
||||
size_t capture_count = -1;
|
||||
for (auto& sel : m_selections)
|
||||
{
|
||||
SelectionAndCaptures res = selector(sel.selection);
|
||||
|
@ -170,17 +167,8 @@ void Editor::select(const Selector& selector, SelectMode mode)
|
|||
else
|
||||
sel.selection = std::move(res.selection);
|
||||
|
||||
assert(capture_count == -1 or capture_count == res.captures.size());
|
||||
capture_count = res.captures.size();
|
||||
|
||||
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]);
|
||||
if (not res.captures.empty())
|
||||
sel.captures = std::move(res.captures);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,35 +181,23 @@ void Editor::multi_select(const MultiSelector& selector)
|
|||
{
|
||||
check_invariant();
|
||||
|
||||
std::array<std::vector<String>, 10> captures;
|
||||
|
||||
size_t capture_count = -1;
|
||||
SelectionAndCapturesList new_selections;
|
||||
for (auto& sel : m_selections)
|
||||
{
|
||||
SelectionAndCapturesList res = selector(sel.selection);
|
||||
for (auto& sel_and_cap : res)
|
||||
{
|
||||
new_selections.push_back(sel_and_cap.selection);
|
||||
|
||||
assert(capture_count == -1 or
|
||||
capture_count == sel_and_cap.captures.size());
|
||||
capture_count = sel_and_cap.captures.size();
|
||||
|
||||
for (size_t i = 0; i < sel_and_cap.captures.size(); ++i)
|
||||
captures[i].push_back(sel_and_cap.captures[i]);
|
||||
// preserve captures when selectors captures nothing.
|
||||
if (sel_and_cap.captures.empty())
|
||||
new_selections.emplace_back(sel_and_cap.selection, sel.captures);
|
||||
else
|
||||
new_selections.push_back(std::move(sel_and_cap));
|
||||
}
|
||||
}
|
||||
if (new_selections.empty())
|
||||
throw nothing_selected();
|
||||
|
||||
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
|
||||
|
@ -351,7 +327,7 @@ IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
|
|||
--first;
|
||||
if (last.is_end())
|
||||
--last;
|
||||
sel = Selection(first, last);
|
||||
sel.selection = Selection(first, last);
|
||||
}
|
||||
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
|
||||
if (sel.first() == buffer().begin() + 1)
|
||||
sel = Selection(buffer().begin(), buffer().begin());
|
||||
sel.selection = Selection(buffer().begin(), buffer().begin());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
src/main.cc
14
src/main.cc
|
@ -468,8 +468,18 @@ int main(int argc, char* argv[])
|
|||
[](const String& name, const Context& context)
|
||||
{ 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 context.editor().selections_content(); });
|
||||
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(); });
|
||||
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_highlighters();
|
||||
|
|
|
@ -57,8 +57,11 @@ struct SelectionAndCaptures
|
|||
|
||||
SelectionAndCaptures(const BufferIterator& first,
|
||||
const BufferIterator& last,
|
||||
CaptureList&& captures_list)
|
||||
CaptureList 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)
|
||||
: selection(sel) {}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user