2011-11-29 23:37:20 +01:00
|
|
|
#ifndef highlighter_hh_INCLUDED
|
|
|
|
#define highlighter_hh_INCLUDED
|
|
|
|
|
2014-12-02 20:56:17 +01:00
|
|
|
#include "coord.hh"
|
2014-12-23 23:15:53 +01:00
|
|
|
#include "completion.hh"
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "id_map.hh"
|
2015-01-06 14:40:56 +01:00
|
|
|
#include "array_view.hh"
|
2012-11-23 13:40:20 +01:00
|
|
|
#include "string.hh"
|
|
|
|
#include "utils.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
|
|
|
|
#include <functional>
|
2011-11-29 23:37:20 +01:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
class DisplayBuffer;
|
2013-12-17 00:24:08 +01:00
|
|
|
class Context;
|
2011-11-29 23:37:20 +01:00
|
|
|
|
2014-01-18 02:56:51 +01:00
|
|
|
enum class HighlightFlags
|
|
|
|
{
|
|
|
|
Highlight,
|
|
|
|
MoveOnly
|
|
|
|
};
|
|
|
|
|
2012-01-11 15:21:58 +01:00
|
|
|
// 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)
|
|
|
|
|
2014-10-22 01:20:09 +02:00
|
|
|
struct Highlighter;
|
|
|
|
|
|
|
|
using HighlighterAndId = std::pair<String, std::unique_ptr<Highlighter>>;
|
|
|
|
|
2014-12-02 20:56:17 +01:00
|
|
|
using BufferRange = std::pair<ByteCoord, ByteCoord>;
|
|
|
|
|
2014-10-22 01:20:09 +02:00
|
|
|
struct Highlighter
|
|
|
|
{
|
2014-10-25 11:40:26 +02:00
|
|
|
virtual ~Highlighter() {}
|
2014-12-02 20:56:17 +01:00
|
|
|
virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange range) = 0;
|
2014-10-22 01:20:09 +02:00
|
|
|
|
|
|
|
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)) {}
|
2014-12-02 20:56:17 +01:00
|
|
|
virtual void highlight(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer, BufferRange range) override
|
2014-10-22 01:20:09 +02:00
|
|
|
{
|
2014-12-02 20:56:17 +01:00
|
|
|
m_func(context, flags, display_buffer, range);
|
2014-10-22 01:20:09 +02:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-01-06 14:40:56 +01:00
|
|
|
using HighlighterParameters = ArrayView<String>;
|
2013-11-19 00:19:48 +01:00
|
|
|
using HighlighterFactory = std::function<HighlighterAndId (HighlighterParameters params)>;
|
2013-03-27 13:41:41 +01:00
|
|
|
|
2015-02-19 14:54:03 +01:00
|
|
|
struct HighlighterFactoryAndDocstring
|
|
|
|
{
|
|
|
|
HighlighterFactory factory;
|
|
|
|
String docstring;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct HighlighterRegistry : IdMap<HighlighterFactoryAndDocstring>,
|
2012-11-23 13:40:20 +01:00
|
|
|
Singleton<HighlighterRegistry>
|
|
|
|
{};
|
2011-11-29 23:37:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // highlighter_hh_INCLUDED
|