gdscript_highlighter.cpp 33 KB

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