From a80cee0d2c1b18e2de5c90442812a325f151532a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 29 Mar 2013 19:31:06 +0100 Subject: [PATCH] Add support for tuple Options, implement LineAndFlag using that --- src/color.cc | 10 +++++++ src/color.hh | 3 ++ src/commands.cc | 1 + src/highlighters.cc | 8 +++--- src/highlighters.hh | 4 +++ src/option_types.cc | 43 ---------------------------- src/option_types.hh | 69 ++++++++++++++++++++++++++++++++++++--------- src/string.cc | 18 ++++++++++++ src/string.hh | 3 ++ 9 files changed, 98 insertions(+), 61 deletions(-) delete mode 100644 src/option_types.cc diff --git a/src/color.cc b/src/color.cc index 051d233c..b5214eb2 100644 --- a/src/color.cc +++ b/src/color.cc @@ -38,4 +38,14 @@ String color_to_str(const Color& color) return "default"; } +String option_to_string(const Color& color) +{ + return color_to_str(color); +} + +void option_from_string(const String& str, Color& color) +{ + color = str_to_color(str); +} + } diff --git a/src/color.hh b/src/color.hh index 96f28d0c..0cd7d18c 100644 --- a/src/color.hh +++ b/src/color.hh @@ -26,6 +26,9 @@ using ColorPair = std::pair; Color str_to_color(const String& color); String color_to_str(const Color& color); +String option_to_string(const Color& color); +void option_from_string(const String& str, Color& color); + } #endif // color_hh_INCLUDED diff --git a/src/commands.cc b/src/commands.cc index fccae7c3..b9ebd3b4 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -10,6 +10,7 @@ #include "input_handler.hh" #include "string.hh" #include "highlighter.hh" +#include "highlighters.hh" #include "filter.hh" #include "register_manager.hh" #include "completion.hh" diff --git a/src/highlighters.cc b/src/highlighters.cc index 676e3b56..2aee8bd6 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -348,14 +348,14 @@ public: CharCount width = 0; for (auto& l : lines) - width = std::max(width, l.flag.char_length()); + width = std::max(width, std::get<2>(l).char_length()); const String empty{' ', width}; for (auto& line : display_buffer.lines()) { int line_num = (int)line.buffer_line() + 1; - auto it = find_if(lines, [&](const LineAndFlag& l) { return l.line == line_num; }); - DisplayAtom atom{AtomContent(it != lines.end() ? it->flag : empty)}; - atom.colors = { it != lines.end() ? it->color : Color::Default , m_bg }; + auto it = find_if(lines, [&](const LineAndFlag& l) { return std::get<0>(l) == line_num; }); + DisplayAtom atom{AtomContent(it != lines.end() ? std::get<2>(*it) : empty)}; + atom.colors = { it != lines.end() ? std::get<1>(*it) : Color::Default , m_bg }; line.insert(line.begin(), std::move(atom)); } } diff --git a/src/highlighters.hh b/src/highlighters.hh index 83c9dbd7..f38f1a7e 100644 --- a/src/highlighters.hh +++ b/src/highlighters.hh @@ -3,11 +3,15 @@ #include "highlighter.hh" +#include "color.hh" + namespace Kakoune { void register_highlighters(); +using LineAndFlag = std::tuple; + } #endif // highlighters_hh_INCLUDED diff --git a/src/option_types.cc b/src/option_types.cc deleted file mode 100644 index bacf4562..00000000 --- a/src/option_types.cc +++ /dev/null @@ -1,43 +0,0 @@ -#include "option_types.hh" - -#include "exception.hh" - -namespace Kakoune -{ - -String option_to_string(const Regex& re) -{ - return String{re.str()}; -} - -void option_from_string(const String& str, Regex& re) -{ - try - { - re = Regex{str.begin(), str.end()}; - } - catch (boost::regex_error& err) - { - throw runtime_error("unable to create regex: "_str + err.what()); - } -} - -String option_to_string(const LineAndFlag& opt) -{ - return int_to_str((int)opt.line) + ":" + color_to_str(opt.color) + ":" + opt.flag; -} - -void option_from_string(const String& str, LineAndFlag& opt) -{ - static Regex re{R"((\d+):(\w+):(.+))"}; - - boost::smatch res; - if (not boost::regex_match(str.begin(), str.end(), res, re)) - throw runtime_error("wrong syntax, expected ::"); - - opt.line = str_to_int(String{res[1].first, res[1].second}); - opt.color = str_to_color(String{res[2].first, res[2].second}); - opt.flag = String{res[3].first, res[3].second}; -} - -} diff --git a/src/option_types.hh b/src/option_types.hh index 73cbf315..ab2955ca 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -1,11 +1,13 @@ #ifndef option_types_hh_INCLUDED #define option_types_hh_INCLUDED -#include "units.hh" #include "string.hh" -#include "color.hh" +#include "units.hh" #include "exception.hh" +#include +#include + namespace Kakoune { @@ -52,24 +54,63 @@ void option_from_string(const String& str, std::vector& opt) } } -String option_to_string(const Regex& re); -void option_from_string(const String& str, Regex& re); -struct LineAndFlag +template +struct TupleOptionDetail { - LineCount line; - Color color; - String flag; + static String to_string(const std::tuple& opt) + { + return TupleOptionDetail::to_string(opt) + ":" + option_to_string(std::get(opt)); + } - bool operator==(const LineAndFlag& other) const - { return line == other.line and color == other.color and flag == other.flag; } + static void from_string(const String& str, std::tuple& opt) + { + auto it = str.begin(); + auto end = str.end(); + for (size_t i = 0; i < I; ++i) + it = std::find(it+1, end, ':'); + if (it == end) + throw runtime_error("not enough elements in tuple"); - bool operator!=(const LineAndFlag& other) const - { return not (*this == other); } + option_from_string(String{it+1, std::find(it+1, end, ':')}, std::get(opt)); + TupleOptionDetail::from_string(str, opt); + } }; -String option_to_string(const LineAndFlag& opt); -void option_from_string(const String& str, LineAndFlag& opt); +template +struct TupleOptionDetail<0, Types...> +{ + static String to_string(const std::tuple& opt) + { + return option_to_string(std::get<0>(opt)); + } + + static void from_string(const String& str, std::tuple& opt) + { + option_from_string(String{str.begin(), std::find(str.begin(), str.end(), ':')}, std::get<0>(opt)); + } +}; + +template +String option_to_string(const std::tuple& opt) +{ + return TupleOptionDetail::to_string(opt); +} + +template +void option_from_string(const String& str, std::tuple& opt) +{ + TupleOptionDetail::from_string(str, opt); +} + +template +inline String option_to_string(const StronglyTypedNumber& opt) { return int_to_str((int)opt); } + +template +inline void option_from_string(const String& str, StronglyTypedNumber& opt) +{ + opt = StronglyTypedNumber{str_to_int(str)}; +} } diff --git a/src/string.cc b/src/string.cc index 8664ce16..a5981f94 100644 --- a/src/string.cc +++ b/src/string.cc @@ -1,4 +1,5 @@ #include "string.hh" +#include "exception.hh" namespace Kakoune { @@ -55,4 +56,21 @@ String String::replace(const String& expression, return String(boost::regex_replace(*this, re, replacement)); } +String option_to_string(const Regex& re) +{ + return String{re.str()}; +} + +void option_from_string(const String& str, Regex& re) +{ + try + { + re = Regex{str.begin(), str.end()}; + } + catch (boost::regex_error& err) + { + throw runtime_error("unable to create regex: "_str + err.what()); + } +} + } diff --git a/src/string.hh b/src/string.hh index ec376fd8..213c8fef 100644 --- a/src/string.hh +++ b/src/string.hh @@ -87,6 +87,9 @@ inline String codepoint_to_str(Codepoint cp) return String(str); } +String option_to_string(const Regex& re); +void option_from_string(const String& str, Regex& re); + } namespace std