Add a join function for joining strings using a specific char

This commit is contained in:
Maxime Coste 2014-12-28 11:16:51 +00:00
parent 71bfe5498d
commit 8cc96ec36b
3 changed files with 28 additions and 36 deletions

View File

@ -59,15 +59,9 @@ void register_env_vars()
}, { }, {
"buflist", "buflist",
[](StringView name, const Context& context) [](StringView name, const Context& context)
{ { return join(transformed(BufferManager::instance(),
String res; [](const safe_ptr<Buffer>& b)
for (auto& buf : BufferManager::instance()) { return b->display_name(); }), ':'); }
{
if (not res.empty())
res += ":";
res += buf->display_name();
}
return res; }
}, { }, {
"timestamp", "timestamp",
[](StringView name, const Context& context) [](StringView name, const Context& context)
@ -80,15 +74,7 @@ void register_env_vars()
}, { }, {
"selections", "selections",
[](StringView name, const Context& context) [](StringView name, const Context& context)
{ auto sels = context.selections_content(); { return join(context.selections_content(), ':'); }
String res;
for (size_t i = 0; i < sels.size(); ++i)
{
res += escape(sels[i], ':', '\\');
if (i != sels.size() - 1)
res += ':';
}
return res; }
}, { }, {
"runtime", "runtime",
[](StringView name, const Context& context) [](StringView name, const Context& context)
@ -136,18 +122,11 @@ void register_env_vars()
}, { }, {
"selections_desc", "selections_desc",
[](StringView name, const Context& context) [](StringView name, const Context& context)
{ { return join(transformed(context.selections(), [&](const Selection& sel) {
String res;
for (auto& sel : context.selections())
{
auto beg = sel.min(); auto beg = sel.min();
if (not res.empty()) return to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
res += ':';
res += to_string(beg.line + 1) + "." + to_string(beg.column + 1) + "+" +
to_string((int)context.buffer().distance(beg, sel.max())+1); to_string((int)context.buffer().distance(beg, sel.max())+1);
} }), ':'); }
return res;
}
}, { }, {
"window_width", "window_width",
[](StringView name, const Context& context) [](StringView name, const Context& context)

View File

@ -721,7 +721,7 @@ void split_lines(Context& context, NormalParams)
selections = std::move(res); selections = std::move(res);
} }
void join_select_spaces(Context& context, NormalParams) void join_lines_select_spaces(Context& context, NormalParams)
{ {
auto& buffer = context.buffer(); auto& buffer = context.buffer();
std::vector<Selection> selections; std::vector<Selection> selections;
@ -746,7 +746,7 @@ void join_select_spaces(Context& context, NormalParams)
context.selections().insert(" "_str, InsertMode::Replace); context.selections().insert(" "_str, InsertMode::Replace);
} }
void join(Context& context, NormalParams params) void join_lines(Context& context, NormalParams params)
{ {
SelectionList sels{context.selections()}; SelectionList sels{context.selections()};
auto restore_sels = on_scope_end([&]{ auto restore_sels = on_scope_end([&]{
@ -754,7 +754,7 @@ void join(Context& context, NormalParams params)
context.selections() = std::move(sels); context.selections() = std::move(sels);
}); });
join_select_spaces(context, params); join_lines_select_spaces(context, params);
} }
template<bool matching> template<bool matching>
@ -1430,8 +1430,8 @@ KeyMap keymap =
{ alt('{'), { "extend to inner object start", select_object<ObjectFlags::ToBegin | ObjectFlags::Inner, SelectMode::Extend> } }, { alt('{'), { "extend to inner object start", select_object<ObjectFlags::ToBegin | ObjectFlags::Inner, SelectMode::Extend> } },
{ alt('}'), { "extend to inner object end", select_object<ObjectFlags::ToEnd | ObjectFlags::Inner, SelectMode::Extend> } }, { alt('}'), { "extend to inner object end", select_object<ObjectFlags::ToEnd | ObjectFlags::Inner, SelectMode::Extend> } },
{ alt('j'), { "join lines", join } }, { alt('j'), { "join lines", join_lines } },
{ alt('J'), { "join lines and select spaces", join_select_spaces } }, { alt('J'), { "join lines and select spaces", join_lines_select_spaces } },
{ alt('k'), { "keep selections matching given regex", keep<true> } }, { alt('k'), { "keep selections matching given regex", keep<true> } },
{ alt('K'), { "keep selections not matching given regex", keep<false> } }, { alt('K'), { "keep selections not matching given regex", keep<false> } },

View File

@ -235,6 +235,19 @@ std::vector<StringView> split(StringView str, char separator);
String escape(StringView str, StringView characters, char escape); String escape(StringView str, StringView characters, char escape);
String unescape(StringView str, StringView characters, char escape); String unescape(StringView str, StringView characters, char escape);
template<typename Container>
String join(const Container& container, char joiner)
{
String res;
for (const auto& str : container)
{
if (not res.empty())
res += joiner;
res += escape(str, joiner, '\\');
}
return res;
}
inline String operator"" _str(const char* str, size_t) inline String operator"" _str(const char* str, size_t)
{ {
return String(str); return String(str);