Add support for tuple Options, implement LineAndFlag using that

This commit is contained in:
Maxime Coste 2013-03-29 19:31:06 +01:00
parent 4b3067f8d0
commit a80cee0d2c
9 changed files with 98 additions and 61 deletions

View File

@ -38,4 +38,14 @@ String color_to_str(const Color& color)
return "default"; 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);
}
} }

View File

@ -26,6 +26,9 @@ using ColorPair = std::pair<Color, Color>;
Color str_to_color(const String& color); Color str_to_color(const String& color);
String color_to_str(const Color& 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 #endif // color_hh_INCLUDED

View File

@ -10,6 +10,7 @@
#include "input_handler.hh" #include "input_handler.hh"
#include "string.hh" #include "string.hh"
#include "highlighter.hh" #include "highlighter.hh"
#include "highlighters.hh"
#include "filter.hh" #include "filter.hh"
#include "register_manager.hh" #include "register_manager.hh"
#include "completion.hh" #include "completion.hh"

View File

@ -348,14 +348,14 @@ public:
CharCount width = 0; CharCount width = 0;
for (auto& l : lines) 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}; const String empty{' ', width};
for (auto& line : display_buffer.lines()) for (auto& line : display_buffer.lines())
{ {
int line_num = (int)line.buffer_line() + 1; int line_num = (int)line.buffer_line() + 1;
auto it = find_if(lines, [&](const LineAndFlag& l) { return l.line == line_num; }); auto it = find_if(lines, [&](const LineAndFlag& l) { return std::get<0>(l) == line_num; });
DisplayAtom atom{AtomContent(it != lines.end() ? it->flag : empty)}; DisplayAtom atom{AtomContent(it != lines.end() ? std::get<2>(*it) : empty)};
atom.colors = { it != lines.end() ? it->color : Color::Default , m_bg }; atom.colors = { it != lines.end() ? std::get<1>(*it) : Color::Default , m_bg };
line.insert(line.begin(), std::move(atom)); line.insert(line.begin(), std::move(atom));
} }
} }

View File

@ -3,11 +3,15 @@
#include "highlighter.hh" #include "highlighter.hh"
#include "color.hh"
namespace Kakoune namespace Kakoune
{ {
void register_highlighters(); void register_highlighters();
using LineAndFlag = std::tuple<LineCount, Color, String>;
} }
#endif // highlighters_hh_INCLUDED #endif // highlighters_hh_INCLUDED

View File

@ -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 <line>:<color>:<flag>");
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};
}
}

View File

@ -1,11 +1,13 @@
#ifndef option_types_hh_INCLUDED #ifndef option_types_hh_INCLUDED
#define option_types_hh_INCLUDED #define option_types_hh_INCLUDED
#include "units.hh"
#include "string.hh" #include "string.hh"
#include "color.hh" #include "units.hh"
#include "exception.hh" #include "exception.hh"
#include <tuple>
#include <vector>
namespace Kakoune namespace Kakoune
{ {
@ -52,24 +54,63 @@ void option_from_string(const String& str, std::vector<T>& opt)
} }
} }
String option_to_string(const Regex& re);
void option_from_string(const String& str, Regex& re);
struct LineAndFlag template<size_t I, typename... Types>
struct TupleOptionDetail
{ {
LineCount line; static String to_string(const std::tuple<Types...>& opt)
Color color; {
String flag; return TupleOptionDetail<I-1, Types...>::to_string(opt) + ":" + option_to_string(std::get<I>(opt));
}
bool operator==(const LineAndFlag& other) const static void from_string(const String& str, std::tuple<Types...>& opt)
{ return line == other.line and color == other.color and flag == other.flag; } {
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 option_from_string(String{it+1, std::find(it+1, end, ':')}, std::get<I>(opt));
{ return not (*this == other); } TupleOptionDetail<I-1, Types...>::from_string(str, opt);
}
}; };
String option_to_string(const LineAndFlag& opt); template<typename... Types>
void option_from_string(const String& str, LineAndFlag& opt); struct TupleOptionDetail<0, Types...>
{
static String to_string(const std::tuple<Types...>& opt)
{
return option_to_string(std::get<0>(opt));
}
static void from_string(const String& str, std::tuple<Types...>& opt)
{
option_from_string(String{str.begin(), std::find(str.begin(), str.end(), ':')}, std::get<0>(opt));
}
};
template<typename... Types>
String option_to_string(const std::tuple<Types...>& opt)
{
return TupleOptionDetail<sizeof...(Types)-1, Types...>::to_string(opt);
}
template<typename... Types>
void option_from_string(const String& str, std::tuple<Types...>& opt)
{
TupleOptionDetail<sizeof...(Types)-1, Types...>::from_string(str, opt);
}
template<typename RealType, typename ValueType = int>
inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt) { return int_to_str((int)opt); }
template<typename RealType, typename ValueType = int>
inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt)
{
opt = StronglyTypedNumber<RealType, ValueType>{str_to_int(str)};
}
} }

View File

@ -1,4 +1,5 @@
#include "string.hh" #include "string.hh"
#include "exception.hh"
namespace Kakoune namespace Kakoune
{ {
@ -55,4 +56,21 @@ String String::replace(const String& expression,
return String(boost::regex_replace(*this, re, replacement)); 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());
}
}
} }

View File

@ -87,6 +87,9 @@ inline String codepoint_to_str(Codepoint cp)
return String(str); return String(str);
} }
String option_to_string(const Regex& re);
void option_from_string(const String& str, Regex& re);
} }
namespace std namespace std