gdscript_highlighter.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 "scene/gui/text_edit.h"
  32. inline bool _is_symbol(CharType c) {
  33. return is_symbol(c);
  34. }
  35. static bool _is_text_char(CharType c) {
  36. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  37. }
  38. static bool _is_whitespace(CharType c) {
  39. return c == '\t' || c == ' ';
  40. }
  41. static bool _is_char(CharType c) {
  42. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  43. }
  44. static bool _is_number(CharType c) {
  45. return (c >= '0' && c <= '9');
  46. }
  47. static bool _is_hex_symbol(CharType c) {
  48. return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
  49. }
  50. Map<int, TextEdit::HighlighterInfo> GDScriptSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
  51. Map<int, TextEdit::HighlighterInfo> color_map;
  52. Type next_type = NONE;
  53. Type current_type = NONE;
  54. Type previous_type = NONE;
  55. String previous_text = "";
  56. int previous_column = 0;
  57. bool prev_is_char = false;
  58. bool prev_is_number = false;
  59. bool in_keyword = false;
  60. bool in_word = false;
  61. bool in_function_name = false;
  62. bool in_member_variable = false;
  63. bool is_hex_notation = false;
  64. Color keyword_color;
  65. Color color;
  66. int in_region = text_editor->_is_line_in_region(p_line);
  67. int deregion = 0;
  68. const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
  69. const String &str = text_editor->get_line(p_line);
  70. Color prev_color;
  71. for (int j = 0; j < str.length(); j++) {
  72. TextEdit::HighlighterInfo highlighter_info;
  73. if (deregion > 0) {
  74. deregion--;
  75. if (deregion == 0) {
  76. in_region = -1;
  77. }
  78. }
  79. if (deregion != 0) {
  80. if (color != prev_color) {
  81. prev_color = color;
  82. highlighter_info.color = color;
  83. color_map[j] = highlighter_info;
  84. }
  85. continue;
  86. }
  87. color = font_color;
  88. bool is_char = _is_text_char(str[j]);
  89. bool is_symbol = _is_symbol(str[j]);
  90. bool is_number = _is_number(str[j]);
  91. // allow ABCDEF in hex notation
  92. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  93. is_number = true;
  94. } else {
  95. is_hex_notation = false;
  96. }
  97. // check for dot or underscore or 'x' for hex notation in floating point number
  98. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) {
  99. is_number = true;
  100. is_symbol = false;
  101. is_char = false;
  102. if (str[j] == 'x' && str[j - 1] == '0') {
  103. is_hex_notation = true;
  104. }
  105. }
  106. if (!in_word && _is_char(str[j]) && !is_number) {
  107. in_word = true;
  108. }
  109. if ((in_keyword || in_word) && !is_hex_notation) {
  110. is_number = false;
  111. }
  112. if (is_symbol && str[j] != '.' && in_word) {
  113. in_word = false;
  114. }
  115. if (is_symbol && cri_map.has(j)) {
  116. const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
  117. if (in_region == -1) {
  118. if (!cri.end) {
  119. in_region = cri.region;
  120. }
  121. } else {
  122. TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
  123. if (in_region == cri.region && !cr.line_only) { //ignore otherwise
  124. if (cri.end || cr.eq) {
  125. deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length();
  126. }
  127. }
  128. }
  129. }
  130. if (!is_char) {
  131. in_keyword = false;
  132. }
  133. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  134. int to = j;
  135. while (to < str.length() && _is_text_char(str[to]))
  136. to++;
  137. String word = str.substr(j, to - j);
  138. Color col = Color();
  139. if (text_editor->has_keyword_color(word)) {
  140. col = text_editor->get_keyword_color(word);
  141. } else if (text_editor->has_member_color(word)) {
  142. col = text_editor->get_member_color(word);
  143. for (int k = j - 1; k >= 0; k--) {
  144. if (str[k] == '.') {
  145. col = Color(); //member indexing not allowed
  146. break;
  147. } else if (str[k] > 32) {
  148. break;
  149. }
  150. }
  151. }
  152. if (col != Color()) {
  153. in_keyword = true;
  154. keyword_color = col;
  155. }
  156. }
  157. if (!in_function_name && in_word && !in_keyword) {
  158. int k = j;
  159. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  160. k++;
  161. }
  162. // check for space between name and bracket
  163. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  164. k++;
  165. }
  166. if (str[k] == '(') {
  167. in_function_name = true;
  168. }
  169. }
  170. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  171. int k = j;
  172. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  173. k--;
  174. }
  175. if (str[k] == '.') {
  176. in_member_variable = true;
  177. }
  178. }
  179. if (is_symbol) {
  180. in_function_name = false;
  181. in_member_variable = false;
  182. }
  183. if (in_region >= 0) {
  184. next_type = REGION;
  185. color = text_editor->_get_color_region(in_region).color;
  186. } else if (in_keyword) {
  187. next_type = KEYWORD;
  188. color = keyword_color;
  189. } else if (in_member_variable) {
  190. next_type = MEMBER;
  191. color = member_color;
  192. } else if (in_function_name) {
  193. next_type = FUNCTION;
  194. color = function_color;
  195. } else if (is_symbol) {
  196. next_type = SYMBOL;
  197. color = symbol_color;
  198. } else if (is_number) {
  199. next_type = NUMBER;
  200. color = number_color;
  201. } else {
  202. next_type = IDENTIFIER;
  203. }
  204. if (next_type != current_type) {
  205. if (current_type == NONE) {
  206. current_type = next_type;
  207. } else {
  208. previous_type = current_type;
  209. current_type = next_type;
  210. // no need to store regions...
  211. if (previous_type == REGION) {
  212. previous_text = "";
  213. previous_column = j;
  214. } else {
  215. String text = str.substr(previous_column, j - previous_column).strip_edges();
  216. previous_column = j;
  217. // ignore if just whitespace
  218. if (text != "") {
  219. previous_text = text;
  220. }
  221. }
  222. }
  223. }
  224. prev_is_char = is_char;
  225. prev_is_number = is_number;
  226. if (color != prev_color) {
  227. prev_color = color;
  228. highlighter_info.color = color;
  229. color_map[j] = highlighter_info;
  230. }
  231. }
  232. return color_map;
  233. }
  234. String GDScriptSyntaxHighlighter::get_name() {
  235. return "GDScript";
  236. }
  237. List<String> GDScriptSyntaxHighlighter::get_supported_languages() {
  238. List<String> languages;
  239. languages.push_back("GDScript");
  240. return languages;
  241. }
  242. void GDScriptSyntaxHighlighter::_update_cache() {
  243. font_color = text_editor->get_color("font_color");
  244. symbol_color = text_editor->get_color("symbol_color");
  245. function_color = text_editor->get_color("function_color");
  246. number_color = text_editor->get_color("number_color");
  247. member_color = text_editor->get_color("member_variable_color");
  248. }
  249. SyntaxHighlighter *GDScriptSyntaxHighlighter::create() {
  250. return memnew(GDScriptSyntaxHighlighter);
  251. }