gdscript_highlighter.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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> GDSyntaxHighlighter::_get_line_syntax_highlighting(int p_line) {
  51. Map<int, TextEdit::HighlighterInfo> color_map;
  52. bool prev_is_char = false;
  53. bool prev_is_number = false;
  54. bool in_keyword = false;
  55. bool in_word = false;
  56. bool in_function_name = false;
  57. bool in_member_variable = false;
  58. bool is_hex_notation = false;
  59. Color keyword_color;
  60. Color color;
  61. int in_region = -1;
  62. int deregion = 0;
  63. for (int i = 0; i < p_line; i++) {
  64. int ending_color_region = text_editor->_get_line_ending_color_region(i);
  65. if (in_region == -1) {
  66. in_region = ending_color_region;
  67. } else if (in_region == ending_color_region) {
  68. in_region = -1;
  69. } else {
  70. const Map<int, TextEdit::Text::ColorRegionInfo> &cri_map = text_editor->_get_line_color_region_info(i);
  71. for (const Map<int, TextEdit::Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
  72. const TextEdit::Text::ColorRegionInfo &cri = E->get();
  73. if (cri.region == in_region) {
  74. in_region = -1;
  75. }
  76. }
  77. }
  78. }
  79. const Map<int, TextEdit::Text::ColorRegionInfo> cri_map = text_editor->_get_line_color_region_info(p_line);
  80. const String &str = text_editor->get_line(p_line);
  81. Color prev_color;
  82. for (int j = 0; j < str.length(); j++) {
  83. TextEdit::HighlighterInfo highlighter_info;
  84. if (deregion > 0) {
  85. deregion--;
  86. if (deregion == 0) {
  87. in_region = -1;
  88. }
  89. }
  90. if (deregion != 0) {
  91. if (color != prev_color) {
  92. prev_color = color;
  93. highlighter_info.color = color;
  94. color_map[j] = highlighter_info;
  95. }
  96. continue;
  97. }
  98. color = font_color;
  99. bool is_char = _is_text_char(str[j]);
  100. bool is_symbol = _is_symbol(str[j]);
  101. bool is_number = _is_number(str[j]);
  102. // allow ABCDEF in hex notation
  103. if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
  104. is_number = true;
  105. } else {
  106. is_hex_notation = false;
  107. }
  108. // check for dot or underscore or 'x' for hex notation in floating point number
  109. if ((str[j] == '.' || str[j] == 'x' || str[j] == '_') && !in_word && prev_is_number && !is_number) {
  110. is_number = true;
  111. is_symbol = false;
  112. is_char = false;
  113. if (str[j] == 'x' && str[j - 1] == '0') {
  114. is_hex_notation = true;
  115. }
  116. }
  117. if (!in_word && _is_char(str[j]) && !is_number) {
  118. in_word = true;
  119. }
  120. if ((in_keyword || in_word) && !is_hex_notation) {
  121. is_number = false;
  122. }
  123. if (is_symbol && str[j] != '.' && in_word) {
  124. in_word = false;
  125. }
  126. if (is_symbol && cri_map.has(j)) {
  127. const TextEdit::Text::ColorRegionInfo &cri = cri_map[j];
  128. if (in_region == -1) {
  129. if (!cri.end) {
  130. in_region = cri.region;
  131. }
  132. } else {
  133. TextEdit::ColorRegion cr = text_editor->_get_color_region(cri.region);
  134. if (in_region == cri.region && !cr.line_only) { //ignore otherwise
  135. if (cri.end || cr.eq) {
  136. deregion = cr.eq ? cr.begin_key.length() : cr.end_key.length();
  137. }
  138. }
  139. }
  140. }
  141. if (!is_char) {
  142. in_keyword = false;
  143. }
  144. if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
  145. int to = j;
  146. while (to < str.length() && _is_text_char(str[to]))
  147. to++;
  148. String word = str.substr(j, to - j);
  149. Color col = Color();
  150. if (text_editor->has_keyword_color(word)) {
  151. col = text_editor->get_keyword_color(word);
  152. } else if (text_editor->has_member_color(word)) {
  153. col = text_editor->get_member_color(word);
  154. for (int k = j - 1; k >= 0; k--) {
  155. if (str[k] == '.') {
  156. col = Color(); //member indexing not allowed
  157. break;
  158. } else if (str[k] > 32) {
  159. break;
  160. }
  161. }
  162. }
  163. if (col != Color()) {
  164. in_keyword = true;
  165. keyword_color = col;
  166. }
  167. }
  168. if (!in_function_name && in_word && !in_keyword) {
  169. int k = j;
  170. while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  171. k++;
  172. }
  173. // check for space between name and bracket
  174. while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
  175. k++;
  176. }
  177. if (str[k] == '(') {
  178. in_function_name = true;
  179. }
  180. }
  181. if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
  182. int k = j;
  183. while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
  184. k--;
  185. }
  186. if (str[k] == '.') {
  187. in_member_variable = true;
  188. }
  189. }
  190. if (is_symbol) {
  191. in_function_name = false;
  192. in_member_variable = false;
  193. }
  194. if (in_region >= 0)
  195. color = text_editor->_get_color_region(in_region).color;
  196. else if (in_keyword)
  197. color = keyword_color;
  198. else if (in_member_variable)
  199. color = member_color;
  200. else if (in_function_name)
  201. color = function_color;
  202. else if (is_symbol)
  203. color = symbol_color;
  204. else if (is_number)
  205. color = number_color;
  206. prev_is_char = is_char;
  207. prev_is_number = is_number;
  208. if (color != prev_color) {
  209. prev_color = color;
  210. highlighter_info.color = color;
  211. color_map[j] = highlighter_info;
  212. }
  213. }
  214. return color_map;
  215. }
  216. String GDSyntaxHighlighter::get_name() {
  217. return "GDScript";
  218. }
  219. List<String> GDSyntaxHighlighter::get_supported_languages() {
  220. List<String> languages;
  221. languages.push_back("GDScript");
  222. return languages;
  223. }
  224. void GDSyntaxHighlighter::_update_cache() {
  225. font_color = text_editor->get_color("font_color");
  226. symbol_color = text_editor->get_color("symbol_color");
  227. function_color = text_editor->get_color("function_color");
  228. number_color = text_editor->get_color("number_color");
  229. member_color = text_editor->get_color("member_variable_color");
  230. }
  231. SyntaxHighlighter *GDSyntaxHighlighter::create() {
  232. return memnew(GDSyntaxHighlighter);
  233. }