2014-06-10 20:58:02 +02:00
|
|
|
#include "highlighter_group.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
static constexpr Codepoint path_separator = '/';
|
|
|
|
|
|
|
|
|
2014-06-12 22:52:23 +02:00
|
|
|
void HighlighterGroup::operator()(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer) const
|
2014-06-10 20:58:02 +02:00
|
|
|
{
|
|
|
|
for (auto& hl : m_highlighters)
|
|
|
|
hl.second(context, flags, display_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HighlighterGroup::append(HighlighterAndId&& hl)
|
|
|
|
{
|
|
|
|
if (m_highlighters.contains(hl.first))
|
|
|
|
throw runtime_error("duplicate id: " + hl.first);
|
|
|
|
|
|
|
|
m_highlighters.append(std::move(hl));
|
|
|
|
}
|
|
|
|
void HighlighterGroup::remove(StringView id)
|
|
|
|
{
|
|
|
|
m_highlighters.remove(id);
|
|
|
|
}
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
HighlighterGroup& HighlighterGroup::get_group(StringView path)
|
2014-06-10 20:58:02 +02:00
|
|
|
{
|
2014-06-15 17:04:38 +02:00
|
|
|
auto sep_it = find(path, path_separator);
|
2014-06-10 20:58:02 +02:00
|
|
|
StringView id(path.begin(), sep_it);
|
|
|
|
auto it = m_highlighters.find(id);
|
|
|
|
if (it == m_highlighters.end())
|
|
|
|
throw group_not_found("no such id: "_str + id);
|
2014-06-10 22:35:03 +02:00
|
|
|
if (auto* group = it->second.target<HighlighterGroup>())
|
|
|
|
{
|
|
|
|
if (sep_it != path.end())
|
2014-06-15 17:04:38 +02:00
|
|
|
return group->get_group({sep_it+1, path.end()});
|
2014-06-10 20:58:02 +02:00
|
|
|
return *group;
|
2014-06-10 22:35:03 +02:00
|
|
|
}
|
|
|
|
else if (auto* hier_group = it->second.target<HierachicalHighlighter>())
|
|
|
|
{
|
|
|
|
if (sep_it == path.end())
|
|
|
|
throw group_not_found("not a leaf group: "_str + id);
|
2014-06-15 17:04:38 +02:00
|
|
|
return hier_group->get_group({sep_it+1, path.end()});
|
2014-06-10 22:35:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw group_not_found("not a group: "_str + id);
|
2014-06-10 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
HighlighterFunc HighlighterGroup::get_highlighter(StringView path) const
|
2014-06-12 22:52:23 +02:00
|
|
|
{
|
2014-06-15 17:04:38 +02:00
|
|
|
auto sep_it = find(path, path_separator);
|
2014-06-12 22:52:23 +02:00
|
|
|
StringView id(path.begin(), sep_it);
|
|
|
|
auto it = m_highlighters.find(id);
|
|
|
|
if (it == m_highlighters.end())
|
|
|
|
throw group_not_found("no such id: "_str + id);
|
|
|
|
if (sep_it == path.end())
|
|
|
|
return HighlighterFunc{std::ref(it->second)};
|
|
|
|
else if (auto* group = it->second.target<HighlighterGroup>())
|
2014-06-15 17:04:38 +02:00
|
|
|
return group->get_highlighter({sep_it+1, path.end()});
|
2014-06-12 22:52:23 +02:00
|
|
|
else if (auto* hier_group = it->second.target<HierachicalHighlighter>())
|
2014-06-15 17:04:38 +02:00
|
|
|
return hier_group->get_highlighter({sep_it+1, path.end()});
|
2014-06-12 22:52:23 +02:00
|
|
|
else
|
|
|
|
throw group_not_found("not a group: "_str + id);
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
template<Completions (HighlighterGroup::*hg_complete)(StringView path, ByteCount cursor_pos) const,
|
|
|
|
Completions (HierachicalHighlighter::*hh_complete)(StringView path, ByteCount cursor_pos) const,
|
|
|
|
typename Condition>
|
|
|
|
Completions complete_impl(const id_map<HighlighterFunc>& highlighters, Condition condition,
|
|
|
|
StringView path, ByteCount cursor_pos)
|
2014-06-10 20:58:02 +02:00
|
|
|
{
|
2014-06-15 17:04:38 +02:00
|
|
|
auto sep_it = find(path, path_separator);
|
|
|
|
StringView id(path.begin(), sep_it);
|
|
|
|
if (sep_it == path.end())
|
2014-06-16 01:49:39 +02:00
|
|
|
return { 0_byte, path.length(), highlighters.complete_id_if(path, cursor_pos, condition) };
|
2014-06-15 17:04:38 +02:00
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
auto it = highlighters.find(id);
|
|
|
|
if (it != highlighters.end())
|
2014-06-15 17:04:38 +02:00
|
|
|
{
|
|
|
|
const ByteCount offset = (int)(sep_it + 1 - path.begin());
|
|
|
|
cursor_pos -= offset;
|
|
|
|
if (auto* group = it->second.target<HighlighterGroup>())
|
2014-06-16 01:49:39 +02:00
|
|
|
return offset_pos((group->*hg_complete)({sep_it+1, path.end()}, cursor_pos), offset);
|
2014-06-15 17:04:38 +02:00
|
|
|
if (auto* hier_group = it->second.target<HierachicalHighlighter>())
|
2014-06-16 01:49:39 +02:00
|
|
|
return offset_pos((hier_group->*hh_complete)({sep_it+1, path.end()}, cursor_pos), offset);
|
2014-06-15 17:04:38 +02:00
|
|
|
}
|
|
|
|
return {};
|
2014-06-10 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
Completions HighlighterGroup::complete_id(StringView path, ByteCount cursor_pos) const
|
2014-06-10 20:58:02 +02:00
|
|
|
{
|
2014-06-16 01:49:39 +02:00
|
|
|
return complete_impl<
|
|
|
|
&HighlighterGroup::complete_id,
|
|
|
|
&HierachicalHighlighter::complete_id
|
|
|
|
>(m_highlighters, [](const HighlighterAndId&) { return true; }, path, cursor_pos);
|
|
|
|
}
|
2014-06-15 17:04:38 +02:00
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
Completions HighlighterGroup::complete_group_id(StringView path, ByteCount cursor_pos) const
|
|
|
|
{
|
|
|
|
return complete_impl<
|
|
|
|
&HighlighterGroup::complete_group_id,
|
|
|
|
&HierachicalHighlighter::complete_group_id
|
|
|
|
>(m_highlighters, [](const HighlighterAndId& func) {
|
|
|
|
return func.second.target<HighlighterGroup>() or
|
|
|
|
func.second.target<HierachicalHighlighter>();
|
|
|
|
}, path, cursor_pos);
|
2014-06-10 20:58:02 +02:00
|
|
|
}
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
HighlighterGroup& HierachicalHighlighter::get_group(StringView path)
|
2014-06-10 22:35:03 +02:00
|
|
|
{
|
2014-06-15 17:04:38 +02:00
|
|
|
auto sep_it = find(path, path_separator);
|
2014-06-10 22:35:03 +02:00
|
|
|
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())
|
2014-06-15 17:04:38 +02:00
|
|
|
return it->second.get_group(StringView(sep_it+1, path.end()));
|
2014-06-10 22:35:03 +02:00
|
|
|
else
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2014-06-15 17:04:38 +02:00
|
|
|
HighlighterFunc HierachicalHighlighter::get_highlighter(StringView path) const
|
2014-06-12 22:52:23 +02:00
|
|
|
{
|
2014-06-15 17:04:38 +02:00
|
|
|
auto sep_it = find(path, path_separator);
|
2014-06-12 22:52:23 +02:00
|
|
|
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())
|
2014-06-15 17:04:38 +02:00
|
|
|
return it->second.get_highlighter(StringView(sep_it+1, path.end()));
|
2014-06-12 22:52:23 +02:00
|
|
|
else
|
|
|
|
return HighlighterFunc(std::ref(it->second));
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
template<Completions (HighlighterGroup::*complete)(StringView path, ByteCount cursor_pos) const>
|
|
|
|
Completions complete_impl(const HierachicalHighlighter::GroupMap& groups,
|
|
|
|
StringView path, ByteCount cursor_pos)
|
2014-06-15 17:04:38 +02:00
|
|
|
{
|
|
|
|
auto sep_it = find(path, path_separator);
|
|
|
|
StringView id(path.begin(), sep_it);
|
2014-06-16 01:49:39 +02:00
|
|
|
auto it = groups.find(id);
|
2014-06-15 17:04:38 +02:00
|
|
|
if (sep_it == path.end())
|
2014-06-16 01:49:39 +02:00
|
|
|
return { 0_byte, id.length(), groups.complete_id(id, cursor_pos) };
|
2014-06-15 17:04:38 +02:00
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
if (it != groups.end())
|
2014-06-15 17:04:38 +02:00
|
|
|
{
|
2014-06-16 01:49:39 +02:00
|
|
|
const ByteCount offset = (int)(sep_it + 1- path.begin());
|
2014-06-15 17:04:38 +02:00
|
|
|
return offset_pos(
|
2014-06-16 01:49:39 +02:00
|
|
|
(it->second.*complete)({sep_it+1, path.end()},
|
2014-06-15 17:04:38 +02:00
|
|
|
cursor_pos - offset), offset);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
Completions HierachicalHighlighter::complete_id(StringView path, ByteCount cursor_pos) const
|
2014-06-15 17:04:38 +02:00
|
|
|
{
|
2014-06-16 01:49:39 +02:00
|
|
|
return complete_impl<&HighlighterGroup::complete_id>(m_groups, path, cursor_pos);
|
|
|
|
}
|
2014-06-15 17:04:38 +02:00
|
|
|
|
2014-06-16 01:49:39 +02:00
|
|
|
Completions HierachicalHighlighter::complete_group_id(StringView path, ByteCount cursor_pos) const
|
|
|
|
{
|
|
|
|
return complete_impl<&HighlighterGroup::complete_group_id>(m_groups, path, cursor_pos);
|
2014-06-15 17:04:38 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 20:58:02 +02:00
|
|
|
}
|