code_edit.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**************************************************************************/
  2. /* code_edit.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef CODE_EDIT_H
  31. #define CODE_EDIT_H
  32. #include "scene/gui/text_edit.h"
  33. class CodeEdit : public TextEdit {
  34. GDCLASS(CodeEdit, TextEdit)
  35. public:
  36. /* Keep enum in sync with: */
  37. /* /core/object/script_language.h - ScriptLanguage::CodeCompletionKind */
  38. enum CodeCompletionKind {
  39. KIND_CLASS,
  40. KIND_FUNCTION,
  41. KIND_SIGNAL,
  42. KIND_VARIABLE,
  43. KIND_MEMBER,
  44. KIND_ENUM,
  45. KIND_CONSTANT,
  46. KIND_NODE_PATH,
  47. KIND_FILE_PATH,
  48. KIND_PLAIN_TEXT,
  49. };
  50. private:
  51. /* Indent management */
  52. int indent_size = 4;
  53. String indent_text = "\t";
  54. bool auto_indent = false;
  55. HashSet<char32_t> auto_indent_prefixes;
  56. bool indent_using_spaces = false;
  57. int _calculate_spaces_till_next_left_indent(int p_column) const;
  58. int _calculate_spaces_till_next_right_indent(int p_column) const;
  59. void _new_line(bool p_split_current_line = true, bool p_above = false);
  60. /* Auto brace completion */
  61. bool auto_brace_completion_enabled = false;
  62. /* BracePair open_key must be uniquie and ordered by length. */
  63. struct BracePair {
  64. String open_key = "";
  65. String close_key = "";
  66. };
  67. Vector<BracePair> auto_brace_completion_pairs;
  68. int _get_auto_brace_pair_open_at_pos(int p_line, int p_col);
  69. int _get_auto_brace_pair_close_at_pos(int p_line, int p_col);
  70. /* Main Gutter */
  71. enum MainGutterType {
  72. MAIN_GUTTER_BREAKPOINT = 0x01,
  73. MAIN_GUTTER_BOOKMARK = 0x02,
  74. MAIN_GUTTER_EXECUTING = 0x04
  75. };
  76. int main_gutter = -1;
  77. void _update_draw_main_gutter();
  78. void _main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);
  79. // breakpoints
  80. HashMap<int, bool> breakpointed_lines;
  81. bool draw_breakpoints = false;
  82. Color breakpoint_color = Color(1, 1, 1);
  83. Ref<Texture2D> breakpoint_icon = Ref<Texture2D>();
  84. // bookmarks
  85. bool draw_bookmarks = false;
  86. Color bookmark_color = Color(1, 1, 1);
  87. Ref<Texture2D> bookmark_icon = Ref<Texture2D>();
  88. // executing lines
  89. bool draw_executing_lines = false;
  90. Color executing_line_color = Color(1, 1, 1);
  91. Ref<Texture2D> executing_line_icon = Ref<Texture2D>();
  92. /* Line numbers */
  93. int line_number_gutter = -1;
  94. int line_number_digits = 0;
  95. String line_number_padding = " ";
  96. Color line_number_color = Color(1, 1, 1);
  97. void _line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);
  98. /* Fold Gutter */
  99. int fold_gutter = -1;
  100. bool draw_fold_gutter = false;
  101. Color folding_color = Color(1, 1, 1);
  102. Ref<Texture2D> can_fold_icon = Ref<Texture2D>();
  103. Ref<Texture2D> folded_icon = Ref<Texture2D>();
  104. void _fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region);
  105. void _gutter_clicked(int p_line, int p_gutter);
  106. void _update_gutter_indexes();
  107. /* Line Folding */
  108. bool line_folding_enabled = false;
  109. /* Delimiters */
  110. enum DelimiterType {
  111. TYPE_STRING,
  112. TYPE_COMMENT,
  113. };
  114. struct Delimiter {
  115. DelimiterType type;
  116. String start_key = "";
  117. String end_key = "";
  118. bool line_only = true;
  119. };
  120. bool setting_delimiters = false;
  121. Vector<Delimiter> delimiters;
  122. /*
  123. * Vector entry per line, contains a Map of column numbers to delimiter index, -1 marks the end of a region.
  124. * e.g the following text will be stored as so:
  125. *
  126. * 0: nothing here
  127. * 1:
  128. * 2: # test
  129. * 3: "test" text "multiline
  130. * 4:
  131. * 5: test
  132. * 6: string"
  133. *
  134. * Vector [
  135. * 0 = []
  136. * 1 = []
  137. * 2 = [
  138. * 1 = 1
  139. * 6 = -1
  140. * ]
  141. * 3 = [
  142. * 1 = 0
  143. * 6 = -1
  144. * 13 = 0
  145. * ]
  146. * 4 = [
  147. * 0 = 0
  148. * ]
  149. * 5 = [
  150. * 5 = 0
  151. * ]
  152. * 6 = [
  153. * 7 = -1
  154. * ]
  155. * ]
  156. */
  157. Vector<RBMap<int, int>> delimiter_cache;
  158. void _update_delimiter_cache(int p_from_line = 0, int p_to_line = -1);
  159. int _is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const;
  160. void _add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type);
  161. void _remove_delimiter(const String &p_start_key, DelimiterType p_type);
  162. bool _has_delimiter(const String &p_start_key, DelimiterType p_type) const;
  163. void _set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type);
  164. void _clear_delimiters(DelimiterType p_type);
  165. TypedArray<String> _get_delimiters(DelimiterType p_type) const;
  166. /* Code Hint */
  167. String code_hint = "";
  168. bool code_hint_draw_below = true;
  169. int code_hint_xpos = -0xFFFF;
  170. /* Code Completion */
  171. bool code_completion_enabled = false;
  172. bool code_completion_forced = false;
  173. int code_completion_max_width = 0;
  174. int code_completion_max_lines = 7;
  175. int code_completion_scroll_width = 0;
  176. Color code_completion_scroll_color = Color(0, 0, 0, 0);
  177. Color code_completion_scroll_hovered_color = Color(0, 0, 0, 0);
  178. Color code_completion_background_color = Color(0, 0, 0, 0);
  179. Color code_completion_selected_color = Color(0, 0, 0, 0);
  180. Color code_completion_existing_color = Color(0, 0, 0, 0);
  181. bool code_completion_active = false;
  182. bool is_code_completion_scroll_hovered = false;
  183. bool is_code_completion_scroll_pressed = false;
  184. Vector<ScriptLanguage::CodeCompletionOption> code_completion_options;
  185. int code_completion_line_ofs = 0;
  186. int code_completion_current_selected = 0;
  187. int code_completion_force_item_center = -1;
  188. int code_completion_longest_line = 0;
  189. Rect2i code_completion_rect;
  190. Rect2i code_completion_scroll_rect;
  191. HashSet<char32_t> code_completion_prefixes;
  192. List<ScriptLanguage::CodeCompletionOption> code_completion_option_submitted;
  193. List<ScriptLanguage::CodeCompletionOption> code_completion_option_sources;
  194. String code_completion_base;
  195. void _update_scroll_selected_line(float p_mouse_y);
  196. void _filter_code_completion_candidates_impl();
  197. /* Line length guidelines */
  198. TypedArray<int> line_length_guideline_columns;
  199. Color line_length_guideline_color;
  200. /* Symbol lookup */
  201. bool symbol_lookup_on_click_enabled = false;
  202. String symbol_lookup_new_word = "";
  203. String symbol_lookup_word = "";
  204. Point2i symbol_lookup_pos;
  205. /* Visual */
  206. Ref<StyleBox> style_normal;
  207. Ref<Font> font;
  208. int font_size = 16;
  209. int line_spacing = 1;
  210. /* Callbacks */
  211. int lines_edited_changed = 0;
  212. int lines_edited_from = -1;
  213. int lines_edited_to = -1;
  214. void _lines_edited_from(int p_from_line, int p_to_line);
  215. void _text_set();
  216. void _text_changed();
  217. protected:
  218. void _notification(int p_what);
  219. static void _bind_methods();
  220. /* Text manipulation */
  221. // Overridable actions
  222. virtual void _handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) override;
  223. virtual void _backspace_internal(int p_caret) override;
  224. GDVIRTUAL1(_confirm_code_completion, bool)
  225. GDVIRTUAL1(_request_code_completion, bool)
  226. GDVIRTUAL1RC(TypedArray<Dictionary>, _filter_code_completion_candidates, TypedArray<Dictionary>)
  227. public:
  228. /* General overrides */
  229. virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
  230. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  231. /* Indent management */
  232. void set_indent_size(const int p_size);
  233. int get_indent_size() const;
  234. void set_indent_using_spaces(const bool p_use_spaces);
  235. bool is_indent_using_spaces() const;
  236. void set_auto_indent_enabled(bool p_enabled);
  237. bool is_auto_indent_enabled() const;
  238. void set_auto_indent_prefixes(const TypedArray<String> &p_prefixes);
  239. TypedArray<String> get_auto_indent_prefixes() const;
  240. void do_indent();
  241. void indent_lines();
  242. void unindent_lines();
  243. /* Auto brace completion */
  244. void set_auto_brace_completion_enabled(bool p_enabled);
  245. bool is_auto_brace_completion_enabled() const;
  246. void set_highlight_matching_braces_enabled(bool p_enabled);
  247. bool is_highlight_matching_braces_enabled() const;
  248. void add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key);
  249. void set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs);
  250. Dictionary get_auto_brace_completion_pairs() const;
  251. bool has_auto_brace_completion_open_key(const String &p_open_key) const;
  252. bool has_auto_brace_completion_close_key(const String &p_close_key) const;
  253. String get_auto_brace_completion_close_key(const String &p_open_key) const;
  254. /* Main Gutter */
  255. void set_draw_breakpoints_gutter(bool p_draw);
  256. bool is_drawing_breakpoints_gutter() const;
  257. void set_draw_bookmarks_gutter(bool p_draw);
  258. bool is_drawing_bookmarks_gutter() const;
  259. void set_draw_executing_lines_gutter(bool p_draw);
  260. bool is_drawing_executing_lines_gutter() const;
  261. // breakpoints
  262. void set_line_as_breakpoint(int p_line, bool p_breakpointed);
  263. bool is_line_breakpointed(int p_line) const;
  264. void clear_breakpointed_lines();
  265. PackedInt32Array get_breakpointed_lines() const;
  266. // bookmarks
  267. void set_line_as_bookmarked(int p_line, bool p_bookmarked);
  268. bool is_line_bookmarked(int p_line) const;
  269. void clear_bookmarked_lines();
  270. PackedInt32Array get_bookmarked_lines() const;
  271. // executing lines
  272. void set_line_as_executing(int p_line, bool p_executing);
  273. bool is_line_executing(int p_line) const;
  274. void clear_executing_lines();
  275. PackedInt32Array get_executing_lines() const;
  276. /* Line numbers */
  277. void set_draw_line_numbers(bool p_draw);
  278. bool is_draw_line_numbers_enabled() const;
  279. void set_line_numbers_zero_padded(bool p_zero_padded);
  280. bool is_line_numbers_zero_padded() const;
  281. /* Fold gutter */
  282. void set_draw_fold_gutter(bool p_draw);
  283. bool is_drawing_fold_gutter() const;
  284. /* Line Folding */
  285. void set_line_folding_enabled(bool p_enabled);
  286. bool is_line_folding_enabled() const;
  287. bool can_fold_line(int p_line) const;
  288. void fold_line(int p_line);
  289. void unfold_line(int p_line);
  290. void fold_all_lines();
  291. void unfold_all_lines();
  292. void toggle_foldable_line(int p_line);
  293. bool is_line_folded(int p_line) const;
  294. TypedArray<int> get_folded_lines() const;
  295. /* Delimiters */
  296. void add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
  297. void remove_string_delimiter(const String &p_start_key);
  298. bool has_string_delimiter(const String &p_start_key) const;
  299. void set_string_delimiters(const TypedArray<String> &p_string_delimiters);
  300. void clear_string_delimiters();
  301. TypedArray<String> get_string_delimiters() const;
  302. int is_in_string(int p_line, int p_column = -1) const;
  303. void add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
  304. void remove_comment_delimiter(const String &p_start_key);
  305. bool has_comment_delimiter(const String &p_start_key) const;
  306. void set_comment_delimiters(const TypedArray<String> &p_comment_delimiters);
  307. void clear_comment_delimiters();
  308. TypedArray<String> get_comment_delimiters() const;
  309. int is_in_comment(int p_line, int p_column = -1) const;
  310. String get_delimiter_start_key(int p_delimiter_idx) const;
  311. String get_delimiter_end_key(int p_delimiter_idx) const;
  312. Point2 get_delimiter_start_position(int p_line, int p_column) const;
  313. Point2 get_delimiter_end_position(int p_line, int p_column) const;
  314. /* Code hint */
  315. void set_code_hint(const String &p_hint);
  316. void set_code_hint_draw_below(bool p_below);
  317. /* Code Completion */
  318. void set_code_completion_enabled(bool p_enable);
  319. bool is_code_completion_enabled() const;
  320. void set_code_completion_prefixes(const TypedArray<String> &p_prefixes);
  321. TypedArray<String> get_code_completion_prefixes() const;
  322. String get_text_for_code_completion() const;
  323. void request_code_completion(bool p_force = false);
  324. void add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color = Color(1, 1, 1), const Ref<Resource> &p_icon = Ref<Resource>(), const Variant &p_value = Variant::NIL);
  325. void update_code_completion_options(bool p_forced = false);
  326. TypedArray<Dictionary> get_code_completion_options() const;
  327. Dictionary get_code_completion_option(int p_index) const;
  328. int get_code_completion_selected_index() const;
  329. void set_code_completion_selected_index(int p_index);
  330. void confirm_code_completion(bool p_replace = false);
  331. void cancel_code_completion();
  332. /* Line length guidelines */
  333. void set_line_length_guidelines(TypedArray<int> p_guideline_columns);
  334. TypedArray<int> get_line_length_guidelines() const;
  335. /* Symbol lookup */
  336. void set_symbol_lookup_on_click_enabled(bool p_enabled);
  337. bool is_symbol_lookup_on_click_enabled() const;
  338. String get_text_for_symbol_lookup();
  339. void set_symbol_lookup_word_as_valid(bool p_valid);
  340. CodeEdit();
  341. ~CodeEdit();
  342. };
  343. VARIANT_ENUM_CAST(CodeEdit::CodeCompletionKind);
  344. #endif // CODE_EDIT_H