code_edit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. // bookmarks
  83. bool draw_bookmarks = false;
  84. // executing lines
  85. bool draw_executing_lines = false;
  86. /* Line numbers */
  87. int line_number_gutter = -1;
  88. int line_number_digits = 1;
  89. String line_number_padding = " ";
  90. void _line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);
  91. /* Fold Gutter */
  92. int fold_gutter = -1;
  93. bool draw_fold_gutter = false;
  94. void _fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region);
  95. void _gutter_clicked(int p_line, int p_gutter);
  96. void _update_gutter_indexes();
  97. /* Line Folding */
  98. bool line_folding_enabled = false;
  99. /* Delimiters */
  100. enum DelimiterType {
  101. TYPE_STRING,
  102. TYPE_COMMENT,
  103. };
  104. struct Delimiter {
  105. DelimiterType type;
  106. String start_key = "";
  107. String end_key = "";
  108. bool line_only = true;
  109. };
  110. bool setting_delimiters = false;
  111. Vector<Delimiter> delimiters;
  112. /*
  113. * Vector entry per line, contains a Map of column numbers to delimiter index, -1 marks the end of a region.
  114. * e.g the following text will be stored as so:
  115. *
  116. * 0: nothing here
  117. * 1:
  118. * 2: # test
  119. * 3: "test" text "multiline
  120. * 4:
  121. * 5: test
  122. * 6: string"
  123. *
  124. * Vector [
  125. * 0 = []
  126. * 1 = []
  127. * 2 = [
  128. * 1 = 1
  129. * 6 = -1
  130. * ]
  131. * 3 = [
  132. * 1 = 0
  133. * 6 = -1
  134. * 13 = 0
  135. * ]
  136. * 4 = [
  137. * 0 = 0
  138. * ]
  139. * 5 = [
  140. * 5 = 0
  141. * ]
  142. * 6 = [
  143. * 7 = -1
  144. * ]
  145. * ]
  146. */
  147. Vector<RBMap<int, int>> delimiter_cache;
  148. void _update_delimiter_cache(int p_from_line = 0, int p_to_line = -1);
  149. int _is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const;
  150. void _add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type);
  151. void _remove_delimiter(const String &p_start_key, DelimiterType p_type);
  152. bool _has_delimiter(const String &p_start_key, DelimiterType p_type) const;
  153. void _set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type);
  154. void _clear_delimiters(DelimiterType p_type);
  155. TypedArray<String> _get_delimiters(DelimiterType p_type) const;
  156. /* Code Hint */
  157. String code_hint = "";
  158. bool code_hint_draw_below = true;
  159. int code_hint_xpos = -0xFFFF;
  160. /* Code Completion */
  161. bool code_completion_enabled = false;
  162. bool code_completion_forced = false;
  163. bool code_completion_active = false;
  164. bool is_code_completion_scroll_hovered = false;
  165. bool is_code_completion_scroll_pressed = false;
  166. bool is_code_completion_drag_started = false;
  167. Vector<ScriptLanguage::CodeCompletionOption> code_completion_options;
  168. int code_completion_line_ofs = 0;
  169. int code_completion_current_selected = 0;
  170. int code_completion_force_item_center = -1;
  171. int code_completion_longest_line = 0;
  172. Rect2i code_completion_rect;
  173. Rect2i code_completion_scroll_rect;
  174. HashSet<char32_t> code_completion_prefixes;
  175. List<ScriptLanguage::CodeCompletionOption> code_completion_option_submitted;
  176. List<ScriptLanguage::CodeCompletionOption> code_completion_option_sources;
  177. String code_completion_base;
  178. void _update_scroll_selected_line(float p_mouse_y);
  179. void _filter_code_completion_candidates_impl();
  180. /* Line length guidelines */
  181. TypedArray<int> line_length_guideline_columns;
  182. /* Symbol lookup */
  183. bool symbol_lookup_on_click_enabled = false;
  184. String symbol_lookup_new_word = "";
  185. String symbol_lookup_word = "";
  186. Point2i symbol_lookup_pos;
  187. /* Visual */
  188. struct ThemeCache {
  189. /* Gutters */
  190. Color code_folding_color = Color(1, 1, 1);
  191. Ref<Texture2D> can_fold_icon;
  192. Ref<Texture2D> folded_icon;
  193. Ref<Texture2D> folded_eol_icon;
  194. Color breakpoint_color = Color(1, 1, 1);
  195. Ref<Texture2D> breakpoint_icon = Ref<Texture2D>();
  196. Color bookmark_color = Color(1, 1, 1);
  197. Ref<Texture2D> bookmark_icon = Ref<Texture2D>();
  198. Color executing_line_color = Color(1, 1, 1);
  199. Ref<Texture2D> executing_line_icon = Ref<Texture2D>();
  200. Color line_number_color = Color(1, 1, 1);
  201. /* Code Completion */
  202. Ref<StyleBox> code_completion_style;
  203. int code_completion_icon_separation = 0;
  204. int code_completion_max_width = 0;
  205. int code_completion_max_lines = 7;
  206. int code_completion_scroll_width = 0;
  207. Color code_completion_scroll_color = Color(0, 0, 0, 0);
  208. Color code_completion_scroll_hovered_color = Color(0, 0, 0, 0);
  209. Color code_completion_background_color = Color(0, 0, 0, 0);
  210. Color code_completion_selected_color = Color(0, 0, 0, 0);
  211. Color code_completion_existing_color = Color(0, 0, 0, 0);
  212. /* Code hint */
  213. Ref<StyleBox> code_hint_style;
  214. Color code_hint_color;
  215. /* Line length guideline */
  216. Color line_length_guideline_color;
  217. /* Other visuals */
  218. Ref<StyleBox> style_normal;
  219. Ref<Font> font;
  220. int font_size = 16;
  221. int line_spacing = 1;
  222. } theme_cache;
  223. /* Callbacks */
  224. int lines_edited_changed = 0;
  225. int lines_edited_from = -1;
  226. int lines_edited_to = -1;
  227. void _lines_edited_from(int p_from_line, int p_to_line);
  228. void _text_set();
  229. void _text_changed();
  230. protected:
  231. void _notification(int p_what);
  232. static void _bind_methods();
  233. virtual void _update_theme_item_cache() override;
  234. /* Text manipulation */
  235. // Overridable actions
  236. virtual void _handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) override;
  237. virtual void _backspace_internal(int p_caret) override;
  238. GDVIRTUAL1(_confirm_code_completion, bool)
  239. GDVIRTUAL1(_request_code_completion, bool)
  240. GDVIRTUAL1RC(TypedArray<Dictionary>, _filter_code_completion_candidates, TypedArray<Dictionary>)
  241. public:
  242. /* General overrides */
  243. virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
  244. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  245. /* Indent management */
  246. void set_indent_size(const int p_size);
  247. int get_indent_size() const;
  248. void set_indent_using_spaces(const bool p_use_spaces);
  249. bool is_indent_using_spaces() const;
  250. void set_auto_indent_enabled(bool p_enabled);
  251. bool is_auto_indent_enabled() const;
  252. void set_auto_indent_prefixes(const TypedArray<String> &p_prefixes);
  253. TypedArray<String> get_auto_indent_prefixes() const;
  254. void do_indent();
  255. void indent_lines();
  256. void unindent_lines();
  257. void convert_indent(int p_from_line = -1, int p_to_line = -1);
  258. /* Auto brace completion */
  259. void set_auto_brace_completion_enabled(bool p_enabled);
  260. bool is_auto_brace_completion_enabled() const;
  261. void set_highlight_matching_braces_enabled(bool p_enabled);
  262. bool is_highlight_matching_braces_enabled() const;
  263. void add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key);
  264. void set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs);
  265. Dictionary get_auto_brace_completion_pairs() const;
  266. bool has_auto_brace_completion_open_key(const String &p_open_key) const;
  267. bool has_auto_brace_completion_close_key(const String &p_close_key) const;
  268. String get_auto_brace_completion_close_key(const String &p_open_key) const;
  269. /* Main Gutter */
  270. void set_draw_breakpoints_gutter(bool p_draw);
  271. bool is_drawing_breakpoints_gutter() const;
  272. void set_draw_bookmarks_gutter(bool p_draw);
  273. bool is_drawing_bookmarks_gutter() const;
  274. void set_draw_executing_lines_gutter(bool p_draw);
  275. bool is_drawing_executing_lines_gutter() const;
  276. // breakpoints
  277. void set_line_as_breakpoint(int p_line, bool p_breakpointed);
  278. bool is_line_breakpointed(int p_line) const;
  279. void clear_breakpointed_lines();
  280. PackedInt32Array get_breakpointed_lines() const;
  281. // bookmarks
  282. void set_line_as_bookmarked(int p_line, bool p_bookmarked);
  283. bool is_line_bookmarked(int p_line) const;
  284. void clear_bookmarked_lines();
  285. PackedInt32Array get_bookmarked_lines() const;
  286. // executing lines
  287. void set_line_as_executing(int p_line, bool p_executing);
  288. bool is_line_executing(int p_line) const;
  289. void clear_executing_lines();
  290. PackedInt32Array get_executing_lines() const;
  291. /* Line numbers */
  292. void set_draw_line_numbers(bool p_draw);
  293. bool is_draw_line_numbers_enabled() const;
  294. void set_line_numbers_zero_padded(bool p_zero_padded);
  295. bool is_line_numbers_zero_padded() const;
  296. /* Fold gutter */
  297. void set_draw_fold_gutter(bool p_draw);
  298. bool is_drawing_fold_gutter() const;
  299. /* Line Folding */
  300. void set_line_folding_enabled(bool p_enabled);
  301. bool is_line_folding_enabled() const;
  302. bool can_fold_line(int p_line) const;
  303. void fold_line(int p_line);
  304. void unfold_line(int p_line);
  305. void fold_all_lines();
  306. void unfold_all_lines();
  307. void toggle_foldable_line(int p_line);
  308. bool is_line_folded(int p_line) const;
  309. TypedArray<int> get_folded_lines() const;
  310. /* Delimiters */
  311. void add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
  312. void remove_string_delimiter(const String &p_start_key);
  313. bool has_string_delimiter(const String &p_start_key) const;
  314. void set_string_delimiters(const TypedArray<String> &p_string_delimiters);
  315. void clear_string_delimiters();
  316. TypedArray<String> get_string_delimiters() const;
  317. int is_in_string(int p_line, int p_column = -1) const;
  318. void add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
  319. void remove_comment_delimiter(const String &p_start_key);
  320. bool has_comment_delimiter(const String &p_start_key) const;
  321. void set_comment_delimiters(const TypedArray<String> &p_comment_delimiters);
  322. void clear_comment_delimiters();
  323. TypedArray<String> get_comment_delimiters() const;
  324. int is_in_comment(int p_line, int p_column = -1) const;
  325. String get_delimiter_start_key(int p_delimiter_idx) const;
  326. String get_delimiter_end_key(int p_delimiter_idx) const;
  327. Point2 get_delimiter_start_position(int p_line, int p_column) const;
  328. Point2 get_delimiter_end_position(int p_line, int p_column) const;
  329. /* Code hint */
  330. void set_code_hint(const String &p_hint);
  331. void set_code_hint_draw_below(bool p_below);
  332. /* Code Completion */
  333. void set_code_completion_enabled(bool p_enable);
  334. bool is_code_completion_enabled() const;
  335. void set_code_completion_prefixes(const TypedArray<String> &p_prefixes);
  336. TypedArray<String> get_code_completion_prefixes() const;
  337. String get_text_for_code_completion() const;
  338. void request_code_completion(bool p_force = false);
  339. 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);
  340. void update_code_completion_options(bool p_forced = false);
  341. TypedArray<Dictionary> get_code_completion_options() const;
  342. Dictionary get_code_completion_option(int p_index) const;
  343. int get_code_completion_selected_index() const;
  344. void set_code_completion_selected_index(int p_index);
  345. void confirm_code_completion(bool p_replace = false);
  346. void cancel_code_completion();
  347. /* Line length guidelines */
  348. void set_line_length_guidelines(TypedArray<int> p_guideline_columns);
  349. TypedArray<int> get_line_length_guidelines() const;
  350. /* Symbol lookup */
  351. void set_symbol_lookup_on_click_enabled(bool p_enabled);
  352. bool is_symbol_lookup_on_click_enabled() const;
  353. String get_text_for_symbol_lookup();
  354. void set_symbol_lookup_word_as_valid(bool p_valid);
  355. CodeEdit();
  356. ~CodeEdit();
  357. };
  358. VARIANT_ENUM_CAST(CodeEdit::CodeCompletionKind);
  359. #endif // CODE_EDIT_H