1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-24 03:56:32 +03:00
kakoune/src/highlighter.hh
Maxime Coste def4221ac7 Pass a BufferRange to highlighters, fix subregions
Regions highlighter can now correctly be applied to only subrange
of the buffer, fixing some corner cases in recursive regions.
2014-12-02 19:56:17 +00:00

91 lines
2.6 KiB
C++

#ifndef highlighter_hh_INCLUDED
#define highlighter_hh_INCLUDED
#include "coord.hh"
#include "function_registry.hh"
#include "memoryview.hh"
#include "string.hh"
#include "utils.hh"
#include <functional>
namespace std
{
template<>
struct hash<Kakoune::ByteCoord>
{
size_t operator()(const Kakoune::ByteCoord& val) const
{
size_t seed = std::hash<int>()((int)val.line);
return seed ^ (std::hash<int>()((int)val.column) + 0x9e3779b9 +
(seed << 6) + (seed >> 2));
}
};
}
namespace Kakoune
{
class DisplayBuffer;
class Context;
enum class HighlightFlags
{
Highlight,
MoveOnly
};
// An Highlighter is a function which mutates a DisplayBuffer in order to
// change the visual representation of a file. It could be changing text
// color, adding information text (line numbering for example) or replacing
// buffer content (folding for example)
struct Highlighter;
using HighlighterAndId = std::pair<String, std::unique_ptr<Highlighter>>;
using BufferRange = std::pair<ByteCoord, ByteCoord>;
struct Highlighter
{
virtual ~Highlighter() {}
virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange range) = 0;
virtual bool has_children() const { return false; }
virtual Highlighter& get_child(StringView path) { throw runtime_error("this highlighter do not hold children"); }
virtual void add_child(HighlighterAndId&& hl) { throw runtime_error("this highlighter do not hold children"); }
virtual void remove_child(StringView id) { throw runtime_error("this highlighter do not hold children"); }
virtual Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const { throw runtime_error("this highlighter do not hold children"); }
};
template<typename Func>
struct SimpleHighlighter : public Highlighter
{
SimpleHighlighter(Func func) : m_func(std::move(func)) {}
virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange range) override
{
m_func(context, flags, display_buffer, range);
}
private:
Func m_func;
};
template<typename T>
std::unique_ptr<SimpleHighlighter<T>> make_simple_highlighter(T func)
{
return make_unique<SimpleHighlighter<T>>(std::move(func));
}
using HighlighterParameters = memoryview<String>;
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
struct HighlighterRegistry : FunctionRegistry<HighlighterFactory>,
Singleton<HighlighterRegistry>
{};
}
#endif // highlighter_hh_INCLUDED