gdscript_highlighter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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_tokenizer.h"
  32. #include "editor/editor_settings.h"
  33. #include "scene/gui/text_edit.h"
  34. inline bool _is_symbol(CharType c) {
  35. return is_symbol(c);
  36. }
  37. static bool _is_text_char(CharType c) {
  38. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  39. }
  40. static bool _is_char(CharType c) {
  41. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  42. }
  43. static bool _is_number(CharType c) {
  44. return (c >= '0' && c <= '9');
  45. }
  46. static bool _is_hex_symbol(CharType c) {
  47. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  48. }
  49. static bool _is_bin_symbol(CharType c) {
  50. return (c == '0' || c == '1');
  51. }
  52. Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
  53. Map<int, TextEdit::HighlighterInfo> color_map;
  54. Type next_type = NONE;
  55. Type current_type = NONE;
  56. Type previous_type = NONE;
  57. String previous_text = "";
  58. int previous_column = 0;
  59. bool prev_is_char = false;
  60. bool prev_is_number = false;
  61. bool in_keyword = false;
  62. bool in_word = false;
  63. bool in_function_name = false;
  64. bool in_variable_declaration = false;
  65. bool in_function_args = false;
  66. bool in_member_variable = false;
  67. bool in_node_path = false;
  68. bool is_hex_notation = false;
  69. bool is_bin_notation = false;
  70. bool expect_type = false;
  71. Color keyword_color;
  72. Color color;
  73. int in_region = text_editor->_is_line_in_region(p_line);
  74. int deregion = 0;
  75. const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
  76. const String &str = text_editor->get_line(p_line);
  77. Color prev_color;
  78. for (int j = 0; j < str.length(); j++) {
  79. TextEdit::HighlighterInfo highlighter_info;
  80. if (deregion > 0) {
  81. deregion--;
  82. if (deregion == 0) {
  83. in_region = -1;
  84. }
  85. }
  86. if (deregion != 0) {
  87. if (color != prev_color) {
  88. prev_color = color;
  89. highlighter_info.color = color;
  90. color_map[j] = highlighter_info;
  91. }
  92. continue;
  93. }
  94. color = font_color;
  95. bool is_char = _is_text_char(str[j]);
  96. bool is_symbol = _is_symbol(str[j]);
  97. bool is_number = _is_number(str[j]);
  98. // allow ABCDEF in hex notation
  99. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  100. is_number = true;
  101. } else {
  102. is_hex_notation = false;
  103. }
  104. // disallow anything not a 0 or 1
  105. if (is_bin_notation && (_is_bin_symbol(str[j]))) {
  106. is_number = true;
  107. } else if (is_bin_notation) {
  108. is_bin_notation = false;
  109. is_number = false;
  110. } else {
  111. is_bin_notation = false;
  112. }
  113. // check for dot or underscore or 'x' for hex notation in floating point number or 'e' for scientific notation
  114. if ((str[j] == '.' || str[j] == 'x' || str[j] == 'b' || str[j] == '_' || str[j] == 'e') && !in_word && prev_is_number && !is_number) {
  115. is_number = true;
  116. is_symbol = false;
  117. is_char = false;
  118. if (str[j] == 'x' && str[j - 1] == '0') {
  119. is_hex_notation = true;
  120. } else if (str[j] == 'b' && str[j - 1] == '0') {
  121. is_bin_notation = true;
  122. }
  123. }
  124. if (!in_word && _is_char(str[j]) && !is_number) {
  125. in_word = true;
  126. }
  127. if ((in_keyword || in_word) && !is_hex_notation) {
  128. is_number = false;
  129. }
  130. if (is_symbol && str[j] != '.' && in_word) {
  131. in_word = false;
  132. }
  133. if (is_symbol && cri_map.has(j)) {
  134. const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
  135. if (in_region == -1) {
  136. if (!cri.end) {
  137. in_region = cri.region;
  138. }
  139. } else {
  140. TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
  141. if (in_region == cri.region && !cr.line_only) { //ignore otherwise
  142. if (cri.end || cr.eq) {
  143. deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length();
  144. }
  145. }
  146. }
  147. }
  148. if (!is_char) {
  149. in_keyword = false;
  150. }
  151. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  152. int to = j;
  153. while (to < str.length() && _is_text_char(str[to])) {
  154. to++;
  155. }
  156. String word = str.substr(j, to - j);
  157. Color col = Color();
  158. if (text_editor->has_keyword_color(word)) {
  159. col = text_editor->get_keyword_color(word);
  160. } else if (text_editor->has_member_color(word)) {
  161. col = text_editor->get_member_color(word);
  162. }
  163. if (col != Color()) {
  164. for (int k = j - 1; k >= 0; k--) {
  165. if (str[k] == '.') {
  166. col = Color(); // keyword & member indexing not allowed
  167. break;
  168. } else if (str[k] > 32) {
  169. break;
  170. }
  171. }
  172. if (col != Color()) {
  173. in_keyword = true;
  174. keyword_color = col;
  175. }
  176. }
  177. }
  178. if (!in_function_name && in_word && !in_keyword) {
  179. int k = j;
  180. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  181. k++;
  182. }
  183. // check for space between name and bracket
  184. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  185. k++;
  186. }
  187. if (str[k] == '(') {
  188. in_function_name = true;
  189. } else if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_VAR)) {
  190. in_variable_declaration = true;
  191. }
  192. }
  193. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  194. int k = j;
  195. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  196. k--;
  197. }
  198. if (str[k] == '.') {
  199. in_member_variable = true;
  200. }
  201. }
  202. if (is_symbol) {
  203. if (in_function_name) {
  204. in_function_args = true;
  205. }
  206. if (in_function_args && str[j] == ')') {
  207. in_function_args = false;
  208. }
  209. if (expect_type && (prev_is_char || str[j] == '=')) {
  210. expect_type = false;
  211. }
  212. if (j > 0 && str[j] == '>' && str[j - 1] == '-') {
  213. expect_type = true;
  214. }
  215. if (in_variable_declaration || in_function_args) {
  216. int k = j;
  217. // Skip space
  218. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  219. k++;
  220. }
  221. if (str[k] == ':') {
  222. // has type hint
  223. expect_type = true;
  224. }
  225. }
  226. in_variable_declaration = false;
  227. in_function_name = false;
  228. in_member_variable = false;
  229. }
  230. if (!in_node_path && in_region == -1 && str[j] == '$') {
  231. in_node_path = true;
  232. } else if (in_region != -1 || (is_symbol && str[j] != '/')) {
  233. in_node_path = false;
  234. }
  235. if (in_region >= 0) {
  236. next_type = REGION;
  237. color = text_editor->_get_color_region(in_region).color;
  238. } else if (in_node_path) {
  239. next_type = NODE_PATH;
  240. color = node_path_color;
  241. } else if (in_keyword) {
  242. next_type = KEYWORD;
  243. color = keyword_color;
  244. } else if (in_member_variable) {
  245. next_type = MEMBER;
  246. color = member_color;
  247. } else if (in_function_name) {
  248. next_type = FUNCTION;
  249. if (previous_text == GDScriptTokenizer::get_token_name(GDScriptTokenizer::TK_PR_FUNCTION)) {
  250. color = function_definition_color;
  251. } else {
  252. color = function_color;
  253. }
  254. } else if (is_symbol) {
  255. next_type = SYMBOL;
  256. color = symbol_color;
  257. } else if (is_number) {
  258. next_type = NUMBER;
  259. color = number_color;
  260. } else if (expect_type) {
  261. next_type = TYPE;
  262. color = type_color;
  263. } else {
  264. next_type = IDENTIFIER;
  265. }
  266. if (next_type != current_type) {
  267. if (current_type == NONE) {
  268. current_type = next_type;
  269. } else {
  270. previous_type = current_type;
  271. current_type = next_type;
  272. // no need to store regions...
  273. if (previous_type == REGION) {
  274. previous_text = "";
  275. previous_column = j;
  276. } else {
  277. String text = str.substr(previous_column, j - previous_column).strip_edges();
  278. previous_column = j;
  279. // ignore if just whitespace
  280. if (text != "") {
  281. previous_text = text;
  282. }
  283. }
  284. }
  285. }
  286. prev_is_char = is_char;
  287. prev_is_number = is_number;
  288. if (color != prev_color) {
  289. prev_color = color;
  290. highlighter_info.color = color;
  291. color_map[j] = highlighter_info;
  292. }
  293. }
  294. return color_map;
  295. }
  296. String GDScriptSyntaxHighlighter::get_name() const {
  297. return "GDScript";
  298. }
  299. List<String> GDScriptSyntaxHighlighter::get_supported_languages() {
  300. List<String> languages;
  301. languages.push_back("GDScript");
  302. return languages;
  303. }
  304. void GDScriptSyntaxHighlighter::_update_cache() {
  305. font_color = text_editor->get_color("font_color");
  306. symbol_color = text_editor->get_color("symbol_color");
  307. function_color = text_editor->get_color("function_color");
  308. number_color = text_editor->get_color("number_color");
  309. member_color = text_editor->get_color("member_variable_color");
  310. const String text_editor_color_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
  311. const bool default_theme = text_editor_color_theme == "Default";
  312. if (default_theme || EditorSettings::get_singleton()->is_dark_theme()) {
  313. function_definition_color = Color(0.4, 0.9, 1.0);
  314. node_path_color = Color(0.39, 0.76, 0.35);
  315. } else {
  316. function_definition_color = Color(0.0, 0.65, 0.73);
  317. node_path_color = Color(0.32, 0.55, 0.29);
  318. }
  319. EDITOR_DEF("text_editor/highlighting/gdscript/function_definition_color", function_definition_color);
  320. EDITOR_DEF("text_editor/highlighting/gdscript/node_path_color", node_path_color);
  321. if (text_editor_color_theme == "Adaptive" || default_theme) {
  322. EditorSettings::get_singleton()->set_initial_value(
  323. "text_editor/highlighting/gdscript/function_definition_color",
  324. function_definition_color,
  325. true);
  326. EditorSettings::get_singleton()->set_initial_value(
  327. "text_editor/highlighting/gdscript/node_path_color",
  328. node_path_color,
  329. true);
  330. }
  331. function_definition_color = EDITOR_GET("text_editor/highlighting/gdscript/function_definition_color");
  332. node_path_color = EDITOR_GET("text_editor/highlighting/gdscript/node_path_color");
  333. type_color = EDITOR_GET("text_editor/highlighting/base_type_color");
  334. }
  335. SyntaxHighlighter *GDScriptSyntaxHighlighter::create() {
  336. return memnew(GDScriptSyntaxHighlighter);
  337. }