code_edit.h 17 KB

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