gdscript_highlighter.cpp 18 KB

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