2
0

code_edit.h 18 KB

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