1 | /**************************************************************************/ |
2 | /* syntax_highlighter.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef SYNTAX_HIGHLIGHTER_H |
32 | #define SYNTAX_HIGHLIGHTER_H |
33 | |
34 | #include "core/io/resource.h" |
35 | #include "core/object/gdvirtual.gen.inc" |
36 | |
37 | class TextEdit; |
38 | |
39 | class SyntaxHighlighter : public Resource { |
40 | GDCLASS(SyntaxHighlighter, Resource) |
41 | |
42 | private: |
43 | RBMap<int, Dictionary> highlighting_cache; |
44 | void _lines_edited_from(int p_from_line, int p_to_line); |
45 | |
46 | protected: |
47 | ObjectID text_edit_instance_id; // For validity check |
48 | TextEdit *text_edit = nullptr; |
49 | |
50 | static void _bind_methods(); |
51 | |
52 | GDVIRTUAL1RC(Dictionary, _get_line_syntax_highlighting, int) |
53 | GDVIRTUAL0(_clear_highlighting_cache) |
54 | GDVIRTUAL0(_update_cache) |
55 | public: |
56 | Dictionary get_line_syntax_highlighting(int p_line); |
57 | virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) { return Dictionary(); } |
58 | |
59 | void clear_highlighting_cache(); |
60 | virtual void _clear_highlighting_cache() {} |
61 | |
62 | void update_cache(); |
63 | virtual void _update_cache() {} |
64 | |
65 | void set_text_edit(TextEdit *p_text_edit); |
66 | TextEdit *get_text_edit() const; |
67 | |
68 | SyntaxHighlighter() {} |
69 | virtual ~SyntaxHighlighter() {} |
70 | }; |
71 | |
72 | /////////////////////////////////////////////////////////////////////////////// |
73 | |
74 | class CodeHighlighter : public SyntaxHighlighter { |
75 | GDCLASS(CodeHighlighter, SyntaxHighlighter) |
76 | |
77 | private: |
78 | struct ColorRegion { |
79 | Color color; |
80 | String start_key; |
81 | String end_key; |
82 | bool line_only = false; |
83 | }; |
84 | Vector<ColorRegion> color_regions; |
85 | HashMap<int, int> color_region_cache; |
86 | |
87 | Dictionary keywords; |
88 | Dictionary member_keywords; |
89 | |
90 | Color font_color; |
91 | Color member_color; |
92 | Color function_color; |
93 | Color symbol_color; |
94 | Color number_color; |
95 | |
96 | protected: |
97 | static void _bind_methods(); |
98 | |
99 | public: |
100 | virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override; |
101 | |
102 | virtual void _clear_highlighting_cache() override; |
103 | virtual void _update_cache() override; |
104 | |
105 | void add_keyword_color(const String &p_keyword, const Color &p_color); |
106 | void remove_keyword_color(const String &p_keyword); |
107 | bool has_keyword_color(const String &p_keyword) const; |
108 | Color get_keyword_color(const String &p_keyword) const; |
109 | |
110 | void set_keyword_colors(const Dictionary p_keywords); |
111 | void clear_keyword_colors(); |
112 | Dictionary get_keyword_colors() const; |
113 | |
114 | void add_member_keyword_color(const String &p_member_keyword, const Color &p_color); |
115 | void remove_member_keyword_color(const String &p_member_keyword); |
116 | bool has_member_keyword_color(const String &p_member_keyword) const; |
117 | Color get_member_keyword_color(const String &p_member_keyword) const; |
118 | |
119 | void set_member_keyword_colors(const Dictionary &p_color_regions); |
120 | void clear_member_keyword_colors(); |
121 | Dictionary get_member_keyword_colors() const; |
122 | |
123 | void add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only = false); |
124 | void remove_color_region(const String &p_start_key); |
125 | bool has_color_region(const String &p_start_key) const; |
126 | |
127 | void set_color_regions(const Dictionary &p_member_keyword); |
128 | void clear_color_regions(); |
129 | Dictionary get_color_regions() const; |
130 | |
131 | void set_number_color(Color p_color); |
132 | Color get_number_color() const; |
133 | |
134 | void set_symbol_color(Color p_color); |
135 | Color get_symbol_color() const; |
136 | |
137 | void set_function_color(Color p_color); |
138 | Color get_function_color() const; |
139 | |
140 | void set_member_variable_color(Color p_color); |
141 | Color get_member_variable_color() const; |
142 | }; |
143 | |
144 | #endif // SYNTAX_HIGHLIGHTER_H |
145 | |