gdscript_highlighter.cpp 26 KB

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