gdscript_highlighter.cpp 11 KB

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