Refactor DynamicRegexHighlighter, search hl uses Search colalias

fixes #122
This commit is contained in:
Maxime Coste 2014-05-13 19:35:28 +01:00
parent 1bb8fc3dad
commit 6b42c48c3f
2 changed files with 47 additions and 22 deletions

View File

@ -64,6 +64,7 @@ ColorRegistry::ColorRegistry()
{ "StatusCursor", { Colors::Black, Colors::Cyan } }, { "StatusCursor", { Colors::Black, Colors::Cyan } },
{ "Prompt", { Colors::Yellow, Colors::Default } }, { "Prompt", { Colors::Yellow, Colors::Default } },
{ "MatchingChar", { Colors::Default, Colors::Magenta } }, { "MatchingChar", { Colors::Default, Colors::Magenta } },
{ "Search", { Colors::Default, Colors::Magenta } },
} }
{} {}

View File

@ -267,12 +267,14 @@ HighlighterAndId colorize_regex_factory(HighlighterParameters params)
} }
} }
template<typename RegexGetter> template<typename RegexGetter, typename ColorGetter>
class DynamicRegexHighlighter class DynamicRegexHighlighter
{ {
public: public:
DynamicRegexHighlighter(const ColorSpec& colors, RegexGetter getter) DynamicRegexHighlighter(RegexGetter regex_getter, ColorGetter color_getter)
: m_regex_getter(getter), m_colors(colors), m_colorizer(Regex(), m_colors) {} : m_regex_getter(std::move(regex_getter)),
m_color_getter(std::move(color_getter)),
m_colorizer(Regex(), ColorSpec{}) {}
void operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) void operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)
{ {
@ -280,40 +282,56 @@ public:
return; return;
Regex regex = m_regex_getter(context); Regex regex = m_regex_getter(context);
if (regex != m_last_regex) ColorSpec color = m_color_getter(context);
if (regex != m_last_regex or color != m_last_color)
{ {
m_last_regex = regex; m_last_regex = regex;
m_last_color = color;
if (not m_last_regex.empty()) if (not m_last_regex.empty())
m_colorizer = RegexColorizer{m_last_regex, m_colors}; m_colorizer = RegexColorizer{m_last_regex, color};
} }
if (not m_last_regex.empty()) if (not m_last_regex.empty() and not m_last_color.empty())
m_colorizer(context, flags, display_buffer); m_colorizer(context, flags, display_buffer);
} }
private: private:
Regex m_last_regex; Regex m_last_regex;
ColorSpec m_colors;
RegexColorizer m_colorizer;
RegexGetter m_regex_getter; RegexGetter m_regex_getter;
ColorSpec m_last_color;
ColorGetter m_color_getter;
RegexColorizer m_colorizer;
}; };
template<typename RegexGetter, typename ColorGetter>
DynamicRegexHighlighter<RegexGetter, ColorGetter>
make_dynamic_regex_highlighter(RegexGetter regex_getter, ColorGetter color_getter)
{
return DynamicRegexHighlighter<RegexGetter, ColorGetter>(
std::move(regex_getter), std::move(color_getter));
}
HighlighterAndId highlight_search_factory(HighlighterParameters params) HighlighterAndId highlight_search_factory(HighlighterParameters params)
{ {
if (params.size() != 1) if (params.size() != 0)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
try auto get_color = [](const Context& context){
{ return ColorSpec{ { 0, &Kakoune::get_color("Search") } };
ColorSpec colors { { 0, &get_color(params[0]) } }; };
auto get_regex = [](const Context&){ auto get_regex = [](const Context&){
auto s = RegisterManager::instance()['/'].values(Context{}); auto s = RegisterManager::instance()['/'].values(Context{});
return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()}; try
{
return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()};
}
catch (boost::regex_error& err)
{
return Regex{};
}
}; };
return {"hlsearch", DynamicRegexHighlighter<decltype(get_regex)>{colors, get_regex}}; return {"hlsearch", make_dynamic_regex_highlighter(get_regex, get_color)};
}
catch (boost::regex_error& err)
{
throw runtime_error(String("regex error: ") + err.what());
}
} }
HighlighterAndId highlight_regex_option_factory(HighlighterParameters params) HighlighterAndId highlight_regex_option_factory(HighlighterParameters params)
@ -321,13 +339,19 @@ HighlighterAndId highlight_regex_option_factory(HighlighterParameters params)
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
ColorSpec colors { { 0, &get_color(params[1]) } }; const ColorPair& color = get_color(params[1]);
auto get_color = [&](const Context&){
return ColorSpec{ { 0, &color } };
};
String option_name = params[0]; String option_name = params[0];
// verify option type now // verify option type now
GlobalOptions::instance()[option_name].get<Regex>(); GlobalOptions::instance()[option_name].get<Regex>();
auto get_regex = [option_name](const Context& context){ return context.options()[option_name].get<Regex>(); }; auto get_regex = [option_name](const Context& context){
return {"hloption_" + option_name, DynamicRegexHighlighter<decltype(get_regex)>{colors, get_regex}}; return context.options()[option_name].get<Regex>();
};
return {"hloption_" + option_name, make_dynamic_regex_highlighter(get_regex, get_color)};
} }
void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer)