gdscript_highlighter.cpp 35 KB

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