gdscript_highlighter.cpp 33 KB

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