Code style cleanups around object selection code

This commit is contained in:
Maxime Coste 2022-04-11 19:48:57 +10:00
parent eae8ea8a54
commit e40ff99eae

View File

@ -1,7 +1,5 @@
#include "normal.hh" #include "normal.hh"
#include <functional>
#include "buffer.hh" #include "buffer.hh"
#include "buffer_manager.hh" #include "buffer_manager.hh"
#include "buffer_utils.hh" #include "buffer_utils.hh"
@ -31,8 +29,6 @@
namespace Kakoune namespace Kakoune
{ {
using namespace std::placeholders;
enum class SelectMode enum class SelectMode
{ {
Replace, Replace,
@ -1302,7 +1298,15 @@ void select_object(Context& context, NormalParams params)
auto obj_it = find(selectors | transform(&ObjectType::key), key).base(); auto obj_it = find(selectors | transform(&ObjectType::key), key).base();
if (obj_it != std::end(selectors)) if (obj_it != std::end(selectors))
return select_and_set_last<mode>( return select_and_set_last<mode>(
context, std::bind(obj_it->func, _1, _2, count, flags)); context, [=](Context& context, Selection& sel) { return obj_it->func(context, sel, count, flags); });
static constexpr auto regex_selector = [=](StringView open, StringView close, int count) {
return [open=Regex{open, RegexCompileFlags::Backward},
close=Regex{close, RegexCompileFlags::Backward},
count](Context& context, Selection& sel) {
return select_surrounding(context, sel, open, close, count, flags);
};
};
if (key == 'c') if (key == 'c')
{ {
@ -1323,17 +1327,13 @@ void select_object(Context& context, NormalParams params)
struct error : runtime_error { error(size_t) : runtime_error{"desc parsing failed, expected <open>,<close>"} {} }; struct error : runtime_error { error(size_t) : runtime_error{"desc parsing failed, expected <open>,<close>"} {} };
auto params = cmdline | split<StringView>(',', '\\') | auto params = cmdline | split<StringView>(',', '\\')
transform(unescape<',', '\\'>) | static_gather<error, 2>(); | transform(unescape<',', '\\'>)
| static_gather<error, 2>();
if (params[0].empty() or params[1].empty()) if (params[0].empty() or params[1].empty())
throw error{0}; throw error{0};
select_and_set_last<mode>( select_and_set_last<mode>(context, regex_selector(params[0], params[1], count));
context, std::bind(select_surrounding, _1, _2,
Regex{params[0], RegexCompileFlags::Backward},
Regex{params[1], RegexCompileFlags::Backward},
count, flags));
}); });
return; return;
} }
@ -1350,10 +1350,10 @@ void select_object(Context& context, NormalParams params)
return; return;
} }
static constexpr struct SurroundingPair static constexpr struct
{ {
char opening; char open;
char closing; char close;
char name; char name;
} surrounding_pairs[] = { } surrounding_pairs[] = {
{ '(', ')', 'b' }, { '(', ')', 'b' },
@ -1364,28 +1364,15 @@ void select_object(Context& context, NormalParams params)
{ '\'', '\'', 'q' }, { '\'', '\'', 'q' },
{ '`', '`', 'g' }, { '`', '`', 'g' },
}; };
auto pair_it = find_if(surrounding_pairs, if (auto it = find_if(surrounding_pairs, [key](auto s) { return key == s.open or key == s.close or key == s.name; });
[key](const SurroundingPair& s) { it != std::end(surrounding_pairs))
return key == s.opening or key == s.closing or
(s.name != 0 and key == s.name);
});
if (pair_it != std::end(surrounding_pairs))
return select_and_set_last<mode>( return select_and_set_last<mode>(
context, std::bind(select_surrounding, _1, _2, context, regex_selector(format("\\Q{}", it->open), format("\\Q{}", it->close), count));
Regex{format("\\Q{}", pair_it->opening), RegexCompileFlags::Backward},
Regex{format("\\Q{}", pair_it->closing), RegexCompileFlags::Backward},
count, flags));
if (not key.codepoint()) if (auto cp = key.codepoint(); cp and is_punctuation(*cp, {}))
return;
const Codepoint cp = *key.codepoint();
if (is_punctuation(cp, {}))
{ {
auto re = Regex{"\\Q" + to_string(cp), RegexCompileFlags::Backward}; auto re = "\\Q" + to_string(*cp);
return select_and_set_last<mode>( return select_and_set_last<mode>(context, regex_selector(re, re, count));
context, std::bind(select_surrounding, _1, _2,
re, re, count, flags));
} }
}, get_title(), }, get_title(),
build_autoinfo_for_mapping(context, KeymapMode::Object, build_autoinfo_for_mapping(context, KeymapMode::Object,
@ -1537,11 +1524,10 @@ void select_to_next_char(Context& context, NormalParams params)
constexpr auto new_flags = flags & SelectFlags::Extend ? SelectMode::Extend constexpr auto new_flags = flags & SelectFlags::Extend ? SelectMode::Extend
: SelectMode::Replace; : SelectMode::Replace;
select_and_set_last<new_flags>( select_and_set_last<new_flags>(
context, context, [cp=*cp, count=params.count] (auto& context, auto& sel) {
std::bind(flags & SelectFlags::Reverse ? select_to_reverse auto& func = flags & SelectFlags::Reverse ? select_to_reverse : select_to;
: select_to, return func(context, sel, cp, count, flags & SelectFlags::Inclusive);
_1, _2, *cp, params.count, });
flags & SelectFlags::Inclusive));
}, get_title(), "enter char to select to"); }, get_title(), "enter char to select to");
} }