gdscript_highlighter.cpp 36 KB

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