gdscript_highlighter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*************************************************************************/
  2. /* gdscript_highlighter.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_function_name = false;
  49. bool in_lambda = false;
  50. bool in_variable_declaration = false;
  51. bool in_signal_declaration = false;
  52. bool in_function_args = false;
  53. bool in_member_variable = false;
  54. bool in_node_path = false;
  55. bool in_node_ref = false;
  56. bool in_annotation = false;
  57. bool in_string_name = false;
  58. bool is_hex_notation = false;
  59. bool is_bin_notation = 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. // A bit of a hack, 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. if (prev_text == "true" || prev_text == "false" || prev_text == "self" || prev_text == "null" || prev_text == "PI" || prev_text == "TAU" || prev_text == "INF" || prev_text == "NAN") {
  219. is_binary_op = true;
  220. } else if (!keywords.has(prev_text)) {
  221. int k = j - 1;
  222. while (k > 0 && is_whitespace(str[k])) {
  223. k--;
  224. }
  225. if (!is_symbol(str[k]) || str[k] == '"' || str[k] == '\'' || str[k] == ')' || str[k] == ']' || str[k] == '}') {
  226. is_binary_op = true;
  227. }
  228. }
  229. }
  230. if (!is_char) {
  231. in_keyword = false;
  232. }
  233. // allow ABCDEF in hex notation
  234. if (is_hex_notation && (is_hex_digit(str[j]) || is_a_digit)) {
  235. is_a_digit = true;
  236. } else {
  237. is_hex_notation = false;
  238. }
  239. // disallow anything not a 0 or 1 in binary notation
  240. if (is_bin_notation && !is_binary_digit(str[j])) {
  241. is_a_digit = false;
  242. is_bin_notation = false;
  243. }
  244. if (!in_number && !in_word && is_a_digit) {
  245. in_number = true;
  246. }
  247. // Special cases for numbers
  248. if (in_number && !is_a_digit) {
  249. if (str[j] == 'b' && str[j - 1] == '0') {
  250. is_bin_notation = true;
  251. } else if (str[j] == 'x' && str[j - 1] == '0') {
  252. is_hex_notation = true;
  253. } else if (!((str[j] == '-' || str[j] == '+') && str[j - 1] == 'e' && !prev_is_digit) &&
  254. !(str[j] == '_' && (prev_is_digit || str[j - 1] == 'b' || str[j - 1] == 'x' || str[j - 1] == '.')) &&
  255. !((str[j] == 'e' || str[j] == '.') && (prev_is_digit || str[j - 1] == '_')) &&
  256. !((str[j] == '-' || str[j] == '+' || str[j] == '~') && !prev_is_binary_op && str[j - 1] != 'e')) {
  257. /* 1st row of condition: '+' or '-' after scientific notation;
  258. 2nd row of condition: '_' as a numeric separator;
  259. 3rd row of condition: Scientific notation 'e' and floating points;
  260. 4th row of condition: Multiple unary operators. */
  261. in_number = false;
  262. }
  263. } else if ((str[j] == '-' || str[j] == '+' || str[j] == '~' || (str[j] == '.' && str[j + 1] != '.' && (j == 0 || (j > 0 && str[j - 1] != '.')))) && !is_binary_op) {
  264. // Start a number from unary mathematical operators and floating points, except for '..'
  265. in_number = true;
  266. }
  267. if (!in_word && (is_ascii_char(str[j]) || is_underscore(str[j])) && !in_number) {
  268. in_word = true;
  269. }
  270. if (is_a_symbol && str[j] != '.' && in_word) {
  271. in_word = false;
  272. }
  273. if (!in_keyword && is_char && !prev_is_char) {
  274. int to = j;
  275. while (to < line_length && !is_symbol(str[to])) {
  276. to++;
  277. }
  278. String word = str.substr(j, to - j);
  279. Color col = Color();
  280. if (global_functions.has(word)) {
  281. // "assert" and "preload" are reserved, so highlight even if not followed by a bracket.
  282. if (word == "assert" || word == "preload" || str[to] == '(') {
  283. col = global_function_color;
  284. }
  285. } else if (keywords.has(word)) {
  286. col = keywords[word];
  287. } else if (member_keywords.has(word)) {
  288. col = member_keywords[word];
  289. }
  290. if (col != Color()) {
  291. for (int k = j - 1; k >= 0; k--) {
  292. if (str[k] == '.') {
  293. col = Color(); // keyword, member & global func indexing not allowed
  294. break;
  295. } else if (str[k] > 32) {
  296. break;
  297. }
  298. }
  299. if (col != Color()) {
  300. in_keyword = true;
  301. keyword_color = col;
  302. }
  303. }
  304. }
  305. if (!in_function_name && in_word && !in_keyword) {
  306. if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::SIGNAL)) {
  307. in_signal_declaration = true;
  308. } else {
  309. int k = j;
  310. while (k < line_length && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  311. k++;
  312. }
  313. // check for space between name and bracket
  314. while (k < line_length && is_whitespace(str[k])) {
  315. k++;
  316. }
  317. if (str[k] == '(') {
  318. in_function_name = true;
  319. } else if (prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
  320. in_variable_declaration = true;
  321. }
  322. // Check for lambda.
  323. if (in_function_name && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  324. k = j - 1;
  325. while (k > 0 && is_whitespace(str[k])) {
  326. k--;
  327. }
  328. if (str[k] == ':') {
  329. in_lambda = true;
  330. }
  331. }
  332. }
  333. }
  334. if (!in_function_name && !in_member_variable && !in_keyword && !in_number && in_word) {
  335. int k = j;
  336. while (k > 0 && !is_symbol(str[k]) && !is_whitespace(str[k])) {
  337. k--;
  338. }
  339. if (str[k] == '.') {
  340. in_member_variable = true;
  341. }
  342. }
  343. if (is_a_symbol) {
  344. if (in_function_name) {
  345. in_function_args = true;
  346. }
  347. if (in_function_args && str[j] == ')') {
  348. in_function_args = false;
  349. }
  350. if (expect_type && (prev_is_char || str[j] == '=')) {
  351. expect_type = false;
  352. }
  353. if (j > 0 && str[j] == '>' && str[j - 1] == '-') {
  354. expect_type = true;
  355. }
  356. if (in_variable_declaration || in_function_args) {
  357. int k = j;
  358. // Skip space
  359. while (k < line_length && is_whitespace(str[k])) {
  360. k++;
  361. }
  362. if (str[k] == ':') {
  363. // has type hint
  364. expect_type = true;
  365. }
  366. }
  367. in_variable_declaration = false;
  368. in_signal_declaration = false;
  369. in_function_name = false;
  370. in_lambda = false;
  371. in_member_variable = false;
  372. }
  373. // Keep symbol color for binary '&&'. In the case of '&&&' use StringName color for the last ampersand
  374. if (!in_string_name && in_region == -1 && str[j] == '&' && !is_binary_op) {
  375. if (j >= 2 && str[j - 1] == '&' && str[j - 2] != '&' && prev_is_binary_op) {
  376. is_binary_op = true;
  377. } else if (j == 0 || (j > 0 && str[j - 1] != '&') || prev_is_binary_op) {
  378. in_string_name = true;
  379. }
  380. } else if (in_region != -1 || is_a_symbol) {
  381. in_string_name = false;
  382. }
  383. // '^^' has no special meaning, so unlike StringName, when binary, use NodePath color for the last caret
  384. if (!in_node_path && in_region == -1 && str[j] == '^' && !is_binary_op && (j == 0 || (j > 0 && str[j - 1] != '^') || prev_is_binary_op)) {
  385. in_node_path = true;
  386. } else if (in_region != -1 || is_a_symbol) {
  387. in_node_path = false;
  388. }
  389. if (!in_node_ref && in_region == -1 && (str[j] == '$' || (str[j] == '%' && !is_binary_op))) {
  390. in_node_ref = true;
  391. } else if (in_region != -1 || (is_a_symbol && str[j] != '/' && str[j] != '%')) {
  392. in_node_ref = false;
  393. }
  394. if (!in_annotation && in_region == -1 && str[j] == '@') {
  395. in_annotation = true;
  396. } else if (in_region != -1 || is_a_symbol) {
  397. in_annotation = false;
  398. }
  399. if (in_node_ref) {
  400. next_type = NODE_REF;
  401. color = node_ref_color;
  402. } else if (in_annotation) {
  403. next_type = ANNOTATION;
  404. color = annotation_color;
  405. } else if (in_string_name) {
  406. next_type = STRING_NAME;
  407. color = string_name_color;
  408. } else if (in_node_path) {
  409. next_type = NODE_PATH;
  410. color = node_path_color;
  411. } else if (in_keyword) {
  412. next_type = KEYWORD;
  413. color = keyword_color;
  414. } else if (in_member_variable) {
  415. next_type = MEMBER;
  416. color = member_color;
  417. } else if (in_signal_declaration) {
  418. next_type = SIGNAL;
  419. color = member_color;
  420. } else if (in_function_name) {
  421. next_type = FUNCTION;
  422. if (!in_lambda && prev_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  423. color = function_definition_color;
  424. } else {
  425. color = function_color;
  426. }
  427. } else if (in_number) {
  428. next_type = NUMBER;
  429. color = number_color;
  430. } else if (is_a_symbol) {
  431. next_type = SYMBOL;
  432. color = symbol_color;
  433. } else if (expect_type) {
  434. next_type = TYPE;
  435. color = type_color;
  436. } else {
  437. next_type = IDENTIFIER;
  438. }
  439. if (next_type != current_type) {
  440. if (current_type == NONE) {
  441. current_type = next_type;
  442. } else {
  443. prev_type = current_type;
  444. current_type = next_type;
  445. // no need to store regions...
  446. if (prev_type == REGION) {
  447. prev_text = "";
  448. prev_column = j;
  449. } else {
  450. String text = str.substr(prev_column, j - prev_column).strip_edges();
  451. prev_column = j;
  452. // ignore if just whitespace
  453. if (!text.is_empty()) {
  454. prev_text = text;
  455. }
  456. }
  457. }
  458. }
  459. prev_is_char = is_char;
  460. prev_is_digit = is_a_digit;
  461. prev_is_binary_op = is_binary_op;
  462. if (color != prev_color) {
  463. prev_color = color;
  464. highlighter_info["color"] = color;
  465. color_map[j] = highlighter_info;
  466. }
  467. }
  468. return color_map;
  469. }
  470. String GDScriptSyntaxHighlighter::_get_name() const {
  471. return "GDScript";
  472. }
  473. PackedStringArray GDScriptSyntaxHighlighter::_get_supported_languages() const {
  474. PackedStringArray languages;
  475. languages.push_back("GDScript");
  476. return languages;
  477. }
  478. void GDScriptSyntaxHighlighter::_update_cache() {
  479. keywords.clear();
  480. member_keywords.clear();
  481. global_functions.clear();
  482. color_regions.clear();
  483. color_region_cache.clear();
  484. font_color = text_edit->get_theme_color(SNAME("font_color"));
  485. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  486. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  487. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  488. member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  489. /* Engine types. */
  490. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  491. List<StringName> types;
  492. ClassDB::get_class_list(&types);
  493. for (const StringName &E : types) {
  494. keywords[E] = types_color;
  495. }
  496. /* User types. */
  497. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  498. List<StringName> global_classes;
  499. ScriptServer::get_global_class_list(&global_classes);
  500. for (const StringName &E : global_classes) {
  501. keywords[E] = usertype_color;
  502. }
  503. /* Autoloads. */
  504. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  505. const ProjectSettings::AutoloadInfo &info = E.value;
  506. if (info.is_singleton) {
  507. keywords[info.name] = usertype_color;
  508. }
  509. }
  510. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  511. /* Core types. */
  512. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  513. List<String> core_types;
  514. gdscript->get_core_type_words(&core_types);
  515. for (const String &E : core_types) {
  516. keywords[StringName(E)] = basetype_color;
  517. }
  518. /* Reserved words. */
  519. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  520. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  521. List<String> keyword_list;
  522. gdscript->get_reserved_words(&keyword_list);
  523. for (const String &E : keyword_list) {
  524. if (gdscript->is_control_flow_keyword(E)) {
  525. keywords[StringName(E)] = control_flow_keyword_color;
  526. } else {
  527. keywords[StringName(E)] = keyword_color;
  528. }
  529. }
  530. /* Global functions. */
  531. List<StringName> global_function_list;
  532. GDScriptUtilityFunctions::get_function_list(&global_function_list);
  533. Variant::get_utility_function_list(&global_function_list);
  534. // "assert" and "preload" are not utility functions, but are global nonetheless, so insert them.
  535. global_functions.insert(SNAME("assert"));
  536. global_functions.insert(SNAME("preload"));
  537. for (const StringName &E : global_function_list) {
  538. global_functions.insert(E);
  539. }
  540. /* Comments */
  541. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  542. List<String> comments;
  543. gdscript->get_comment_delimiters(&comments);
  544. for (const String &comment : comments) {
  545. String beg = comment.get_slice(" ", 0);
  546. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  547. add_color_region(beg, end, comment_color, end.is_empty());
  548. }
  549. /* Strings */
  550. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  551. List<String> strings;
  552. gdscript->get_string_delimiters(&strings);
  553. for (const String &string : strings) {
  554. String beg = string.get_slice(" ", 0);
  555. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  556. add_color_region(beg, end, string_color, end.is_empty());
  557. }
  558. const Ref<Script> script = _get_edited_resource();
  559. if (script.is_valid()) {
  560. /* Member types. */
  561. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  562. StringName instance_base = script->get_instance_base_type();
  563. if (instance_base != StringName()) {
  564. List<PropertyInfo> plist;
  565. ClassDB::get_property_list(instance_base, &plist);
  566. for (const PropertyInfo &E : plist) {
  567. String name = E.name;
  568. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  569. continue;
  570. }
  571. if (name.contains("/")) {
  572. continue;
  573. }
  574. member_keywords[name] = member_variable_color;
  575. }
  576. List<String> clist;
  577. ClassDB::get_integer_constant_list(instance_base, &clist);
  578. for (const String &E : clist) {
  579. member_keywords[E] = member_variable_color;
  580. }
  581. }
  582. }
  583. const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  584. const bool godot_2_theme = text_edit_color_theme == "Godot 2";
  585. if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  586. function_definition_color = Color(0.4, 0.9, 1.0);
  587. global_function_color = Color(0.6, 0.6, 0.9);
  588. node_path_color = Color(0.72, 0.77, 0.49);
  589. node_ref_color = Color(0.39, 0.76, 0.35);
  590. annotation_color = Color(1.0, 0.7, 0.45);
  591. string_name_color = Color(1.0, 0.76, 0.65);
  592. } else {
  593. function_definition_color = Color(0, 0.6, 0.6);
  594. global_function_color = Color(0.4, 0.2, 0.8);
  595. node_path_color = Color(0.18, 0.55, 0);
  596. node_ref_color = Color(0.0, 0.5, 0);
  597. annotation_color = Color(0.8, 0.37, 0);
  598. string_name_color = Color(0.8, 0.56, 0.45);
  599. }
  600. EDITOR_DEF("text_editor/theme/highlighting/gdscript/function_definition_color", function_definition_color);
  601. EDITOR_DEF("text_editor/theme/highlighting/gdscript/global_function_color", global_function_color);
  602. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_path_color", node_path_color);
  603. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_reference_color", node_ref_color);
  604. EDITOR_DEF("text_editor/theme/highlighting/gdscript/annotation_color", annotation_color);
  605. EDITOR_DEF("text_editor/theme/highlighting/gdscript/string_name_color", string_name_color);
  606. if (text_edit_color_theme == "Default" || godot_2_theme) {
  607. EditorSettings::get_singleton()->set_initial_value(
  608. "text_editor/theme/highlighting/gdscript/function_definition_color",
  609. function_definition_color,
  610. true);
  611. EditorSettings::get_singleton()->set_initial_value(
  612. "text_editor/theme/highlighting/gdscript/global_function_color",
  613. global_function_color,
  614. true);
  615. EditorSettings::get_singleton()->set_initial_value(
  616. "text_editor/theme/highlighting/gdscript/node_path_color",
  617. node_path_color,
  618. true);
  619. EditorSettings::get_singleton()->set_initial_value(
  620. "text_editor/theme/highlighting/gdscript/node_reference_color",
  621. node_ref_color,
  622. true);
  623. EditorSettings::get_singleton()->set_initial_value(
  624. "text_editor/theme/highlighting/gdscript/annotation_color",
  625. annotation_color,
  626. true);
  627. EditorSettings::get_singleton()->set_initial_value(
  628. "text_editor/theme/highlighting/gdscript/string_name_color",
  629. string_name_color,
  630. true);
  631. }
  632. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  633. global_function_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/global_function_color");
  634. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  635. node_ref_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_reference_color");
  636. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  637. string_name_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/string_name_color");
  638. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  639. }
  640. void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  641. for (int i = 0; i < p_start_key.length(); i++) {
  642. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
  643. }
  644. if (p_end_key.length() > 0) {
  645. for (int i = 0; i < p_end_key.length(); i++) {
  646. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
  647. }
  648. }
  649. int at = 0;
  650. for (int i = 0; i < color_regions.size(); i++) {
  651. ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
  652. if (p_start_key.length() < color_regions[i].start_key.length()) {
  653. at++;
  654. }
  655. }
  656. ColorRegion color_region;
  657. color_region.color = p_color;
  658. color_region.start_key = p_start_key;
  659. color_region.end_key = p_end_key;
  660. color_region.line_only = p_line_only;
  661. color_regions.insert(at, color_region);
  662. clear_highlighting_cache();
  663. }
  664. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  665. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  666. syntax_highlighter.instantiate();
  667. return syntax_highlighter;
  668. }