Regex Highlighter: highlight 10 lines above and below displayed range

and some code cleanup
This commit is contained in:
Maxime Coste 2012-08-08 19:02:36 +02:00
parent 1ede3f6b13
commit b29b75aa81

View File

@ -53,24 +53,28 @@ 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, void colorize_regex(DisplayBuffer& display_buffer, const Regex& ex,
const Regex& ex, const ColorSpec& colors)
ColorSpec colors)
{ {
const BufferRange& range = display_buffer.range(); BufferRange range = display_buffer.range();
const Buffer& buffer = range.first.buffer();
range.first = buffer.iterator_at({ range.first.line() - 10, 0 });
range.second = buffer.iterator_at({ range.second.line() + 10, 0 });
RegexIterator re_it(range.first, range.second, ex); RegexIterator re_it(range.first, range.second, ex);
RegexIterator re_end; RegexIterator re_end;
for (; re_it != re_end; ++re_it) for (; re_it != re_end; ++re_it)
{ {
for (size_t n = 0; n < re_it->size(); ++n) for (size_t n = 0; n < re_it->size(); ++n)
{ {
if (colors.find(n) == colors.end()) auto col_it = colors.find(n);
if (col_it == colors.end())
continue; continue;
highlight_range(display_buffer, (*re_it)[n].first, (*re_it)[n].second, true, highlight_range(display_buffer, (*re_it)[n].first, (*re_it)[n].second, true,
[&](DisplayAtom& atom) { [&](DisplayAtom& atom) {
atom.fg_color = colors[n].first; atom.fg_color = col_it->second.first;
atom.bg_color = colors[n].second; atom.bg_color = col_it->second.second;
}); });
} }
} }
@ -120,9 +124,9 @@ 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, std::bind(colorize_regex, _1, ex, return HighlighterAndId(id, [=](DisplayBuffer& db)
std::move(colors))); { colorize_regex(db, ex, colors); });
} }
catch (boost::regex_error& err) catch (boost::regex_error& err)
{ {
throw runtime_error(String("regex error: ") + err.what()); throw runtime_error(String("regex error: ") + err.what());