highlighter.hh (2933B)
1 #ifndef highlighter_hh_INCLUDED 2 #define highlighter_hh_INCLUDED 3 4 #include "coord.hh" 5 #include "completion.hh" 6 #include "range.hh" 7 #include "hash_map.hh" 8 #include "array_view.hh" 9 #include "string.hh" 10 #include "utils.hh" 11 #include "parameters_parser.hh" 12 #include "unique_ptr.hh" 13 14 namespace Kakoune 15 { 16 17 class Context; 18 class DisplayBuffer; 19 20 using BufferRange = Range<BufferCoord>; 21 22 enum class HighlightPass 23 { 24 Replace = 1 << 0, 25 Wrap = 1 << 1, 26 Move = 1 << 2, 27 Colorize = 1 << 3, 28 29 All = Replace | Wrap | Move | Colorize, 30 }; 31 constexpr bool with_bit_ops(Meta::Type<HighlightPass>) { return true; } 32 33 // A Highlighter is a function which mutates a DisplayBuffer in order to 34 // change the visual representation of a file. It could be changing text 35 // color, adding information text (line numbering for example) or replacing 36 // buffer content (folding for example) 37 38 struct Highlighter; 39 40 struct DisplaySetup 41 { 42 LineCount first_line{}; 43 LineCount line_count{}; 44 ColumnCount first_column{}; 45 ColumnCount widget_columns{}; 46 // Offset of line and columns that must remain visible around cursor 47 DisplayCoord scroll_offset{}; 48 }; 49 50 using HighlighterIdList = ConstArrayView<StringView>; 51 52 struct HighlightContext 53 { 54 const Context& context; 55 const DisplaySetup& setup; 56 HighlightPass pass; 57 HighlighterIdList disabled_ids; 58 }; 59 60 struct Highlighter 61 { 62 Highlighter(HighlightPass passes) : m_passes{passes} {} 63 virtual ~Highlighter() = default; 64 65 void highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range); 66 void compute_display_setup(HighlightContext context, DisplaySetup& setup) const; 67 68 virtual bool has_children() const; 69 virtual Highlighter& get_child(StringView path); 70 virtual void add_child(String name, UniquePtr<Highlighter>&& hl, bool override = false); 71 virtual void remove_child(StringView id); 72 virtual Completions complete_child(StringView path, ByteCount cursor_pos, bool group) const; 73 virtual void fill_unique_ids(Vector<StringView>& unique_ids) const; 74 75 HighlightPass passes() const { return m_passes; } 76 77 private: 78 virtual void do_highlight(HighlightContext context, DisplayBuffer& display_buffer, BufferRange range) = 0; 79 virtual void do_compute_display_setup(HighlightContext context, DisplaySetup& setup) const {} 80 81 const HighlightPass m_passes; 82 }; 83 84 using HighlighterParameters = ConstArrayView<String>; 85 using HighlighterFactory = UniquePtr<Highlighter> (*)(HighlighterParameters params, Highlighter* parent); 86 87 struct HighlighterDesc 88 { 89 const char* docstring; 90 ParameterDesc params; 91 }; 92 93 struct HighlighterFactoryAndDescription 94 { 95 HighlighterFactory factory; 96 const HighlighterDesc* description; 97 }; 98 99 struct HighlighterRegistry : HashMap<String, HighlighterFactoryAndDescription, MemoryDomain::Highlight>, 100 Singleton<HighlighterRegistry> 101 {}; 102 103 } 104 105 #endif // highlighter_hh_INCLUDED