gdscript_highlighter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*************************************************************************/
  2. /* gdscript_highlighter.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "gdscript_highlighter.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_tokenizer.h"
  33. #include "editor/editor_settings.h"
  34. static bool _is_char(char32_t c) {
  35. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  36. }
  37. static bool _is_hex_symbol(char32_t c) {
  38. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  39. }
  40. static bool _is_bin_symbol(char32_t c) {
  41. return (c == '0' || c == '1');
  42. }
  43. Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
  44. Dictionary color_map;
  45. Type next_type = NONE;
  46. Type current_type = NONE;
  47. Type previous_type = NONE;
  48. String previous_text = "";
  49. int previous_column = 0;
  50. bool prev_is_char = false;
  51. bool prev_is_number = false;
  52. bool in_keyword = false;
  53. bool in_word = false;
  54. bool in_function_name = false;
  55. bool in_lambda = false;
  56. bool in_variable_declaration = false;
  57. bool in_signal_declaration = false;
  58. bool in_function_args = false;
  59. bool in_member_variable = false;
  60. bool in_node_path = false;
  61. bool in_annotation = false;
  62. bool is_hex_notation = false;
  63. bool is_bin_notation = false;
  64. bool expect_type = false;
  65. Color keyword_color;
  66. Color color;
  67. color_region_cache[p_line] = -1;
  68. int in_region = -1;
  69. if (p_line != 0) {
  70. int prev_region_line = p_line - 1;
  71. while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
  72. prev_region_line--;
  73. }
  74. for (int i = prev_region_line; i < p_line - 1; i++) {
  75. get_line_syntax_highlighting(i);
  76. }
  77. if (!color_region_cache.has(p_line - 1)) {
  78. get_line_syntax_highlighting(p_line - 1);
  79. }
  80. in_region = color_region_cache[p_line - 1];
  81. }
  82. const String &str = text_edit->get_line(p_line);
  83. const int line_length = str.length();
  84. Color prev_color;
  85. if (in_region != -1 && str.length() == 0) {
  86. color_region_cache[p_line] = in_region;
  87. }
  88. for (int j = 0; j < str.length(); j++) {
  89. Dictionary highlighter_info;
  90. color = font_color;
  91. bool is_char = !is_symbol(str[j]);
  92. bool is_a_symbol = is_symbol(str[j]);
  93. bool is_number = (str[j] >= '0' && str[j] <= '9');
  94. /* color regions */
  95. if (is_a_symbol || in_region != -1) {
  96. int from = j;
  97. if (in_region == -1) {
  98. for (; from < line_length; from++) {
  99. if (str[from] == '\\') {
  100. from++;
  101. continue;
  102. }
  103. break;
  104. }
  105. }
  106. if (from != line_length) {
  107. /* check if we are in entering a region */
  108. if (in_region == -1) {
  109. for (int c = 0; c < color_regions.size(); c++) {
  110. /* check there is enough room */
  111. int chars_left = line_length - from;
  112. int start_key_length = color_regions[c].start_key.length();
  113. int end_key_length = color_regions[c].end_key.length();
  114. if (chars_left < start_key_length) {
  115. continue;
  116. }
  117. /* search the line */
  118. bool match = true;
  119. const char32_t *start_key = color_regions[c].start_key.get_data();
  120. for (int k = 0; k < start_key_length; k++) {
  121. if (start_key[k] != str[from + k]) {
  122. match = false;
  123. break;
  124. }
  125. }
  126. if (!match) {
  127. continue;
  128. }
  129. in_region = c;
  130. from += start_key_length;
  131. /* check if it's the whole line */
  132. if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
  133. if (from + end_key_length > line_length) {
  134. // If it's key length and there is a '\', dont skip to highlight esc chars.
  135. if (str.find("\\", from) >= 0) {
  136. break;
  137. }
  138. }
  139. prev_color = color_regions[in_region].color;
  140. highlighter_info["color"] = color_regions[c].color;
  141. color_map[j] = highlighter_info;
  142. j = line_length;
  143. if (!color_regions[c].line_only) {
  144. color_region_cache[p_line] = c;
  145. }
  146. }
  147. break;
  148. }
  149. if (j == line_length) {
  150. continue;
  151. }
  152. }
  153. /* if we are in one find the end key */
  154. if (in_region != -1) {
  155. Color region_color = color_regions[in_region].color;
  156. if (in_node_path && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  157. region_color = node_path_color;
  158. }
  159. prev_color = region_color;
  160. highlighter_info["color"] = region_color;
  161. color_map[j] = highlighter_info;
  162. /* search the line */
  163. int region_end_index = -1;
  164. int end_key_length = color_regions[in_region].end_key.length();
  165. const char32_t *end_key = color_regions[in_region].end_key.get_data();
  166. for (; from < line_length; from++) {
  167. if (line_length - from < end_key_length) {
  168. // Don't break if '\' to highlight esc chars.
  169. if (str.find("\\", from) < 0) {
  170. break;
  171. }
  172. }
  173. if (!is_symbol(str[from])) {
  174. continue;
  175. }
  176. if (str[from] == '\\') {
  177. Dictionary escape_char_highlighter_info;
  178. escape_char_highlighter_info["color"] = symbol_color;
  179. color_map[from] = escape_char_highlighter_info;
  180. from++;
  181. Dictionary region_continue_highlighter_info;
  182. prev_color = region_color;
  183. region_continue_highlighter_info["color"] = region_color;
  184. color_map[from + 1] = region_continue_highlighter_info;
  185. continue;
  186. }
  187. region_end_index = from;
  188. for (int k = 0; k < end_key_length; k++) {
  189. if (end_key[k] != str[from + k]) {
  190. region_end_index = -1;
  191. break;
  192. }
  193. }
  194. if (region_end_index != -1) {
  195. break;
  196. }
  197. }
  198. previous_type = REGION;
  199. previous_text = "";
  200. previous_column = j;
  201. j = from + (end_key_length - 1);
  202. if (region_end_index == -1) {
  203. color_region_cache[p_line] = in_region;
  204. }
  205. in_region = -1;
  206. prev_is_char = false;
  207. prev_is_number = false;
  208. continue;
  209. }
  210. }
  211. }
  212. // allow ABCDEF in hex notation
  213. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  214. is_number = true;
  215. } else {
  216. is_hex_notation = false;
  217. }
  218. // disallow anything not a 0 or 1
  219. if (is_bin_notation && (_is_bin_symbol(str[j]))) {
  220. is_number = true;
  221. } else if (is_bin_notation) {
  222. is_bin_notation = false;
  223. is_number = false;
  224. } else {
  225. is_bin_notation = false;
  226. }
  227. // check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation
  228. if ((str[j] == '.' || str[j] == 'x' || str[j] == 'b' || str[j] == '_' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
  229. is_number = true;
  230. is_a_symbol = false;
  231. is_char = false;
  232. if (str[j] == 'x' && str[j - 1] == '0') {
  233. is_hex_notation = true;
  234. } else if (str[j] == 'b' && str[j - 1] == '0') {
  235. is_bin_notation = true;
  236. }
  237. }
  238. if (!in_word && _is_char(str[j]) && !is_number) {
  239. in_word = true;
  240. }
  241. if ((in_keyword || in_word) && !is_hex_notation) {
  242. is_number = false;
  243. }
  244. if (is_a_symbol && str[j] != '.' && in_word) {
  245. in_word = false;
  246. }
  247. if (!is_char) {
  248. in_keyword = false;
  249. }
  250. if (!in_keyword && is_char && !prev_is_char) {
  251. int to = j;
  252. while (to < str.length() && !is_symbol(str[to])) {
  253. to++;
  254. }
  255. String word = str.substr(j, to - j);
  256. Color col = Color();
  257. if (keywords.has(word)) {
  258. col = keywords[word];
  259. } else if (member_keywords.has(word)) {
  260. col = member_keywords[word];
  261. }
  262. if (col != Color()) {
  263. for (int k = j - 1; k >= 0; k--) {
  264. if (str[k] == '.') {
  265. col = Color(); // keyword & member indexing not allowed
  266. break;
  267. } else if (str[k] > 32) {
  268. break;
  269. }
  270. }
  271. if (col != Color()) {
  272. in_keyword = true;
  273. keyword_color = col;
  274. }
  275. }
  276. }
  277. if (!in_function_name && in_word && !in_keyword) {
  278. if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
  279. in_signal_declaration = true;
  280. } else {
  281. int k = j;
  282. while (k < str.length() && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  283. k++;
  284. }
  285. // check for space between name and bracket
  286. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  287. k++;
  288. }
  289. if (str[k] == '(') {
  290. in_function_name = true;
  291. } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
  292. in_variable_declaration = true;
  293. }
  294. // Check for lambda.
  295. if (in_function_name && previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  296. k = j - 1;
  297. while (k > 0 && (str[k] == '\t' || str[k] == ' ')) {
  298. k--;
  299. }
  300. if (str[k] == ':') {
  301. in_lambda = true;
  302. }
  303. }
  304. }
  305. }
  306. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  307. int k = j;
  308. while (k > 0 && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  309. k--;
  310. }
  311. if (str[k] == '.') {
  312. in_member_variable = true;
  313. }
  314. }
  315. if (is_a_symbol) {
  316. if (in_function_name) {
  317. in_function_args = true;
  318. }
  319. if (in_function_args && str[j] == ')') {
  320. in_function_args = false;
  321. }
  322. if (expect_type && (prev_is_char || str[j] == '=')) {
  323. expect_type = false;
  324. }
  325. if (j > 0 && str[j] == '>' && str[j - 1] == '-') {
  326. expect_type = true;
  327. }
  328. if (in_variable_declaration || in_function_args) {
  329. int k = j;
  330. // Skip space
  331. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  332. k++;
  333. }
  334. if (str[k] == ':') {
  335. // has type hint
  336. expect_type = true;
  337. }
  338. }
  339. in_variable_declaration = false;
  340. in_signal_declaration = false;
  341. in_function_name = false;
  342. in_lambda = false;
  343. in_member_variable = false;
  344. }
  345. if (!in_node_path && in_region == -1 && str[j] == '$') {
  346. in_node_path = true;
  347. } else if (in_region != -1 || (is_a_symbol && str[j] != '/')) {
  348. in_node_path = false;
  349. }
  350. if (!in_annotation && in_region == -1 && str[j] == '@') {
  351. in_annotation = true;
  352. } else if (in_region != -1 || is_a_symbol) {
  353. in_annotation = false;
  354. }
  355. if (in_node_path) {
  356. next_type = NODE_PATH;
  357. color = node_path_color;
  358. } else if (in_annotation) {
  359. next_type = ANNOTATION;
  360. color = annotation_color;
  361. } else if (in_keyword) {
  362. next_type = KEYWORD;
  363. color = keyword_color;
  364. } else if (in_member_variable) {
  365. next_type = MEMBER;
  366. color = member_color;
  367. } else if (in_signal_declaration) {
  368. next_type = SIGNAL;
  369. color = member_color;
  370. } else if (in_function_name) {
  371. next_type = FUNCTION;
  372. if (!in_lambda && previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  373. color = function_definition_color;
  374. } else {
  375. color = function_color;
  376. }
  377. } else if (is_a_symbol) {
  378. next_type = SYMBOL;
  379. color = symbol_color;
  380. } else if (is_number) {
  381. next_type = NUMBER;
  382. color = number_color;
  383. } else if (expect_type) {
  384. next_type = TYPE;
  385. color = type_color;
  386. } else {
  387. next_type = IDENTIFIER;
  388. }
  389. if (next_type != current_type) {
  390. if (current_type == NONE) {
  391. current_type = next_type;
  392. } else {
  393. previous_type = current_type;
  394. current_type = next_type;
  395. // no need to store regions...
  396. if (previous_type == REGION) {
  397. previous_text = "";
  398. previous_column = j;
  399. } else {
  400. String text = str.substr(previous_column, j - previous_column).strip_edges();
  401. previous_column = j;
  402. // ignore if just whitespace
  403. if (!text.is_empty()) {
  404. previous_text = text;
  405. }
  406. }
  407. }
  408. }
  409. prev_is_char = is_char;
  410. prev_is_number = is_number;
  411. if (color != prev_color) {
  412. prev_color = color;
  413. highlighter_info["color"] = color;
  414. color_map[j] = highlighter_info;
  415. }
  416. }
  417. return color_map;
  418. }
  419. String GDScriptSyntaxHighlighter::_get_name() const {
  420. return "GDScript";
  421. }
  422. Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
  423. Array languages;
  424. languages.push_back("GDScript");
  425. return languages;
  426. }
  427. void GDScriptSyntaxHighlighter::_update_cache() {
  428. keywords.clear();
  429. member_keywords.clear();
  430. color_regions.clear();
  431. color_region_cache.clear();
  432. font_color = text_edit->get_theme_color(SNAME("font_color"));
  433. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  434. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  435. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  436. member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  437. /* Engine types. */
  438. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  439. List<StringName> types;
  440. ClassDB::get_class_list(&types);
  441. for (const StringName &E : types) {
  442. keywords[E] = types_color;
  443. }
  444. /* User types. */
  445. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  446. List<StringName> global_classes;
  447. ScriptServer::get_global_class_list(&global_classes);
  448. for (const StringName &E : global_classes) {
  449. keywords[E] = usertype_color;
  450. }
  451. /* Autoloads. */
  452. OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  453. for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
  454. const ProjectSettings::AutoloadInfo &info = E.value();
  455. if (info.is_singleton) {
  456. keywords[info.name] = usertype_color;
  457. }
  458. }
  459. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  460. /* Core types. */
  461. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  462. List<String> core_types;
  463. gdscript->get_core_type_words(&core_types);
  464. for (const String &E : core_types) {
  465. keywords[StringName(E)] = basetype_color;
  466. }
  467. /* Reserved words. */
  468. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  469. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  470. List<String> keyword_list;
  471. gdscript->get_reserved_words(&keyword_list);
  472. for (const String &E : keyword_list) {
  473. if (gdscript->is_control_flow_keyword(E)) {
  474. keywords[StringName(E)] = control_flow_keyword_color;
  475. } else {
  476. keywords[StringName(E)] = keyword_color;
  477. }
  478. }
  479. /* Comments */
  480. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  481. List<String> comments;
  482. gdscript->get_comment_delimiters(&comments);
  483. for (const String &comment : comments) {
  484. String beg = comment.get_slice(" ", 0);
  485. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  486. add_color_region(beg, end, comment_color, end.is_empty());
  487. }
  488. /* Strings */
  489. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  490. List<String> strings;
  491. gdscript->get_string_delimiters(&strings);
  492. for (const String &string : strings) {
  493. String beg = string.get_slice(" ", 0);
  494. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  495. add_color_region(beg, end, string_color, end.is_empty());
  496. }
  497. const Ref<Script> script = _get_edited_resource();
  498. if (script.is_valid()) {
  499. /* Member types. */
  500. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  501. StringName instance_base = script->get_instance_base_type();
  502. if (instance_base != StringName()) {
  503. List<PropertyInfo> plist;
  504. ClassDB::get_property_list(instance_base, &plist);
  505. for (const PropertyInfo &E : plist) {
  506. String name = E.name;
  507. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  508. continue;
  509. }
  510. if (name.find("/") != -1) {
  511. continue;
  512. }
  513. member_keywords[name] = member_variable_color;
  514. }
  515. List<String> clist;
  516. ClassDB::get_integer_constant_list(instance_base, &clist);
  517. for (const String &E : clist) {
  518. member_keywords[E] = member_variable_color;
  519. }
  520. }
  521. }
  522. const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  523. const bool godot_2_theme = text_edit_color_theme == "Godot 2";
  524. if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  525. function_definition_color = Color(0.4, 0.9, 1.0);
  526. node_path_color = Color(0.39, 0.76, 0.35);
  527. annotation_color = Color(1.0, 0.7, 0.45);
  528. } else {
  529. function_definition_color = Color(0.0, 0.65, 0.73);
  530. node_path_color = Color(0.32, 0.55, 0.29);
  531. annotation_color = Color(0.8, 0.5, 0.25);
  532. }
  533. EDITOR_DEF("text_editor/theme/highlighting/gdscript/function_definition_color", function_definition_color);
  534. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_path_color", node_path_color);
  535. EDITOR_DEF("text_editor/theme/highlighting/gdscript/annotation_color", annotation_color);
  536. if (text_edit_color_theme == "Default" || godot_2_theme) {
  537. EditorSettings::get_singleton()->set_initial_value(
  538. "text_editor/theme/highlighting/gdscript/function_definition_color",
  539. function_definition_color,
  540. true);
  541. EditorSettings::get_singleton()->set_initial_value(
  542. "text_editor/theme/highlighting/gdscript/node_path_color",
  543. node_path_color,
  544. true);
  545. EditorSettings::get_singleton()->set_initial_value(
  546. "text_editor/theme/highlighting/gdscript/annotation_color",
  547. annotation_color,
  548. true);
  549. }
  550. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  551. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  552. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  553. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  554. }
  555. void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  556. for (int i = 0; i < p_start_key.length(); i++) {
  557. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
  558. }
  559. if (p_end_key.length() > 0) {
  560. for (int i = 0; i < p_end_key.length(); i++) {
  561. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
  562. }
  563. }
  564. int at = 0;
  565. for (int i = 0; i < color_regions.size(); i++) {
  566. ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
  567. if (p_start_key.length() < color_regions[i].start_key.length()) {
  568. at++;
  569. }
  570. }
  571. ColorRegion color_region;
  572. color_region.color = p_color;
  573. color_region.start_key = p_start_key;
  574. color_region.end_key = p_end_key;
  575. color_region.line_only = p_line_only;
  576. color_regions.insert(at, color_region);
  577. clear_highlighting_cache();
  578. }
  579. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  580. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  581. syntax_highlighter.instantiate();
  582. return syntax_highlighter;
  583. }