gdscript_highlighter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 prev_type = NONE;
  40. String prev_text = "";
  41. int prev_column = 0;
  42. bool prev_is_char = false;
  43. bool prev_is_digit = false;
  44. bool prev_is_binary_op = false;
  45. bool in_keyword = false;
  46. bool in_word = false;
  47. bool in_number = false;
  48. bool in_node_path = false;
  49. bool in_node_ref = false;
  50. bool in_annotation = false;
  51. bool in_string_name = false;
  52. bool is_hex_notation = false;
  53. bool is_bin_notation = false;
  54. bool in_member_variable = false;
  55. bool in_lambda = false;
  56. bool in_function_name = false;
  57. bool in_function_args = false;
  58. bool in_variable_declaration = false;
  59. bool in_signal_declaration = false;
  60. bool expect_type = false;
  61. Color keyword_color;
  62. Color color;
  63. color_region_cache[p_line] = -1;
  64. int in_region = -1;
  65. if (p_line != 0) {
  66. int prev_region_line = p_line - 1;
  67. while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
  68. prev_region_line--;
  69. }
  70. for (int i = prev_region_line; i < p_line - 1; i++) {
  71. get_line_syntax_highlighting(i);
  72. }
  73. if (!color_region_cache.has(p_line - 1)) {
  74. get_line_syntax_highlighting(p_line - 1);
  75. }
  76. in_region = color_region_cache[p_line - 1];
  77. }
  78. const String &str = text_edit->get_line(p_line);
  79. const int line_length = str.length();
  80. Color prev_color;
  81. if (in_region != -1 && line_length == 0) {
  82. color_region_cache[p_line] = in_region;
  83. }
  84. for (int j = 0; j < line_length; j++) {
  85. Dictionary highlighter_info;
  86. color = font_color;
  87. bool is_char = !is_symbol(str[j]);
  88. bool is_a_symbol = is_symbol(str[j]);
  89. bool is_a_digit = is_digit(str[j]);
  90. bool is_binary_op = false;
  91. /* color regions */
  92. if (is_a_symbol || in_region != -1) {
  93. int from = j;
  94. if (in_region == -1) {
  95. for (; from < line_length; from++) {
  96. if (str[from] == '\\') {
  97. from++;
  98. continue;
  99. }
  100. break;
  101. }
  102. }
  103. if (from != line_length) {
  104. /* check if we are in entering a region */
  105. if (in_region == -1) {
  106. for (int c = 0; c < color_regions.size(); c++) {
  107. /* check there is enough room */
  108. int chars_left = line_length - from;
  109. int start_key_length = color_regions[c].start_key.length();
  110. int end_key_length = color_regions[c].end_key.length();
  111. if (chars_left < start_key_length) {
  112. continue;
  113. }
  114. /* search the line */
  115. bool match = true;
  116. const char32_t *start_key = color_regions[c].start_key.get_data();
  117. for (int k = 0; k < start_key_length; k++) {
  118. if (start_key[k] != str[from + k]) {
  119. match = false;
  120. break;
  121. }
  122. }
  123. if (!match) {
  124. continue;
  125. }
  126. in_region = c;
  127. from += start_key_length;
  128. /* check if it's the whole line */
  129. if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
  130. if (from + end_key_length > line_length) {
  131. // If it's key length and there is a '\', dont skip to highlight esc chars.
  132. if (str.find("\\", from) >= 0) {
  133. break;
  134. }
  135. }
  136. prev_color = color_regions[in_region].color;
  137. highlighter_info["color"] = color_regions[c].color;
  138. color_map[j] = highlighter_info;
  139. j = line_length;
  140. if (!color_regions[c].line_only) {
  141. color_region_cache[p_line] = c;
  142. }
  143. }
  144. break;
  145. }
  146. if (j == line_length) {
  147. continue;
  148. }
  149. }
  150. /* if we are in one find the end key */
  151. if (in_region != -1) {
  152. Color region_color = color_regions[in_region].color;
  153. if (in_node_path && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  154. region_color = node_path_color;
  155. }
  156. if (in_node_ref && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  157. region_color = node_ref_color;
  158. }
  159. if (in_string_name && (color_regions[in_region].start_key == "\"" || color_regions[in_region].start_key == "\'")) {
  160. region_color = string_name_color;
  161. }
  162. prev_color = region_color;
  163. highlighter_info["color"] = region_color;
  164. color_map[j] = highlighter_info;
  165. /* search the line */
  166. int region_end_index = -1;
  167. int end_key_length = color_regions[in_region].end_key.length();
  168. const char32_t *end_key = color_regions[in_region].end_key.get_data();
  169. for (; from < line_length; from++) {
  170. if (line_length - from < end_key_length) {
  171. // Don't break if '\' to highlight esc chars.
  172. if (str.find("\\", from) < 0) {
  173. break;
  174. }
  175. }
  176. if (!is_symbol(str[from])) {
  177. continue;
  178. }
  179. if (str[from] == '\\') {
  180. Dictionary escape_char_highlighter_info;
  181. escape_char_highlighter_info["color"] = symbol_color;
  182. color_map[from] = escape_char_highlighter_info;
  183. from++;
  184. Dictionary region_continue_highlighter_info;
  185. prev_color = region_color;
  186. region_continue_highlighter_info["color"] = region_color;
  187. color_map[from + 1] = region_continue_highlighter_info;
  188. continue;
  189. }
  190. region_end_index = from;
  191. for (int k = 0; k < end_key_length; k++) {
  192. if (end_key[k] != str[from + k]) {
  193. region_end_index = -1;
  194. break;
  195. }
  196. }
  197. if (region_end_index != -1) {
  198. break;
  199. }
  200. }
  201. prev_type = REGION;
  202. prev_text = "";
  203. prev_column = j;
  204. j = from + (end_key_length - 1);
  205. if (region_end_index == -1) {
  206. color_region_cache[p_line] = in_region;
  207. }
  208. in_region = -1;
  209. prev_is_char = false;
  210. prev_is_digit = false;
  211. prev_is_binary_op = false;
  212. continue;
  213. }
  214. }
  215. }
  216. // VERY hacky... but couldn't come up with anything better.
  217. if (j > 0 && (str[j] == '&' || str[j] == '^' || str[j] == '%' || str[j] == '+' || str[j] == '-' || str[j] == '~' || str[j] == '.')) {
  218. int to = j - 1;
  219. // Find what the last text was (prev_text won't work if there's no whitespace, so we need to do it manually).
  220. while (to > 0 && is_whitespace(str[to])) {
  221. to--;
  222. }
  223. int from = to;
  224. while (from > 0 && !is_symbol(str[from])) {
  225. from--;
  226. }
  227. String word = str.substr(from + 1, to - from);
  228. // Keywords need to be exceptions, except for keywords that represent a value.
  229. if (word == "true" || word == "false" || word == "null" || word == "PI" || word == "TAU" || word == "INF" || word == "NAN" || word == "self" || word == "super" || !reserved_keywords.has(word)) {
  230. if (!is_symbol(str[to]) || str[to] == '"' || str[to] == '\'' || str[to] == ')' || str[to] == ']' || str[to] == '}') {
  231. is_binary_op = true;
  232. }
  233. }
  234. }
  235. if (!is_char) {
  236. in_keyword = false;
  237. }
  238. // allow ABCDEF in hex notation
  239. if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {
  240. is_a_digit = true;
  241. } else {
  242. is_hex_notation = false;
  243. }
  244. // disallow anything not a 0 or 1 in binary notation
  245. if (is_bin_notation && !is_binary_digit(str[j])) {
  246. is_a_digit = false;
  247. is_bin_notation = false;
  248. }
  249. if (!in_number && !in_word && is_a_digit) {
  250. in_number = true;
  251. }
  252. // Special cases for numbers
  253. if (in_number && !is_a_digit) {
  254. if (str[j] == 'b' && str[j - 1] == '0') {
  255. is_bin_notation = true;
  256. } else if (str[j] == 'x' && str[j - 1] == '0') {
  257. is_hex_notation = true;
  258. } else if (!((str[j] == '-' || str[j] == '+') && str[j - 1] == 'e' && !prev_is_digit) &&
  259. !(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'x' || str[j - 1] == '.')) &&
  260. !(str[j] == 'e' && (prev_is_digit || str[j - 1] == '_')) &&
  261. !(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&
  262. !((str[j] == '-' || str[j] == '+' || str[j] == '~') && !prev_is_binary_op && str[j - 1] != 'e')) {
  263. /* This condition continues Number highlighting in special cases.
  264. 1st row: '+' or '-' after scientific notation;
  265. 2nd row: '_' as a numeric separator;
  266. 3rd row: Scientific notation 'e' and floating points;
  267. 4th row: Floating points inside the number, or leading if after a unary mathematical operator;
  268. 5th row: Multiple unary mathematical operators */
  269. in_number = false;
  270. }
  271. } else if (!is_binary_op && (str[j] == '-' || str[j] == '+' || str[j] == '~' || (str[j] == '.' && str[j + 1] != '.' && (j == 0 || (j > 0 && str[j - 1] != '.'))))) {
  272. in_number = true;
  273. }
  274. if (!in_word && (is_ascii_char(str[j]) || is_underscore(str[j])) && !in_number) {
  275. in_word = true;
  276. }
  277. if (is_a_symbol && str[j] != '.' && in_word) {
  278. in_word = false;
  279. }
  280. if (!in_keyword && is_char && !prev_is_char) {
  281. int to = j;
  282. while (to < line_length && !is_symbol(str[to])) {
  283. to++;
  284. }
  285. String word = str.substr(j, to - j);
  286. Color col = Color();
  287. if (global_functions.has(word)) {
  288. // "assert" and "preload" are reserved, so highlight even if not followed by a bracket.
  289. if (word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::ASSERT) || word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::PRELOAD)) {
  290. col = global_function_color;
  291. } else {
  292. // For other global functions, check if followed by bracket.
  293. int k = to;
  294. while (k < line_length && is_whitespace(str[k])) {
  295. k++;
  296. }
  297. if (str[k] == '(') {
  298. col = global_function_color;
  299. }
  300. }
  301. } else if (class_names.has(word)) {
  302. col = class_names[word];
  303. } else if (reserved_keywords.has(word)) {
  304. col = reserved_keywords[word];
  305. } else if (member_keywords.has(word)) {
  306. col = member_keywords[word];
  307. }
  308. if (col != Color()) {
  309. for (int k = j - 1; k >= 0; k--) {
  310. if (str[k] == '.') {
  311. col = Color(); // keyword, member & global func indexing not allowed
  312. break;
  313. } else if (str[k] > 32) {
  314. break;
  315. }
  316. }
  317. if (col != Color()) {
  318. in_keyword = true;
  319. keyword_color = col;
  320. }
  321. }
  322. }
  323. if (!in_function_name && in_word && !in_keyword) {
  324. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
  325. in_signal_declaration = true;
  326. } else {
  327. int k = j;
  328. while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  329. k++;
  330. }
  331. // check for space between name and bracket
  332. while (k < line_length && is_whitespace(str[k])) {
  333. k++;
  334. }
  335. if (str[k] == '(') {
  336. in_function_name = true;
  337. } else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
  338. in_variable_declaration = true;
  339. }
  340. // Check for lambda.
  341. if (in_function_name && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  342. k = j - 1;
  343. while (k > 0 && is_whitespace(str[k])) {
  344. k--;
  345. }
  346. if (str[k] == ':') {
  347. in_lambda = true;
  348. }
  349. }
  350. }
  351. }
  352. if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {
  353. int k = j;
  354. while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  355. k--;
  356. }
  357. if (str[k] == '.') {
  358. in_member_variable = true;
  359. }
  360. }
  361. if (is_a_symbol) {
  362. if (in_function_name) {
  363. in_function_args = true;
  364. }
  365. if (in_function_args && str[j] == ')') {
  366. in_function_args = false;
  367. }
  368. if (expect_type && (prev_is_char || str[j] == '=') && str[j] != '[') {
  369. expect_type = false;
  370. }
  371. if (j > 0 && str[j - 1] == '-' && str[j] == '>') {
  372. expect_type = true;
  373. }
  374. if (in_variable_declaration || in_function_args) {
  375. int k = j;
  376. // Skip space
  377. while (k < line_length && is_whitespace(str[k])) {
  378. k++;
  379. }
  380. if (str[k] == ':') {
  381. // has type hint
  382. expect_type = true;
  383. }
  384. }
  385. in_variable_declaration = false;
  386. in_signal_declaration = false;
  387. in_function_name = false;
  388. in_lambda = false;
  389. in_member_variable = false;
  390. }
  391. // Keep symbol color for binary '&&'. In the case of '&&&' use StringName color for the last ampersand
  392. if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
  393. if (j >= 2 && str[j - 1] == '&' && str[j - 2] != '&' && prev_is_binary_op) {
  394. is_binary_op = true;
  395. } else if (j == 0 || (j > 0 && str[j - 1] != '&') || prev_is_binary_op) {
  396. in_string_name = true;
  397. }
  398. } else if (in_region != -1 || is_a_symbol) {
  399. in_string_name = false;
  400. }
  401. // '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret
  402. if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {
  403. in_node_path = true;
  404. } else if (in_region != -1 || is_a_symbol) {
  405. in_node_path = false;
  406. }
  407. if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {
  408. in_node_ref = true;
  409. } else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%')) {
  410. in_node_ref = false;
  411. }
  412. if (!in_annotation && in_region == -1 && str[j] == '@') {
  413. in_annotation = true;
  414. } else if (in_region != -1 || is_a_symbol) {
  415. in_annotation = false;
  416. }
  417. if (in_node_ref) {
  418. next_type = NODE_REF;
  419. color = node_ref_color;
  420. } else if (in_annotation) {
  421. next_type = ANNOTATION;
  422. color = annotation_color;
  423. } else if (in_string_name) {
  424. next_type = STRING_NAME;
  425. color = string_name_color;
  426. } else if (in_node_path) {
  427. next_type = NODE_PATH;
  428. color = node_path_color;
  429. } else if (in_keyword) {
  430. next_type = KEYWORD;
  431. color = keyword_color;
  432. } else if (in_member_variable) {
  433. next_type = MEMBER;
  434. color = member_color;
  435. } else if (in_signal_declaration) {
  436. next_type = SIGNAL;
  437. color = member_color;
  438. } else if (in_function_name) {
  439. next_type = FUNCTION;
  440. if (!in_lambda && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  441. color = function_definition_color;
  442. } else {
  443. color = function_color;
  444. }
  445. } else if (in_number) {
  446. next_type = NUMBER;
  447. color = number_color;
  448. } else if (is_a_symbol) {
  449. next_type = SYMBOL;
  450. color = symbol_color;
  451. } else if (expect_type) {
  452. next_type = TYPE;
  453. color = type_color;
  454. } else {
  455. next_type = IDENTIFIER;
  456. }
  457. if (next_type != current_type) {
  458. if (current_type == NONE) {
  459. current_type = next_type;
  460. } else {
  461. prev_type = current_type;
  462. current_type = next_type;
  463. // no need to store regions...
  464. if (prev_type == REGION) {
  465. prev_text = "";
  466. prev_column = j;
  467. } else {
  468. String text = str.substr(prev_column, j - prev_column).strip_edges();
  469. prev_column = j;
  470. // ignore if just whitespace
  471. if (!text.is_empty()) {
  472. prev_text = text;
  473. }
  474. }
  475. }
  476. }
  477. prev_is_char = is_char;
  478. prev_is_digit = is_a_digit;
  479. prev_is_binary_op = is_binary_op;
  480. if (color != prev_color) {
  481. prev_color = color;
  482. highlighter_info["color"] = color;
  483. color_map[j] = highlighter_info;
  484. }
  485. }
  486. return color_map;
  487. }
  488. String GDScriptSyntaxHighlighter::_get_name() const {
  489. return "GDScript";
  490. }
  491. PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {
  492. PackedStringArray languages;
  493. languages.push_back("GDScript");
  494. return languages;
  495. }
  496. void GDScriptSyntaxHighlighter::_update_cache() {
  497. class_names.clear();
  498. reserved_keywords.clear();
  499. member_keywords.clear();
  500. global_functions.clear();
  501. color_regions.clear();
  502. color_region_cache.clear();
  503. font_color = text_edit->get_theme_color(SNAME("font_color"));
  504. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  505. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  506. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  507. member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  508. /* Engine types. */
  509. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  510. List<StringName> types;
  511. ClassDB::get_class_list(&types);
  512. for (const StringName &E : types) {
  513. class_names[E] = types_color;
  514. }
  515. /* User types. */
  516. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  517. List<StringName> global_classes;
  518. ScriptServer::get_global_class_list(&global_classes);
  519. for (const StringName &E : global_classes) {
  520. class_names[E] = usertype_color;
  521. }
  522. /* Autoloads. */
  523. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  524. const ProjectSettings::AutoloadInfo &info = E.value;
  525. if (info.is_singleton) {
  526. class_names[info.name] = usertype_color;
  527. }
  528. }
  529. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  530. /* Core types. */
  531. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  532. List<String> core_types;
  533. gdscript->get_core_type_words(&core_types);
  534. for (const String &E : core_types) {
  535. class_names[StringName(E)] = basetype_color;
  536. }
  537. /* Reserved words. */
  538. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  539. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  540. List<String> keyword_list;
  541. gdscript->get_reserved_words(&keyword_list);
  542. for (const String &E : keyword_list) {
  543. if (gdscript->is_control_flow_keyword(E)) {
  544. reserved_keywords[StringName(E)] = control_flow_keyword_color;
  545. } else {
  546. reserved_keywords[StringName(E)] = keyword_color;
  547. }
  548. }
  549. /* Global functions. */
  550. List<StringName> global_function_list;
  551. GDScriptUtilityFunctions::get_function_list(&global_function_list);
  552. Variant::get_utility_function_list(&global_function_list);
  553. // "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.
  554. global_functions.insert(SNAME("assert"));
  555. global_functions.insert(SNAME("preload"));
  556. for (const StringName &E : global_function_list) {
  557. global_functions.insert(E);
  558. }
  559. /* Comments */
  560. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  561. List<String> comments;
  562. gdscript->get_comment_delimiters(&comments);
  563. for (const String &comment : comments) {
  564. String beg = comment.get_slice(" ", 0);
  565. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  566. add_color_region(beg, end, comment_color, end.is_empty());
  567. }
  568. /* Strings */
  569. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  570. List<String> strings;
  571. gdscript->get_string_delimiters(&strings);
  572. for (const String &string : strings) {
  573. String beg = string.get_slice(" ", 0);
  574. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  575. add_color_region(beg, end, string_color, end.is_empty());
  576. }
  577. const Ref<Script> scr = _get_edited_resource();
  578. if (scr.is_valid()) {
  579. /* Member types. */
  580. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  581. StringName instance_base = scr->get_instance_base_type();
  582. if (instance_base != StringName()) {
  583. List<PropertyInfo> plist;
  584. ClassDB::get_property_list(instance_base, &plist);
  585. for (const PropertyInfo &E : plist) {
  586. String prop_name = E.name;
  587. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  588. continue;
  589. }
  590. if (prop_name.contains("/")) {
  591. continue;
  592. }
  593. member_keywords[prop_name] = member_variable_color;
  594. }
  595. List<String> clist;
  596. ClassDB::get_integer_constant_list(instance_base, &clist);
  597. for (const String &E : clist) {
  598. member_keywords[E] = member_variable_color;
  599. }
  600. }
  601. }
  602. const String text_edit_color_theme = EDITOR_GET("text_editor/theme/color_theme");
  603. const bool godot_2_theme = text_edit_color_theme == "Godot 2";
  604. if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  605. function_definition_color = Color(0.4, 0.9, 1.0);
  606. global_function_color = Color(0.64, 0.64, 0.96);
  607. node_path_color = Color(0.72, 0.77, 0.49);
  608. node_ref_color = Color(0.39, 0.76, 0.35);
  609. annotation_color = Color(1.0, 0.7, 0.45);
  610. string_name_color = Color(1.0, 0.76, 0.65);
  611. } else {
  612. function_definition_color = Color(0, 0.6, 0.6);
  613. global_function_color = Color(0.36, 0.18, 0.72);
  614. node_path_color = Color(0.18, 0.55, 0);
  615. node_ref_color = Color(0.0, 0.5, 0);
  616. annotation_color = Color(0.8, 0.37, 0);
  617. string_name_color = Color(0.8, 0.56, 0.45);
  618. }
  619. EDITOR_DEF("text_editor/theme/highlighting/gdscript/function_definition_color", function_definition_color);
  620. EDITOR_DEF("text_editor/theme/highlighting/gdscript/global_function_color", global_function_color);
  621. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_path_color", node_path_color);
  622. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_reference_color", node_ref_color);
  623. EDITOR_DEF("text_editor/theme/highlighting/gdscript/annotation_color", annotation_color);
  624. EDITOR_DEF("text_editor/theme/highlighting/gdscript/string_name_color", string_name_color);
  625. if (text_edit_color_theme == "Default" || godot_2_theme) {
  626. EditorSettings::get_singleton()->set_initial_value(
  627. "text_editor/theme/highlighting/gdscript/function_definition_color",
  628. function_definition_color,
  629. true);
  630. EditorSettings::get_singleton()->set_initial_value(
  631. "text_editor/theme/highlighting/gdscript/global_function_color",
  632. global_function_color,
  633. true);
  634. EditorSettings::get_singleton()->set_initial_value(
  635. "text_editor/theme/highlighting/gdscript/node_path_color",
  636. node_path_color,
  637. true);
  638. EditorSettings::get_singleton()->set_initial_value(
  639. "text_editor/theme/highlighting/gdscript/node_reference_color",
  640. node_ref_color,
  641. true);
  642. EditorSettings::get_singleton()->set_initial_value(
  643. "text_editor/theme/highlighting/gdscript/annotation_color",
  644. annotation_color,
  645. true);
  646. EditorSettings::get_singleton()->set_initial_value(
  647. "text_editor/theme/highlighting/gdscript/string_name_color",
  648. string_name_color,
  649. true);
  650. }
  651. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  652. global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");
  653. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  654. node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
  655. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  656. string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  657. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  658. }
  659. void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  660. for (int i = 0; i < p_start_key.length(); i++) {
  661. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
  662. }
  663. if (p_end_key.length() > 0) {
  664. for (int i = 0; i < p_end_key.length(); i++) {
  665. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
  666. }
  667. }
  668. int at = 0;
  669. for (int i = 0; i < color_regions.size(); i++) {
  670. ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
  671. if (p_start_key.length() < color_regions[i].start_key.length()) {
  672. at++;
  673. }
  674. }
  675. ColorRegion color_region;
  676. color_region.color = p_color;
  677. color_region.start_key = p_start_key;
  678. color_region.end_key = p_end_key;
  679. color_region.line_only = p_line_only;
  680. color_regions.insert(at, color_region);
  681. clear_highlighting_cache();
  682. }
  683. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  684. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  685. syntax_highlighter.instantiate();
  686. return syntax_highlighter;
  687. }