home/src/highlighter_group.hh
Maxime Coste 318e77b25e Highlighters: Introduce unique highlighter support
Some highlighters, such as wrap or line numbers, are not intended
to be used multiple times on the same display. Add support for unique
ids that are used by highlighters to disable themselves if another
unique highlighter with the same id is supposed to override them.

The usual highlighter "precedence" takes, place, that it, that most
nested highlighter will the the one to run (window in priority to
buffer in priority to global).
2017-11-25 12:53:33 +08:00

69 lines
2.0 KiB
C++

#ifndef highlighter_group_hh_INCLUDED
#define highlighter_group_hh_INCLUDED
#include "exception.hh"
#include "hash_map.hh"
#include "highlighter.hh"
#include "utils.hh"
#include "safe_ptr.hh"
namespace Kakoune
{
struct child_not_found : public runtime_error
{
using runtime_error::runtime_error;
};
class HighlighterGroup : public Highlighter
{
public:
HighlighterGroup(HighlightPass passes) : Highlighter{passes} {}
bool has_children() const override { return true; }
void add_child(HighlighterAndId&& hl) override;
void remove_child(StringView id) override;
Highlighter& get_child(StringView path) override;
Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const override;
void fill_unique_ids(Vector<StringView>& unique_ids) const override;
protected:
void do_highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range) override;
void do_compute_display_setup(HighlightContext context, DisplaySetup& setup) override;
using HighlighterMap = HashMap<String, std::unique_ptr<Highlighter>, MemoryDomain::Highlight>;
HighlighterMap m_highlighters;
};
class Highlighters : public SafeCountable
{
public:
Highlighters(Highlighters& parent) : SafeCountable{}, m_parent{&parent}, m_group{HighlightPass::All} {}
HighlighterGroup& group() { return m_group; }
const HighlighterGroup& group() const { return m_group; }
void highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range);
void compute_display_setup(HighlightContext context, DisplaySetup& setup);
private:
friend class Scope;
Highlighters() : m_group{HighlightPass::All} {}
SafePtr<Highlighters> m_parent;
HighlighterGroup m_group;
};
struct DefinedHighlighters : public HighlighterGroup,
public Singleton<DefinedHighlighters>
{
DefinedHighlighters() : HighlighterGroup{HighlightPass::All} {}
};
}
#endif // highlighter_group_hh_INCLUDED