summaryrefslogtreecommitdiff
path: root/scene/gui/code_edit.h
blob: 52b3f52a0359e70e6c7d37923e8289f58b4af031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*************************************************************************/
/*  code_edit.h                                                          */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef CODEEDIT_H
#define CODEEDIT_H

#include "scene/gui/text_edit.h"

class CodeEdit : public TextEdit {
	GDCLASS(CodeEdit, TextEdit)

public:
	/* Keep enum in sync with:                                           */
	/* /core/object/script_language.h - ScriptCodeCompletionOption::Kind */
	enum CodeCompletionKind {
		KIND_CLASS,
		KIND_FUNCTION,
		KIND_SIGNAL,
		KIND_VARIABLE,
		KIND_MEMBER,
		KIND_ENUM,
		KIND_CONSTANT,
		KIND_NODE_PATH,
		KIND_FILE_PATH,
		KIND_PLAIN_TEXT,
	};

private:
	/* Main Gutter */
	enum MainGutterType {
		MAIN_GUTTER_BREAKPOINT = 0x01,
		MAIN_GUTTER_BOOKMARK = 0x02,
		MAIN_GUTTER_EXECUTING = 0x04
	};

	int main_gutter = -1;
	void _update_draw_main_gutter();
	void _main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);

	// breakpoints
	HashMap<int, bool> breakpointed_lines;
	bool draw_breakpoints = false;
	Color breakpoint_color = Color(1, 1, 1);
	Ref<Texture2D> breakpoint_icon = Ref<Texture2D>();

	// bookmarks
	bool draw_bookmarks = false;
	Color bookmark_color = Color(1, 1, 1);
	Ref<Texture2D> bookmark_icon = Ref<Texture2D>();

	// executing lines
	bool draw_executing_lines = false;
	Color executing_line_color = Color(1, 1, 1);
	Ref<Texture2D> executing_line_icon = Ref<Texture2D>();

	/* Line numbers */
	int line_number_gutter = -1;
	int line_number_digits = 0;
	String line_number_padding = " ";
	Color line_number_color = Color(1, 1, 1);
	void _line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);

	/* Fold Gutter */
	int fold_gutter = -1;
	bool draw_fold_gutter = false;
	Color folding_color = Color(1, 1, 1);
	Ref<Texture2D> can_fold_icon = Ref<Texture2D>();
	Ref<Texture2D> folded_icon = Ref<Texture2D>();
	void _fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region);

	void _gutter_clicked(int p_line, int p_gutter);
	void _update_gutter_indexes();

	/* Line Folding */
	bool line_folding_enabled = true;

	/* Delimiters */
	enum DelimiterType {
		TYPE_STRING,
		TYPE_COMMENT,
	};

	struct Delimiter {
		DelimiterType type;
		String start_key = "";
		String end_key = "";
		bool line_only = true;
	};
	bool setting_delimiters = false;
	Vector<Delimiter> delimiters;
	/*
	 *  Vector entry per line, contains a Map of column numbers to delimiter index, -1 marks the end of a region.
	 *  e.g the following text will be stored as so:
	 *
	 *  0: nothing here
	 *  1:
	 *  2: # test
	 *  3: "test" text "multiline
	 *  4:
	 *  5: test
	 *  6: string"
	 *
	 *  Vector [
	 *      0 = []
	 *      1 = []
	 *      2 = [
	 *          1 = 1
	 *          6 = -1
	 *      ]
	 *      3 = [
	 *	        1 = 0
	 *          6 = -1
	 *          13 = 0
	 *      ]
	 *      4 = [
	 *          0 = 0
	 *      ]
	 *      5 = [
	 *          5 = 0
	 *      ]
	 *      6 = [
	 *          7 = -1
	 *      ]
	 *  ]
	 */
	Vector<Map<int, int>> delimiter_cache;

	void _update_delimiter_cache(int p_from_line = 0, int p_to_line = -1);
	int _is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const;

	void _add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type);
	void _remove_delimiter(const String &p_start_key, DelimiterType p_type);
	bool _has_delimiter(const String &p_start_key, DelimiterType p_type) const;

	void _set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type);
	void _clear_delimiters(DelimiterType p_type);
	TypedArray<String> _get_delimiters(DelimiterType p_type) const;

	/* Code Hint */
	String code_hint = "";

	bool code_hint_draw_below = true;
	int code_hint_xpos = -0xFFFF;

	/* Code Completion */
	bool code_completion_enabled = false;
	bool code_completion_forced = false;

	int code_completion_max_width = 0;
	int code_completion_max_lines = 7;
	int code_completion_scroll_width = 0;
	Color code_completion_scroll_color = Color(0, 0, 0, 0);
	Color code_completion_background_color = Color(0, 0, 0, 0);
	Color code_completion_selected_color = Color(0, 0, 0, 0);
	Color code_completion_existing_color = Color(0, 0, 0, 0);

	bool code_completion_active = false;
	Vector<ScriptCodeCompletionOption> code_completion_options;
	int code_completion_line_ofs = 0;
	int code_completion_current_selected = 0;
	int code_completion_longest_line = 0;
	Rect2i code_completion_rect;

	Set<String> code_completion_prefixes;
	List<ScriptCodeCompletionOption> code_completion_option_submitted;
	List<ScriptCodeCompletionOption> code_completion_option_sources;
	String code_completion_base;

	void _filter_code_completion_candidates();

	void _lines_edited_from(int p_from_line, int p_to_line);

