gdscript_highlighter.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. col = Color(); // Keyword, member & global func indexing not allowed.
  393. break;
  394. } else if (str[k] > 32) {
  395. break;
  396. }
  397. }
  398. if (!in_member_variable && col != Color()) {
  399. in_keyword = true;
  400. keyword_color = col;
  401. }
  402. }
  403. }
  404. if (!in_function_name && in_word && !in_keyword) {
  405. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
  406. in_signal_declaration = true;
  407. } else {
  408. int k = j;
  409. while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  410. k++;
  411. }
  412. // Check for space between name and bracket.
  413. while (k < line_length && is_whitespace(str[k])) {
  414. k++;
  415. }
  416. if (str[k] == '(') {
  417. in_function_name = true;
  418. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  419. in_function_declaration = true;
  420. }
  421. } 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)) {
  422. in_var_const_declaration = true;
  423. }
  424. // Check for lambda.
  425. if (in_function_declaration) {
  426. k = j - 1;
  427. while (k > 0 && is_whitespace(str[k])) {
  428. k--;
  429. }
  430. if (str[k] == ':') {
  431. in_lambda = true;
  432. }
  433. }
  434. }
  435. }
  436. if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {
  437. int k = j;
  438. while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  439. k--;
  440. }
  441. if (str[k] == '.' && (k < 1 || str[k - 1] != '.')) {
  442. in_member_variable = true;
  443. }
  444. }
  445. if (is_a_symbol) {
  446. if (in_function_declaration || in_signal_declaration) {
  447. is_after_func_signal_declaration = true;
  448. }
  449. if (in_var_const_declaration) {
  450. is_after_var_const_declaration = true;
  451. }
  452. if (in_declaration_params > 0) {
  453. switch (str[j]) {
  454. case '(':
  455. in_declaration_params += 1;
  456. break;
  457. case ')':
  458. in_declaration_params -= 1;
  459. break;
  460. case '{':
  461. in_declaration_param_dicts += 1;
  462. break;
  463. case '}':
  464. in_declaration_param_dicts -= 1;
  465. break;
  466. }
  467. } else if ((is_after_func_signal_declaration || prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) && str[j] == '(') {
  468. in_declaration_params = 1;
  469. in_declaration_param_dicts = 0;
  470. }
  471. if (expect_type) {
  472. switch (str[j]) {
  473. case '[':
  474. in_type_params += 1;
  475. break;
  476. case ']':
  477. in_type_params -= 1;
  478. break;
  479. case ',':
  480. if (in_type_params <= 0) {
  481. expect_type = false;
  482. }
  483. break;
  484. case ' ':
  485. case '\t':
  486. case '.':
  487. break;
  488. default:
  489. expect_type = false;
  490. break;
  491. }
  492. } else {
  493. if (j > 0 && str[j - 1] == '-' && str[j] == '>') {
  494. expect_type = true;
  495. in_type_params = 0;
  496. }
  497. if ((is_after_var_const_declaration || (in_declaration_params == 1 && in_declaration_param_dicts == 0)) && str[j] == ':') {
  498. expect_type = true;
  499. in_type_params = 0;
  500. }
  501. }
  502. in_function_name = false;
  503. in_function_declaration = false;
  504. in_signal_declaration = false;
  505. in_var_const_declaration = false;
  506. in_lambda = false;
  507. in_member_variable = false;
  508. if (!is_whitespace(str[j])) {
  509. is_after_func_signal_declaration = false;
  510. is_after_var_const_declaration = false;
  511. }
  512. }
  513. // Set color of StringName, keeping symbol color for binary '&&' and '&'.
  514. if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
  515. if (j + 1 <= line_length - 1 && (str[j + 1] == '\'' || str[j + 1] == '"')) {
  516. in_string_name = true;
  517. // Cover edge cases of i.e. '+&""' and '&&&""', so the StringName is properly colored.
  518. if (prev_is_binary_op && j >= 2 && str[j - 1] == '&' && str[j - 2] != '&') {
  519. in_string_name = false;
  520. is_binary_op = true;
  521. }
  522. } else {
  523. is_binary_op = true;
  524. }
  525. } else if (in_region != -1 || is_a_symbol) {
  526. in_string_name = false;
  527. }
  528. // '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret.
  529. if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {
  530. in_node_path = true;
  531. } else if (in_region != -1 || is_a_symbol) {
  532. in_node_path = false;
  533. }
  534. if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {
  535. in_node_ref = true;
  536. } 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] == '%'))) {
  537. // NodeRefs can't start with digits, so point out wrong syntax immediately.
  538. in_node_ref = false;
  539. }
  540. if (!in_annotation && in_region == -1 && str[j] == '@') {
  541. in_annotation = true;
  542. } else if (in_region != -1 || is_a_symbol) {
  543. in_annotation = false;
  544. }
  545. const bool in_raw_string_prefix = in_region == -1 && str[j] == 'r' && j + 1 < line_length && (str[j + 1] == '"' || str[j + 1] == '\'');
  546. if (in_raw_string_prefix) {
  547. color = string_color;
  548. } else if (in_node_ref) {
  549. next_type = NODE_REF;
  550. color = node_ref_color;
  551. } else if (in_annotation) {
  552. next_type = ANNOTATION;
  553. color = annotation_color;
  554. } else if (in_string_name) {
  555. next_type = STRING_NAME;
  556. color = string_name_color;
  557. } else if (in_node_path) {
  558. next_type = NODE_PATH;
  559. color = node_path_color;
  560. } else if (in_keyword) {
  561. next_type = KEYWORD;
  562. color = keyword_color;
  563. } else if (in_signal_declaration) {
  564. next_type = SIGNAL;
  565. color = member_variable_color;
  566. } else if (in_function_name) {
  567. next_type = FUNCTION;
  568. if (!in_lambda && in_function_declaration) {
  569. color = function_definition_color;
  570. } else {
  571. color = function_color;
  572. }
  573. } else if (in_number) {
  574. next_type = NUMBER;
  575. color = number_color;
  576. } else if (is_a_symbol) {
  577. next_type = SYMBOL;
  578. color = symbol_color;
  579. } else if (expect_type) {
  580. next_type = TYPE;
  581. color = type_color;
  582. } else if (in_member_variable) {
  583. next_type = MEMBER;
  584. color = member_variable_color;
  585. } else {
  586. next_type = IDENTIFIER;
  587. }
  588. if (next_type != current_type) {
  589. if (current_type == NONE) {
  590. current_type = next_type;
  591. } else {
  592. prev_type = current_type;
  593. current_type = next_type;
  594. // No need to store regions...
  595. if (prev_type == REGION) {
  596. prev_text = "";
  597. prev_column = j;
  598. } else {
  599. String text = str.substr(prev_column, j - prev_column).strip_edges();
  600. prev_column = j;
  601. // Ignore if just whitespace.
  602. if (!text.is_empty()) {
  603. prev_text = text;
  604. }
  605. }
  606. }
  607. }
  608. prev_is_char = is_char;
  609. prev_is_digit = is_a_digit;
  610. prev_is_binary_op = is_binary_op;
  611. if (color != prev_color) {
  612. prev_color = color;
  613. highlighter_info["color"] = color;
  614. color_map[j] = highlighter_info;
  615. }
  616. }
  617. return color_map;
  618. }
  619. String GDScriptSyntaxHighlighter::_get_name() const {
  620. return "GDScript";
  621. }
  622. PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {
  623. PackedStringArray languages;
  624. languages.push_back("GDScript");
  625. return languages;
  626. }
  627. void GDScriptSyntaxHighlighter::_update_cache() {
  628. class_names.clear();
  629. reserved_keywords.clear();
  630. member_keywords.clear();
  631. global_functions.clear();
  632. color_regions.clear();
  633. color_region_cache.clear();
  634. font_color = text_edit->get_theme_color(SceneStringName(font_color));
  635. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  636. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  637. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  638. member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  639. /* Engine types. */
  640. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  641. LocalVector<StringName> types;
  642. ClassDB::get_class_list(types);
  643. for (const StringName &type : types) {
  644. if (ClassDB::is_class_exposed(type)) {
  645. class_names[type] = types_color;
  646. }
  647. }
  648. /* Global enums. */
  649. List<StringName> global_enums;
  650. CoreConstants::get_global_enums(&global_enums);
  651. for (const StringName &enum_name : global_enums) {
  652. class_names[enum_name] = types_color;
  653. }
  654. /* User types. */
  655. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  656. LocalVector<StringName> global_classes;
  657. ScriptServer::get_global_class_list(global_classes);
  658. for (const StringName &class_name : global_classes) {
  659. class_names[class_name] = usertype_color;
  660. }
  661. /* Autoloads. */
  662. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  663. const ProjectSettings::AutoloadInfo &info = E.value;
  664. if (info.is_singleton) {
  665. class_names[info.name] = usertype_color;
  666. }
  667. }
  668. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  669. /* Core types. */
  670. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  671. List<String> core_types;
  672. gdscript->get_core_type_words(&core_types);
  673. for (const String &E : core_types) {
  674. class_names[StringName(E)] = basetype_color;
  675. }
  676. class_names[SNAME("Variant")] = basetype_color;
  677. class_names[SNAME("void")] = basetype_color;
  678. // `get_core_type_words()` doesn't return primitive types.
  679. class_names[SNAME("bool")] = basetype_color;
  680. class_names[SNAME("int")] = basetype_color;
  681. class_names[SNAME("float")] = basetype_color;
  682. /* Reserved words. */
  683. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  684. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  685. for (const String &keyword : gdscript->get_reserved_words()) {
  686. if (gdscript->is_control_flow_keyword(keyword)) {
  687. reserved_keywords[StringName(keyword)] = control_flow_keyword_color;
  688. } else {
  689. reserved_keywords[StringName(keyword)] = keyword_color;
  690. }
  691. }
  692. // Highlight `set` and `get` as "keywords" with the function color to avoid conflicts with method calls.
  693. reserved_keywords[SNAME("set")] = function_color;
  694. reserved_keywords[SNAME("get")] = function_color;
  695. /* Global functions. */
  696. List<StringName> global_function_list;
  697. GDScriptUtilityFunctions::get_function_list(&global_function_list);
  698. Variant::get_utility_function_list(&global_function_list);
  699. // "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.
  700. global_functions.insert(SNAME("assert"));
  701. global_functions.insert(SNAME("preload"));
  702. for (const StringName &E : global_function_list) {
  703. global_functions.insert(E);
  704. }
  705. /* Comments. */
  706. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  707. for (const String &comment : gdscript->get_comment_delimiters()) {
  708. String beg = comment.get_slicec(' ', 0);
  709. String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();
  710. add_color_region(ColorRegion::TYPE_COMMENT, beg, end, comment_color, end.is_empty());
  711. }
  712. /* Doc comments */
  713. const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  714. for (const String &doc_comment : gdscript->get_doc_comment_delimiters()) {
  715. String beg = doc_comment.get_slicec(' ', 0);
  716. String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();
  717. add_color_region(ColorRegion::TYPE_COMMENT, beg, end, doc_comment_color, end.is_empty());
  718. }
  719. /* Code regions */
  720. const Color code_region_color = Color(EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color").operator Color(), 1.0);
  721. add_color_region(ColorRegion::TYPE_CODE_REGION, "#region", "", code_region_color, true);
  722. add_color_region(ColorRegion::TYPE_CODE_REGION, "#endregion", "", code_region_color, true);
  723. /* Strings */
  724. string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  725. add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color);
  726. add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color);
  727. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color);
  728. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color);
  729. add_color_region(ColorRegion::TYPE_STRING, "\"", "\"", string_color, false, true);
  730. add_color_region(ColorRegion::TYPE_STRING, "'", "'", string_color, false, true);
  731. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "\"\"\"", "\"\"\"", string_color, false, true);
  732. add_color_region(ColorRegion::TYPE_MULTILINE_STRING, "'''", "'''", string_color, false, true);
  733. /* Members. */
  734. Ref<Script> scr = _get_edited_resource();
  735. if (scr.is_valid()) {
  736. StringName instance_base = scr->get_instance_base_type();
  737. if (instance_base != StringName()) {
  738. List<PropertyInfo> property_list;
  739. ClassDB::get_property_list(instance_base, &property_list);
  740. for (const PropertyInfo &E : property_list) {
  741. String prop_name = E.name;
  742. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  743. continue;
  744. }
  745. if (prop_name.contains_char('/')) {
  746. continue;
  747. }
  748. member_keywords[prop_name] = member_variable_color;
  749. }
  750. List<MethodInfo> signal_list;
  751. ClassDB::get_signal_list(instance_base, &signal_list);
  752. for (const MethodInfo &E : signal_list) {
  753. member_keywords[E.name] = member_variable_color;
  754. }
  755. // For callables.
  756. List<MethodInfo> method_list;
  757. ClassDB::get_method_list(instance_base, &method_list);
  758. for (const MethodInfo &E : method_list) {
  759. member_keywords[E.name] = member_variable_color;
  760. }
  761. List<String> constant_list;
  762. ClassDB::get_integer_constant_list(instance_base, &constant_list);
  763. for (const String &E : constant_list) {
  764. member_keywords[E] = member_variable_color;
  765. }
  766. List<StringName> builtin_enums;
  767. ClassDB::get_enum_list(instance_base, &builtin_enums);
  768. for (const StringName &E : builtin_enums) {
  769. member_keywords[E] = types_color;
  770. }
  771. }
  772. List<PropertyInfo> scr_property_list;
  773. scr->get_script_property_list(&scr_property_list);
  774. for (const PropertyInfo &E : scr_property_list) {
  775. String prop_name = E.name;
  776. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  777. continue;
  778. }
  779. if (prop_name.contains_char('/')) {
  780. continue;
  781. }
  782. member_keywords[prop_name] = member_variable_color;
  783. }
  784. List<MethodInfo> scr_signal_list;
  785. scr->get_script_signal_list(&scr_signal_list);
  786. for (const MethodInfo &E : scr_signal_list) {
  787. member_keywords[E.name] = member_variable_color;
  788. }
  789. // For callables.
  790. List<MethodInfo> scr_method_list;
  791. scr->get_script_method_list(&scr_method_list);
  792. for (const MethodInfo &E : scr_method_list) {
  793. member_keywords[E.name] = member_variable_color;
  794. }
  795. Ref<Script> scr_class = scr;
  796. while (scr_class.is_valid()) {
  797. HashMap<StringName, Variant> scr_constant_list;
  798. scr_class->get_constants(&scr_constant_list);
  799. for (const KeyValue<StringName, Variant> &E : scr_constant_list) {
  800. member_keywords[E.key.operator String()] = member_variable_color;
  801. }
  802. scr_class = scr_class->get_base_script();
  803. }
  804. }
  805. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  806. global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");
  807. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  808. node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
  809. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  810. string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  811. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  812. comment_marker_colors[COMMENT_MARKER_CRITICAL] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_color");
  813. comment_marker_colors[COMMENT_MARKER_WARNING] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_color");
  814. comment_marker_colors[COMMENT_MARKER_NOTICE] = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_color");
  815. comment_markers.clear();
  816. Vector<String> critical_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/critical_list").operator String().split(",", false);
  817. for (int i = 0; i < critical_list.size(); i++) {
  818. comment_markers[critical_list[i]] = COMMENT_MARKER_CRITICAL;
  819. }
  820. Vector<String> warning_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/warning_list").operator String().split(",", false);
  821. for (int i = 0; i < warning_list.size(); i++) {
  822. comment_markers[warning_list[i]] = COMMENT_MARKER_WARNING;
  823. }
  824. Vector<String> notice_list = EDITOR_GET("text_editor/theme/highlighting/comment_markers/notice_list").operator String().split(",", false);
  825. for (int i = 0; i < notice_list.size(); i++) {
  826. comment_markers[notice_list[i]] = COMMENT_MARKER_NOTICE;
  827. }
  828. }
  829. 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) {
  830. ERR_FAIL_COND_MSG(p_start_key.is_empty(), "Color region start key cannot be empty.");
  831. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[0]), "Color region start key must start with a symbol.");
  832. if (!p_end_key.is_empty()) {
  833. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[0]), "Color region end key must start with a symbol.");
  834. }
  835. int at = 0;
  836. for (const ColorRegion &region : color_regions) {
  837. 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.");
  838. if (p_start_key.length() < region.start_key.length()) {
  839. at++;
  840. } else {
  841. break;
  842. }
  843. }
  844. ColorRegion color_region;
  845. color_region.type = p_type;
  846. color_region.color = p_color;
  847. color_region.start_key = p_start_key;
  848. color_region.end_key = p_end_key;
  849. color_region.line_only = p_line_only;
  850. color_region.r_prefix = p_r_prefix;
  851. color_region.is_string = p_type == ColorRegion::TYPE_STRING || p_type == ColorRegion::TYPE_MULTILINE_STRING;
  852. color_region.is_comment = p_type == ColorRegion::TYPE_COMMENT || p_type == ColorRegion::TYPE_CODE_REGION;
  853. color_regions.insert(at, color_region);
  854. clear_highlighting_cache();
  855. }
  856. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  857. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  858. syntax_highlighter.instantiate();
  859. return syntax_highlighter;
  860. }