colorize_regex become RegexColorizer and maintain a cache of matches

This commit is contained in:
Maxime Coste 2012-08-15 17:08:48 +02:00
parent 5393e9e78b
commit 6e70b805df

View File

@ -54,33 +54,60 @@ void highlight_range(DisplayBuffer& display_buffer,
} }
typedef std::unordered_map<size_t, std::pair<Color, Color>> ColorSpec; typedef std::unordered_map<size_t, std::pair<Color, Color>> ColorSpec;
void colorize_regex(DisplayBuffer& display_buffer, const Regex& ex,
const ColorSpec& colors) class RegexColorizer
{ {
BufferRange range = display_buffer.range(); public:
const Buffer& buffer = range.first.buffer(); RegexColorizer(Regex regex, ColorSpec colors)
range.first = buffer.iterator_at({ range.first.line() - 10, 0 }); : m_regex(std::move(regex)), m_colors(std::move(colors)),
range.second = buffer.iterator_at({ range.second.line() + 10, m_cache_buffer(nullptr), m_cache_timestamp(0)
range.second.column() });
RegexIterator re_it(range.first, range.second, ex);
RegexIterator re_end;
for (; re_it != re_end; ++re_it)
{ {
for (size_t n = 0; n < re_it->size(); ++n) }
{
auto col_it = colors.find(n);
if (col_it == colors.end())
continue;
highlight_range(display_buffer, (*re_it)[n].first, (*re_it)[n].second, true, void operator()(DisplayBuffer& display_buffer)
[&](DisplayAtom& atom) { {
atom.fg_color = col_it->second.first; update_cache_ifn(display_buffer.range().first.buffer());
atom.bg_color = col_it->second.second; for (auto& match : m_cache_matches)
}); {
for (size_t n = 0; n < match.size(); ++n)
{
auto col_it = m_colors.find(n);
if (col_it == m_colors.end())
continue;
highlight_range(display_buffer, match[n].first, match[n].second, true,
[&](DisplayAtom& atom) {
atom.fg_color = col_it->second.first;
atom.bg_color = col_it->second.second;
});
}
} }
} }
}
private:
const Buffer* m_cache_buffer;
size_t m_cache_timestamp;
std::vector<boost::match_results<BufferIterator>> m_cache_matches;
Regex m_regex;
ColorSpec m_colors;
void update_cache_ifn(const Buffer& buffer)
{
if (&buffer == m_cache_buffer and
buffer.timestamp() == m_cache_timestamp)
return;
m_cache_matches.clear();
m_cache_buffer = &buffer;
m_cache_timestamp = buffer.timestamp();
RegexIterator re_it(buffer.begin(), buffer.end(), m_regex);
RegexIterator re_end;
for (; re_it != re_end; ++re_it)
m_cache_matches.push_back(*re_it);
}
};
Color parse_color(const String& color) Color parse_color(const String& color)
{ {
@ -126,8 +153,8 @@ HighlighterAndId colorize_regex_factory(Window& window,
Regex ex(params[0].begin(), params[0].end(), Regex ex(params[0].begin(), params[0].end(),
boost::regex::perl | boost::regex::optimize); boost::regex::perl | boost::regex::optimize);
return HighlighterAndId(id, [=](DisplayBuffer& db) return HighlighterAndId(id, RegexColorizer(std::move(ex),
{ colorize_regex(db, ex, colors); }); std::move(colors)));
} }
catch (boost::regex_error& err) catch (boost::regex_error& err)
{ {