gdscript_highlighter.cpp 31 KB

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