kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

highlighter_group.cc (3627B)


      1 #include "highlighter_group.hh"
      2 
      3 #include "flags.hh"
      4 #include "format.hh"
      5 #include "ranges.hh"
      6 
      7 
      8 namespace Kakoune
      9 {
     10 
     11 void HighlighterGroup::do_highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range)
     12 {
     13     for (auto& hl : m_highlighters)
     14         hl.value->highlight(context, display_buffer, range);
     15 }
     16 
     17 void HighlighterGroup::do_compute_display_setup(HighlightContext context, DisplaySetup& setup) const
     18 {
     19     for (auto& hl : m_highlighters)
     20         hl.value->compute_display_setup(context, setup);
     21 }
     22 
     23 void HighlighterGroup::fill_unique_ids(Vector<StringView>& unique_ids) const
     24 {
     25     for (auto& hl : m_highlighters)
     26         hl.value->fill_unique_ids(unique_ids);
     27 }
     28 
     29 void HighlighterGroup::add_child(String name, UniquePtr<Highlighter>&& hl, bool override)
     30 {
     31     if ((hl->passes() & passes()) != hl->passes())
     32         throw runtime_error{"cannot add that highlighter to this group, passes don't match"};
     33 
     34     auto it = m_highlighters.find(name);
     35     if (it != m_highlighters.end())
     36     {
     37         if (not override)
     38             throw runtime_error(format("duplicate id: '{}'", name));
     39         it->value = std::move(hl);
     40         return;
     41     }
     42 
     43     m_highlighters.insert({std::move(name), std::move(hl)});
     44 }
     45 
     46 void HighlighterGroup::remove_child(StringView id)
     47 {
     48     auto it = m_highlighters.find(id);
     49     if (it == m_highlighters.end())
     50         throw child_not_found(format("no such id: '{}'", id));
     51     m_highlighters.remove(it);
     52 }
     53 
     54 Highlighter& HighlighterGroup::get_child(StringView path)
     55 {
     56     auto sep_it = find(path, '/');
     57     StringView id(path.begin(), sep_it);
     58     auto it = m_highlighters.find(id);
     59     if (it == m_highlighters.end())
     60         throw child_not_found(format("no such id: '{}'", id));
     61     if (sep_it == path.end())
     62         return *it->value;
     63     else
     64         return it->value->get_child({sep_it+1, path.end()});
     65 }
     66 
     67 Completions HighlighterGroup::complete_child(StringView path, ByteCount cursor_pos, bool group) const
     68 {
     69     auto sep_it = find(path, '/');
     70     if (sep_it != path.end())
     71     {
     72         ByteCount offset = sep_it+1 - path.begin();
     73         Highlighter& hl = const_cast<HighlighterGroup*>(this)->get_child({path.begin(), sep_it});
     74         return offset_pos(hl.complete_child(path.substr(offset), cursor_pos - offset, group), offset);
     75     }
     76 
     77     auto candidates = complete(
     78         path, cursor_pos,
     79         m_highlighters | filter([=](auto& hl) { return not group or hl.value->has_children(); })
     80                        | transform([](auto& hl) { return hl.value->has_children() ? hl.key + "/" : hl.key; })
     81                        | gather<Vector<String>>());
     82 
     83     auto completions_flags = group ? Completions::Flags::None : Completions::Flags::Menu;
     84     return { 0, 0, std::move(candidates), completions_flags };
     85 }
     86 
     87 void Highlighters::highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range)
     88 {
     89     Vector<StringView> disabled_ids(context.disabled_ids.begin(), context.disabled_ids.end());
     90     m_group.fill_unique_ids(disabled_ids);
     91 
     92     if (m_parent)
     93         m_parent->highlight({context.context, context.setup, context.pass, disabled_ids}, display_buffer, range);
     94     m_group.highlight(context, display_buffer, range);
     95 }
     96 
     97 void Highlighters::compute_display_setup(HighlightContext context, DisplaySetup& setup) const
     98 {
     99     Vector<StringView> disabled_ids(context.disabled_ids.begin(), context.disabled_ids.end());
    100     m_group.fill_unique_ids(disabled_ids);
    101 
    102     if (m_parent)
    103         m_parent->compute_display_setup({context.context, context.setup, context.pass, disabled_ids}, setup);
    104     m_group.compute_display_setup(context, setup);
    105 }
    106 
    107 }