protected:
	void _gui_input(const Ref<InputEvent> &p_gui_input) override;
	void _notification(int p_what);

	static void _bind_methods();

public:
	virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;

	/* Main Gutter */
	void set_draw_breakpoints_gutter(bool p_draw);
	bool is_drawing_breakpoints_gutter() const;

	void set_draw_bookmarks_gutter(bool p_draw);
	bool is_drawing_bookmarks_gutter() const;

	void set_draw_executing_lines_gutter(bool p_draw);
	bool is_drawing_executing_lines_gutter() const;

	// breakpoints
	void set_line_as_breakpoint(int p_line, bool p_breakpointed);
	bool is_line_breakpointed(int p_line) const;
	void clear_breakpointed_lines();
	Array get_breakpointed_lines() const;

	// bookmarks
	void set_line_as_bookmarked(int p_line, bool p_bookmarked);
	bool is_line_bookmarked(int p_line) const;
	void clear_bookmarked_lines();
	Array get_bookmarked_lines() const;

	// executing lines
	void set_line_as_executing(int p_line, bool p_executing);
	bool is_line_executing(int p_line) const;
	void clear_executing_lines();
	Array get_executing_lines() const;

	/* Line numbers */
	void set_draw_line_numbers(bool p_draw);
	bool is_draw_line_numbers_enabled() const;
	void set_line_numbers_zero_padded(bool p_zero_padded);
	bool is_line_numbers_zero_padded() const;

	/* Fold gutter */
	void set_draw_fold_gutter(bool p_draw);
	bool is_drawing_fold_gutter() const;

	/* Line Folding */
	void set_line_folding_enabled(bool p_enabled);
	bool is_line_folding_enabled() const;

	bool can_fold_line(int p_line) const;

	void fold_line(int p_line);
	void unfold_line(int p_line);
	void fold_all_lines();
	void unfold_all_lines();
	void toggle_foldable_line(int p_line);

	bool is_line_folded(int p_line) const;
	TypedArray<int> get_folded_lines() const;

	/* Delimiters */
	void add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
	void remove_string_delimiter(const String &p_start_key);
	bool has_string_delimiter(const String &p_start_key) const;

	void set_string_delimiters(const TypedArray<String> &p_string_delimiters);
	void clear_string_delimiters();
	TypedArray<String> get_string_delimiters() const;

	int is_in_string(int p_line, int p_column = -1) const;

	void add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
	void remove_comment_delimiter(const String &p_start_key);
	bool has_comment_delimiter(const String &p_start_key) const;

	void set_comment_delimiters(const TypedArray<String> &p_comment_delimiters);
	void clear_comment_delimiters();
	TypedArray<String> get_comment_delimiters() const;

	int is_in_comment(int p_line, int p_column = -1) const;

	String get_delimiter_start_key(int p_delimiter_idx) const;
	String get_delimiter_end_key(int p_delimiter_idx) const;

	Point2 get_delimiter_start_position(int p_line, int p_column) const;
	Point2 get_delimiter_end_position(int p_line, int p_column) const;

	/* Code hint */
	void set_code_hint(const String &p_hint);
	void set_code_hint_draw_below(bool p_below);

	/* Code Completion */
	void set_code_completion_enabled(bool p_enable);
	bool is_code_completion_enabled() const;

	void set_code_completion_prefixes(const TypedArray<String> &p_prefixes);
	TypedArray<String> get_code_completion_prefixes() const;

	String get_text_for_code_completion() const;

	void request_code_completion(bool p_force = false);

	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 RES &p_icon = RES(), const Variant &p_value = Variant::NIL);
	void update_code_completion_options(bool p_forced = false);

	TypedArray<Dictionary> get_code_completion_options() const;
	Dictionary get_code_completion_option(int p_index) const;

	int get_code_completion_selected_index() const;
	void set_code_completion_selected_index(int p_index);

	void confirm_code_completion(bool p_replace = false);
	void cancel_code_completion();

	CodeEdit();
	~CodeEdit();
};

VARIANT_ENUM_CAST(CodeEdit::CodeCompletionKind);

#endif // CODEEDIT_H