gdscript_highlighter.cpp 35 KB

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