diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-06-28 20:48:44 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-07-05 07:54:28 +1000 |
| commit | 64d0fd91ae1ed395f7fcc88fbb793572fcec6ad4 (patch) | |
| tree | bfbabd4cc6ec73fad2b51617b28f52485af83594 /src | |
| parent | b0ccf97b44f1cfde6094bcf830d57352e9e6d170 (diff) | |
Auto-generate highlighter name if only path is given
Diffstat (limited to 'src')
| -rw-r--r-- | src/commands.cc | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/commands.cc b/src/commands.cc index f77da3b2..80c60cbb 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -749,21 +749,27 @@ const CommandDesc add_highlighter_cmd = { auto begin = parser.begin(); StringView path = *begin++; - StringView name = *begin++; + StringView type = *begin++; Vector<String> highlighter_params; for (; begin != parser.end(); ++begin) highlighter_params.push_back(*begin); - auto it = registry.find(name); + auto it = registry.find(type); if (it == registry.end()) - throw runtime_error(format("no such highlighter factory: '{}'", name)); + throw runtime_error(format("no such highlighter type: '{}'", type)); auto slash = find(path | reverse(), '/'); if (slash == path.rend()) - throw runtime_error("expected name in path"); + throw runtime_error("no parent in path"); - get_highlighter(context, {path.begin(), slash.base() - 1}).add_child( - {slash.base(), path.end()}, it->value.factory(highlighter_params)); + auto auto_name = [](ConstArrayView<String> params) { + return join(params | transform([](StringView s) { return replace(s, "/", "_"); }), "_"); + }; + + const StringView parent{path.begin(), slash.base() - 1}; + String name{slash.base(), path.end()}; + get_highlighter(context, parent).add_child(name.empty() ? auto_name(parser.positionals_from(1)) : std::move(name), + it->value.factory(highlighter_params)); // TODO: better, this will fail if we touch scopes highlighters that impact multiple windows if (context.has_window()) |
