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"
|
2015-04-23 22:38:45 +02:00
|
|
|
#include "display_buffer.hh"
|
2014-12-23 23:15:53 +01:00
|
|
|
#include "exception.hh"
|
2017-05-03 20:41:37 +02:00
|
|
|
#include "flags.hh"
|
2017-03-07 02:12:37 +01:00
|
|
|
#include "hash_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
|
|
|
|
{
|
|
|
|
|
2013-12-17 00:24:08 +01:00
|
|
|
class Context;
|
2011-11-29 23:37:20 +01:00
|
|
|
|
2017-04-27 20:27:28 +02:00
|
|
|
enum class HighlightPass
|
2014-01-18 02:56:51 +01:00
|
|
|
{
|
2017-05-03 20:41:37 +02:00
|
|
|
Wrap = 1 << 0,
|
|
|
|
Move = 1 << 1,
|
|
|
|
Colorize = 1 << 2,
|
|
|
|
|
|
|
|
All = Wrap | Move | Colorize,
|
2014-01-18 02:56:51 +01:00
|
|
|
};
|
2017-05-03 20:41:37 +02:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<HighlightPass>) { return true; }
|
2014-01-18 02:56:51 +01:00
|
|
|
|
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>>;
|
|
|
|
|
2017-04-27 20:27:28 +02:00
|
|
|
struct DisplaySetup
|
|
|
|
{
|
|
|
|
// Window position relative to the buffer origin
|
|
|
|
DisplayCoord window_pos;
|
|
|
|
// Range of lines and columns from the buffer that will get displayed
|
|
|
|
DisplayCoord window_range;
|
|
|
|
// Position of the cursor in the window
|
|
|
|
DisplayCoord cursor_pos;
|
2017-05-07 14:43:43 +02:00
|
|
|
// Offset of line and columns that must remain visible around cursor
|
2017-05-11 10:23:20 +02:00
|
|
|
DisplayCoord scroll_offset;
|
2017-06-09 17:00:22 +02:00
|
|
|
// Put full lines in the initial display buffer
|
|
|
|
bool full_lines;
|
2017-04-27 20:27:28 +02:00
|
|
|
};
|
|
|
|
|
2017-11-25 05:53:33 +01:00
|
|
|
using HighlighterIdList = ConstArrayView<StringView>;
|
|
|
|
|
|
|
|
struct HighlightContext
|
|
|
|
{
|
|
|
|
const Context& context;
|
|
|
|
HighlightPass pass;
|
|
|
|
HighlighterIdList disabled_ids;
|
|
|
|
};
|
|
|
|
|
2014-10-22 01:20:09 +02:00
|
|
|
struct Highlighter
|
|
|
|
{
|
2017-05-03 20:41:37 +02:00
|
|
|
Highlighter(HighlightPass passes) : m_passes{passes} {}
|
2017-01-08 23:30:15 +01:00
|
|
|
virtual ~Highlighter() = default;
|
2017-04-27 20:27:28 +02:00
|
|
|
|
2017-11-25 05:53:33 +01:00
|
|
|
void highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range)
|
2017-05-03 20:41:37 +02:00
|
|
|
{
|
2017-11-25 05:53:33 +01:00
|
|
|
if (context.pass & m_passes)
|
|
|
|
do_highlight(context, display_buffer, range);
|
2017-05-03 20:41:37 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 05:53:33 +01:00
|
|
|
void compute_display_setup(HighlightContext context, DisplaySetup& setup)
|
2017-05-03 20:41:37 +02:00
|
|
|
{
|
2017-11-25 05:53:33 +01:00
|
|
|
if (context.pass & m_passes)
|
|
|
|
do_compute_display_setup(context, setup);
|
2017-05-03 20:41:37 +02:00
|
|
|
}
|
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"); }
|
2017-05-03 20:41:37 +02:00
|
|
|
|
2017-11-25 05:53:33 +01:00
|
|
|
virtual void fill_unique_ids(Vector<StringView>& unique_ids) const {}
|
|
|
|
|
2017-05-03 20:41:37 +02:00
|
|
|
HighlightPass passes() const { return m_passes; }
|
|
|
|
|
|
|
|
private:
|
2017-11-25 05:53:33 +01:00
|
|
|
virtual void do_highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range) = 0;
|
|
|
|
virtual void do_compute_display_setup(HighlightContext context, DisplaySetup& setup) {}
|
2017-05-03 20:41:37 +02:00
|
|
|
|
|
|
|
const HighlightPass m_passes;
|
2014-10-22 01:20:09 +02:00
|
|
|
};
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
using HighlighterParameters = ConstArrayView<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;
|
|
|
|
};
|
|
|
|
|
2017-03-07 02:12:37 +01:00
|
|
|
struct HighlighterRegistry : HashMap<String, HighlighterFactoryAndDocstring, MemoryDomain::Highlight>,
|
2012-11-23 13:40:20 +01:00
|
|
|
Singleton<HighlighterRegistry>
|
|
|
|
{};
|
2011-11-29 23:37:20 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // highlighter_hh_INCLUDED
|