| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  syntax_highlighter.cpp                                                */ | 
|---|
| 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 | #include "syntax_highlighter.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/object/script_language.h" | 
|---|
| 34 | #include "scene/gui/text_edit.h" | 
|---|
| 35 |  | 
|---|
| 36 | Dictionary SyntaxHighlighter::get_line_syntax_highlighting(int p_line) { | 
|---|
| 37 | if (highlighting_cache.has(p_line)) { | 
|---|
| 38 | return highlighting_cache[p_line]; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | Dictionary color_map; | 
|---|
| 42 | if (text_edit == nullptr) { | 
|---|
| 43 | return color_map; | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 | if (!GDVIRTUAL_CALL(_get_line_syntax_highlighting, p_line, color_map)) { | 
|---|
| 47 | color_map = _get_line_syntax_highlighting_impl(p_line); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | highlighting_cache[p_line] = color_map; | 
|---|
| 51 | return color_map; | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | void SyntaxHighlighter::_lines_edited_from(int p_from_line, int p_to_line) { | 
|---|
| 55 | if (highlighting_cache.size() < 1) { | 
|---|
| 56 | return; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | int cache_size = highlighting_cache.back()->key(); | 
|---|
| 60 | for (int i = MIN(p_from_line, p_to_line) - 1; i <= cache_size; i++) { | 
|---|
| 61 | if (highlighting_cache.has(i)) { | 
|---|
| 62 | highlighting_cache.erase(i); | 
|---|
| 63 | } | 
|---|
| 64 | } | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void SyntaxHighlighter::clear_highlighting_cache() { | 
|---|
| 68 | highlighting_cache.clear(); | 
|---|
| 69 |  | 
|---|
| 70 | if (GDVIRTUAL_CALL(_clear_highlighting_cache)) { | 
|---|
| 71 | return; | 
|---|
| 72 | } | 
|---|
| 73 | _clear_highlighting_cache(); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void SyntaxHighlighter::update_cache() { | 
|---|
| 77 | clear_highlighting_cache(); | 
|---|
| 78 |  | 
|---|
| 79 | if (text_edit == nullptr) { | 
|---|
| 80 | return; | 
|---|
| 81 | } | 
|---|
| 82 | if (GDVIRTUAL_CALL(_update_cache)) { | 
|---|
| 83 | return; | 
|---|
| 84 | } | 
|---|
| 85 | _update_cache(); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void SyntaxHighlighter::set_text_edit(TextEdit *p_text_edit) { | 
|---|
| 89 | if (text_edit && ObjectDB::get_instance(text_edit_instance_id)) { | 
|---|
| 90 | text_edit->disconnect( "lines_edited_from", callable_mp(this, &SyntaxHighlighter::_lines_edited_from)); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | text_edit = p_text_edit; | 
|---|
| 94 | if (p_text_edit == nullptr) { | 
|---|
| 95 | return; | 
|---|
| 96 | } | 
|---|
| 97 | text_edit_instance_id = text_edit->get_instance_id(); | 
|---|
| 98 | text_edit->connect( "lines_edited_from", callable_mp(this, &SyntaxHighlighter::_lines_edited_from)); | 
|---|
| 99 | update_cache(); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | TextEdit *SyntaxHighlighter::get_text_edit() const { | 
|---|
| 103 | return text_edit; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void SyntaxHighlighter::_bind_methods() { | 
|---|
| 107 | ClassDB::bind_method(D_METHOD( "get_line_syntax_highlighting", "line"), &SyntaxHighlighter::get_line_syntax_highlighting); | 
|---|
| 108 | ClassDB::bind_method(D_METHOD( "update_cache"), &SyntaxHighlighter::update_cache); | 
|---|
| 109 | ClassDB::bind_method(D_METHOD( "clear_highlighting_cache"), &SyntaxHighlighter::clear_highlighting_cache); | 
|---|
| 110 | ClassDB::bind_method(D_METHOD( "get_text_edit"), &SyntaxHighlighter::get_text_edit); | 
|---|
| 111 |  | 
|---|
| 112 | GDVIRTUAL_BIND(_get_line_syntax_highlighting, "line") | 
|---|
| 113 | GDVIRTUAL_BIND(_clear_highlighting_cache) | 
|---|
| 114 | GDVIRTUAL_BIND(_update_cache) | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 118 |  | 
|---|
| 119 | Dictionary CodeHighlighter::_get_line_syntax_highlighting_impl(int p_line) { | 
|---|
| 120 | Dictionary color_map; | 
|---|
| 121 |  | 
|---|
| 122 | bool prev_is_char = false; | 
|---|
| 123 | bool prev_is_number = false; | 
|---|
| 124 | bool in_keyword = false; | 
|---|
| 125 | bool in_word = false; | 
|---|
| 126 | bool in_function_name = false; | 
|---|
| 127 | bool in_member_variable = false; | 
|---|
| 128 | bool is_hex_notation = false; | 
|---|
| 129 | Color keyword_color; | 
|---|
| 130 | Color color; | 
|---|
| 131 |  | 
|---|
| 132 | color_region_cache[p_line] = -1; | 
|---|
| 133 | int in_region = -1; | 
|---|
| 134 | if (p_line != 0) { | 
|---|
| 135 | int prev_region_line = p_line - 1; | 
|---|
| 136 | while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) { | 
|---|
| 137 | prev_region_line--; | 
|---|
| 138 | } | 
|---|
| 139 | for (int i = prev_region_line; i < p_line - 1; i++) { | 
|---|
| 140 | get_line_syntax_highlighting(i); | 
|---|
| 141 | } | 
|---|
| 142 | if (!color_region_cache.has(p_line - 1)) { | 
|---|
| 143 | get_line_syntax_highlighting(p_line - 1); | 
|---|
| 144 | } | 
|---|
| 145 | in_region = color_region_cache[p_line - 1]; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | const String &str = text_edit->get_line(p_line); | 
|---|
| 149 | const int line_length = str.length(); | 
|---|
| 150 | Color prev_color; | 
|---|
| 151 |  | 
|---|
| 152 | if (in_region != -1 && str.length() == 0) { | 
|---|
| 153 | color_region_cache[p_line] = in_region; | 
|---|
| 154 | } | 
|---|
| 155 | for (int j = 0; j < line_length; j++) { | 
|---|
| 156 | Dictionary highlighter_info; | 
|---|
| 157 |  | 
|---|
| 158 | color = font_color; | 
|---|
| 159 | bool is_char = !is_symbol(str[j]); | 
|---|
| 160 | bool is_a_symbol = is_symbol(str[j]); | 
|---|
| 161 | bool is_number = is_digit(str[j]); | 
|---|
| 162 |  | 
|---|
| 163 | /* color regions */ | 
|---|
| 164 | if (is_a_symbol || in_region != -1) { | 
|---|
| 165 | int from = j; | 
|---|
| 166 |  | 
|---|
| 167 | if (in_region == -1) { | 
|---|
| 168 | for (; from < line_length; from++) { | 
|---|
| 169 | if (str[from] == '\\') { | 
|---|
| 170 | from++; | 
|---|
| 171 | continue; | 
|---|
| 172 | } | 
|---|
| 173 | break; | 
|---|
| 174 | } | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | if (from != line_length) { | 
|---|
| 178 | /* check if we are in entering a region */ | 
|---|
| 179 | if (in_region == -1) { | 
|---|
| 180 | for (int c = 0; c < color_regions.size(); c++) { | 
|---|
| 181 | /* check there is enough room */ | 
|---|
| 182 | int chars_left = line_length - from; | 
|---|
| 183 | int start_key_length = color_regions[c].start_key.length(); | 
|---|
| 184 | int end_key_length = color_regions[c].end_key.length(); | 
|---|
| 185 | if (chars_left < start_key_length) { | 
|---|
| 186 | continue; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | /* search the line */ | 
|---|
| 190 | bool match = true; | 
|---|
| 191 | const char32_t *start_key = color_regions[c].start_key.get_data(); | 
|---|
| 192 | for (int k = 0; k < start_key_length; k++) { | 
|---|
| 193 | if (start_key[k] != str[from + k]) { | 
|---|
| 194 | match = false; | 
|---|
| 195 | break; | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 | if (!match) { | 
|---|
| 199 | continue; | 
|---|
| 200 | } | 
|---|
| 201 | in_region = c; | 
|---|
| 202 | from += start_key_length; | 
|---|
| 203 |  | 
|---|
| 204 | /* check if it's the whole line */ | 
|---|
| 205 | if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) { | 
|---|
| 206 | if (from + end_key_length > line_length && (color_regions[in_region].start_key == "\""|| color_regions[in_region].start_key == "\'")) { | 
|---|
| 207 | // If it's key length and there is a '\', dont skip to highlight esc chars. | 
|---|
| 208 | if (str.find( "\\", from) >= 0) { | 
|---|
| 209 | break; | 
|---|
| 210 | } | 
|---|
| 211 | } | 
|---|
| 212 | prev_color = color_regions[in_region].color; | 
|---|
| 213 | highlighter_info[ "color"] = color_regions[c].color; | 
|---|
| 214 | color_map[j] = highlighter_info; | 
|---|
| 215 |  | 
|---|
| 216 | j = line_length; | 
|---|
| 217 | if (!color_regions[c].line_only) { | 
|---|
| 218 | color_region_cache[p_line] = c; | 
|---|
| 219 | } | 
|---|
| 220 | } | 
|---|
| 221 | break; | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | if (j == line_length) { | 
|---|
| 225 | continue; | 
|---|
| 226 | } | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | /* if we are in one find the end key */ | 
|---|
| 230 | if (in_region != -1) { | 
|---|
| 231 | bool is_string = (color_regions[in_region].start_key == "\""|| color_regions[in_region].start_key == "\'"); | 
|---|
| 232 |  | 
|---|
| 233 | Color region_color = color_regions[in_region].color; | 
|---|
| 234 | prev_color = region_color; | 
|---|
| 235 | highlighter_info[ "color"] = region_color; | 
|---|
| 236 | color_map[j] = highlighter_info; | 
|---|
| 237 |  | 
|---|
| 238 | /* search the line */ | 
|---|
| 239 | int region_end_index = -1; | 
|---|
| 240 | int end_key_length = color_regions[in_region].end_key.length(); | 
|---|
| 241 | const char32_t *end_key = color_regions[in_region].end_key.get_data(); | 
|---|
| 242 | for (; from < line_length; from++) { | 
|---|
| 243 | if (line_length - from < end_key_length) { | 
|---|
| 244 | // Don't break if '\' to highlight esc chars. | 
|---|
| 245 | if (!is_string || str.find( "\\", from) < 0) { | 
|---|
| 246 | break; | 
|---|
| 247 | } | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | if (!is_symbol(str[from])) { | 
|---|
| 251 | continue; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | if (str[from] == '\\') { | 
|---|
| 255 | if (is_string) { | 
|---|
| 256 | Dictionary escape_char_highlighter_info; | 
|---|
| 257 | escape_char_highlighter_info[ "color"] = symbol_color; | 
|---|
| 258 | color_map[from] = escape_char_highlighter_info; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | from++; | 
|---|
| 262 |  | 
|---|
| 263 | if (is_string) { | 
|---|
| 264 | Dictionary region_continue_highlighter_info; | 
|---|
| 265 | prev_color = region_color; | 
|---|
| 266 | region_continue_highlighter_info[ "color"] = region_color; | 
|---|
| 267 | color_map[from + 1] = region_continue_highlighter_info; | 
|---|
| 268 | } | 
|---|
| 269 | continue; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | region_end_index = from; | 
|---|
| 273 | for (int k = 0; k < end_key_length; k++) { | 
|---|
| 274 | if (end_key[k] != str[from + k]) { | 
|---|
| 275 | region_end_index = -1; | 
|---|
| 276 | break; | 
|---|
| 277 | } | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | if (region_end_index != -1) { | 
|---|
| 281 | break; | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | j = from + (end_key_length - 1); | 
|---|
| 286 | if (region_end_index == -1) { | 
|---|
| 287 | color_region_cache[p_line] = in_region; | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | in_region = -1; | 
|---|
| 291 | prev_is_char = false; | 
|---|
| 292 | prev_is_number = false; | 
|---|
| 293 | continue; | 
|---|
| 294 | } | 
|---|
| 295 | } | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | // Allow ABCDEF in hex notation. | 
|---|
| 299 | if (is_hex_notation && (is_hex_digit(str[j]) || is_number)) { | 
|---|
| 300 | is_number = true; | 
|---|
| 301 | } else { | 
|---|
| 302 | is_hex_notation = false; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | // Check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation. | 
|---|
| 306 | if ((str[j] == '.' || str[j] == 'x' || str[j] == '_' || str[j] == 'f' || str[j] == 'e') && !in_word && prev_is_number && !is_number) { | 
|---|
| 307 | is_number = true; | 
|---|
| 308 | is_a_symbol = false; | 
|---|
| 309 | is_char = false; | 
|---|
| 310 |  | 
|---|
| 311 | if (str[j] == 'x' && str[j - 1] == '0') { | 
|---|
| 312 | is_hex_notation = true; | 
|---|
| 313 | } | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | if (!in_word && (is_ascii_char(str[j]) || is_underscore(str[j])) && !is_number) { | 
|---|
| 317 | in_word = true; | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | if ((in_keyword || in_word) && !is_hex_notation) { | 
|---|
| 321 | is_number = false; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | if (is_a_symbol && str[j] != '.' && in_word) { | 
|---|
| 325 | in_word = false; | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | if (!is_char) { | 
|---|
| 329 | in_keyword = false; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | if (!in_keyword && is_char && !prev_is_char) { | 
|---|
| 333 | int to = j; | 
|---|
| 334 | while (to < line_length && !is_symbol(str[to])) { | 
|---|
| 335 | to++; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | String word = str.substr(j, to - j); | 
|---|
| 339 | Color col; | 
|---|
| 340 | if (keywords.has(word)) { | 
|---|
| 341 | col = keywords[word]; | 
|---|
| 342 | } else if (member_keywords.has(word)) { | 
|---|
| 343 | col = member_keywords[word]; | 
|---|
| 344 | for (int k = j - 1; k >= 0; k--) { | 
|---|
| 345 | if (str[k] == '.') { | 
|---|
| 346 | col = Color(); //member indexing not allowed | 
|---|
| 347 | break; | 
|---|
| 348 | } else if (str[k] > 32) { | 
|---|
| 349 | break; | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | if (col != Color()) { | 
|---|
| 355 | in_keyword = true; | 
|---|
| 356 | keyword_color = col; | 
|---|
| 357 | } | 
|---|
| 358 | } | 
|---|
| 359 |  | 
|---|
| 360 | if (!in_function_name && in_word && !in_keyword) { | 
|---|
| 361 | int k = j; | 
|---|
| 362 | while (k < line_length && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { | 
|---|
| 363 | k++; | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|
| 366 | // Check for space between name and bracket. | 
|---|
| 367 | while (k < line_length && (str[k] == '\t' || str[k] == ' ')) { | 
|---|
| 368 | k++; | 
|---|
| 369 | } | 
|---|
| 370 |  | 
|---|
| 371 | if (str[k] == '(') { | 
|---|
| 372 | in_function_name = true; | 
|---|
| 373 | } | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) { | 
|---|
| 377 | int k = j; | 
|---|
| 378 | while (k > 0 && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') { | 
|---|
| 379 | k--; | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | if (str[k] == '.') { | 
|---|
| 383 | in_member_variable = true; | 
|---|
| 384 | } | 
|---|
| 385 | } | 
|---|
| 386 |  | 
|---|
| 387 | if (is_a_symbol) { | 
|---|
| 388 | in_function_name = false; | 
|---|
| 389 | in_member_variable = false; | 
|---|
| 390 | } | 
|---|
| 391 |  | 
|---|
| 392 | if (in_keyword) { | 
|---|
| 393 | color = keyword_color; | 
|---|
| 394 | } else if (in_member_variable) { | 
|---|
| 395 | color = member_color; | 
|---|
| 396 | } else if (in_function_name) { | 
|---|
| 397 | color = function_color; | 
|---|
| 398 | } else if (is_a_symbol) { | 
|---|
| 399 | color = symbol_color; | 
|---|
| 400 | } else if (is_number) { | 
|---|
| 401 | color = number_color; | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | prev_is_char = is_char; | 
|---|
| 405 | prev_is_number = is_number; | 
|---|
| 406 |  | 
|---|
| 407 | if (color != prev_color) { | 
|---|
| 408 | prev_color = color; | 
|---|
| 409 | highlighter_info[ "color"] = color; | 
|---|
| 410 | color_map[j] = highlighter_info; | 
|---|
| 411 | } | 
|---|
| 412 | } | 
|---|
| 413 |  | 
|---|
| 414 | return color_map; | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 | void CodeHighlighter::_clear_highlighting_cache() { | 
|---|
| 418 | color_region_cache.clear(); | 
|---|
| 419 | } | 
|---|
| 420 |  | 
|---|
| 421 | void CodeHighlighter::_update_cache() { | 
|---|
| 422 | font_color = text_edit->theme_cache.font_color; | 
|---|
| 423 | } | 
|---|
| 424 |  | 
|---|
| 425 | void CodeHighlighter::add_keyword_color(const String &p_keyword, const Color &p_color) { | 
|---|
| 426 | keywords[p_keyword] = p_color; | 
|---|
| 427 | clear_highlighting_cache(); | 
|---|
| 428 | } | 
|---|
| 429 |  | 
|---|
| 430 | void CodeHighlighter::remove_keyword_color(const String &p_keyword) { | 
|---|
| 431 | keywords.erase(p_keyword); | 
|---|
| 432 | clear_highlighting_cache(); | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | bool CodeHighlighter::has_keyword_color(const String &p_keyword) const { | 
|---|
| 436 | return keywords.has(p_keyword); | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | Color CodeHighlighter::get_keyword_color(const String &p_keyword) const { | 
|---|
| 440 | ERR_FAIL_COND_V(!keywords.has(p_keyword), Color()); | 
|---|
| 441 | return keywords[p_keyword]; | 
|---|
| 442 | } | 
|---|
| 443 |  | 
|---|
| 444 | void CodeHighlighter::set_keyword_colors(const Dictionary p_keywords) { | 
|---|
| 445 | keywords.clear(); | 
|---|
| 446 | keywords = p_keywords; | 
|---|
| 447 | clear_highlighting_cache(); | 
|---|
| 448 | } | 
|---|
| 449 |  | 
|---|
| 450 | void CodeHighlighter::clear_keyword_colors() { | 
|---|
| 451 | keywords.clear(); | 
|---|
| 452 | clear_highlighting_cache(); | 
|---|
| 453 | } | 
|---|
| 454 |  | 
|---|
| 455 | Dictionary CodeHighlighter::get_keyword_colors() const { | 
|---|
| 456 | return keywords; | 
|---|
| 457 | } | 
|---|
| 458 |  | 
|---|
| 459 | void CodeHighlighter::add_member_keyword_color(const String &p_member_keyword, const Color &p_color) { | 
|---|
| 460 | member_keywords[p_member_keyword] = p_color; | 
|---|
| 461 | clear_highlighting_cache(); | 
|---|
| 462 | } | 
|---|
| 463 |  | 
|---|
| 464 | void CodeHighlighter::remove_member_keyword_color(const String &p_member_keyword) { | 
|---|
| 465 | member_keywords.erase(p_member_keyword); | 
|---|
| 466 | clear_highlighting_cache(); | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | bool CodeHighlighter::has_member_keyword_color(const String &p_member_keyword) const { | 
|---|
| 470 | return member_keywords.has(p_member_keyword); | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | Color CodeHighlighter::get_member_keyword_color(const String &p_member_keyword) const { | 
|---|
| 474 | ERR_FAIL_COND_V(!member_keywords.has(p_member_keyword), Color()); | 
|---|
| 475 | return member_keywords[p_member_keyword]; | 
|---|
| 476 | } | 
|---|
| 477 |  | 
|---|
| 478 | void CodeHighlighter::set_member_keyword_colors(const Dictionary &p_member_keywords) { | 
|---|
| 479 | member_keywords.clear(); | 
|---|
| 480 | member_keywords = p_member_keywords; | 
|---|
| 481 | clear_highlighting_cache(); | 
|---|
| 482 | } | 
|---|
| 483 |  | 
|---|
| 484 | void CodeHighlighter::clear_member_keyword_colors() { | 
|---|
| 485 | member_keywords.clear(); | 
|---|
| 486 | clear_highlighting_cache(); | 
|---|
| 487 | } | 
|---|
| 488 |  | 
|---|
| 489 | Dictionary CodeHighlighter::get_member_keyword_colors() const { | 
|---|
| 490 | return member_keywords; | 
|---|
| 491 | } | 
|---|
| 492 |  | 
|---|
| 493 | void CodeHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) { | 
|---|
| 494 | for (int i = 0; i < p_start_key.length(); i++) { | 
|---|
| 495 | ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol"); | 
|---|
| 496 | } | 
|---|
| 497 |  | 
|---|
| 498 | if (p_end_key.length() > 0) { | 
|---|
| 499 | for (int i = 0; i < p_end_key.length(); i++) { | 
|---|
| 500 | ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol"); | 
|---|
| 501 | } | 
|---|
| 502 | } | 
|---|
| 503 |  | 
|---|
| 504 | int at = 0; | 
|---|
| 505 | for (int i = 0; i < color_regions.size(); i++) { | 
|---|
| 506 | ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '"+ p_start_key + "' already exists."); | 
|---|
| 507 | if (p_start_key.length() < color_regions[i].start_key.length()) { | 
|---|
| 508 | at++; | 
|---|
| 509 | } | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | ColorRegion color_region; | 
|---|
| 513 | color_region.color = p_color; | 
|---|
| 514 | color_region.start_key = p_start_key; | 
|---|
| 515 | color_region.end_key = p_end_key; | 
|---|
| 516 | color_region.line_only = p_line_only || p_end_key.is_empty(); | 
|---|
| 517 | color_regions.insert(at, color_region); | 
|---|
| 518 | clear_highlighting_cache(); | 
|---|
| 519 | } | 
|---|
| 520 |  | 
|---|
| 521 | void CodeHighlighter::remove_color_region(const String &p_start_key) { | 
|---|
| 522 | for (int i = 0; i < color_regions.size(); i++) { | 
|---|
| 523 | if (color_regions[i].start_key == p_start_key) { | 
|---|
| 524 | color_regions.remove_at(i); | 
|---|
| 525 | break; | 
|---|
| 526 | } | 
|---|
| 527 | } | 
|---|
| 528 | clear_highlighting_cache(); | 
|---|
| 529 | } | 
|---|
| 530 |  | 
|---|
| 531 | bool CodeHighlighter::has_color_region(const String &p_start_key) const { | 
|---|
| 532 | for (int i = 0; i < color_regions.size(); i++) { | 
|---|
| 533 | if (color_regions[i].start_key == p_start_key) { | 
|---|
| 534 | return true; | 
|---|
| 535 | } | 
|---|
| 536 | } | 
|---|
| 537 | return false; | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | void CodeHighlighter::set_color_regions(const Dictionary &p_color_regions) { | 
|---|
| 541 | color_regions.clear(); | 
|---|
| 542 |  | 
|---|
| 543 | List<Variant> keys; | 
|---|
| 544 | p_color_regions.get_key_list(&keys); | 
|---|
| 545 |  | 
|---|
| 546 | for (const Variant &E : keys) { | 
|---|
| 547 | String key = E; | 
|---|
| 548 |  | 
|---|
| 549 | String start_key = key.get_slice( " ", 0); | 
|---|
| 550 | String end_key = key.get_slice_count( " ") > 1 ? key.get_slice( " ", 1) : String(); | 
|---|
| 551 |  | 
|---|
| 552 | add_color_region(start_key, end_key, p_color_regions[key], end_key.is_empty()); | 
|---|
| 553 | } | 
|---|
| 554 | clear_highlighting_cache(); | 
|---|
| 555 | } | 
|---|
| 556 |  | 
|---|
| 557 | void CodeHighlighter::clear_color_regions() { | 
|---|
| 558 | color_regions.clear(); | 
|---|
| 559 | clear_highlighting_cache(); | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | Dictionary CodeHighlighter::get_color_regions() const { | 
|---|
| 563 | Dictionary r_color_regions; | 
|---|
| 564 | for (int i = 0; i < color_regions.size(); i++) { | 
|---|
| 565 | ColorRegion region = color_regions[i]; | 
|---|
| 566 | r_color_regions[region.start_key + (region.end_key.is_empty() ? "": " "+ region.end_key)] = region.color; | 
|---|
| 567 | } | 
|---|
| 568 | return r_color_regions; | 
|---|
| 569 | } | 
|---|
| 570 |  | 
|---|
| 571 | void CodeHighlighter::_bind_methods() { | 
|---|
| 572 | ClassDB::bind_method(D_METHOD( "add_keyword_color", "keyword", "color"), &CodeHighlighter::add_keyword_color); | 
|---|
| 573 | ClassDB::bind_method(D_METHOD( "remove_keyword_color", "keyword"), &CodeHighlighter::remove_keyword_color); | 
|---|
| 574 | ClassDB::bind_method(D_METHOD( "has_keyword_color", "keyword"), &CodeHighlighter::has_keyword_color); | 
|---|
| 575 | ClassDB::bind_method(D_METHOD( "get_keyword_color", "keyword"), &CodeHighlighter::get_keyword_color); | 
|---|
| 576 |  | 
|---|
| 577 | ClassDB::bind_method(D_METHOD( "set_keyword_colors", "keywords"), &CodeHighlighter::set_keyword_colors); | 
|---|
| 578 | ClassDB::bind_method(D_METHOD( "clear_keyword_colors"), &CodeHighlighter::clear_keyword_colors); | 
|---|
| 579 | ClassDB::bind_method(D_METHOD( "get_keyword_colors"), &CodeHighlighter::get_keyword_colors); | 
|---|
| 580 |  | 
|---|
| 581 | ClassDB::bind_method(D_METHOD( "add_member_keyword_color", "member_keyword", "color"), &CodeHighlighter::add_member_keyword_color); | 
|---|
| 582 | ClassDB::bind_method(D_METHOD( "remove_member_keyword_color", "member_keyword"), &CodeHighlighter::remove_member_keyword_color); | 
|---|
| 583 | ClassDB::bind_method(D_METHOD( "has_member_keyword_color", "member_keyword"), &CodeHighlighter::has_member_keyword_color); | 
|---|
| 584 | ClassDB::bind_method(D_METHOD( "get_member_keyword_color", "member_keyword"), &CodeHighlighter::get_member_keyword_color); | 
|---|
| 585 |  | 
|---|
| 586 | ClassDB::bind_method(D_METHOD( "set_member_keyword_colors", "member_keyword"), &CodeHighlighter::set_member_keyword_colors); | 
|---|
| 587 | ClassDB::bind_method(D_METHOD( "clear_member_keyword_colors"), &CodeHighlighter::clear_member_keyword_colors); | 
|---|
| 588 | ClassDB::bind_method(D_METHOD( "get_member_keyword_colors"), &CodeHighlighter::get_member_keyword_colors); | 
|---|
| 589 |  | 
|---|
| 590 | ClassDB::bind_method(D_METHOD( "add_color_region", "start_key", "end_key", "color", "line_only"), &CodeHighlighter::add_color_region, DEFVAL(false)); | 
|---|
| 591 | ClassDB::bind_method(D_METHOD( "remove_color_region", "start_key"), &CodeHighlighter::remove_color_region); | 
|---|
| 592 | ClassDB::bind_method(D_METHOD( "has_color_region", "start_key"), &CodeHighlighter::has_color_region); | 
|---|
| 593 |  | 
|---|
| 594 | ClassDB::bind_method(D_METHOD( "set_color_regions", "color_regions"), &CodeHighlighter::set_color_regions); | 
|---|
| 595 | ClassDB::bind_method(D_METHOD( "clear_color_regions"), &CodeHighlighter::clear_color_regions); | 
|---|
| 596 | ClassDB::bind_method(D_METHOD( "get_color_regions"), &CodeHighlighter::get_color_regions); | 
|---|
| 597 |  | 
|---|
| 598 | ClassDB::bind_method(D_METHOD( "set_function_color", "color"), &CodeHighlighter::set_function_color); | 
|---|
| 599 | ClassDB::bind_method(D_METHOD( "get_function_color"), &CodeHighlighter::get_function_color); | 
|---|
| 600 |  | 
|---|
| 601 | ClassDB::bind_method(D_METHOD( "set_number_color", "color"), &CodeHighlighter::set_number_color); | 
|---|
| 602 | ClassDB::bind_method(D_METHOD( "get_number_color"), &CodeHighlighter::get_number_color); | 
|---|
| 603 |  | 
|---|
| 604 | ClassDB::bind_method(D_METHOD( "set_symbol_color", "color"), &CodeHighlighter::set_symbol_color); | 
|---|
| 605 | ClassDB::bind_method(D_METHOD( "get_symbol_color"), &CodeHighlighter::get_symbol_color); | 
|---|
| 606 |  | 
|---|
| 607 | ClassDB::bind_method(D_METHOD( "set_member_variable_color", "color"), &CodeHighlighter::set_member_variable_color); | 
|---|
| 608 | ClassDB::bind_method(D_METHOD( "get_member_variable_color"), &CodeHighlighter::get_member_variable_color); | 
|---|
| 609 |  | 
|---|
| 610 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "number_color"), "set_number_color", "get_number_color"); | 
|---|
| 611 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "symbol_color"), "set_symbol_color", "get_symbol_color"); | 
|---|
| 612 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "function_color"), "set_function_color", "get_function_color"); | 
|---|
| 613 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "member_variable_color"), "set_member_variable_color", "get_member_variable_color"); | 
|---|
| 614 |  | 
|---|
| 615 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "keyword_colors"), "set_keyword_colors", "get_keyword_colors"); | 
|---|
| 616 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "member_keyword_colors"), "set_member_keyword_colors", "get_member_keyword_colors"); | 
|---|
| 617 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_regions"), "set_color_regions", "get_color_regions"); | 
|---|
| 618 | } | 
|---|
| 619 |  | 
|---|
| 620 | void CodeHighlighter::set_number_color(Color p_color) { | 
|---|
| 621 | number_color = p_color; | 
|---|
| 622 | clear_highlighting_cache(); | 
|---|
| 623 | } | 
|---|
| 624 |  | 
|---|
| 625 | Color CodeHighlighter::get_number_color() const { | 
|---|
| 626 | return number_color; | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | void CodeHighlighter::set_symbol_color(Color p_color) { | 
|---|
| 630 | symbol_color = p_color; | 
|---|
| 631 | clear_highlighting_cache(); | 
|---|
| 632 | } | 
|---|
| 633 |  | 
|---|
| 634 | Color CodeHighlighter::get_symbol_color() const { | 
|---|
| 635 | return symbol_color; | 
|---|
| 636 | } | 
|---|
| 637 |  | 
|---|
| 638 | void CodeHighlighter::set_function_color(Color p_color) { | 
|---|
| 639 | function_color = p_color; | 
|---|
| 640 | clear_highlighting_cache(); | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | Color CodeHighlighter::get_function_color() const { | 
|---|
| 644 | return function_color; | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | void CodeHighlighter::set_member_variable_color(Color p_color) { | 
|---|
| 648 | member_color = p_color; | 
|---|
| 649 | clear_highlighting_cache(); | 
|---|
| 650 | } | 
|---|
| 651 |  | 
|---|
| 652 | Color CodeHighlighter::get_member_variable_color() const { | 
|---|
| 653 | return member_color; | 
|---|
| 654 | } | 
|---|
| 655 |  | 
|---|