gdscript_highlighter.cpp 11 KB

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