gdscript_highlighter.cpp 36 KB

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