Region highlighter takes regexes for region start/end

This commit is contained in:
Maxime Coste 2013-12-12 21:01:40 +00:00
parent 563f62b467
commit ed484dc333
2 changed files with 61 additions and 60 deletions

View File

@ -505,11 +505,9 @@ template<typename HighlightFunc>
struct RegionHighlighter struct RegionHighlighter
{ {
public: public:
RegionHighlighter(String begin, String end, String ignore_prefix, RegionHighlighter(Regex begin, Regex end, HighlightFunc func)
HighlightFunc func)
: m_begin(std::move(begin)), : m_begin(std::move(begin)),
m_end(std::move(end)), m_end(std::move(end)),
m_ignore_prefix(std::move(ignore_prefix)),
m_func(std::move(func)) m_func(std::move(func))
{} {}
@ -520,9 +518,8 @@ public:
m_func(window, display_buffer, pair.first, pair.second); m_func(window, display_buffer, pair.first, pair.second);
} }
private: private:
String m_begin; Regex m_begin;
String m_end; Regex m_end;
String m_ignore_prefix;
HighlightFunc m_func; HighlightFunc m_func;
struct RegionCache struct RegionCache
@ -539,26 +536,20 @@ private:
return cache; return cache;
cache.regions.clear(); cache.regions.clear();
auto end = buffer.end();
auto reg_beg = std::search(buffer.begin(), end,
m_begin.begin(), m_begin.end());
while (reg_beg != end)
{
auto reg_end = reg_beg + m_begin.length();
do
{
reg_end = std::search(reg_end, end, m_end.begin(), m_end.end());
if (reg_end != end)
reg_end += m_end.length();
}
while (reg_end != end and not m_ignore_prefix.empty() and
std::equal(m_ignore_prefix.begin(), m_ignore_prefix.end(),
reg_end - m_end.length() - m_ignore_prefix.length()));
cache.regions.emplace_back(reg_beg.coord(), reg_end.coord()); boost::match_results<BufferIterator> results;
if (reg_end == end) auto pos = buffer.begin();
auto end = buffer.end();
while (boost::regex_search(pos, end, results, m_begin))
{
pos = results[0].first;
if (boost::regex_search(results[0].second, end, results, m_end))
{
cache.regions.emplace_back(pos.coord(), results[0].second.coord());
pos = results[0].second;
}
else
break; break;
reg_beg = std::search(reg_end, end, m_begin.begin(), m_begin.end());
} }
cache.timestamp = buffer.timestamp(); cache.timestamp = buffer.timestamp();
return cache; return cache;
@ -567,22 +558,21 @@ private:
template<typename HighlightFunc> template<typename HighlightFunc>
RegionHighlighter<HighlightFunc> RegionHighlighter<HighlightFunc>
make_region_highlighter(String begin, String end, String ignore_prefix, make_region_highlighter(Regex begin, Regex end, HighlightFunc func)
HighlightFunc func)
{ {
return RegionHighlighter<HighlightFunc>(std::move(begin), std::move(end), return RegionHighlighter<HighlightFunc>(std::move(begin), std::move(end), std::move(func));
std::move(ignore_prefix), std::move(func));
} }
HighlighterAndId region_factory(HighlighterParameters params) HighlighterAndId region_factory(HighlighterParameters params)
{ {
if (params.size() != 3 and params.size() != 4) try
{
if (params.size() != 3)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
const String& begin = params[0]; Regex begin{params[0]};
const String& end = params[1]; Regex end{params[1]};
const String& ignore_prefix = params.size() == 4 ? params[2] : ""; const ColorPair colors = get_color(params[2]);
const ColorPair colors = get_color(params.back());
auto func = [colors](const Window&, DisplayBuffer& display_buffer, auto func = [colors](const Window&, DisplayBuffer& display_buffer,
BufferCoord begin, BufferCoord end) BufferCoord begin, BufferCoord end)
@ -591,19 +581,25 @@ HighlighterAndId region_factory(HighlighterParameters params)
[&colors](DisplayAtom& atom) { atom.colors = colors; }); [&colors](DisplayAtom& atom) { atom.colors = colors; });
}; };
return HighlighterAndId("region(" + begin + "," + end + "," + ignore_prefix + ")", return HighlighterAndId("region(" + params[0] + "," + params[1] + ")",
make_region_highlighter(begin, end, ignore_prefix, func)); make_region_highlighter(std::move(begin), std::move(end), func));
}
catch (boost::regex_error& err)
{
throw runtime_error(String("regex error: ") + err.what());
}
} }
HighlighterAndId region_ref_factory(HighlighterParameters params) HighlighterAndId region_ref_factory(HighlighterParameters params)
{
try
{ {
if (params.size() != 3 and params.size() != 4) if (params.size() != 3 and params.size() != 4)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
const String& begin = params[0]; Regex begin{params[0]};
const String& end = params[1]; Regex end{params[1]};
const String& ignore_prefix = params.size() == 4 ? params[2] : ""; const String& name = params[2];
const String& name = params.back();
auto func = [name](const Window& window, DisplayBuffer& display_buffer, auto func = [name](const Window& window, DisplayBuffer& display_buffer,
BufferCoord begin, BufferCoord end) BufferCoord begin, BufferCoord end)
@ -612,8 +608,13 @@ HighlighterAndId region_ref_factory(HighlighterParameters params)
apply_highlighter(window, display_buffer, begin, end, ref); apply_highlighter(window, display_buffer, begin, end, ref);
}; };
return HighlighterAndId("regionref(" + begin + "," + end + "," + ignore_prefix + "," + name + ")", return HighlighterAndId("regionref(" + params[0] + "," + params[1] + "," + name + ")",
make_region_highlighter(begin, end, ignore_prefix, func)); make_region_highlighter(std::move(begin), std::move(end), func));
}
catch (boost::regex_error& err)
{
throw runtime_error(String("regex error: ") + err.what());
}
} }
void register_highlighters() void register_highlighters()

View File

@ -43,8 +43,8 @@ addhl -def-group cpp regex "\<(void|int|char|unsigned|float|bool|size_t)\>" 0:ty
addhl -def-group cpp regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not|operator|explicit)\>" 0:keyword addhl -def-group cpp regex "\<(while|for|if|else|do|switch|case|default|goto|break|continue|return|using|try|catch|throw|new|delete|and|or|not|operator|explicit)\>" 0:keyword
addhl -def-group cpp regex "\<(const|mutable|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename|override|final)\>" 0:attribute addhl -def-group cpp regex "\<(const|mutable|auto|namespace|inline|static|volatile|class|struct|enum|union|public|protected|private|template|typedef|virtual|friend|extern|typename|override|final)\>" 0:attribute
addhl -def-group cpp regex "^\h*?#.*?(?<!\\)$" 0:macro addhl -def-group cpp regex "^\h*?#.*?(?<!\\)$" 0:macro
addhl -def-group cpp region '"' '"' %{\} string addhl -def-group cpp region %{(?<!')"} %{(\`|[^\\](\\\\)*)"} string
addhl -def-group cpp region /* */ comment addhl -def-group cpp region /\* \*/ comment
addhl -def-group cpp regex "(//[^\n]*\n)" 0:comment addhl -def-group cpp regex "(//[^\n]*\n)" 0:comment
hook global WinSetOption filetype=cpp %[ hook global WinSetOption filetype=cpp %[