gdscript_highlighter.cpp 34 KB

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