Convert Region highlighter to a HierarchicalHighlighter

add a region using addhl region <id> <begin_regex> <end_regex>
then fill the region with addhl -group <id>/content ...
This commit is contained in:
Maxime Coste 2014-06-10 22:02:42 +01:00
parent 4c942c4a3a
commit 37d66b1e0b
3 changed files with 35 additions and 65 deletions

View File

@ -45,8 +45,13 @@ 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 /\* \*/ comment addhl -def-group cpp region string %{(?<!')"} %{(?<!\\)(\\\\)*"}
addhl -def-group cpp/string/content fill string
addhl -def-group cpp region comment /\* \*/
addhl -def-group cpp/comment/content fill 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 %[

View File

@ -8,10 +8,17 @@ addhl -def-group kakrc regex \<(default|black|red|green|yellow|blue|magenta|cyan
addhl -def-group kakrc regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string addhl -def-group kakrc regex (?<=\<hook)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\H+) 2:attribute 3:error 4:identifier 5:string
addhl -def-group kakrc regex (?<=\<set)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\S+) 2:attribute 3:error 4:identifier 5:value addhl -def-group kakrc regex (?<=\<set)\h+((global|buffer|window)|(\S+))\h+(\S+)\h+(\S+) 2:attribute 3:error 4:identifier 5:value
addhl -def-group kakrc regex (?<=\<regex)\h+(\S+) 1:string addhl -def-group kakrc regex (?<=\<regex)\h+(\S+) 1:string
addhl -def-group kakrc region %{(^|\h)"} %{(?<!\\)(\\\\)*"} string
addhl -def-group kakrc region %{(^|\h)'} %{(?<!\\)(\\\\)*'} string addhl -def-group kakrc region double_string %{(^|\h)"} %{(?<!\\)(\\\\)*"}
addhl -def-group kakrc/double_string/content fill string
addhl -def-group kakrc region single_string %{(^|\h)'} %{(?<!\\)(\\\\)*'}
addhl -def-group kakrc/single_string/content fill string
addhl -def-group kakrc regex (^|\h)\#[^\n]*\n 0:comment addhl -def-group kakrc regex (^|\h)\#[^\n]*\n 0:comment
addhl -def-group kakrc region_ref '%sh\{' '\}' sh
addhl -def-group kakrc region shell '%sh\{' '\}'
addhl -def-group kakrc/shell/content ref sh
hook global WinSetOption filetype=kak %{ addhl ref kakrc } hook global WinSetOption filetype=kak %{ addhl ref kakrc }
hook global WinSetOption filetype=(?!kak).* %{ rmhl kakrc } hook global WinSetOption filetype=(?!kak).* %{ rmhl kakrc }

View File

@ -667,20 +667,24 @@ HighlighterAndId reference_factory(HighlighterParameters params)
}); });
} }
template<typename HighlightFunc>
struct RegionHighlighter struct RegionHighlighter
{ {
public: public:
RegionHighlighter(Regex begin, Regex end, HighlightFunc func) RegionHighlighter(Regex begin, Regex end)
: m_begin(std::move(begin)), : m_begin(std::move(begin)),
m_end(std::move(end)), m_end(std::move(end))
m_func(std::move(func))
{} {}
void operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) void operator()(HierachicalHighlighter::GroupMap groups, const Context& context,
HighlightFlags flags, DisplayBuffer& display_buffer)
{ {
if (flags != HighlightFlags::Highlight) if (flags != HighlightFlags::Highlight)
return; return;
auto it = groups.find("content");
if (it == groups.end())
return;
auto range = display_buffer.range(); auto range = display_buffer.range();
const auto& buffer = context.buffer(); const auto& buffer = context.buffer();
auto& regions = update_cache_ifn(buffer); auto& regions = update_cache_ifn(buffer);
@ -694,13 +698,13 @@ public:
return c; return c;
}; };
for (; begin != end; ++begin) for (; begin != end; ++begin)
m_func(context, flags, display_buffer, apply_highlighter(context, flags, display_buffer,
correct(begin->begin), correct(begin->end)); correct(begin->begin), correct(begin->end),
it->second);
} }
private: private:
Regex m_begin; Regex m_begin;
Regex m_end; Regex m_end;
HighlightFunc m_func;
struct Region struct Region
{ {
@ -851,13 +855,6 @@ private:
} }
}; };
template<typename HighlightFunc>
RegionHighlighter<HighlightFunc>
make_region_highlighter(Regex begin, Regex end, HighlightFunc func)
{
return RegionHighlighter<HighlightFunc>(std::move(begin), std::move(end), std::move(func));
}
HighlighterAndId region_factory(HighlighterParameters params) HighlighterAndId region_factory(HighlighterParameters params)
{ {
try try
@ -865,50 +862,12 @@ HighlighterAndId region_factory(HighlighterParameters params)
if (params.size() != 3) if (params.size() != 3)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
Regex begin{params[0], Regex::nosubs | Regex::optimize }; Regex begin{params[1], Regex::nosubs | Regex::optimize };
Regex end{params[1], Regex::nosubs | Regex::optimize }; Regex end{params[2], Regex::nosubs | Regex::optimize };
const ColorPair colors = get_color(params[2]); return {params[0],
HierachicalHighlighter(RegionHighlighter(std::move(begin),
auto func = [colors](const Context&, HighlightFlags flags, DisplayBuffer& display_buffer, std::move(end)),
ByteCoord begin, ByteCoord end) { { "content", HighlighterGroup{} } })};
{
highlight_range(display_buffer, begin, end, true,
[&colors](DisplayAtom& atom) { atom.colors = colors; });
};
return HighlighterAndId("region(" + params[0] + "," + params[1] + ")",
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)
{
try
{
if (params.size() != 3 and params.size() != 4)
throw runtime_error("wrong parameter count");
Regex begin{params[0], Regex::nosubs | Regex::optimize };
Regex end{params[1], Regex::nosubs | Regex::optimize };
const String& name = params[2];
auto func = [name](const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer,
ByteCoord begin, ByteCoord end)
{
try
{
HighlighterGroup& ref = DefinedHighlighters::instance().get_group(name, '/');
apply_highlighter(context, flags, display_buffer, begin, end, ref);
}
catch (group_not_found&) {}
};
return HighlighterAndId("regionref(" + params[0] + "," + params[1] + "," + name + ")",
make_region_highlighter(std::move(begin), std::move(end), func));
} }
catch (boost::regex_error& err) catch (boost::regex_error& err)
{ {
@ -931,7 +890,6 @@ void register_highlighters()
registry.register_func("flag_lines", flag_lines_factory); registry.register_func("flag_lines", flag_lines_factory);
registry.register_func("ref", reference_factory); registry.register_func("ref", reference_factory);
registry.register_func("region", region_factory); registry.register_func("region", region_factory);
registry.register_func("region_ref", region_ref_factory);
} }
} }