Refactor add-highlighter to make naming explicit

The highlighter name must be given as part of the path of the
highlighter, as the last element.

Fixes #1712
This commit is contained in:
Maxime Coste 2018-06-25 22:04:33 +10:00
parent 18dfecfa9d
commit b4b335155e
9 changed files with 120 additions and 118 deletions

View File

@ -136,7 +136,7 @@ evaluate-commands %sh{
fi fi
printf %s\\n ' printf %s\\n '
add-highlighter shared/ regions -default code -match-capture FT \ add-highlighter shared/FT regions -default code -match-capture \
string %{MAYBEAT(?<!QUOTE)(?<!QUOTE\\)"} %{(?<!\\)(?:\\\\)*"} "" \ string %{MAYBEAT(?<!QUOTE)(?<!QUOTE\\)"} %{(?<!\\)(?:\\\\)*"} "" \
string %{R"([^(]*)\(} %{\)([^")]*)"} "" \ string %{R"([^(]*)\(} %{\)([^")]*)"} "" \
comment /\* \*/ "" \ comment /\* \*/ "" \
@ -144,17 +144,17 @@ evaluate-commands %sh{
disabled ^\h*?#\h*if\h+(?:0|FALSE)\b "#\h*(?:else|elif|endif)" "#\h*if(?:def)?" \ disabled ^\h*?#\h*if\h+(?:0|FALSE)\b "#\h*(?:else|elif|endif)" "#\h*if(?:def)?" \
macro %{^\h*?\K#} %{(?<!\\)\n} "" macro %{^\h*?\K#} %{(?<!\\)\n} ""
add-highlighter shared/FT/string fill string add-highlighter shared/FT/string/fill fill string
add-highlighter shared/FT/comment fill comment add-highlighter shared/FT/comment/fill fill comment
add-highlighter shared/FT/disabled fill rgb:666666 add-highlighter shared/FT/disabled/fill fill rgb:666666
add-highlighter shared/FT/macro fill meta add-highlighter shared/FT/macro/fill fill meta
add-highlighter shared/FT/macro regex ^\h*#include\h+(\S*) 1:module add-highlighter shared/FT/macro/include regex ^\h*#include\h+(\S*) 1:module
' | sed -e "s/FT/${ft}/g; s/QUOTE/'/g; s/MAYBEAT/${maybe_at}/;" ' | sed -e "s/FT/${ft}/g; s/QUOTE/'/g; s/MAYBEAT/${maybe_at}/;"
done done
} }
# c specific # c specific
add-highlighter shared/c/code regex %{\b-?(0x[0-9a-fA-F]+|\d+)[fdiu]?|'((\\.)?|[^'\\])'} 0:value add-highlighter shared/c/code/numbers regex %{\b-?(0x[0-9a-fA-F]+|\d+)[fdiu]?|'((\\.)?|[^'\\])'} 0:value
evaluate-commands %sh{ evaluate-commands %sh{
# Grammar # Grammar
keywords="asm break case continue default do else for goto if return keywords="asm break case continue default do else for goto if return
@ -173,31 +173,31 @@ evaluate-commands %sh{
# Highlight keywords # Highlight keywords
printf %s " printf %s "
add-highlighter shared/c/code regex \b($(join "${keywords}" '|'))\b 0:keyword add-highlighter shared/c/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
add-highlighter shared/c/code regex \b($(join "${attributes}" '|'))\b 0:attribute add-highlighter shared/c/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
add-highlighter shared/c/code regex \b($(join "${types}" '|'))\b 0:type add-highlighter shared/c/code/types regex \b($(join "${types}" '|'))\b 0:type
add-highlighter shared/c/code regex \b($(join "${values}" '|'))\b 0:value add-highlighter shared/c/code/values regex \b($(join "${values}" '|'))\b 0:value
" "
} }
# c++ specific # c++ specific
# integer literals # integer literals
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b[1-9]('?\d+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value add-highlighter shared/cpp/code/integer1 regex %{(?i)(?<!\.)\b[1-9]('?\d+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0b[01]('?[01]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value add-highlighter shared/cpp/code/integer2 regex %{(?i)(?<!\.)\b0b[01]('?[01]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0('?[0-7]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value add-highlighter shared/cpp/code/integer3 regex %{(?i)(?<!\.)\b0('?[0-7]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value add-highlighter shared/cpp/code/integer4 regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*(ul?l?|ll?u?)?\b(?!\.)} 0:value
# floating point literals # floating point literals
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b\d('?\d+)*\.([fl]\b|\B)(?!\.)} 0:value add-highlighter shared/cpp/code/float1 regex %{(?i)(?<!\.)\b\d('?\d+)*\.([fl]\b|\B)(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b\d('?\d+)*\.?e[+-]?\d('?\d+)*[fl]?\b(?!\.)} 0:value add-highlighter shared/cpp/code/float2 regex %{(?i)(?<!\.)\b\d('?\d+)*\.?e[+-]?\d('?\d+)*[fl]?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)(\b(\d('?\d+)*)|\B)\.\d('?[\d]+)*(e[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value add-highlighter shared/cpp/code/float3 regex %{(?i)(?<!\.)(\b(\d('?\d+)*)|\B)\.\d('?[\d]+)*(e[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.([fl]\b|\B)(?!\.)} 0:value add-highlighter shared/cpp/code/float4 regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.([fl]\b|\B)(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.?p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value add-highlighter shared/cpp/code/float5 regex %{(?i)(?<!\.)\b0x[\da-f]('?[\da-f]+)*\.?p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
add-highlighter shared/cpp/code regex %{(?i)(?<!\.)\b0x([\da-f]('?[\da-f]+)*)?\.\d('?[\d]+)*(p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value add-highlighter shared/cpp/code/float6 regex %{(?i)(?<!\.)\b0x([\da-f]('?[\da-f]+)*)?\.\d('?[\d]+)*(p[+-]?\d('?\d+)*)?[fl]?\b(?!\.)} 0:value
# character literals (no multi-character literals) # character literals (no multi-character literals)
add-highlighter shared/cpp/code regex %{(\b(u8|u|U|L)|\B)'((\\.)|[^'\\])'\B} 0:value add-highlighter shared/cpp/code/char regex %{(\b(u8|u|U|L)|\B)'((\\.)|[^'\\])'\B} 0:value
evaluate-commands %sh{ evaluate-commands %sh{
# Grammar # Grammar
@ -223,10 +223,10 @@ evaluate-commands %sh{
# Highlight keywords # Highlight keywords
printf %s " printf %s "
add-highlighter shared/cpp/code regex \b($(join "${keywords}" '|'))\b 0:keyword add-highlighter shared/cpp/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
add-highlighter shared/cpp/code regex \b($(join "${attributes}" '|'))\b 0:attribute add-highlighter shared/cpp/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
add-highlighter shared/cpp/code regex \b($(join "${types}" '|'))\b 0:type add-highlighter shared/cpp/code/types regex \b($(join "${types}" '|'))\b 0:type
add-highlighter shared/cpp/code regex \b($(join "${values}" '|'))\b 0:value add-highlighter shared/cpp/code/values regex \b($(join "${values}" '|'))\b 0:value
" "
} }
@ -235,13 +235,13 @@ evaluate-commands %sh{
builtin_macros="__cplusplus|__STDC_HOSTED__|__FILE__|__LINE__|__DATE__|__TIME__|__STDCPP_DEFAULT_NEW_ALIGNMENT__" builtin_macros="__cplusplus|__STDC_HOSTED__|__FILE__|__LINE__|__DATE__|__TIME__|__STDCPP_DEFAULT_NEW_ALIGNMENT__"
printf %s " printf %s "
add-highlighter shared/c/code regex \b(${builtin_macros})\b 0:builtin add-highlighter shared/c/code/macros regex \b(${builtin_macros})\b 0:builtin
add-highlighter shared/cpp/code regex \b(${builtin_macros})\b 0:builtin add-highlighter shared/cpp/code/macros regex \b(${builtin_macros})\b 0:builtin
" "
} }
# objective-c specific # objective-c specific
add-highlighter shared/objc/code regex %{\b-?\d+[fdiu]?|'((\\.)?|[^'\\])'} 0:value add-highlighter shared/objc/code/number regex %{\b-?\d+[fdiu]?|'((\\.)?|[^'\\])'} 0:value
evaluate-commands %sh{ evaluate-commands %sh{
# Grammar # Grammar
@ -265,11 +265,11 @@ evaluate-commands %sh{
# Highlight keywords # Highlight keywords
printf %s " printf %s "
add-highlighter shared/objc/code regex \b($(join "${keywords}" '|'))\b 0:keyword add-highlighter shared/objc/code/keywords regex \b($(join "${keywords}" '|'))\b 0:keyword
add-highlighter shared/objc/code regex \b($(join "${attributes}" '|'))\b 0:attribute add-highlighter shared/objc/code/attributes regex \b($(join "${attributes}" '|'))\b 0:attribute
add-highlighter shared/objc/code regex \b($(join "${types}" '|'))\b 0:type add-highlighter shared/objc/code/types regex \b($(join "${types}" '|'))\b 0:type
add-highlighter shared/objc/code regex \b($(join "${values}" '|'))\b 0:value add-highlighter shared/objc/code/values regex \b($(join "${values}" '|'))\b 0:value
add-highlighter shared/objc/code regex @($(join "${decorators}" '|'))\b 0:attribute add-highlighter shared/objc/code/decorators regex @($(join "${decorators}" '|'))\b 0:attribute
" "
} }
@ -298,13 +298,13 @@ hook global WinSetOption filetype=(?!c)(?!cpp)(?!objc).* %[
unalias window alt c-family-alternative-file unalias window alt c-family-alternative-file
] ]
hook -group c-highlight global WinSetOption filetype=c %[ add-highlighter window ref c ] hook -group c-highlight global WinSetOption filetype=c %[ add-highlighter window/c ref c ]
hook -group c-highlight global WinSetOption filetype=(?!c).* %[ remove-highlighter window/c ] hook -group c-highlight global WinSetOption filetype=(?!c).* %[ remove-highlighter window/c ]
hook -group cpp-highlight global WinSetOption filetype=cpp %[ add-highlighter window ref cpp ] hook -group cpp-highlight global WinSetOption filetype=cpp %[ add-highlighter window/cpp ref cpp ]
hook -group cpp-highlight global WinSetOption filetype=(?!cpp).* %[ remove-highlighter window/cpp ] hook -group cpp-highlight global WinSetOption filetype=(?!cpp).* %[ remove-highlighter window/cpp ]
hook -group objc-highlight global WinSetOption filetype=objc %[ add-highlighter window ref objc ] hook -group objc-highlight global WinSetOption filetype=objc %[ add-highlighter window/objc ref objc ]
hook -group objc-highlight global WinSetOption filetype=(?!objc).* %[ remove-highlighter window/objc ] hook -group objc-highlight global WinSetOption filetype=(?!objc).* %[ remove-highlighter window/objc ]
declare-option -docstring %{control the type of include guard to be inserted in empty headers declare-option -docstring %{control the type of include guard to be inserted in empty headers

View File

@ -11,7 +11,7 @@ hook global BufCreate (.*/)?(kakrc|.*.kak) %{
# Highlighters & Completion # Highlighters & Completion
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
add-highlighter shared/ regions -default code kakrc \ add-highlighter shared/kakrc regions -default code \
comment (^|\h)\K# $ '' \ comment (^|\h)\K# $ '' \
double_string %{(^|\h)\K"} %{"(?!")} %{(?<!")("")+(?!")} \ double_string %{(^|\h)\K"} %{"(?!")} %{(?<!")("")+(?!")} \
single_string %{(^|\h)\K'} %{'(?!')} %{(?<!')('')+(?!')} \ single_string %{(^|\h)\K'} %{'(?!')} %{(?<!')('')+(?!')} \
@ -48,20 +48,21 @@ evaluate-commands %sh{
}" }"
# Highlight keywords (which are always surrounded by whitespace) # Highlight keywords (which are always surrounded by whitespace)
printf '%s\n' "add-highlighter shared/kakrc/code regex (?:\s|\A)\K($(join "${keywords}" '|'))(?:(?=\s)|\z) 0:keyword printf '%s\n' "add-highlighter shared/kakrc/code/keywords regex (?:\s|\A)\K($(join "${keywords}" '|'))(?:(?=\s)|\z) 0:keyword
add-highlighter shared/kakrc/code regex (?:\s|\A)\K($(join "${attributes}" '|'))(?:(?=\s)|\z) 0:attribute add-highlighter shared/kakrc/code/attributes regex (?:\s|\A)\K($(join "${attributes}" '|'))(?:(?=\s)|\z) 0:attribute
add-highlighter shared/kakrc/code regex (?:\s|\A)\K($(join "${types}" '|'))(?:(?=\s)|\z) 0:type add-highlighter shared/kakrc/code/types regex (?:\s|\A)\K($(join "${types}" '|'))(?:(?=\s)|\z) 0:type
add-highlighter shared/kakrc/code regex (?:\s|\A)\K($(join "${values}" '|'))(?:(?=\s)|\z) 0:value" add-highlighter shared/kakrc/code/values regex (?:\s|\A)\K($(join "${values}" '|'))(?:(?=\s)|\z) 0:value"
} }
add-highlighter shared/kakrc/code regex \brgb:[0-9a-fA-F]{6}\b 0:value add-highlighter shared/kakrc/code/colors regex \brgb:[0-9a-fA-F]{6}\b 0:value
add-highlighter shared/kakrc/code/scopes regex \b(global|shared|buffer|window)(?:\b|/) 0:value
add-highlighter shared/kakrc/double_string fill string add-highlighter shared/kakrc/double_string/fill fill string
add-highlighter shared/kakrc/double_string regex '""' 0:default+b add-highlighter shared/kakrc/double_string/escape regex '""' 0:default+b
add-highlighter shared/kakrc/single_string fill string add-highlighter shared/kakrc/single_string/fill fill string
add-highlighter shared/kakrc/single_string regex "''" 0:default+b add-highlighter shared/kakrc/single_string/escape regex "''" 0:default+b
add-highlighter shared/kakrc/comment fill comment add-highlighter shared/kakrc/comment/fill fill comment
add-highlighter shared/kakrc/shell ref sh add-highlighter shared/kakrc/shell/sh ref sh
# Commands # Commands
# ‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾
@ -82,7 +83,7 @@ define-command -hidden kak-indent-on-new-line %{
# Initialization # Initialization
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾ # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
hook -group kak-highlight global WinSetOption filetype=kak %{ add-highlighter window ref kakrc } hook -group kak-highlight global WinSetOption filetype=kak %{ add-highlighter window/kakrc ref kakrc }
hook global WinSetOption filetype=kak %{ hook global WinSetOption filetype=kak %{
hook window InsertChar \n -group kak-indent kak-indent-on-new-line hook window InsertChar \n -group kak-indent kak-indent-on-new-line

View File

@ -2,16 +2,16 @@ hook global BufCreate .*\.(z|ba|c|k|mk)?sh(rc|_profile)? %{
set-option buffer filetype sh set-option buffer filetype sh
} }
add-highlighter shared/ regions -default code -match-capture sh \ add-highlighter shared/sh regions -default code -match-capture \
double_string %{(?<!\\)(?:\\\\)*\K"} %{(?<!\\)(?:\\\\)*"} '' \ double_string %{(?<!\\)(?:\\\\)*\K"} %{(?<!\\)(?:\\\\)*"} '' \
single_string %{(?<!\\)(?:\\\\)*\K'} %{'} '' \ single_string %{(?<!\\)(?:\\\\)*\K'} %{'} '' \
comment '(?<!\$)#' '$' '' \ comment '(?<!\$)#' '$' '' \
heredoc '<<-?(\w+)' '^\t*(\w+)$' '' heredoc '<<-?(\w+)' '^\t*(\w+)$' ''
add-highlighter shared/sh/double_string fill string add-highlighter shared/sh/double_string/fill fill string
add-highlighter shared/sh/single_string fill string add-highlighter shared/sh/single_string/fill fill string
add-highlighter shared/sh/comment fill comment add-highlighter shared/sh/comment/fill fill comment
add-highlighter shared/sh/heredoc fill string add-highlighter shared/sh/heredoc/fill fill string
evaluate-commands %sh{ evaluate-commands %sh{
# Grammar # Grammar
@ -27,15 +27,15 @@ evaluate-commands %sh{
}" | tr '|' ' ' }" | tr '|' ' '
# Highlight keywords # Highlight keywords
printf %s "add-highlighter shared/sh/code regex \b(${keywords})\b 0:keyword" printf %s "add-highlighter shared/sh/code/keywords regex \b(${keywords})\b 0:keyword"
} }
add-highlighter shared/sh/code regex [\[\]\(\)&|]{1,2} 0:operator add-highlighter shared/sh/code/operators regex [\[\]\(\)&|]{1,2} 0:operator
add-highlighter shared/sh/code regex (\w+)= 1:variable add-highlighter shared/sh/code/variable regex (\w+)= 1:variable
add-highlighter shared/sh/code regex ^\h*(\w+)\h*\(\) 1:variable add-highlighter shared/sh/code/function regex ^\h*(\w+)\h*\(\) 1:function
add-highlighter shared/sh/code regex \$(\w+|\{.+?\}|#|@|\?|\$|!|-|\*) 0:value add-highlighter shared/sh/code/expansion regex \$(\w+|\{.+?\}|#|@|\?|\$|!|-|\*) 0:value
add-highlighter shared/sh/double_string regex \$(\w+|\{.+?\}) 0:value add-highlighter shared/sh/double_string/expansion regex \$(\w+|\{.+?\}) 0:value
hook -group sh-highlight global WinSetOption filetype=sh %{ add-highlighter window ref sh } hook -group sh-highlight global WinSetOption filetype=sh %{ add-highlighter window/sh ref sh }
hook -group sh-highlight global WinSetOption filetype=(?!sh).* %{ remove-highlighter window/sh } hook -group sh-highlight global WinSetOption filetype=(?!sh).* %{ remove-highlighter window/sh }

View File

@ -757,7 +757,13 @@ const CommandDesc add_highlighter_cmd = {
auto it = registry.find(name); auto it = registry.find(name);
if (it == registry.end()) if (it == registry.end())
throw runtime_error(format("no such highlighter factory: '{}'", name)); throw runtime_error(format("no such highlighter factory: '{}'", name));
get_highlighter(context, path).add_child(it->value.factory(highlighter_params));
auto slash = find(path | reverse(), '/');
if (slash == path.rend())
throw runtime_error("expected name in path");
get_highlighter(context, {path.begin(), slash.base() - 1}).add_child(
{slash.base(), path.end()}, it->value.factory(highlighter_params));
// TODO: better, this will fail if we touch scopes highlighters that impact multiple windows // TODO: better, this will fail if we touch scopes highlighters that impact multiple windows
if (context.has_window()) if (context.has_window())

View File

@ -34,7 +34,7 @@ Highlighter& Highlighter::get_child(StringView path)
throw runtime_error("this highlighter does not hold children"); throw runtime_error("this highlighter does not hold children");
} }
void Highlighter::add_child(HighlighterAndId&& hl) void Highlighter::add_child(String name, std::unique_ptr<Highlighter>&& hl)
{ {
throw runtime_error("this highlighter does not hold children"); throw runtime_error("this highlighter does not hold children");
} }

View File

@ -35,8 +35,6 @@ constexpr bool with_bit_ops(Meta::Type<HighlightPass>) { return true; }
struct Highlighter; struct Highlighter;
using HighlighterAndId = std::pair<String, std::unique_ptr<Highlighter>>;
struct DisplaySetup struct DisplaySetup
{ {
// Window position relative to the buffer origin // Window position relative to the buffer origin
@ -71,7 +69,7 @@ struct Highlighter
virtual bool has_children() const; virtual bool has_children() const;
virtual Highlighter& get_child(StringView path); virtual Highlighter& get_child(StringView path);
virtual void add_child(HighlighterAndId&& hl); virtual void add_child(String name, std::unique_ptr<Highlighter>&& hl);
virtual void remove_child(StringView id); virtual void remove_child(StringView id);
virtual Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const; virtual Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const;
virtual void fill_unique_ids(Vector<StringView>& unique_ids) const; virtual void fill_unique_ids(Vector<StringView>& unique_ids) const;
@ -86,7 +84,7 @@ private:
}; };
using HighlighterParameters = ConstArrayView<String>; using HighlighterParameters = ConstArrayView<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>; using HighlighterFactory = std::function<std::unique_ptr<Highlighter> (HighlighterParameters params)>;
struct HighlighterFactoryAndDocstring struct HighlighterFactoryAndDocstring
{ {

View File

@ -24,17 +24,17 @@ void HighlighterGroup::fill_unique_ids(Vector<StringView>& unique_ids) const
hl.value->fill_unique_ids(unique_ids); hl.value->fill_unique_ids(unique_ids);
} }
void HighlighterGroup::add_child(HighlighterAndId&& hl) void HighlighterGroup::add_child(String name, std::unique_ptr<Highlighter>&& hl)
{ {
if ((hl.second->passes() & passes()) != hl.second->passes()) if ((hl->passes() & passes()) != hl->passes())
throw runtime_error{"cannot add that highlighter to this group, passes don't match"}; throw runtime_error{"cannot add that highlighter to this group, passes don't match"};
hl.first = replace(hl.first, "/", "<slash>"); name = replace(name, "/", "<slash>");
if (m_highlighters.contains(hl.first)) if (m_highlighters.contains(name))
throw runtime_error(format("duplicate id: '{}'", hl.first)); throw runtime_error(format("duplicate id: '{}'", name));
m_highlighters.insert({std::move(hl.first), std::move(hl.second)}); m_highlighters.insert({std::move(name), std::move(hl)});
} }
void HighlighterGroup::remove_child(StringView id) void HighlighterGroup::remove_child(StringView id)

View File

@ -21,7 +21,7 @@ public:
HighlighterGroup(HighlightPass passes) : Highlighter{passes} {} HighlighterGroup(HighlightPass passes) : Highlighter{passes} {}
bool has_children() const override { return true; } bool has_children() const override { return true; }
void add_child(HighlighterAndId&& hl) override; void add_child(String name, std::unique_ptr<Highlighter>&& hl) override;
void remove_child(StringView id) override; void remove_child(StringView id) override;
Highlighter& get_child(StringView path) override; Highlighter& get_child(StringView path) override;

View File

@ -220,7 +220,7 @@ auto apply_face = [](const Face& face)
}; };
}; };
static HighlighterAndId create_fill_highlighter(HighlighterParameters params) static std::unique_ptr<Highlighter> create_fill_highlighter(HighlighterParameters params)
{ {
if (params.size() != 1) if (params.size() != 1)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -231,7 +231,7 @@ static HighlighterAndId create_fill_highlighter(HighlighterParameters params)
highlight_range(display_buffer, range.begin, range.end, false, highlight_range(display_buffer, range.begin, range.end, false,
apply_face(context.context.faces()[facespec])); apply_face(context.context.faces()[facespec]));
}; };
return {"fill_" + facespec, make_highlighter(std::move(func))}; return make_highlighter(std::move(func));
} }
template<typename T> template<typename T>
@ -302,7 +302,7 @@ public:
++m_regex_version; ++m_regex_version;
} }
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
if (params.size() < 2) if (params.size() < 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -317,12 +317,9 @@ public:
faces.emplace_back(capture, String{colon+1, spec.end()}); faces.emplace_back(capture, String{colon+1, spec.end()});
} }
String id = format("hlregex'{}'", params[0]);
Regex ex{params[0], RegexCompileFlags::Optimize}; Regex ex{params[0], RegexCompileFlags::Optimize};
return {id, std::make_unique<RegexHighlighter>(std::move(ex), return std::make_unique<RegexHighlighter>(std::move(ex), std::move(faces));
std::move(faces))};
} }
private: private:
@ -481,7 +478,7 @@ private:
RegexHighlighter m_highlighter; RegexHighlighter m_highlighter;
}; };
HighlighterAndId create_dynamic_regex_highlighter(HighlighterParameters params) std::unique_ptr<Highlighter> create_dynamic_regex_highlighter(HighlighterParameters params)
{ {
if (params.size() < 2) if (params.size() < 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -513,7 +510,7 @@ HighlighterAndId create_dynamic_regex_highlighter(HighlighterParameters params)
auto get_regex = [option_name = token->content](const Context& context) { auto get_regex = [option_name = token->content](const Context& context) {
return context.options()[option_name].get<Regex>(); return context.options()[option_name].get<Regex>();
}; };
return {format("dynregex_{}", params[0]), make_hl(get_regex, get_face)}; return make_hl(get_regex, get_face);
} }
auto get_regex = [expr = params[0]](const Context& context){ auto get_regex = [expr = params[0]](const Context& context){
@ -528,10 +525,10 @@ HighlighterAndId create_dynamic_regex_highlighter(HighlighterParameters params)
return Regex{}; return Regex{};
} }
}; };
return {format("dynregex_{}", params[0]), make_hl(get_regex, get_face)}; return make_hl(get_regex, get_face);
} }
HighlighterAndId create_line_highlighter(HighlighterParameters params) std::unique_ptr<Highlighter> create_line_highlighter(HighlighterParameters params)
{ {
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -575,10 +572,10 @@ HighlighterAndId create_line_highlighter(HighlighterParameters params)
it->push_back({ String{' ', remaining}, face }); it->push_back({ String{' ', remaining}, face });
}; };
return {"hlline_" + params[0], make_highlighter(std::move(func))}; return make_highlighter(std::move(func));
} }
HighlighterAndId create_column_highlighter(HighlighterParameters params) std::unique_ptr<Highlighter> create_column_highlighter(HighlighterParameters params)
{ {
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -634,7 +631,7 @@ HighlighterAndId create_column_highlighter(HighlighterParameters params)
} }
}; };
return {"hlcol_" + params[0], make_highlighter(std::move(func))}; return make_highlighter(std::move(func));
} }
struct WrapHighlighter : Highlighter struct WrapHighlighter : Highlighter
@ -853,7 +850,7 @@ struct WrapHighlighter : Highlighter
return get_column(buffer, tabstop, {line, col}); return get_column(buffer, tabstop, {line, col});
} }
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "word", { false, "" } }, { { "word", { false, "" } },
@ -868,9 +865,9 @@ struct WrapHighlighter : Highlighter
if (auto width = parser.get_switch("width")) if (auto width = parser.get_switch("width"))
max_width = str_to_int(*width); max_width = str_to_int(*width);
return {"wrap", std::make_unique<WrapHighlighter>(max_width, (bool)parser.get_switch("word"), return std::make_unique<WrapHighlighter>(max_width, (bool)parser.get_switch("word"),
(bool)parser.get_switch("indent"), (bool)parser.get_switch("indent"),
parser.get_switch("marker").value_or("").str())}; parser.get_switch("marker").value_or("").str());
} }
const bool m_word_wrap; const bool m_word_wrap;
@ -987,7 +984,7 @@ void show_whitespaces(HighlightContext context, DisplayBuffer& display_buffer, B
} }
} }
HighlighterAndId show_whitespaces_factory(HighlighterParameters params) std::unique_ptr<Highlighter> show_whitespaces_factory(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "tab", { true, "" } }, { { "tab", { true, "" } },
@ -1013,7 +1010,7 @@ HighlighterAndId show_whitespaces_factory(HighlighterParameters params)
get_param("lf", "¬"), get_param("lf", "¬"),
get_param("nbsp", "")); get_param("nbsp", ""));
return {"show_whitespaces", make_highlighter(std::move(func), HighlightPass::Move)}; return make_highlighter(std::move(func), HighlightPass::Move);
} }
struct LineNumbersHighlighter : Highlighter struct LineNumbersHighlighter : Highlighter
@ -1024,7 +1021,7 @@ struct LineNumbersHighlighter : Highlighter
m_hl_cursor_line{hl_cursor_line}, m_hl_cursor_line{hl_cursor_line},
m_separator{std::move(separator)} {} m_separator{std::move(separator)} {}
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "relative", { false, "" } }, { { "relative", { false, "" } },
@ -1038,7 +1035,7 @@ struct LineNumbersHighlighter : Highlighter
if (separator.length() > 10) if (separator.length() > 10)
throw runtime_error("separator length is limited to 10 bytes"); throw runtime_error("separator length is limited to 10 bytes");
return {"number_lines", std::make_unique<LineNumbersHighlighter>((bool)parser.get_switch("relative"), (bool)parser.get_switch("hlcursor"), separator.str())}; return std::make_unique<LineNumbersHighlighter>((bool)parser.get_switch("relative"), (bool)parser.get_switch("hlcursor"), separator.str());
} }
private: private:
@ -1161,9 +1158,9 @@ void show_matching_char(HighlightContext context, DisplayBuffer& display_buffer,
} }
} }
HighlighterAndId create_matching_char_highlighter(HighlighterParameters params) std::unique_ptr<Highlighter> create_matching_char_highlighter(HighlighterParameters params)
{ {
return {"show_matching", make_highlighter(show_matching_char)}; return make_highlighter(show_matching_char);
} }
void highlight_selections(HighlightContext context, DisplayBuffer& display_buffer, BufferRange) void highlight_selections(HighlightContext context, DisplayBuffer& display_buffer, BufferRange)
@ -1282,7 +1279,7 @@ struct FlagLinesHighlighter : Highlighter
m_option_name{std::move(option_name)}, m_option_name{std::move(option_name)},
m_default_face{std::move(default_face)} {} m_default_face{std::move(default_face)} {}
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
if (params.size() != 2) if (params.size() != 2)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -1293,7 +1290,7 @@ struct FlagLinesHighlighter : Highlighter
// throw if wrong option type // throw if wrong option type
GlobalScope::instance().options()[option_name].get<LineAndSpecList>(); GlobalScope::instance().options()[option_name].get<LineAndSpecList>();
return {"hlflags_" + params[1], std::make_unique<FlagLinesHighlighter>(option_name, default_face) }; return std::make_unique<FlagLinesHighlighter>(option_name, default_face);
} }
private: private:
@ -1427,7 +1424,7 @@ struct RangesHighlighter : Highlighter
: Highlighter{HighlightPass::Colorize} : Highlighter{HighlightPass::Colorize}
, m_option_name{std::move(option_name)} {} , m_option_name{std::move(option_name)} {}
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
if (params.size() != 1) if (params.size() != 1)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -1436,7 +1433,7 @@ struct RangesHighlighter : Highlighter
// throw if wrong option type // throw if wrong option type
GlobalScope::instance().options()[option_name].get<RangeAndStringList>(); GlobalScope::instance().options()[option_name].get<RangeAndStringList>();
return {"hlranges_" + params[0], std::make_unique<RangesHighlighter>(option_name)}; return std::make_unique<RangesHighlighter>(option_name);
} }
private: private:
@ -1469,7 +1466,7 @@ struct ReplaceRangesHighlighter : Highlighter
: Highlighter{HighlightPass::Colorize} : Highlighter{HighlightPass::Colorize}
, m_option_name{std::move(option_name)} {} , m_option_name{std::move(option_name)} {}
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
if (params.size() != 1) if (params.size() != 1)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -1478,7 +1475,7 @@ struct ReplaceRangesHighlighter : Highlighter
// throw if wrong option type // throw if wrong option type
GlobalScope::instance().options()[option_name].get<RangeAndStringList>(); GlobalScope::instance().options()[option_name].get<RangeAndStringList>();
return {"replace_ranges_" + params[0], std::make_unique<ReplaceRangesHighlighter>(option_name)}; return std::make_unique<ReplaceRangesHighlighter>(option_name);
} }
private: private:
@ -1532,16 +1529,16 @@ HighlightPass parse_passes(StringView str)
return passes; return passes;
} }
HighlighterAndId create_highlighter_group(HighlighterParameters params) std::unique_ptr<Highlighter> create_highlighter_group(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "passes", { true, "" } } }, { { "passes", { true, "" } } },
ParameterDesc::Flags::SwitchesOnlyAtStart, 1, 1 ParameterDesc::Flags::SwitchesOnlyAtStart, 0, 0
}; };
ParametersParser parser{params, param_desc}; ParametersParser parser{params, param_desc};
HighlightPass passes = parse_passes(parser.get_switch("passes").value_or("colorize")); HighlightPass passes = parse_passes(parser.get_switch("passes").value_or("colorize"));
return HighlighterAndId(parser[0], std::make_unique<HighlighterGroup>(passes)); return std::make_unique<HighlighterGroup>(passes);
} }
struct ReferenceHighlighter : Highlighter struct ReferenceHighlighter : Highlighter
@ -1549,7 +1546,7 @@ struct ReferenceHighlighter : Highlighter
ReferenceHighlighter(HighlightPass passes, String name) ReferenceHighlighter(HighlightPass passes, String name)
: Highlighter{passes}, m_name{std::move(name)} {} : Highlighter{passes}, m_name{std::move(name)} {}
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "passes", { true, "" } } }, { { "passes", { true, "" } } },
@ -1557,7 +1554,7 @@ struct ReferenceHighlighter : Highlighter
}; };
ParametersParser parser{params, param_desc}; ParametersParser parser{params, param_desc};
HighlightPass passes = parse_passes(parser.get_switch("passes").value_or("colorize")); HighlightPass passes = parse_passes(parser.get_switch("passes").value_or("colorize"));
return {parser[0], std::make_unique<ReferenceHighlighter>(passes, parser[0])}; return std::make_unique<ReferenceHighlighter>(passes, parser[0]);
} }
private: private:
@ -1855,7 +1852,7 @@ public:
return { 0, 0, complete(path, cursor_pos, container) }; return { 0, 0, complete(path, cursor_pos, container) };
} }
static HighlighterAndId create(HighlighterParameters params) static std::unique_ptr<Highlighter> create(HighlighterParameters params)
{ {
static const ParameterDesc param_desc{ static const ParameterDesc param_desc{
{ { "default", { true, "" } }, { "match-capture", { false, "" } } }, { { "default", { true, "" } }, { "match-capture", { false, "" } } },
@ -1863,12 +1860,12 @@ public:
}; };
ParametersParser parser{params, param_desc}; ParametersParser parser{params, param_desc};
if ((parser.positional_count() % 4) != 1) if ((parser.positional_count() % 4) != 0)
throw runtime_error("wrong parameter count, expected <id> (<group name> <begin> <end> <recurse>)+"); throw runtime_error("wrong parameter count, expected (<group name> <begin> <end> <recurse>)+");
const bool match_capture = (bool)parser.get_switch("match-capture"); const bool match_capture = (bool)parser.get_switch("match-capture");
RegionsHighlighter::RegionDescList regions; RegionsHighlighter::RegionDescList regions;
for (size_t i = 1; i < parser.positional_count(); i += 4) for (size_t i = 0; i < parser.positional_count(); i += 4)
{ {
if (parser[i].empty() or parser[i+1].empty() or parser[i+2].empty()) if (parser[i].empty() or parser[i+1].empty() or parser[i+2].empty())
throw runtime_error("group id, begin and end must not be empty"); throw runtime_error("group id, begin and end must not be empty");
@ -1883,7 +1880,7 @@ public:
} }
auto default_group = parser.get_switch("default").value_or(StringView{}).str(); auto default_group = parser.get_switch("default").value_or(StringView{}).str();
return {parser[0], std::make_unique<RegionsHighlighter>(std::move(regions), default_group)}; return std::make_unique<RegionsHighlighter>(std::move(regions), default_group);
} }
private: private:
@ -1997,9 +1994,9 @@ private:
void setup_builtin_highlighters(HighlighterGroup& group) void setup_builtin_highlighters(HighlighterGroup& group)
{ {
group.add_child({"tabulations"_str, std::make_unique<TabulationHighlighter>()}); group.add_child("tabulations"_str, std::make_unique<TabulationHighlighter>());
group.add_child({"unprintable"_str, make_highlighter(expand_unprintable)}); group.add_child("unprintable"_str, make_highlighter(expand_unprintable));
group.add_child({"selections"_str, make_highlighter(highlight_selections)}); group.add_child("selections"_str, make_highlighter(highlight_selections));
} }
void register_highlighters() void register_highlighters()