gdscript_highlighter.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*************************************************************************/
  2. /* gdscript_highlighter.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "editor/editor_settings.h"
  34. static bool _is_char(char32_t c) {
  35. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  36. }
  37. static bool _is_hex_symbol(char32_t c) {
  38. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  39. }
  40. static bool _is_bin_symbol(char32_t c) {
  41. return (c == '0' || c == '1');
  42. }
  43. Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
  44. Dictionary color_map;
  45. Type next_type = NONE;
  46. Type current_type = NONE;
  47. Type previous_type = NONE;
  48. String previous_text = "";
  49. int previous_column = 0;
  50. bool prev_is_char = false;
  51. bool prev_is_number = false;
  52. bool in_keyword = false;
  53. bool in_word = false;
  54. bool in_function_name = false;
  55. bool in_variable_declaration = false;
  56. bool in_function_args = false;
  57. bool in_member_variable = false;
  58. bool in_node_path = false;
  59. bool in_annotation = false;
  60. bool is_hex_notation = false;
  61. bool is_bin_notation = false;
  62. bool expect_type = false;
  63. Color keyword_color;
  64. Color color;
  65. color_region_cache[p_line] = -1;
  66. int in_region = -1;
  67. if (p_line != 0) {
  68. int prev_region_line = p_line - 1;
  69. while (prev_region_line > 0 && !color_region_cache.has(prev_region_line)) {
  70. prev_region_line--;
  71. }
  72. for (int i = prev_region_line; i < p_line - 1; i++) {
  73. get_line_syntax_highlighting(i);
  74. }
  75. if (!color_region_cache.has(p_line - 1)) {
  76. get_line_syntax_highlighting(p_line - 1);
  77. }
  78. in_region = color_region_cache[p_line - 1];
  79. }
  80. const String &str = text_edit->get_line(p_line);
  81. const int line_length = str.length();
  82. Color prev_color;
  83. if (in_region != -1 && str.length() == 0) {
  84. color_region_cache[p_line] = in_region;
  85. }
  86. for (int j = 0; j < str.length(); j++) {
  87. Dictionary highlighter_info;
  88. color = font_color;
  89. bool is_char = !is_symbol(str[j]);
  90. bool is_a_symbol = is_symbol(str[j]);
  91. bool is_number = (str[j] >= '0' && str[j] <= '9');
  92. /* color regions */
  93. if (is_a_symbol || in_region != -1) {
  94. int from = j;
  95. for (; from < line_length; from++) {
  96. if (str[from] == '\\') {
  97. from++;
  98. continue;
  99. }
  100. break;
  101. }
  102. if (from != line_length) {
  103. /* check if we are in entering a region */
  104. if (in_region == -1) {
  105. for (int c = 0; c < color_regions.size(); c++) {
  106. /* check there is enough room */
  107. int chars_left = line_length - from;
  108. int start_key_length = color_regions[c].start_key.length();
  109. int end_key_length = color_regions[c].end_key.length();
  110. if (chars_left < start_key_length) {
  111. continue;
  112. }
  113. /* search the line */
  114. bool match = true;
  115. const char32_t *start_key = color_regions[c].start_key.get_data();
  116. for (int k = 0; k < start_key_length; k++) {
  117. if (start_key[k] != str[from + k]) {
  118. match = false;
  119. break;
  120. }
  121. }
  122. if (!match) {
  123. continue;
  124. }
  125. in_region = c;
  126. from += start_key_length;
  127. /* check if it's the whole line */
  128. if (end_key_length == 0 || color_regions[c].line_only || from + end_key_length > line_length) {
  129. prev_color = color_regions[in_region].color;
  130. highlighter_info["color"] = color_regions[c].color;
  131. color_map[j] = highlighter_info;
  132. j = line_length;
  133. if (!color_regions[c].line_only) {
  134. color_region_cache[p_line] = c;
  135. }
  136. }
  137. break;
  138. }
  139. if (j == line_length) {
  140. continue;
  141. }
  142. }
  143. /* if we are in one find the end key */
  144. if (in_region != -1) {
  145. /* search the line */
  146. int region_end_index = -1;
  147. int end_key_length = color_regions[in_region].end_key.length();
  148. const char32_t *end_key = color_regions[in_region].end_key.get_data();
  149. for (; from < line_length; from++) {
  150. if (line_length - from < end_key_length) {
  151. break;
  152. }
  153. if (!is_symbol(str[from])) {
  154. continue;
  155. }
  156. if (str[from] == '\\') {
  157. from++;
  158. continue;
  159. }
  160. region_end_index = from;
  161. for (int k = 0; k < end_key_length; k++) {
  162. if (end_key[k] != str[from + k]) {
  163. region_end_index = -1;
  164. break;
  165. }
  166. }
  167. if (region_end_index != -1) {
  168. break;
  169. }
  170. }
  171. prev_color = color_regions[in_region].color;
  172. highlighter_info["color"] = color_regions[in_region].color;
  173. color_map[j] = highlighter_info;
  174. previous_type = REGION;
  175. previous_text = "";
  176. previous_column = j;
  177. j = from + (end_key_length - 1);
  178. if (region_end_index == -1) {
  179. color_region_cache[p_line] = in_region;
  180. }
  181. in_region = -1;
  182. prev_is_char = false;
  183. prev_is_number = false;
  184. continue;
  185. }
  186. }
  187. }
  188. // allow ABCDEF in hex notation
  189. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  190. is_number = true;
  191. } else {
  192. is_hex_notation = false;
  193. }
  194. // disallow anything not a 0 or 1
  195. if (is_bin_notation && (_is_bin_symbol(str[j]))) {
  196. is_number = true;
  197. } else if (is_bin_notation) {
  198. is_bin_notation = false;
  199. is_number = false;
  200. } else {
  201. is_bin_notation = false;
  202. }
  203. // check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation
  204. if ((str[j] == '.' || str[j] == 'x' || str[j] == 'b' || str[j] == '_' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
  205. is_number = true;
  206. is_a_symbol = false;
  207. is_char = false;
  208. if (str[j] == 'x' && str[j - 1] == '0') {
  209. is_hex_notation = true;
  210. } else if (str[j] == 'b' && str[j - 1] == '0') {
  211. is_bin_notation = true;
  212. }
  213. }
  214. if (!in_word && _is_char(str[j]) && !is_number) {
  215. in_word = true;
  216. }
  217. if ((in_keyword || in_word) && !is_hex_notation) {
  218. is_number = false;
  219. }
  220. if (is_a_symbol && str[j] != '.' && in_word) {
  221. in_word = false;
  222. }
  223. if (!is_char) {
  224. in_keyword = false;
  225. }
  226. if (!in_keyword && is_char && !prev_is_char) {
  227. int to = j;
  228. while (to < str.length() && !is_symbol(str[to])) {
  229. to++;
  230. }
  231. String word = str.substr(j, to - j);
  232. Color col = Color();
  233. if (keywords.has(word)) {
  234. col = keywords[word];
  235. } else if (member_keywords.has(word)) {
  236. col = member_keywords[word];
  237. }
  238. if (col != Color()) {
  239. for (int k = j - 1; k >= 0; k--) {
  240. if (str[k] == '.') {
  241. col = Color(); // keyword & member indexing not allowed
  242. break;
  243. } else if (str[k] > 32) {
  244. break;
  245. }
  246. }
  247. if (col != Color()) {
  248. in_keyword = true;
  249. keyword_color = col;
  250. }
  251. }
  252. }
  253. if (!in_function_name && in_word && !in_keyword) {
  254. int k = j;
  255. while (k < str.length() && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  256. k++;
  257. }
  258. // check for space between name and bracket
  259. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  260. k++;
  261. }
  262. if (str[k] == '(') {
  263. in_function_name = true;
  264. } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::VAR)) {
  265. in_variable_declaration = true;
  266. }
  267. }
  268. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  269. int k = j;
  270. while (k > 0 && !is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  271. k--;
  272. }
  273. if (str[k] == '.') {
  274. in_member_variable = true;
  275. }
  276. }
  277. if (is_a_symbol) {
  278. if (in_function_name) {
  279. in_function_args = true;
  280. }
  281. if (in_function_args && str[j] == ')') {
  282. in_function_args = false;
  283. }
  284. if (expect_type && (prev_is_char || str[j] == '=')) {
  285. expect_type = false;
  286. }
  287. if (j > 0 && str[j] == '>' && str[j - 1] == '-') {
  288. expect_type = true;
  289. }
  290. if (in_variable_declaration || in_function_args) {
  291. int k = j;
  292. // Skip space
  293. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  294. k++;
  295. }
  296. if (str[k] == ':') {
  297. // has type hint
  298. expect_type = true;
  299. }
  300. }
  301. in_variable_declaration = false;
  302. in_function_name = false;
  303. in_member_variable = false;
  304. }
  305. if (!in_node_path && in_region == -1 && str[j] == '$') {
  306. in_node_path = true;
  307. } else if (in_region != -1 || (is_a_symbol && str[j] != '/')) {
  308. in_node_path = false;
  309. }
  310. if (!in_annotation && in_region == -1 && str[j] == '@') {
  311. in_annotation = true;
  312. } else if (in_region != -1 || is_a_symbol) {
  313. in_annotation = false;
  314. }
  315. if (in_node_path) {
  316. next_type = NODE_PATH;
  317. color = node_path_color;
  318. } else if (in_annotation) {
  319. next_type = ANNOTATION;
  320. color = annotation_color;
  321. } else if (in_keyword) {
  322. next_type = KEYWORD;
  323. color = keyword_color;
  324. } else if (in_member_variable) {
  325. next_type = MEMBER;
  326. color = member_color;
  327. } else if (in_function_name) {
  328. next_type = FUNCTION;
  329. if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::Token::FUNC)) {
  330. color = function_definition_color;
  331. } else {
  332. color = function_color;
  333. }
  334. } else if (is_a_symbol) {
  335. next_type = SYMBOL;
  336. color = symbol_color;
  337. } else if (is_number) {
  338. next_type = NUMBER;
  339. color = number_color;
  340. } else if (expect_type) {
  341. next_type = TYPE;
  342. color = type_color;
  343. } else {
  344. next_type = IDENTIFIER;
  345. }
  346. if (next_type != current_type) {
  347. if (current_type == NONE) {
  348. current_type = next_type;
  349. } else {
  350. previous_type = current_type;
  351. current_type = next_type;
  352. // no need to store regions...
  353. if (previous_type == REGION) {
  354. previous_text = "";
  355. previous_column = j;
  356. } else {
  357. String text = str.substr(previous_column, j - previous_column).strip_edges();
  358. previous_column = j;
  359. // ignore if just whitespace
  360. if (text != "") {
  361. previous_text = text;
  362. }
  363. }
  364. }
  365. }
  366. prev_is_char = is_char;
  367. prev_is_number = is_number;
  368. if (color != prev_color) {
  369. prev_color = color;
  370. highlighter_info["color"] = color;
  371. color_map[j] = highlighter_info;
  372. }
  373. }
  374. return color_map;
  375. }
  376. String GDScriptSyntaxHighlighter::_get_name() const {
  377. return "GDScript";
  378. }
  379. Array GDScriptSyntaxHighlighter::_get_supported_languages() const {
  380. Array languages;
  381. languages.push_back("GDScript");
  382. return languages;
  383. }
  384. void GDScriptSyntaxHighlighter::_update_cache() {
  385. keywords.clear();
  386. member_keywords.clear();
  387. color_regions.clear();
  388. color_region_cache.clear();
  389. font_color = text_edit->get_theme_color(SNAME("font_color"));
  390. symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  391. function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
  392. number_color = EDITOR_GET("text_editor/theme/highlighting/number_color");
  393. member_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  394. /* Engine types. */
  395. const Color types_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  396. List<StringName> types;
  397. ClassDB::get_class_list(&types);
  398. for (const StringName &E : types) {
  399. String n = E;
  400. if (n.begins_with("_")) {
  401. n = n.substr(1, n.length());
  402. }
  403. keywords[n] = types_color;
  404. }
  405. /* User types. */
  406. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  407. List<StringName> global_classes;
  408. ScriptServer::get_global_class_list(&global_classes);
  409. for (const StringName &E : global_classes) {
  410. keywords[String(E)] = usertype_color;
  411. }
  412. /* Autoloads. */
  413. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  414. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  415. const ProjectSettings::AutoloadInfo &info = E->value();
  416. if (info.is_singleton) {
  417. keywords[info.name] = usertype_color;
  418. }
  419. }
  420. const GDScriptLanguage *gdscript = GDScriptLanguage::get_singleton();
  421. /* Core types. */
  422. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  423. List<String> core_types;
  424. gdscript->get_core_type_words(&core_types);
  425. for (const String &E : core_types) {
  426. keywords[E] = basetype_color;
  427. }
  428. /* Reserved words. */
  429. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  430. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  431. List<String> keyword_list;
  432. gdscript->get_reserved_words(&keyword_list);
  433. for (const String &E : keyword_list) {
  434. if (gdscript->is_control_flow_keyword(E)) {
  435. keywords[E] = control_flow_keyword_color;
  436. } else {
  437. keywords[E] = keyword_color;
  438. }
  439. }
  440. /* Comments */
  441. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  442. List<String> comments;
  443. gdscript->get_comment_delimiters(&comments);
  444. for (const String &comment : comments) {
  445. String beg = comment.get_slice(" ", 0);
  446. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  447. add_color_region(beg, end, comment_color, end == "");
  448. }
  449. /* Strings */
  450. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  451. List<String> strings;
  452. gdscript->get_string_delimiters(&strings);
  453. for (const String &string : strings) {
  454. String beg = string.get_slice(" ", 0);
  455. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  456. add_color_region(beg, end, string_color, end == "");
  457. }
  458. const Ref<Script> script = _get_edited_resource();
  459. if (script.is_valid()) {
  460. /* Member types. */
  461. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  462. StringName instance_base = script->get_instance_base_type();
  463. if (instance_base != StringName()) {
  464. List<PropertyInfo> plist;
  465. ClassDB::get_property_list(instance_base, &plist);
  466. for (const PropertyInfo &E : plist) {
  467. String name = E.name;
  468. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  469. continue;
  470. }
  471. if (name.find("/") != -1) {
  472. continue;
  473. }
  474. member_keywords[name] = member_variable_color;
  475. }
  476. List<String> clist;
  477. ClassDB::get_integer_constant_list(instance_base, &clist);
  478. for (const String &E : clist) {
  479. member_keywords[E] = member_variable_color;
  480. }
  481. }
  482. }
  483. const String text_edit_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  484. const bool godot_2_theme = text_edit_color_theme == "Godot 2";
  485. if (godot_2_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  486. function_definition_color = Color(0.4, 0.9, 1.0);
  487. node_path_color = Color(0.39, 0.76, 0.35);
  488. annotation_color = Color(1.0, 0.7, 0.45);
  489. } else {
  490. function_definition_color = Color(0.0, 0.65, 0.73);
  491. node_path_color = Color(0.32, 0.55, 0.29);
  492. annotation_color = Color(0.8, 0.5, 0.25);
  493. }
  494. EDITOR_DEF("text_editor/theme/highlighting/gdscript/function_definition_color", function_definition_color);
  495. EDITOR_DEF("text_editor/theme/highlighting/gdscript/node_path_color", node_path_color);
  496. EDITOR_DEF("text_editor/theme/highlighting/gdscript/annotation_color", annotation_color);
  497. if (text_edit_color_theme == "Default" || godot_2_theme) {
  498. EditorSettings::get_singleton()->set_initial_value(
  499. "text_editor/theme/highlighting/gdscript/function_definition_color",
  500. function_definition_color,
  501. true);
  502. EditorSettings::get_singleton()->set_initial_value(
  503. "text_editor/theme/highlighting/gdscript/node_path_color",
  504. node_path_color,
  505. true);
  506. EditorSettings::get_singleton()->set_initial_value(
  507. "text_editor/theme/highlighting/gdscript/annotation_color",
  508. annotation_color,
  509. true);
  510. }
  511. function_definition_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/function_definition_color");
  512. node_path_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/node_path_color");
  513. annotation_color = EDITOR_GET("text_editor/theme/highlighting/gdscript/annotation_color");
  514. type_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  515. }
  516. void GDScriptSyntaxHighlighter::add_color_region(const String &p_start_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
  517. for (int i = 0; i < p_start_key.length(); i++) {
  518. ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "color regions must start with a symbol");
  519. }
  520. if (p_end_key.length() > 0) {
  521. for (int i = 0; i < p_end_key.length(); i++) {
  522. ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "color regions must end with a symbol");
  523. }
  524. }
  525. int at = 0;
  526. for (int i = 0; i < color_regions.size(); i++) {
  527. ERR_FAIL_COND_MSG(color_regions[i].start_key == p_start_key, "color region with start key '" + p_start_key + "' already exists.");
  528. if (p_start_key.length() < color_regions[i].start_key.length()) {
  529. at++;
  530. }
  531. }
  532. ColorRegion color_region;
  533. color_region.color = p_color;
  534. color_region.start_key = p_start_key;
  535. color_region.end_key = p_end_key;
  536. color_region.line_only = p_line_only;
  537. color_regions.insert(at, color_region);
  538. clear_highlighting_cache();
  539. }
  540. Ref<EditorSyntaxHighlighter> GDScriptSyntaxHighlighter::_create() const {
  541. Ref<GDScriptSyntaxHighlighter> syntax_highlighter;
  542. syntax_highlighter.instantiate();
  543. return syntax_highlighter;
  544. }