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