refactor regex based selectors, move regex construction out of them
This commit is contained in:
parent
e3f097ad40
commit
1ab8120147
77
src/main.cc
77
src/main.cc
|
@ -116,7 +116,7 @@ void do_replace_with_char(Context& context)
|
|||
Editor& editor = context.editor();
|
||||
SelectionList sels = editor.selections();
|
||||
auto restore_sels = on_scope_end([&]{ editor.select(std::move(sels)); });
|
||||
editor.multi_select(std::bind(select_all_matches, _1, "."));
|
||||
editor.multi_select(std::bind(select_all_matches, _1, Regex{"."}));
|
||||
editor.insert(codepoint_to_str(key.key), InsertMode::Replace);
|
||||
});
|
||||
}
|
||||
|
@ -184,20 +184,25 @@ void do_search(Context& context)
|
|||
if (event == PromptEvent::Abort)
|
||||
return;
|
||||
|
||||
String ex = str;
|
||||
Regex ex{str};
|
||||
if (event == PromptEvent::Validate)
|
||||
{
|
||||
if (ex.empty())
|
||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||
if (str.empty())
|
||||
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
|
||||
else
|
||||
RegisterManager::instance()['/'] = ex;
|
||||
RegisterManager::instance()['/'] = str;
|
||||
context.push_jump();
|
||||
}
|
||||
else if (ex.empty() or not context.options()["incsearch"].get<bool>())
|
||||
else if (str.empty() or not context.options()["incsearch"].get<bool>())
|
||||
return;
|
||||
|
||||
context.editor().select(std::bind(select_next_match<forward>, _1, ex), mode);
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
if (event == PromptEvent::Validate)
|
||||
throw runtime_error("regex error: "_str + err.what());
|
||||
}
|
||||
catch (runtime_error&)
|
||||
{
|
||||
context.editor().select(selections);
|
||||
|
@ -206,16 +211,18 @@ void do_search(Context& context)
|
|||
if (event == PromptEvent::Validate)
|
||||
throw;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
template<SelectMode mode, bool forward>
|
||||
void do_search_next(Context& context)
|
||||
{
|
||||
const String& ex = RegisterManager::instance()['/'].values(context)[0];
|
||||
if (not ex.empty())
|
||||
const String& str = RegisterManager::instance()['/'].values(context)[0];
|
||||
if (not str.empty())
|
||||
{
|
||||
try
|
||||
{
|
||||
Regex ex{str};
|
||||
if (mode == SelectMode::Replace)
|
||||
context.push_jump();
|
||||
int count = context.numeric_param();
|
||||
|
@ -223,8 +230,13 @@ void do_search_next(Context& context)
|
|||
context.editor().select(std::bind(select_next_match<forward>, _1, ex), mode);
|
||||
} while (--count > 0);
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error("regex error: "_str + err.what());
|
||||
}
|
||||
}
|
||||
else
|
||||
context.print_status({ "no search pattern", get_color("Error") });
|
||||
throw runtime_error("no search pattern");
|
||||
}
|
||||
|
||||
template<bool smart>
|
||||
|
@ -318,7 +330,16 @@ void regex_prompt(Context& context, const String prompt, T on_validate)
|
|||
context.input_handler().prompt(prompt, get_color("Prompt"), complete_nothing,
|
||||
[=](const String& str, PromptEvent event, Context& context) {
|
||||
if (event == PromptEvent::Validate)
|
||||
on_validate(str, context);
|
||||
{
|
||||
try
|
||||
{
|
||||
on_validate(Regex{str}, context);
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error("regex error: "_str + err.what());
|
||||
}
|
||||
}
|
||||
else if (event == PromptEvent::Change)
|
||||
{
|
||||
const bool ok = Regex{str, boost::regex_constants::no_except}.status() == 0;
|
||||
|
@ -329,12 +350,11 @@ void regex_prompt(Context& context, const String prompt, T on_validate)
|
|||
|
||||
void do_select_regex(Context& context)
|
||||
{
|
||||
regex_prompt(context, "select: ", [](const String& str, Context& context) {
|
||||
String ex = str;
|
||||
regex_prompt(context, "select: ", [](Regex ex, Context& context) {
|
||||
if (ex.empty())
|
||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
|
||||
else
|
||||
RegisterManager::instance()['/'] = ex;
|
||||
RegisterManager::instance()['/'] = String{ex.str()};
|
||||
if (not ex.empty())
|
||||
context.editor().multi_select(std::bind(select_all_matches, _1, ex));
|
||||
});
|
||||
|
@ -342,12 +362,11 @@ void do_select_regex(Context& context)
|
|||
|
||||
void do_split_regex(Context& context)
|
||||
{
|
||||
regex_prompt(context, "split: ", [](const String& str, Context& context) {
|
||||
String ex = str;
|
||||
regex_prompt(context, "split: ", [](Regex ex, Context& context) {
|
||||
if (ex.empty())
|
||||
ex = RegisterManager::instance()['/'].values(context)[0];
|
||||
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
|
||||
else
|
||||
RegisterManager::instance()['/'] = ex;
|
||||
RegisterManager::instance()['/'] = String{ex.str()};
|
||||
if (not ex.empty())
|
||||
context.editor().multi_select(std::bind(split_selection, _1, ex));
|
||||
});
|
||||
|
@ -355,7 +374,7 @@ void do_split_regex(Context& context)
|
|||
|
||||
void do_split_lines(Context& context)
|
||||
{
|
||||
context.editor().multi_select(std::bind(split_selection, _1, "^"));
|
||||
context.editor().multi_select(std::bind(split_selection, _1, Regex{"^"}));
|
||||
}
|
||||
|
||||
void do_join(Context& context)
|
||||
|
@ -367,7 +386,7 @@ void do_join(Context& context)
|
|||
editor.select(select_to_eol, SelectMode::Extend);
|
||||
editor.multi_select([](const Selection& sel)
|
||||
{
|
||||
SelectionList res = select_all_matches(sel, "\n\\h*");
|
||||
SelectionList res = select_all_matches(sel, Regex{"\n\\h*"});
|
||||
// remove last end of line if selected
|
||||
assert(std::is_sorted(res.begin(), res.end(),
|
||||
[](const Selection& lhs, const Selection& rhs)
|
||||
|
@ -383,26 +402,18 @@ template<bool matching>
|
|||
void do_keep(Context& context)
|
||||
{
|
||||
constexpr const char* prompt = matching ? "keep matching: " : "keep not matching: ";
|
||||
regex_prompt(context, prompt, [](const String& str, Context& context) {
|
||||
try
|
||||
{
|
||||
Regex re(str);
|
||||
regex_prompt(context, prompt, [](const Regex& ex, Context& context) {
|
||||
Editor& editor = context.editor();
|
||||
SelectionList sels = editor.selections();
|
||||
SelectionList keep;
|
||||
for (auto& sel : sels)
|
||||
{
|
||||
if (boost::regex_search(sel.begin(), sel.end(), re) == matching)
|
||||
if (boost::regex_search(sel.begin(), sel.end(), ex) == matching)
|
||||
keep.push_back(sel);
|
||||
}
|
||||
if (keep.empty())
|
||||
throw runtime_error("no selections remaining");
|
||||
editor.select(std::move(keep));
|
||||
}
|
||||
catch (boost::regex_error& error)
|
||||
{
|
||||
throw runtime_error("regex_error: "_str + error.what());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -415,7 +426,7 @@ void do_indent(Context& context)
|
|||
DynamicSelectionList sels{editor.buffer(), editor.selections()};
|
||||
auto restore_sels = on_scope_end([&]{ editor.select((SelectionList)std::move(sels)); });
|
||||
editor.select(select_whole_lines);
|
||||
editor.multi_select(std::bind(select_all_matches, _1, "^[^\n]"));
|
||||
editor.multi_select(std::bind(select_all_matches, _1, Regex{"^[^\n]"}));
|
||||
editor.insert(indent, InsertMode::Insert);
|
||||
}
|
||||
|
||||
|
@ -427,7 +438,7 @@ void do_deindent(Context& context)
|
|||
auto restore_sels = on_scope_end([&]{ editor.select((SelectionList)std::move(sels)); });
|
||||
editor.select(select_whole_lines);
|
||||
editor.multi_select(std::bind(select_all_matches, _1,
|
||||
"^\\h{1," + int_to_str(width) + "}"));
|
||||
Regex{"^\\h{1," + int_to_str(width) + "}"}));
|
||||
editor.erase();
|
||||
}
|
||||
|
||||
|
|
|
@ -419,9 +419,7 @@ bool find_match_in_buffer(const BufferIterator pos, MatchResults& matches,
|
|||
|
||||
|
||||
template<bool forward>
|
||||
Selection select_next_match(const Selection& selection, const String& regex)
|
||||
{
|
||||
try
|
||||
Selection select_next_match(const Selection& selection, const Regex& regex)
|
||||
{
|
||||
// regex matching do not use Utf8Iterator as boost::regex handle utf8
|
||||
// decoding itself
|
||||
|
@ -429,10 +427,9 @@ Selection select_next_match(const Selection& selection, const String& regex)
|
|||
BufferIterator end = begin;
|
||||
CaptureList captures;
|
||||
|
||||
Regex ex{regex.begin(), regex.end()};
|
||||
MatchResults matches;
|
||||
|
||||
if (find_match_in_buffer<forward>(utf8::next(begin), matches, ex))
|
||||
if (find_match_in_buffer<forward>(utf8::next(begin), matches, regex))
|
||||
{
|
||||
begin = matches[0].first;
|
||||
end = matches[0].second;
|
||||
|
@ -440,7 +437,7 @@ Selection select_next_match(const Selection& selection, const String& regex)
|
|||
captures.push_back(String(match.first, match.second));
|
||||
}
|
||||
else
|
||||
throw runtime_error("'" + regex + "': no matches found");
|
||||
throw runtime_error("'" + regex.str() + "': no matches found");
|
||||
|
||||
if (begin == end)
|
||||
++end;
|
||||
|
@ -450,20 +447,12 @@ Selection select_next_match(const Selection& selection, const String& regex)
|
|||
std::swap(begin, end);
|
||||
return Selection{begin, end, std::move(captures)};
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error(String("regex error: ") + err.what());
|
||||
}
|
||||
}
|
||||
template Selection select_next_match<true>(const Selection&, const String&);
|
||||
template Selection select_next_match<false>(const Selection&, const String&);
|
||||
template Selection select_next_match<true>(const Selection&, const Regex&);
|
||||
template Selection select_next_match<false>(const Selection&, const Regex&);
|
||||
|
||||
SelectionList select_all_matches(const Selection& selection, const String& regex)
|
||||
SelectionList select_all_matches(const Selection& selection, const Regex& regex)
|
||||
{
|
||||
try
|
||||
{
|
||||
Regex ex(regex.begin(), regex.end());
|
||||
RegexIterator re_it(selection.begin(), selection.end(), ex);
|
||||
RegexIterator re_it(selection.begin(), selection.end(), regex);
|
||||
RegexIterator re_end;
|
||||
|
||||
SelectionList result;
|
||||
|
@ -484,19 +473,11 @@ SelectionList select_all_matches(const Selection& selection, const String& regex
|
|||
}
|
||||
return result;
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error(String("regex error: ") + err.what());
|
||||
}
|
||||
}
|
||||
|
||||
SelectionList split_selection(const Selection& selection,
|
||||
const String& separator_regex)
|
||||
const Regex& regex)
|
||||
{
|
||||
try
|
||||
{
|
||||
Regex ex(separator_regex.begin(), separator_regex.end());
|
||||
RegexIterator re_it(selection.begin(), selection.end(), ex,
|
||||
RegexIterator re_it(selection.begin(), selection.end(), regex,
|
||||
boost::regex_constants::match_nosubs);
|
||||
RegexIterator re_end;
|
||||
|
||||
|
@ -513,10 +494,5 @@ SelectionList split_selection(const Selection& selection,
|
|||
selection.last())));
|
||||
return result;
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
throw runtime_error(String("regex error: ") + err.what());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -31,13 +31,13 @@ Selection select_whole_lines(const Selection& selection);
|
|||
Selection select_whole_buffer(const Selection& selection);
|
||||
|
||||
template<bool forward>
|
||||
Selection select_next_match(const Selection& selection, const String& regex);
|
||||
Selection select_next_match(const Selection& selection, const Regex& regex);
|
||||
|
||||
SelectionList select_all_matches(const Selection& selection,
|
||||
const String& regex);
|
||||
const Regex& regex);
|
||||
|
||||
SelectionList split_selection(const Selection& selection,
|
||||
const String& separator_regex);
|
||||
const Regex& separator_regex);
|
||||
|
||||
enum class SurroundFlags
|
||||
{
|
||||
|
|
|
@ -58,7 +58,7 @@ void test_editor()
|
|||
{
|
||||
scoped_edition edition{editor};
|
||||
editor.select(select_whole_buffer);
|
||||
editor.multi_select(std::bind(select_all_matches, std::placeholders::_1, "\\n\\h*"));
|
||||
editor.multi_select(std::bind(select_all_matches, std::placeholders::_1, Regex{"\\n\\h*"}));
|
||||
for (auto& sel : editor.selections())
|
||||
{
|
||||
assert(*sel.begin() == '\n');
|
||||
|
|
Loading…
Reference in New Issue
Block a user