gdscript_highlighter.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /**************************************************************************/
  2. /* gdscript_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. #include "gdscript_highlighter.h"
  31. #include "../gdscript.h"
  32. #include "../gdscript_tokenizer.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/core_constants.h"
  35. #include "editor/settings/editor_settings.h"
  36. #include "editor/themes/editor_theme_manager.h"
  37. #include "scene/gui/text_edit.h"
  38. Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_line) {
  39. Dictionary color_map;
  40. Type next_type = NONE;
  41. Type current_type = NONE;
  42. Type prev_type = NONE;
  43. String prev_text = "";
  44. int prev_column = 0;
  45. bool prev_is_char = false;
  46. bool prev_is_digit = false;
  47. bool prev_is_binary_op = false;
  48. bool in_keyword = false;
  49. bool in_word = false;
  50. bool in_number = false;
  51. bool in_node_path = false;
  52. bool in_node_ref = false;
  53. bool in_annotation = false;
  54. bool in_string_name = false;
  55. bool is_hex_notation = false;
  56. bool is_bin_notation = false;
  57. bool in_member_variable = false;
  58. bool in_lambda = false;
  59. bool in_function_name = false; // Any call.
  60. bool in_function_declaration = false; // Only declaration.
  61. bool in_signal_declaration = false;
  62. bool is_after_func_signal_declaration = false;
  63. bool in_var_const_declaration = false;
  64. bool is_after_var_const_declaration = false;
  65. bool expect_type = false;
  66. int in_declaration_params = 0; // The number of opened `(` after func/signal name.
  67. int in_declaration_param_dicts = 0; // The number of opened `{` inside func params.
  68. int in_type_params = 0; // The number of opened `[` after type name.
  69. Color keyword_color;
  70. Color color;
  71. color_region_cache[p_line] = -1;
  72. int in_region = -1;
  73. if (p_line != 0) {
  74. int prev_region_line = p_line - 1;
  75. while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
  76. prev_region_line--;
  77. }
  78. for (int i = prev_region_line; i < p_line - 1; i++) {
  79. get_line_syntax_highlighting(i);
  80. }
  81. if (!color_region_cache.has(p_line - 1)) {
  82. get_line_syntax_highlighting(p_line - 1);
  83. }
  84. in_region = color_region_cache[p_line - 1];
  85. }
  86. const String &str = text_edit->get_line_with_ime(p_line);
  87. const int line_length = str.length();
  88. Color prev_color;
  89. if (in_region != -1 && line_length == 0) {
  90. color_region_cache[p_line] = in_region;
  91. }
  92. for (int j = 0; j < line_length; j++) {
  93. Dictionary highlighter_info;
  94. color = font_color;
  95. bool is_char = !is_symbol(str[j]);
  96. bool is_a_symbol = is_symbol(str[j]);
  97. bool is_a_digit = is_digit(str[j]);
  98. bool is_binary_op = false;
  99. /* color regions */
  100. if (is_a_symbol || in_region != -1) {
  101. int from = j;
  102. if (in_region == -1) {
  103. for (; from < line_length; from++) {
  104. if (str[from] == '\\') {
  105. from++;
  106. continue;
  107. }
  108. break;
  109. }
  110. }
  111. if (from != line_length) {
  112. // Check if we are in entering a region.
  113. if (in_region == -1) {
  114. const bool r_prefix = from > 0 && str[from - 1] == 'r';
  115. for (int c = 0; c < color_regions.size(); c++) {
  116. // Check there is enough room.
  117. int chars_left = line_length - from;
  118. int start_key_length = color_regions[c].start_key.length();
  119. int end_key_length = color_regions[c].end_key.length();
  120. if (chars_left < start_key_length) {
  121. continue;
  122. }
  123. if (color_regions[c].is_string && color_regions[c].r_prefix != r_prefix) {
  124. continue;
  125. }
  126. // Search the line.
  127. bool match = true;
  128. const char32_t *start_key = color_regions[c].start_key.get_data();
  129. for (int k = 0; k < start_key_length; k++) {
  130. if (start_key[k] != str[from + k]) {
  131. match = false;
  132. break;
  133. }
  134. }
  135. // "#region" and "#endregion" only highlighted if they're the first region on the line.
  136. if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {
  137. Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);
  138. if (!str_stripped_split.is_empty() &&
  139. str_stripped_split[0] != "#region" &&
  140. str_stripped_split[0] != "#endregion") {
  141. match = false;
  142. }
  143. }
  144. if (!match) {
  145. continue;
  146. }
  147. in_region = c;
  148. from += start_key_length;
  149. // Check if it's the whole line.
  150. if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
  151. // Don't skip comments, for highlighting markers.
  152. if (color_regions[in_region].is_comment) {
  153. break;
  154. }
  155. if (from + end_key_length > line_length) {
  156. // If it's key length and there is a '\', dont skip to highlight esc chars.
  157. if (str.find_char('\\', from) >= 0) {
  158. break;
  159. }
  160. }
  161. prev_color = color_regions[in_region].color;
  162. highlighter_info["color"] = color_regions[c].color;
  163. color_map[j] = highlighter_info;
  164. j = line_length;
  165. if (!color_regions[c].line_only) {
  166. color_region_cache[p_line] = c;
  167. }
  168. }
  169. break;
  170. }
  171. // Don't skip comments, for highlighting markers.
  172. if (j == line_length && !color_regions[in_region].is_comment) {
  173. continue;
  174. }
  175. }
  176. // If we are in one, find the end key.
  177. if (in_region != -1) {
  178. Color region_color = color_regions[in_region].color;
  179. if (in_node_path && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
  180. region_color = node_path_color;
  181. }
  182. if (in_node_ref && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
  183. region_color = node_ref_color;
  184. }
  185. if (in_string_name && color_regions[in_region].type == ColorRegion::TYPE_STRING) {
  186. region_color = string_name_color;
  187. }
  188. prev_color = region_color;
  189. highlighter_info["color"] = region_color;
  190. color_map[j] = highlighter_info;
  191. if (color_regions[in_region].is_comment) {
  192. int marker_start_pos = from;
  193. int marker_len = 0;
  194. while (from <= line_length) {
  195. if (from < line_length && is_unicode_identifier_continue(str[from])) {
  196. marker_len++;
  197. } else {
  198. if (marker_len > 0) {
  199. HashMap<String, CommentMarkerLevel>::ConstIterator E = comment_markers.find(str.substr(marker_start_pos, marker_len));
  200. if (E) {
  201. Dictionary marker_highlighter_info;
  202. marker_highlighter_info["color"] = comment_marker_colors[E->value];
  203. color_map[marker_start_pos] = marker_highlighter_info;
  204. Dictionary marker_continue_highlighter_info;
  205. marker_continue_highlighter_info["color"] = region_color;
  206. color_map[from] = marker_continue_highlighter_info;
  207. }
  208. }
  209. marker_start_pos = from + 1;
  210. marker_len = 0;
  211. }
  212. from++;
  213. }
  214. from = line_length - 1;
  215. j = from;
  216. } else {
  217. // Search the line.
  218. int region_end_index = -1;
  219. int end_key_length = color_regions[in_region].end_key.length();
  220. const char32_t *end_key = color_regions[in_region].end_key.get_data();
  221. for (; from < line_length; from++) {
  222. if (line_length - from < end_key_length) {
  223. // Don't break if '\' to highlight esc chars.
  224. if (str.find_char('\\', from) < 0) {
  225. break;
  226. }
  227. }
  228. if (!is_symbol(str[from])) {
  229. continue;
  230. }
  231. if (str[from] == '\\') {
  232. if (!color_regions[in_region].r_prefix) {
  233. Dictionary escape_char_highlighter_info;
  234. escape_char_highlighter_info["color"] = symbol_color;
  235. color_map[from] = escape_char_highlighter_info;
  236. }
  237. from++;
  238. if (!color_regions[in_region].r_prefix) {
  239. int esc_len = 0;
  240. if (str[from] == 'u') {
  241. esc_len = 4;
  242. } else if (str[from] == 'U') {
  243. esc_len = 6;
  244. }
  245. for (int k = 0; k < esc_len && from < line_length - 1; k++) {
  246. if (!is_hex_digit(str[from + 1])) {
  247. break;
  248. }
  249. from++;
  250. }
  251. Dictionary region_continue_highlighter_info;
  252. region_continue_highlighter_info["color"] = region_color;
  253. color_map[from + 1] = region_continue_highlighter_info;
  254. }
  255. continue;
  256. }
  257. region_end_index = from;
  258. for (int k = 0; k < end_key_length; k++) {
  259. if (end_key[k] != str[from + k]) {
  260. region_end_index = -1;
  261. break;
  262. }
  263. }
  264. if (region_end_index != -1) {
  265. break;
  266. }
  267. }
  268. j = from + (end_key_length - 1);
  269. if (region_end_index == -1) {
  270. color_region_cache[p_line] = in_region;
  271. }
  272. }
  273. prev_type = REGION;
  274. prev_text = "";
  275. prev_column = j;
  276. in_region = -1;
  277. prev_is_char = false;
  278. prev_is_digit = false;
  279. prev_is_binary_op = false;
  280. continue;
  281. }
  282. }
  283. }
  284. // VERY hacky... but couldn't come up with anything better.
  285. if (j > 0 && (str[j] == '&' || str[j] == '^' || str[j] == '%' || str[j] == '+' || str[j] == '-' || str[j] == '~' || str[j] == '.')) {
  286. int to = j - 1;
  287. // Find what the last text was (prev_text won't work if there's no whitespace, so we need to do it manually).
  288. while (to > 0 && is_whitespace(str[to])) {
  289. to--;
  290. }
  291. int from = to;
  292. while (from > 0 && !is_symbol(str[from])) {
  293. from--;
  294. }
  295. String word = str.substr(from + 1, to - from);
  296. // Keywords need to be exceptions, except for keywords that represent a value.
  297. if (word == "true" || word == "false" || word == "null" || word == "PI" || word == "TAU" || word == "INF" || word == "NAN" || word == "self" || word == "super" || !reserved_keywords.has(word)) {
  298. if (!is_symbol(str[to]) || str[to] == '"' || str[to] == '\'' || str[to] == ')' || str[to] == ']' || str[to] == '}') {
  299. is_binary_op = true;
  300. }
  301. }
  302. }
  303. if (!is_char) {
  304. in_keyword = false;
  305. }
  306. // Allow ABCDEF in hex notation.
  307. if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {
  308. is_a_digit = true;
  309. } else if (str[j] != '_') {
  310. is_hex_notation = false;
  311. }
  312. // Disallow anything not a 0 or 1 in binary notation.
  313. if (is_bin_notation && !is_binary_digit(str[j])) {
  314. is_a_digit = false;
  315. is_bin_notation = false;
  316. }
  317. if (!in_number && !in_word && is_a_digit) {
  318. in_number = true;
  319. }
  320. // Special cases for numbers.
  321. if (in_number && !is_a_digit) {
  322. if ((str[j] == 'b' || str[j] == 'B') && str[j - 1] == '0') {
  323. is_bin_notation = true;
  324. } else if ((str[j] == 'x' || str[j] == 'X') && str[j - 1] == '0') {
  325. is_hex_notation = true;
  326. } else if (!((str[j] == '-' || str[j] == '+') && (str[j - 1] == 'e' || str[j - 1] == 'E') && !prev_is_digit) &&
  327. !(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'B' || str[j - 1] == 'x' || str[j - 1] == 'X' || str[j - 1] == '.')) &&
  328. !((str[j] == 'e' || str[j] == 'E') && (prev_is_digit || str[j - 1] == '_')) &&
  329. !(str[j] == '.' && (prev_is_digit || (!prev_is_binary_op && (j > 0 && (str[j - 1] == '_' || str[j - 1] == '-' || str[j - 1] == '+' || str[j - 1] == '~'))))) &&
  330. !((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op && !prev_is_binary_op && str[j - 1] != 'e' && str[j - 1] != 'E')) {
  331. /* This condition continues number highlighting in special cases.
  332. 1st row: '+' or '-' after scientific notation (like 3e-4);
  333. 2nd row: '_' as a numeric separator;
  334. 3rd row: Scientific notation 'e' and floating points;
  335. 4th row: Floating points inside the number, or leading if after a unary mathematical operator;
  336. 5th row: Multiple unary mathematical operators (like ~-7) */
  337. in_number = false;
  338. }
  339. } else if (str[j] == '.' && !is_binary_op && is_digit(str[j + 1]) && (j == 0 || (j > 0 && str[j - 1] != '.'))) {
  340. // Start number highlighting from leading decimal points (like .42)
  341. in_number = true;
  342. } else if ((str[j] == '-' || str[j] == '+' || str[j] == '~') && !is_binary_op) {
  343. // Only start number highlighting on unary operators if a digit follows them.
  344. int non_op = j + 1;
  345. while (str[non_op] == '-' || str[non_op] == '+' || str[non_op] == '~') {
  346. non_op++;
  347. }
  348. if (is_digit(str[non_op]) || (str[non_op] == '.' && non_op < line_length && is_digit(str[non_op + 1]))) {
  349. in_number = true;
  350. }
  351. }
  352. if (!in_word && is_unicode_identifier_start(str[j]) && !in_number) {
  353. in_word = true;
  354. }
  355. if (is_a_symbol && str[j] != '.' && in_word) {
  356. in_word = false;
  357. }
  358. if (!in_keyword && is_char && !prev_is_char) {
  359. int to = j;
  360. while (to < line_length && !is_symbol(str[to])) {
  361. to++;
  362. }
  363. String word = str.substr(j, to - j);
  364. Color col;
  365. if (global_functions.has(word)) {
  366. // "assert" and "preload" are reserved, so highlight even if not followed by a bracket.
  367. if (word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::ASSERT) || word == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::PRELOAD)) {
  368. col = global_function_color;
  369. } else {
  370. // For other global functions, check if followed by bracket.
  371. int k = to;
  372. while (k < line_length && is_whitespace(str[k])) {
  373. k++;
  374. }
  375. if (str[k] == '(') {
  376. col = global_function_color;
  377. }
  378. }
  379. } else if (class_names.has(word)) {
  380. col = class_names[word];
  381. } else if (reserved_keywords.has(word)) {
  382. col = reserved_keywords[word];
  383. // Don't highlight `list` as a type in `for elem: Type in list`.
  384. expect_type = false;
  385. } else if (member_keywords.has(word)) {
  386. col = member_keywords[word];
  387. in_member_variable = true;
  388. }
  389. if (col != Color()) {
  390. for (int k = j - 1; k >= 0; k--) {
  391. if (str[k] == '.') {
  392. // Keyword, member, & global func indexing not allowed,
  393. // but don't reset color if prev was a number like "5."
  394. col = (prev_type == NUMBER) ? col : Color();
  395. break;
  396. } else if (str[k] > 32) {
  397. break;
  398. }
  399. }
  400. if (!in_member_variable && col != Color()) {
  401. in_keyword = true;
  402. keyword_color = col;
  403. }
  404. }
  405. }
  406. if (!in_function_name && in_word && !in_keyword) {
  407. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
  408. in_signal_declaration = true;
  409. } else {
  410. int k = j;
  411. while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  412. k++;
  413. }
  414. // Check for space between name and bracket.
  415. while (k < line_length && is_whitespace(str[k])) {
  416. k++;
  417. }
  418. if (str[k] == '(') {
  419. in_function_name = true;
  420. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  421. in_function_declaration = true;
  422. }
  423. } else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FOR) || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::TK_CONST)) {
  424. in_var_const_declaration = true;
  425. }
  426. // Check for lambda.
  427. if (in_function_declaration) {
  428. k = j - 1;
  429. while (k > 0 && is_whitespace(str[k])) {
  430. k--;
  431. }
  432. if (str[k] == ':') {
  433. in_lambda = true;
  434. }
  435. }
  436. }
  437. }
  438. if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {
  439. int k = j;
  440. while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  441. k--;
  442. }
  443. if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {
  444. in_member_variable = true;
  445. }
  446. }
  447. if (is_a_symbol) {
  448. if (in_function_declaration || in_signal_declaration) {
  449. is_after_func_signal_declaration = true;
  450. }
  451. if (in_var_const_declaration) {
  452. is_after_var_const_declaration = true;
  453. }
  454. if (in_declaration_params > 0) {
  455. switch (str[j]) {
  456. case '(':
  457. in_declaration_params += 1;
  458. break;
  459. case ')':
  460. in_declaration_params -= 1;
  461. break;
  462. case '{':
  463. in_declaration_param_dicts += 1;
  464. break;
  465. case '}':
  466. in_declaration_param_dicts -= 1;
  467. break;
  468. }
  469. } else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {
  470. in_declaration_params = 1;
  471. in_declaration_param_dicts = 0;
  472. }
  473. if (expect_type) {
  474. switch (str[j]) {
  475. case '[':
  476. in_type_params += 1;
  477. break;
  478. case ']':
  479. in_type_params -= 1;
  480. break;
  481. case ',':
  482. if (in_type_params <= 0) {
  483. expect_type = false;
  484. }
  485. break;
  486. case ' ':
  487. case '\t':
  488. case '.':
  489. break;
  490. default:
  491. expect_type = false;
  492. break;
  493. }
  494. } else {
  495. if (j > 0 && str[j - 1] == '-' && str[j] == '>') {
  496. expect_type = true;
  497. in_type_params = 0;
  498. }
  499. if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {
  500. expect_type = true;
  501. in_type_params = 0;
  502. }
  503. }
  504. in_function_name = false;
  505. in_function_declaration = false;
  506. in_signal_declaration = false;
  507. in_var_const_declaration = false;
  508. in_lambda = false;
  509. in_member_variable = false;
  510. if (!is_whitespace(str[j])) {
  511. is_after_func_signal_declaration = false;
  512. is_after_var_const_declaration = false;
  513. }
  514. }
  515. // Set color of StringName, keeping symbol color for binary '&&' and '&'.
  516. if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
  517. if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {
  518. in_string_name = true;
  519. // Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.
  520. if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {
  521. in_string_name = false;
  522. is_binary_op = true;
  523. }
  524. } else {
  525. is_binary_op = true;
  526. }
  527. } else if (in_region != -1 || is_a_symbol) {
  528. in_string_name = false;
  529. }
  530. // '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.
  531. if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {
  532. in_node_path = true;
  533. } else if (in_region != -1 || is_a_symbol) {
  534. in_node_path = false;
  535. }
  536. if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {
  537. in_node_ref = true;
  538. } else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%') || (is_a_digit && j > 0 && (str[j - 1] == '$' || str[j - 1] == '/' || str[j - 1] == '%'))) {
  539. // NodeRefs can't start with digits, so point out wrong syntax immediately.
  540. in_node_ref = false;
  541. }
  542. if (!in_annotation && in_region == -1 && str[j] == '@') {
  543. in_annotation = true;
  544. } else if (in_region != -1 || is_a_symbol) {
  545. in_annotation = false;
  546. }
  547. const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');
  548. if (in_raw_string_prefix) {
  549. color = string_color;
  550. } else if (in_node_ref) {
  551. next_type = NODE_REF;
  552. color = node_ref_color;
  553. } else if (in_annotation) {
  554. next_type = ANNOTATION;
  555. color = annotation_color;
  556. } else if (in_string_name) {
  557. next_type = STRING_NAME;
  558. color = string_name_color;
  559. } else if (in_node_path) {
  560. next_type = NODE_PATH;
  561. color = node_path_color;
  562. } else if (in_keyword) {
  563. next_type = KEYWORD;
  564. color = keyword_color;
  565. } else if (in_signal_declaration) {
  566. next_type = SIGNAL;
  567. color = member_variable_color;
  568. } else if (in_function_name) {
  569. next_type = FUNCTION;
  570. if (!in_lambda && in_function_declaration) {
  571. color = function_definition_color;
  572. } else {
  573. color = function_color;
  574. }
  575. } else if (in_number) {
  576. next_type = NUMBER;
  577. color = number_color;
  578. } else if (is_a_symbol) {
  579. next_type = SYMBOL;
  580. color = symbol_color;
  581. } else if (expect_type) {
  582. next_type = TYPE;
  583. color = type_color;
  584. } else if (in_member_variable) {
  585. next_type = MEMBER;
  586. color = member_variable_color;
  587. } else {
  588. next_type = IDENTIFIER;
  589. }
  590. if (next_type != current_type) {
  591. if (current_type == NONE) {
  592. current_type = next_type;
  593. } else {
  594. prev_type = current_type;
  595. current_type = next_type;
  596. // No need to store regions...
  597. if (prev_type == REGION) {
  598. prev_text = "";
  599. prev_column = j;
  600. } else {
  601. String text = str.substr(prev_column, j - prev_column).strip_edges();
  602. prev_column = j;
  603. // Ignore if just whitespace.
  604. if (!text.is_empty()) {
  605. prev_text = text;
  606. }
  607. }
  608. }
  609. }
  610. prev_is_char = is_char;
  611. prev_is_digit = is_a_digit;
  612. prev_is_binary_op = is_binary_op;
  613. if (color != prev_color) {
  614. prev_color = color;
  615. highlighter_info["color"] = color;
  616. color_map[j] = highlighter_info;
  617. }
  618. }
  619. return color_map;
  620. }
  621. String GDScriptSyntaxHighlighter::_get_name() const {
  622. return "GDScript";
  623. }
  624. PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {
  625. PackedStringArray languages;
  626. languages.push_back("GDScript");
  627. return languages;
  628. }
  629. void GDScriptSyntaxHighlighter::_update_cache() {
  630. class_names.clear();
  631. reserved_keywords.clear();
  632. member_keywords.clear();
  633. global_functions.clear();
  634. color_regions.clear();
  635. color_region_cache.clear();
  636. font_color = text_edit->get_theme_color(SceneStringName(font_color));
  637. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  638. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  639. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  640. member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  641. /* Engine types. */
  642. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  643. List<StringName> types;
  644. ClassDB::get_class_list(&types);
  645. for (const StringName &E : types) {
  646. if (ClassDB::is_class_exposed(E)) {
  647. class_names[E] = types_color;
  648. }
  649. }
  650. /* Global enums. */
  651. List<StringName> global_enums;
  652. CoreConstants::get_global_enums(&global_enums);
  653. for (const StringName &enum_name : global_enums) {
  654. class_names[enum_name] = types_color;
  655. }
  656. /* User types. */
  657. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  658. List<StringName> global_classes;
  659. ScriptServer::get_global_class_list(&global_classes);
  660. for (const StringName &E : global_classes) {
  661. class_names[E] = usertype_color;
  662. }
  663. /* Autoloads. */
  664. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  665. const ProjectSettings::AutoloadInfo &info = E.value;
  666. if (info.is_singleton) {
  667. class_names[info.name] = usertype_color;
  668. }
  669. }
  670. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  671. /* Core types. */
  672. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  673. List<String> core_types;
  674. gdscript->get_core_type_words(&core_types);
  675. for (const String &E : core_types) {
  676. class_names[StringName(E)] = basetype_color;
  677. }
  678. class_names[SNAME("Variant")] = basetype_color;
  679. class_names[SNAME("void")] = basetype_color;
  680. // `get_core_type_words()` doesn't return primitive types.
  681. class_names[SNAME("bool")] = basetype_color;
  682. class_names[SNAME("int")] = basetype_color;
  683. class_names[SNAME("float")] = basetype_color;
  684. /* Reserved words. */
  685. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  686. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  687. for (const String &keyword : gdscript->get_reserved_words()) {
  688. if (gdscript->is_control_flow_keyword(keyword)) {
  689. reserved_keywords[StringName(keyword)] = control_flow_keyword_color;
  690. } else {
  691. reserved_keywords[StringName(keyword)] = keyword_color;
  692. }
  693. }
  694. // Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.
  695. reserved_keywords[SNAME("set")] = function_color;
  696. reserved_keywords[SNAME("get")] = function_color;
  697. /* Global functions. */
  698. List<StringName> global_function_list;
  699. GDScriptUtilityFunctions::get_function_list(&global_function_list);
  700. Variant::get_utility_function_list(&global_function_list);
  701. // "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.
  702. global_functions.insert(SNAME("assert"));
  703. global_functions.insert(SNAME("preload"));
  704. for (const StringName &E : global_function_list) {
  705. global_functions.insert(E);
  706. }
  707. /* Comments. */
  708. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  709. for (const String &comment : gdscript->get_comment_delimiters()) {
  710. String beg = comment.get_slicec(' ', 0);
  711. String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();
  712. add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());
  713. }
  714. /* Doc comments */
  715. const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  716. for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {
  717. String beg = doc_comment.get_slicec(' ', 0);
  718. String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();
  719. add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());
  720. }
  721. /* Code regions */
  722. const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);
  723. add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);
  724. add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);
  725. /* Strings */
  726. string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  727. add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);
  728. add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);
  729. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);
  730. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);
  731. add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);
  732. add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);
  733. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);
  734. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);
  735. /* Members. */
  736. Ref<Script> scr = _get_edited_resource();
  737. if (scr.is_valid()) {
  738. StringName instance_base = scr->get_instance_base_type();
  739. if (instance_base != StringName()) {
  740. List<PropertyInfo> property_list;
  741. ClassDB::get_property_list(instance_base, &property_list);
  742. for (const PropertyInfo &E : property_list) {
  743. String prop_name = E.name;
  744. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  745. continue;
  746. }
  747. if (prop_name.contains_char('/')) {
  748. continue;
  749. }
  750. member_keywords[prop_name] = member_variable_color;
  751. }
  752. List<MethodInfo> signal_list;
  753. ClassDB::get_signal_list(instance_base, &signal_list);
  754. for (const MethodInfo &E : signal_list) {
  755. member_keywords[E.name] = member_variable_color;
  756. }
  757. // For callables.
  758. List<MethodInfo> method_list;
  759. ClassDB::get_method_list(instance_base, &method_list);
  760. for (const MethodInfo &E : method_list) {
  761. member_keywords[E.name] = member_variable_color;
  762. }
  763. List<String> constant_list;
  764. ClassDB::get_integer_constant_list(instance_base, &constant_list);
  765. for (const String &E : constant_list) {
  766. member_keywords[E] = member_variable_color;
  767. }
  768. List<StringName> builtin_enums;
  769. ClassDB::get_enum_list(instance_base, &builtin_enums);
  770. for (const StringName &E : builtin_enums) {
  771. member_keywords[E] = types_color;
  772. }
  773. }
  774. List<PropertyInfo> scr_property_list;
  775. scr->get_script_property_list(&scr_property_list);
  776. for (const PropertyInfo &E : scr_property_list) {
  777. String prop_name = E.name;
  778. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  779. continue;
  780. }
  781. if (prop_name.contains_char('/')) {
  782. continue;
  783. }
  784. member_keywords[prop_name] = member_variable_color;
  785. }
  786. List<MethodInfo> scr_signal_list;
  787. scr->get_script_signal_list(&scr_signal_list);
  788. for (const MethodInfo &E : scr_signal_list) {
  789. member_keywords[E.name] = member_variable_color;
  790. }
  791. // For callables.
  792. List<MethodInfo> scr_method_list;
  793. scr->get_script_method_list(&scr_method_list);
  794. for (const MethodInfo &E : scr_method_list) {
  795. member_keywords[E.name] = member_variable_color;
  796. }
  797. Ref<Script> scr_class = scr;
  798. while (scr_class.is_valid()) {
  799. HashMap<StringName, Variant> scr_constant_list;
  800. scr_class->get_constants(&scr_constant_list);
  801. for (const KeyValue<StringName, Variant> &E : scr_constant_list) {
  802. member_keywords[E.key.operator String()] = member_variable_color;
  803. }
  804. scr_class = scr_class->get_base_script();
  805. }
  806. }
  807. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  808. global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");
  809. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  810. node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
  811. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  812. string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  813. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  814. comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");
  815. comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");
  816. comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");
  817. comment_markers.clear();
  818. Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);
  819. for (int i = 0; i < critical_list.size(); i++) {
  820. comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;
  821. }
  822. Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);
  823. for (int i = 0; i < warning_list.size(); i++) {
  824. comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;
  825. }
  826. Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);
  827. for (int i = 0; i < notice_list.size(); i++) {
  828. comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;
  829. }
  830. }
  831. void GDScriptSyntaxHighlighter::add_color_region(ColorRegion::Type p_type, const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only, bool p_r_prefix) {
  832. ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");
  833. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");
  834. if (!p_end_key.is_empty()) {
  835. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");
  836. }
  837. int at = 0;
  838. for (const ColorRegion &region : color_regions) {
  839. ERR_FAIL_COND_MSG(region.start_key == p_start_key && region.r_prefix == p_r_prefix, "Color region with start key '" + p_start_key + "' already exists.");
  840. if (p_start_key.length() < region.start_key.length()) {
  841. at++;
  842. } else {
  843. break;
  844. }
  845. }
  846. ColorRegion color_region;
  847. color_region.type = p_type;
  848. color_region.color = p_color;
  849. color_region.start_key = p_start_key;
  850. color_region.end_key = p_end_key;
  851. color_region.line_only = p_line_only;
  852. color_region.r_prefix = p_r_prefix;
  853. color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;
  854. color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;
  855. color_regions.insert(at, color_region);
  856. clear_highlighting_cache();
  857. }
  858. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  859. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  860. syntax_highlighter.instantiate();
  861. return syntax_highlighter;
  862. }