gdscript_highlighter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 "core/config/project_settings.h"
  34. #include "editor/editor_settings.h"
  35. Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
  36. Dictionary color_map;
  37. Type next_type = NONE;
  38. Type current_type = NONE;
  39. Type previous_type = NONE;
  40. String previous_text = "";
  41. int previous_column = 0;
  42. bool prev_is_char = false;
  43. bool prev_is_number = false;
  44. bool in_keyword = false;
  45. bool in_word = false;
  46. bool in_function_name = false;
  47. bool in_lambda = false;
  48. bool in_variable_declaration = false;
  49. bool in_signal_declaration = false;
  50. bool in_function_args = false;
  51. bool in_member_variable = false;
  52. bool in_node_path = false;
  53. bool in_node_ref = false;
  54. bool in_annotation = false;
  55. bool in_string_name = false;
  56. bool is_hex_notation = false;
  57. bool is_bin_notation = false;
  58. bool expect_type = false;
  59. Color keyword_color;
  60. Color color;
  61. color_region_cache[p_line] = -1;
  62. int in_region = -1;
  63. if (p_line != 0) {
  64. int prev_region_line = p_line - 1;
  65. while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
  66. prev_region_line--;
  67. }
  68. for (int i = prev_region_line; i < p_line - 1; i++) {
  69. get_line_syntax_highlighting(i);
  70. }
  71. if (!color_region_cache.has(p_line - 1)) {
  72. get_line_syntax_highlighting(p_line - 1);
  73. }
  74. in_region = color_region_cache[p_line - 1];
  75. }
  76. const String &str = text_edit->get_line(p_line);
  77. const int line_length = str.length();
  78. Color prev_color;
  79. if (in_region != -1 && str.length() == 0) {
  80. color_region_cache[p_line] = in_region;
  81. }
  82. for (int j = 0; j < str.length(); j++) {
  83. Dictionary highlighter_info;
  84. color = font_color;
  85. bool is_char = !is_symbol(str[j]);
  86. bool is_a_symbol = is_symbol(str[j]);
  87. bool is_number = is_digit(str[j]);
  88. /* color regions */
  89. if (is_a_symbol || in_region != -1) {
  90. int from = j;
  91. if (in_region == -1) {
  92. for (; from < line_length; from++) {
  93. if (str[from] == '\\') {
  94. from++;
  95. continue;
  96. }
  97. break;
  98. }
  99. }
  100. if (from != line_length) {
  101. /* check if we are in entering a region */
  102. if (in_region == -1) {
  103. for (int c = 0; c < color_regions.size(); c++) {
  104. /* check there is enough room */
  105. int chars_left = line_length - from;
  106. int start_key_length = color_regions[c].start_key.length();
  107. int end_key_length = color_regions[c].end_key.length();
  108. if (chars_left < start_key_length) {
  109. continue;
  110. }
  111. /* search the line */
  112. bool match = true;
  113. const char32_t *start_key = color_regions[c].start_key.get_data();
  114. for (int k = 0; k < start_key_length; k++) {
  115. if (start_key[k] != str[from + k]) {
  116. match = false;
  117. break;
  118. }
  119. }
  120. if (!match) {
  121. continue;
  122. }
  123. in_region = c;
  124. from += start_key_length;
  125. /* check if it's the whole line */
  126. if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
  127. if (from + end_key_length > line_length) {
  128. // If it's key length and there is a '\', dont skip to highlight esc chars.
  129. if (str.find("\\", from) >= 0) {
  130. break;
  131. }
  132. }
  133. prev_color = color_regions[in_region].color;
  134. highlighter_info["color"] = color_regions[c].color;
  135. color_map[j] = highlighter_info;
  136. j = line_length;
  137. if (!color_regions[c].line_only) {
  138. color_region_cache[p_line] = c;
  139. }
  140. }
  141. break;
  142. }
  143. if (j == line_length) {
  144. continue;
  145. }
  146. }
  147. /* if we are in one find the end key */
  148. if (in_region != -1) {
  149. Color region_color = color_regions[in_region].color;
  150. if (in_node_path && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  151. region_color = node_path_color;
  152. }
  153. if (in_node_ref && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  154. region_color = node_ref_color;
  155. }
  156. if (in_string_name && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  157. region_color = string_name_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_digit(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_binary_digit(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_ascii_char(str[j]) || is_underscore(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] != '/' && str[j] != '%')) {
  348. in_node_path = false;
  349. }
  350. if (!in_node_ref && in_region == -1 && (str[j] == '$' || str[j] == '%')) {
  351. in_node_ref = true;
  352. } else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%')) {
  353. in_node_ref = false;
  354. }
  355. if (!in_annotation && in_region == -1 && str[j] == '@') {
  356. in_annotation = true;
  357. } else if (in_region != -1 || is_a_symbol) {
  358. in_annotation = false;
  359. }
  360. if (!in_string_name && in_region == -1 && str[j] == '&') {
  361. in_string_name = true;
  362. } else if (in_region != -1 || is_a_symbol) {
  363. in_string_name = false;
  364. }
  365. if (in_node_path) {
  366. next_type = NODE_PATH;
  367. color = node_path_color;
  368. } else if (in_node_ref) {
  369. next_type = NODE_REF;
  370. color = node_ref_color;
  371. } else if (in_annotation) {
  372. next_type = ANNOTATION;
  373. color = annotation_color;
  374. } else if (in_string_name) {
  375. next_type = STRING_NAME;
  376. color = string_name_color;
  377. } else if (in_keyword) {
  378. next_type = KEYWORD;
  379. color = keyword_color;
  380. } else if (in_member_variable) {
  381. next_type = MEMBER;
  382. color = member_color;
  383. } else if (in_signal_declaration) {
  384. next_type = SIGNAL;
  385. color = member_color;
  386. } else if (in_function_name) {
  387. next_type = FUNCTION;
  388. if (!in_lambda && previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  389. color = function_definition_color;
  390. } else {
  391. color = function_color;
  392. }
  393. } else if (is_a_symbol) {
  394. next_type = SYMBOL;
  395. color = symbol_color;
  396. } else if (is_number) {
  397. next_type = NUMBER;
  398. color = number_color;
  399. } else if (expect_type) {
  400. next_type = TYPE;
  401. color = type_color;
  402. } else {
  403. next_type = IDENTIFIER;
  404. }
  405. if (next_type != current_type) {
  406. if (current_type == NONE) {
  407. current_type = next_type;
  408. } else {
  409. previous_type = current_type;
  410. current_type = next_type;
  411. // no need to store regions...
  412. if (previous_type == REGION) {
  413. previous_text = "";
  414. previous_column = j;
  415. } else {
  416. String text = str.substr(previous_column, j - previous_column).strip_edges();
  417. previous_column = j;
  418. // ignore if just whitespace
  419. if (!text.is_empty()) {
  420. previous_text = text;
  421. }
  422. }
  423. }
  424. }
  425. prev_is_char = is_char;
  426. prev_is_number = is_number;
  427. if (color != prev_color) {
  428. prev_color = color;
  429. highlighter_info["color"] = color;
  430. color_map[j] = highlighter_info;
  431. }
  432. }
  433. return color_map;
  434. }
  435. String GDScriptSyntaxHighlighter::_get_name() const {
  436. return "GDScript";
  437. }
  438. Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
  439. Array languages;
  440. languages.push_back("GDScript");
  441. return languages;
  442. }
  443. void GDScriptSyntaxHighlighter::_update_cache() {
  444. keywords.clear();
  445. member_keywords.clear();
  446. color_regions.clear();
  447. color_region_cache.clear();
  448. font_color = text_edit->get_theme_color(SNAME("font_color"));
  449. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  450. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  451. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  452. member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  453. /* Engine types. */
  454. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  455. List<StringName> types;
  456. ClassDB::get_class_list(&types);
  457. for (const StringName &E : types) {
  458. keywords[E] = types_color;
  459. }
  460. /* User types. */
  461. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  462. List<StringName> global_classes;
  463. ScriptServer::get_global_class_list(&global_classes);
  464. for (const StringName &E : global_classes) {
  465. keywords[E] = usertype_color;
  466. }
  467. /* Autoloads. */
  468. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  469. const ProjectSettings::AutoloadInfo &info = E.value;
  470. if (info.is_singleton) {
  471. keywords[info.name] = usertype_color;
  472. }
  473. }
  474. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  475. /* Core types. */
  476. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  477. List<String> core_types;
  478. gdscript->get_core_type_words(&core_types);
  479. for (const String &E : core_types) {
  480. keywords[StringName(E)] = basetype_color;
  481. }
  482. /* Reserved words. */
  483. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  484. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  485. List<String> keyword_list;
  486. gdscript->get_reserved_words(&keyword_list);
  487. for (const String &E : keyword_list) {
  488. if (gdscript->is_control_flow_keyword(E)) {
  489. keywords[StringName(E)] = control_flow_keyword_color;
  490. } else {
  491. keywords[StringName(E)] = keyword_color;
  492. }
  493. }
  494. /* Comments */
  495. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  496. List<String> comments;
  497. gdscript->get_comment_delimiters(&comments);
  498. for (const String &comment : comments) {
  499. String beg = comment.get_slice(" ", 0);
  500. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  501. add_color_region(beg, end, comment_color, end.is_empty());
  502. }
  503. /* Strings */
  504. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  505. List<String> strings;
  506. gdscript->get_string_delimiters(&strings);
  507. for (const String &string : strings) {
  508. String beg = string.get_slice(" ", 0);
  509. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  510. add_color_region(beg, end, string_color, end.is_empty());
  511. }
  512. const Ref<Script> script = _get_edited_resource();
  513. if (script.is_valid()) {
  514. /* Member types. */
  515. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  516. StringName instance_base = script->get_instance_base_type();
  517. if (instance_base != StringName()) {
  518. List<PropertyInfo> plist;
  519. ClassDB::get_property_list(instance_base, &plist);
  520. for (const PropertyInfo &E : plist) {
  521. String name = E.name;
  522. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  523. continue;
  524. }
  525. if (name.contains("/")) {
  526. continue;
  527. }
  528. member_keywords[name] = member_variable_color;
  529. }
  530. List<String> clist;
  531. ClassDB::get_integer_constant_list(instance_base, &clist);
  532. for (const String &E : clist) {
  533. member_keywords[E] = member_variable_color;
  534. }
  535. }
  536. }
  537. const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  538. const bool godot_2_theme = text_edit_color_theme == "Godot 2";
  539. if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  540. function_definition_color = Color(0.4, 0.9, 1.0);
  541. node_path_color = Color(0.72, 0.77, 0.49);
  542. node_ref_color = Color(0.39, 0.76, 0.35);
  543. annotation_color = Color(1.0, 0.7, 0.45);
  544. string_name_color = Color(1.0, 0.66, 0.72);
  545. } else {
  546. function_definition_color = Color(0.0, 0.65, 0.73);
  547. node_path_color = Color(0.62, 0.67, 0.39);
  548. node_ref_color = Color(0.32, 0.55, 0.29);
  549. annotation_color = Color(0.8, 0.5, 0.25);
  550. string_name_color = Color(0.9, 0.56, 0.62);
  551. }
  552. EDITOR_DEF("text_editor/theme/highlighting/gdscript/function_definition_color", function_definition_color);
  553. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_path_color", node_path_color);
  554. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_reference_color", node_ref_color);
  555. EDITOR_DEF("text_editor/theme/highlighting/gdscript/annotation_color", annotation_color);
  556. EDITOR_DEF("text_editor/theme/highlighting/gdscript/string_name_color", string_name_color);
  557. if (text_edit_color_theme == "Default" || godot_2_theme) {
  558. EditorSettings::get_singleton()->set_initial_value(
  559. "text_editor/theme/highlighting/gdscript/function_definition_color",
  560. function_definition_color,
  561. true);
  562. EditorSettings::get_singleton()->set_initial_value(
  563. "text_editor/theme/highlighting/gdscript/node_path_color",
  564. node_path_color,
  565. true);
  566. EditorSettings::get_singleton()->set_initial_value(
  567. "text_editor/theme/highlighting/gdscript/node_reference_color",
  568. node_ref_color,
  569. true);
  570. EditorSettings::get_singleton()->set_initial_value(
  571. "text_editor/theme/highlighting/gdscript/annotation_color",
  572. annotation_color,
  573. true);
  574. EditorSettings::get_singleton()->set_initial_value(
  575. "text_editor/theme/highlighting/gdscript/string_name_color",
  576. string_name_color,
  577. true);
  578. }
  579. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  580. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  581. node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
  582. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  583. string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  584. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  585. }
  586. void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  587. for (int i = 0; i < p_start_key.length(); i++) {
  588. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
  589. }
  590. if (p_end_key.length() > 0) {
  591. for (int i = 0; i < p_end_key.length(); i++) {
  592. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
  593. }
  594. }
  595. int at = 0;
  596. for (int i = 0; i < color_regions.size(); i++) {
  597. ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
  598. if (p_start_key.length() < color_regions[i].start_key.length()) {
  599. at++;
  600. }
  601. }
  602. ColorRegion color_region;
  603. color_region.color = p_color;
  604. color_region.start_key = p_start_key;
  605. color_region.end_key = p_end_key;
  606. color_region.line_only = p_line_only;
  607. color_regions.insert(at, color_region);
  608. clear_highlighting_cache();
  609. }
  610. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  611. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  612. syntax_highlighter.instantiate();
  613. return syntax_highlighter;
  614. }