home/src/highlighter_group.cc

88 lines
2.8 KiB
C++
Raw Normal View History

#include "highlighter_group.hh"
#include "ranges.hh"
#include "string_utils.hh"
namespace Kakoune
{
void HighlighterGroup::do_highlight(const Context& context, HighlightPass pass,
DisplayBuffer& display_buffer, BufferRange range)
{
for (auto& hl : m_highlighters)
hl.value->highlight(context, pass, display_buffer, range);
}
void HighlighterGroup::do_compute_display_setup(const Context& context, HighlightPass pass, DisplaySetup& setup)
{
for (auto& hl : m_highlighters)
hl.value->compute_display_setup(context, pass, setup);
}
void HighlighterGroup::add_child(HighlighterAndId&& hl)
{
if ((hl.second->passes() & passes()) != hl.second->passes())
throw runtime_error{"Cannot add that highlighter to this group, passes dont match"};
hl.first = replace(hl.first, "/", "<slash>");
if (m_highlighters.contains(hl.first))
throw runtime_error(format("duplicate id: '{}'", hl.first));
2017-03-07 02:12:37 +01:00
m_highlighters.insert({std::move(hl.first), std::move(hl.second)});
}
void HighlighterGroup::remove_child(StringView id)
{
m_highlighters.remove(id);
}
Highlighter& HighlighterGroup::get_child(StringView path)
2014-06-12 22:52:23 +02:00
{
auto sep_it = find(path, '/');
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 child_not_found(format("no such id: '{}'", id));
if (sep_it == path.end())
2015-09-16 20:57:57 +02:00
return *it->value;
else
2015-09-16 20:57:57 +02:00
return it->value->get_child({sep_it+1, path.end()});
}
Completions HighlighterGroup::complete_child(StringView path, ByteCount cursor_pos, bool group) const
2014-06-12 22:52:23 +02:00
{
auto sep_it = find(path, '/');
2014-06-12 22:52:23 +02:00
if (sep_it != path.end())
{
ByteCount offset = sep_it+1 - path.begin();
Highlighter& hl = const_cast<HighlighterGroup*>(this)->get_child({path.begin(), sep_it});
return offset_pos(hl.complete_child(path.substr(offset), cursor_pos - offset, group), offset);
}
2015-11-11 20:14:35 +01:00
auto candidates = complete(
path, cursor_pos,
2017-03-07 02:12:37 +01:00
m_highlighters | filter([=](const HighlighterMap::Item& hl)
{ return not group or hl.value->has_children(); })
2017-03-07 02:12:37 +01:00
| transform(std::mem_fn(&HighlighterMap::Item::key)));
2015-11-11 20:14:35 +01:00
return { 0, 0, std::move(candidates) };
}
void Highlighters::do_highlight(const Context& context, HighlightPass pass,
DisplayBuffer& display_buffer, BufferRange range)
{
if (m_parent)
m_parent->do_highlight(context, pass, display_buffer, range);
HighlighterGroup::do_highlight(context, pass, display_buffer, range);
}
void Highlighters::do_compute_display_setup(const Context& context, HighlightPass pass, DisplaySetup& setup)
{
if (m_parent)
m_parent->do_compute_display_setup(context, pass, setup);
HighlighterGroup::do_compute_display_setup(context, pass, setup);
}
}