Make selection lists use the option list syntax

This commit is contained in:
Maxime Coste 2018-05-31 20:59:21 +10:00
parent 2729042f83
commit 8b2e5ea862
5 changed files with 15 additions and 17 deletions

View File

@ -2113,16 +2113,16 @@ const CommandDesc set_register_cmd = {
const CommandDesc select_cmd = { const CommandDesc select_cmd = {
"select", "select",
nullptr, nullptr,
"select <selections_desc>: select given selections\n" "select <selection_desc>...: select given selections\n"
"\n" "\n"
"selections_desc format is <anchor_line>.<anchor_column>,<cursor_line>.<cursor_column>:...", "selection_desc format is <anchor_line>.<anchor_column>,<cursor_line>.<cursor_column>",
single_param, ParameterDesc{{}, ParameterDesc::Flags::SwitchesAsPositional, 1},
CommandFlags::None, CommandFlags::None,
CommandHelper{}, CommandHelper{},
CommandCompleter{}, CommandCompleter{},
[](const ParametersParser& parser, Context& context, const ShellContext&) [](const ParametersParser& parser, Context& context, const ShellContext&)
{ {
context.selections_write_only() = selection_list_from_string(context.buffer(), parser[0]); context.selections_write_only() = selection_list_from_string(context.buffer(), parser.positionals_from(0));
} }
}; };

View File

@ -18,12 +18,6 @@
namespace Kakoune namespace Kakoune
{ {
inline String quote(StringView s)
{
return format("'{}'", replace(s, "'", "''"));
}
template<typename T> template<typename T>
constexpr decltype(T::option_type_name) option_type_name(Meta::Type<T>) constexpr decltype(T::option_type_name) option_type_name(Meta::Type<T>)
{ {

View File

@ -477,7 +477,7 @@ String selection_list_to_string(const SelectionList& selections)
auto main = beg + selections.main_index(); auto main = beg + selections.main_index();
using View = ConstArrayView<Selection>; using View = ConstArrayView<Selection>;
return join(concatenated(View{main, end}, View{beg, main}) | return join(concatenated(View{main, end}, View{beg, main}) |
transform(selection_to_string), ':', false); transform(selection_to_string), ' ', false);
} }
Selection selection_from_string(StringView desc) Selection selection_from_string(StringView desc)
@ -502,14 +502,13 @@ Selection selection_from_string(StringView desc)
return Selection{anchor, cursor}; return Selection{anchor, cursor};
} }
SelectionList selection_list_from_string(Buffer& buffer, StringView desc) SelectionList selection_list_from_string(Buffer& buffer, ConstArrayView<String> descs)
{ {
if (desc.empty()) if (descs.empty())
throw runtime_error{"empty selection description"}; throw runtime_error{"empty selection description"};
auto sels = desc | split<StringView>(':') auto sels = descs | transform([&](auto&& d) { auto s = selection_from_string(d); clamp(s, buffer); return s; })
| transform([&](auto&& d) { auto s = selection_from_string(d); clamp(s, buffer); return s; }) | gather<Vector<Selection>>();
| gather<Vector<Selection>>();
return {SelectionList::UnsortedTag{}, buffer, std::move(sels)}; return {SelectionList::UnsortedTag{}, buffer, std::move(sels)};
} }

View File

@ -155,7 +155,7 @@ Vector<Selection> compute_modified_ranges(Buffer& buffer, size_t timestamp);
String selection_to_string(const Selection& selection); String selection_to_string(const Selection& selection);
String selection_list_to_string(const SelectionList& selection); String selection_list_to_string(const SelectionList& selection);
Selection selection_from_string(StringView desc); Selection selection_from_string(StringView desc);
SelectionList selection_list_from_string(Buffer& buffer, StringView desc); SelectionList selection_list_from_string(Buffer& buffer, ConstArrayView<String> descs);
} }

View File

@ -128,6 +128,11 @@ StringView format_to(ArrayView<char> buffer, StringView fmt, Types&&... params)
return format_to(buffer, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...}); return format_to(buffer, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...});
} }
inline String quote(StringView s)
{
return format("'{}'", replace(s, "'", "''"));
}
} }
#endif // string_utils_hh_INCLUDED #endif // string_utils_hh_INCLUDED