1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-28 09:07:19 +03:00

Add HierachicalHighlighter class

HierachicalHighlighter contains a map of names to HighlighterGroup
and can wrap any highlighter that wants to access user settable
sub groups.
This commit is contained in:
Maxime Coste 2014-06-10 21:35:03 +01:00
parent 479c31b571
commit 4bb62d63e6
2 changed files with 55 additions and 7 deletions

View File

@ -28,13 +28,20 @@ HighlighterGroup& HighlighterGroup::get_group(StringView path, Codepoint path_se
auto it = m_highlighters.find(id);
if (it == m_highlighters.end())
throw group_not_found("no such id: "_str + id);
HighlighterGroup* group = it->second.template target<HighlighterGroup>();
if (not group)
throw group_not_found("not a group: "_str + id);
if (sep_it != path.end())
return group->get_group(StringView(sep_it+1, path.end()), path_separator);
else
if (auto* group = it->second.target<HighlighterGroup>())
{
if (sep_it != path.end())
return group->get_group({sep_it+1, path.end()}, path_separator);
return *group;
}
else if (auto* hier_group = it->second.target<HierachicalHighlighter>())
{
if (sep_it == path.end())
throw group_not_found("not a leaf group: "_str + id);
return hier_group->get_group({sep_it+1, path.end()}, path_separator);
}
else
throw group_not_found("not a group: "_str + id);
}
CandidateList HighlighterGroup::complete_id(StringView prefix, ByteCount cursor_pos) const
@ -50,4 +57,17 @@ CandidateList HighlighterGroup::complete_group_id(StringView prefix, ByteCount c
});
}
HighlighterGroup& HierachicalHighlighter::get_group(StringView path, Codepoint path_separator)
{
auto sep_it = std::find(path.begin(), path.end(), path_separator);
StringView id(path.begin(), sep_it);
auto it = m_groups.find(id);
if (it == m_groups.end())
throw group_not_found("no such id: "_str + id);
if (sep_it != path.end())
return it->second.get_group(StringView(sep_it+1, path.end()), path_separator);
else
return it->second;
}
}

View File

@ -17,7 +17,9 @@ struct group_not_found : public runtime_error
class HighlighterGroup
{
public:
void operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer);
void operator()(const Context& context,
HighlightFlags flags,
DisplayBuffer& display_buffer);
void append(HighlighterAndId&& hl);
void remove(StringView id);
@ -31,6 +33,32 @@ private:
id_map<HighlighterFunc> m_highlighters;
};
class HierachicalHighlighter
{
public:
using GroupMap = id_map<HighlighterGroup>;
using Callback = std::function<void (GroupMap& groups,
const Context& context,
HighlightFlags flags,
DisplayBuffer& display_buffer)>;
HierachicalHighlighter(Callback callback, GroupMap groups)
: m_callback(std::move(callback)), m_groups(std::move(groups)) {}
void operator()(const Context& context,
HighlightFlags flags,
DisplayBuffer& display_buffer)
{
m_callback(m_groups, context, flags, display_buffer);
}
HighlighterGroup& get_group(StringView path, Codepoint path_separator = 0);
protected:
Callback m_callback;
GroupMap m_groups;
};
struct DefinedHighlighters : public HighlighterGroup,
public Singleton<DefinedHighlighters>
{