gdscript_highlighter.cpp 34 KB

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