1 /*************************************************************************/
2 /*  text_edit.cpp                                                        */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 
31 #include "text_edit.h"
32 #include "os/input.h"
33 #include "os/keyboard.h"
34 #include "os/os.h"
35 
36 #include "globals.h"
37 #include "message_queue.h"
38 
39 #define TAB_PIXELS
40 
_is_text_char(CharType c)41 static bool _is_text_char(CharType c) {
42 
43 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
44 }
45 
_is_symbol(CharType c)46 static bool _is_symbol(CharType c) {
47 
48 	return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
49 }
50 
_is_char(CharType c)51 static bool _is_char(CharType c) {
52 
53 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
54 }
55 
_is_number(CharType c)56 static bool _is_number(CharType c) {
57 	return (c >= '0' && c <= '9');
58 }
59 
_is_hex_symbol(CharType c)60 static bool _is_hex_symbol(CharType c) {
61 	return ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
62 }
63 
_is_pair_right_symbol(CharType c)64 static bool _is_pair_right_symbol(CharType c) {
65 	return c == '"' ||
66 		   c == '\'' ||
67 		   c == ')' ||
68 		   c == ']' ||
69 		   c == '}';
70 }
71 
_is_pair_left_symbol(CharType c)72 static bool _is_pair_left_symbol(CharType c) {
73 	return c == '"' ||
74 		   c == '\'' ||
75 		   c == '(' ||
76 		   c == '[' ||
77 		   c == '{';
78 }
79 
_is_pair_symbol(CharType c)80 static bool _is_pair_symbol(CharType c) {
81 	return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
82 }
83 
_get_right_pair_symbol(CharType c)84 static CharType _get_right_pair_symbol(CharType c) {
85 	if (c == '"')
86 		return '"';
87 	if (c == '\'')
88 		return '\'';
89 	if (c == '(')
90 		return ')';
91 	if (c == '[')
92 		return ']';
93 	if (c == '{')
94 		return '}';
95 	return 0;
96 }
97 
set_font(const Ref<Font> & p_font)98 void TextEdit::Text::set_font(const Ref<Font> &p_font) {
99 
100 	font = p_font;
101 }
102 
set_tab_size(int p_tab_size)103 void TextEdit::Text::set_tab_size(int p_tab_size) {
104 
105 	tab_size = p_tab_size;
106 }
107 
_update_line_cache(int p_line) const108 void TextEdit::Text::_update_line_cache(int p_line) const {
109 
110 	int w = 0;
111 	int tab_w = font->get_char_size(' ').width * tab_size;
112 
113 	int len = text[p_line].data.length();
114 	const CharType *str = text[p_line].data.c_str();
115 
116 	//update width
117 
118 	for (int i = 0; i < len; i++) {
119 		if (str[i] == '\t') {
120 
121 			int left = w % tab_w;
122 			if (left == 0)
123 				w += tab_w;
124 			else
125 				w += tab_w - w % tab_w; // is right...
126 
127 		} else {
128 
129 			w += font->get_char_size(str[i], str[i + 1]).width;
130 		}
131 	}
132 
133 	text[p_line].width_cache = w;
134 
135 	//update regions
136 
137 	text[p_line].region_info.clear();
138 
139 	for (int i = 0; i < len; i++) {
140 
141 		if (!_is_symbol(str[i]))
142 			continue;
143 		if (str[i] == '\\') {
144 			i++; //skip quoted anything
145 			continue;
146 		}
147 
148 		int left = len - i;
149 
150 		for (int j = 0; j < color_regions->size(); j++) {
151 
152 			const ColorRegion &cr = color_regions->operator[](j);
153 
154 			/* BEGIN */
155 
156 			int lr = cr.begin_key.length();
157 			if (lr == 0 || lr > left)
158 				continue;
159 
160 			const CharType *kc = cr.begin_key.c_str();
161 
162 			bool match = true;
163 
164 			for (int k = 0; k < lr; k++) {
165 				if (kc[k] != str[i + k]) {
166 					match = false;
167 					break;
168 				}
169 			}
170 
171 			if (match) {
172 
173 				ColorRegionInfo cri;
174 				cri.end = false;
175 				cri.region = j;
176 				text[p_line].region_info[i] = cri;
177 				i += lr - 1;
178 				break;
179 			}
180 
181 			/* END */
182 
183 			lr = cr.end_key.length();
184 			if (lr == 0 || lr > left)
185 				continue;
186 
187 			kc = cr.end_key.c_str();
188 
189 			match = true;
190 
191 			for (int k = 0; k < lr; k++) {
192 				if (kc[k] != str[i + k]) {
193 					match = false;
194 					break;
195 				}
196 			}
197 
198 			if (match) {
199 
200 				ColorRegionInfo cri;
201 				cri.end = true;
202 				cri.region = j;
203 				text[p_line].region_info[i] = cri;
204 				i += lr - 1;
205 				break;
206 			}
207 		}
208 	}
209 }
210 
get_color_region_info(int p_line)211 const Map<int, TextEdit::Text::ColorRegionInfo> &TextEdit::Text::get_color_region_info(int p_line) {
212 
213 	Map<int, ColorRegionInfo> *cri = NULL;
214 	ERR_FAIL_INDEX_V(p_line, text.size(), *cri); //enjoy your crash
215 
216 	if (text[p_line].width_cache == -1) {
217 		_update_line_cache(p_line);
218 	}
219 
220 	return text[p_line].region_info;
221 }
222 
get_line_width(int p_line) const223 int TextEdit::Text::get_line_width(int p_line) const {
224 
225 	ERR_FAIL_INDEX_V(p_line, text.size(), -1);
226 
227 	if (text[p_line].width_cache == -1) {
228 		_update_line_cache(p_line);
229 	}
230 
231 	return text[p_line].width_cache;
232 }
233 
clear_caches()234 void TextEdit::Text::clear_caches() {
235 
236 	for (int i = 0; i < text.size(); i++)
237 		text[i].width_cache = -1;
238 }
239 
clear()240 void TextEdit::Text::clear() {
241 
242 	text.clear();
243 	insert(0, "");
244 }
245 
get_max_width() const246 int TextEdit::Text::get_max_width() const {
247 	//quite some work.. but should be fast enough.
248 
249 	int max = 0;
250 
251 	for (int i = 0; i < text.size(); i++)
252 		max = MAX(max, get_line_width(i));
253 	return max;
254 }
255 
set(int p_line,const String & p_text)256 void TextEdit::Text::set(int p_line, const String &p_text) {
257 
258 	ERR_FAIL_INDEX(p_line, text.size());
259 
260 	text[p_line].width_cache = -1;
261 	text[p_line].data = p_text;
262 }
263 
insert(int p_at,const String & p_text)264 void TextEdit::Text::insert(int p_at, const String &p_text) {
265 
266 	Line line;
267 	line.marked = false;
268 	line.breakpoint = false;
269 	line.width_cache = -1;
270 	line.data = p_text;
271 	text.insert(p_at, line);
272 }
remove(int p_at)273 void TextEdit::Text::remove(int p_at) {
274 
275 	text.remove(p_at);
276 }
277 
_update_scrollbars()278 void TextEdit::_update_scrollbars() {
279 
280 	Size2 size = get_size();
281 	Size2 hmin = h_scroll->get_combined_minimum_size();
282 	Size2 vmin = v_scroll->get_combined_minimum_size();
283 
284 	v_scroll->set_begin(Point2(size.width - vmin.width, cache.style_normal->get_margin(MARGIN_TOP)));
285 	v_scroll->set_end(Point2(size.width, size.height - cache.style_normal->get_margin(MARGIN_TOP) - cache.style_normal->get_margin(MARGIN_BOTTOM)));
286 
287 	h_scroll->set_begin(Point2(0, size.height - hmin.height));
288 	h_scroll->set_end(Point2(size.width - vmin.width, size.height));
289 
290 	int hscroll_rows = ((hmin.height - 1) / get_row_height()) + 1;
291 	int visible_rows = get_visible_rows();
292 	int total_rows = text.size();
293 	if (scroll_past_end_of_file_enabled) {
294 		total_rows += get_visible_rows() - 1;
295 	}
296 
297 	int vscroll_pixels = v_scroll->get_combined_minimum_size().width;
298 	int visible_width = size.width - cache.style_normal->get_minimum_size().width;
299 	int total_width = text.get_max_width() + vmin.x;
300 
301 	if (line_numbers)
302 		total_width += cache.line_number_w;
303 
304 	if (draw_breakpoint_gutter) {
305 		total_width += cache.breakpoint_gutter_width;
306 	}
307 
308 	bool use_hscroll = true;
309 	bool use_vscroll = true;
310 
311 	if (total_rows <= visible_rows && total_width <= visible_width) {
312 		//thanks yessopie for this clever bit of logic
313 		use_hscroll = false;
314 		use_vscroll = false;
315 
316 	} else {
317 
318 		if (total_rows > visible_rows && total_width <= visible_width - vscroll_pixels) {
319 			//thanks yessopie for this clever bit of logic
320 			use_hscroll = false;
321 		}
322 
323 		if (total_rows <= visible_rows - hscroll_rows && total_width > visible_width) {
324 			//thanks yessopie for this clever bit of logic
325 			use_vscroll = false;
326 		}
327 	}
328 
329 	updating_scrolls = true;
330 
331 	if (use_vscroll) {
332 
333 		v_scroll->show();
334 		v_scroll->set_max(total_rows);
335 		v_scroll->set_page(visible_rows);
336 
337 		if (fabs(v_scroll->get_val() - (double)cursor.line_ofs) >= 1) {
338 			v_scroll->set_val(cursor.line_ofs);
339 		}
340 
341 	} else {
342 		cursor.line_ofs = 0;
343 		v_scroll->hide();
344 	}
345 
346 	if (use_hscroll) {
347 
348 		h_scroll->show();
349 		h_scroll->set_max(total_width);
350 		h_scroll->set_page(visible_width);
351 		if (fabs(h_scroll->get_val() - (double)cursor.x_ofs) >= 1) {
352 			h_scroll->set_val(cursor.x_ofs);
353 		}
354 
355 	} else {
356 
357 		h_scroll->hide();
358 	}
359 
360 	updating_scrolls = false;
361 }
362 
_click_selection_held()363 void TextEdit::_click_selection_held() {
364 
365 	if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && selection.selecting_mode != Selection::MODE_NONE) {
366 
367 		Point2 mp = Input::get_singleton()->get_mouse_pos() - get_global_pos();
368 
369 		int row, col;
370 		_get_mouse_pos(Point2i(mp.x, mp.y), row, col);
371 
372 		select(selection.selecting_line, selection.selecting_column, row, col);
373 
374 		cursor_set_line(row);
375 		cursor_set_column(col);
376 		update();
377 
378 		click_select_held->start();
379 
380 	} else {
381 
382 		click_select_held->stop();
383 	}
384 }
385 
_notification(int p_what)386 void TextEdit::_notification(int p_what) {
387 
388 	switch (p_what) {
389 		case NOTIFICATION_ENTER_TREE: {
390 
391 			_update_caches();
392 			if (cursor_changed_dirty)
393 				MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
394 			if (text_changed_dirty)
395 				MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
396 
397 		} break;
398 		case NOTIFICATION_RESIZED: {
399 
400 			cache.size = get_size();
401 			adjust_viewport_to_cursor();
402 
403 		} break;
404 		case NOTIFICATION_THEME_CHANGED: {
405 
406 			_update_caches();
407 		} break;
408 		case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
409 			window_has_focus = true;
410 			draw_caret = true;
411 			update();
412 		} break;
413 		case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
414 			window_has_focus = false;
415 			draw_caret = false;
416 			update();
417 		} break;
418 		case NOTIFICATION_DRAW: {
419 
420 			if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
421 				draw_caret = false;
422 			}
423 
424 			if (draw_breakpoint_gutter) {
425 				breakpoint_gutter_width = (get_row_height() * 55) / 100;
426 				cache.breakpoint_gutter_width = breakpoint_gutter_width;
427 			} else {
428 				cache.breakpoint_gutter_width = 0;
429 			}
430 
431 			int line_number_char_count = 0;
432 
433 			{
434 				int lc = text.size();
435 				cache.line_number_w = 0;
436 				while (lc) {
437 					cache.line_number_w += 1;
438 					lc /= 10;
439 				};
440 
441 				if (line_numbers) {
442 
443 					line_number_char_count = cache.line_number_w;
444 					cache.line_number_w = (cache.line_number_w + 1) * cache.font->get_char_size('0').width;
445 				} else {
446 					cache.line_number_w = 0;
447 				}
448 			}
449 			_update_scrollbars();
450 
451 			RID ci = get_canvas_item();
452 			int xmargin_beg = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width;
453 			int xmargin_end = cache.size.width - cache.style_normal->get_margin(MARGIN_RIGHT);
454 			//let's do it easy for now:
455 			cache.style_normal->draw(ci, Rect2(Point2(), cache.size));
456 			if (has_focus())
457 				cache.style_focus->draw(ci, Rect2(Point2(), cache.size));
458 
459 			int ascent = cache.font->get_ascent();
460 
461 			int visible_rows = get_visible_rows();
462 
463 			int tab_w = cache.font->get_char_size(' ').width * tab_size;
464 
465 			Color color = cache.font_color;
466 			int in_region = -1;
467 
468 			if (syntax_coloring) {
469 
470 				if (custom_bg_color.a > 0.01) {
471 
472 					Point2i ofs = Point2i(cache.style_normal->get_offset()) / 2.0;
473 					VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(ofs, get_size() - cache.style_normal->get_minimum_size() + ofs), custom_bg_color);
474 				}
475 				//compute actual region to start (may be inside say, a comment).
476 				//slow in very large documments :( but ok for source!
477 
478 				for (int i = 0; i < cursor.line_ofs; i++) {
479 
480 					const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(i);
481 
482 					if (in_region >= 0 && color_regions[in_region].line_only) {
483 						in_region = -1; //reset regions that end at end of line
484 					}
485 
486 					for (const Map<int, Text::ColorRegionInfo>::Element *E = cri_map.front(); E; E = E->next()) {
487 
488 						const Text::ColorRegionInfo &cri = E->get();
489 
490 						if (in_region == -1) {
491 
492 							if (!cri.end) {
493 
494 								in_region = cri.region;
495 							}
496 						} else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
497 
498 							if (cri.end || color_regions[cri.region].eq) {
499 
500 								in_region = -1;
501 							}
502 						}
503 					}
504 				}
505 			}
506 
507 			int brace_open_match_line = -1;
508 			int brace_open_match_column = -1;
509 			bool brace_open_matching = false;
510 			bool brace_open_mismatch = false;
511 			int brace_close_match_line = -1;
512 			int brace_close_match_column = -1;
513 			bool brace_close_matching = false;
514 			bool brace_close_mismatch = false;
515 
516 			if (brace_matching_enabled) {
517 
518 				if (cursor.column < text[cursor.line].length()) {
519 					//check for open
520 					CharType c = text[cursor.line][cursor.column];
521 					CharType closec = 0;
522 
523 					if (c == '[') {
524 						closec = ']';
525 					} else if (c == '{') {
526 						closec = '}';
527 					} else if (c == '(') {
528 						closec = ')';
529 					}
530 
531 					if (closec != 0) {
532 
533 						int stack = 1;
534 
535 						for (int i = cursor.line; i < text.size(); i++) {
536 
537 							int from = i == cursor.line ? cursor.column + 1 : 0;
538 							for (int j = from; j < text[i].length(); j++) {
539 
540 								CharType cc = text[i][j];
541 								//ignore any brackets inside a string
542 								if (cc == '"' || cc == '\'') {
543 									CharType quotation = cc;
544 									do {
545 										j++;
546 										if (!(j < text[i].length())) {
547 											break;
548 										}
549 										cc = text[i][j];
550 										//skip over escaped quotation marks inside strings
551 										if (cc == '\\') {
552 											bool escaped = true;
553 											while (j + 1 < text[i].length() && text[i][j + 1] == '\\') {
554 												escaped = !escaped;
555 												j++;
556 											}
557 											if (escaped) {
558 												j++;
559 												continue;
560 											}
561 										}
562 									} while (cc != quotation);
563 								} else if (cc == c)
564 									stack++;
565 								else if (cc == closec)
566 									stack--;
567 
568 								if (stack == 0) {
569 									brace_open_match_line = i;
570 									brace_open_match_column = j;
571 									brace_open_matching = true;
572 
573 									break;
574 								}
575 							}
576 							if (brace_open_match_line != -1)
577 								break;
578 						}
579 
580 						if (!brace_open_matching)
581 							brace_open_mismatch = true;
582 					}
583 				}
584 
585 				if (cursor.column > 0) {
586 					CharType c = text[cursor.line][cursor.column - 1];
587 					CharType closec = 0;
588 
589 					if (c == ']') {
590 						closec = '[';
591 					} else if (c == '}') {
592 						closec = '{';
593 					} else if (c == ')') {
594 						closec = '(';
595 					}
596 
597 					if (closec != 0) {
598 
599 						int stack = 1;
600 
601 						for (int i = cursor.line; i >= 0; i--) {
602 
603 							int from = i == cursor.line ? cursor.column - 2 : text[i].length() - 1;
604 							for (int j = from; j >= 0; j--) {
605 
606 								CharType cc = text[i][j];
607 								//ignore any brackets inside a string
608 								if (cc == '"' || cc == '\'') {
609 									CharType quotation = cc;
610 									do {
611 										j--;
612 										if (!(j >= 0)) {
613 											break;
614 										}
615 										cc = text[i][j];
616 										//skip over escaped quotation marks inside strings
617 										if (cc == quotation) {
618 											bool escaped = false;
619 											while (j - 1 >= 0 && text[i][j - 1] == '\\') {
620 												escaped = !escaped;
621 												j--;
622 											}
623 											if (escaped) {
624 												j--;
625 												cc = '\\';
626 												continue;
627 											}
628 										}
629 									} while (cc != quotation);
630 								} else if (cc == c)
631 									stack++;
632 								else if (cc == closec)
633 									stack--;
634 
635 								if (stack == 0) {
636 									brace_close_match_line = i;
637 									brace_close_match_column = j;
638 									brace_close_matching = true;
639 
640 									break;
641 								}
642 							}
643 							if (brace_close_match_line != -1)
644 								break;
645 						}
646 
647 						if (!brace_close_matching)
648 							brace_close_mismatch = true;
649 					}
650 				}
651 			}
652 
653 			int deregion = 0; //force it to clear inrgion
654 			Point2 cursor_pos;
655 
656 			// get the highlighted words
657 			String highlighted_text = get_selection_text();
658 
659 			String line_num_padding = line_numbers_zero_padded ? "0" : " ";
660 
661 			for (int i = 0; i < visible_rows; i++) {
662 
663 				int line = i + cursor.line_ofs;
664 
665 				if (line < 0 || line >= (int)text.size())
666 					continue;
667 
668 				const String &str = text[line];
669 
670 				int char_margin = xmargin_beg - cursor.x_ofs;
671 				int char_ofs = 0;
672 				int ofs_y = i * get_row_height() + cache.line_spacing / 2;
673 				bool prev_is_char = false;
674 				bool prev_is_number = false;
675 				bool in_keyword = false;
676 				bool in_word = false;
677 				bool in_function_name = false;
678 				bool in_member_variable = false;
679 				bool is_hex_notation = false;
680 				Color keyword_color;
681 
682 				// check if line contains highlighted word
683 				int highlighted_text_col = -1;
684 				int search_text_col = -1;
685 
686 				if (!search_text.empty())
687 					search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0);
688 
689 				if (highlighted_text.length() != 0 && highlighted_text != search_text)
690 					highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0);
691 
692 				const Map<int, Text::ColorRegionInfo> &cri_map = text.get_color_region_info(line);
693 
694 				if (text.is_marked(line)) {
695 
696 					VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.mark_color);
697 				}
698 
699 				if (text.is_breakpoint(line)) {
700 
701 					VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, get_row_height()), cache.breakpoint_color);
702 				}
703 
704 				if (line == cursor.line) {
705 					VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(0, ofs_y, xmargin_end, get_row_height()), cache.current_line_color);
706 				}
707 
708 				// draw breakpoint marker
709 				if (text.is_breakpoint(line)) {
710 					if (draw_breakpoint_gutter) {
711 						int vertical_gap = (get_row_height() * 40) / 100;
712 						int horizontal_gap = (cache.breakpoint_gutter_width * 30) / 100;
713 						int marker_height = get_row_height() - (vertical_gap * 2);
714 						int marker_width = cache.breakpoint_gutter_width - (horizontal_gap * 2);
715 						// no transparency on marker
716 						VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cache.style_normal->get_margin(MARGIN_LEFT) + horizontal_gap - 2, ofs_y + vertical_gap, marker_width, marker_height), Color(cache.breakpoint_color.r, cache.breakpoint_color.g, cache.breakpoint_color.b));
717 					}
718 				}
719 
720 				if (cache.line_number_w) {
721 					String fc = String::num(line + 1);
722 					while (fc.length() < line_number_char_count) {
723 						fc = line_num_padding + fc;
724 					}
725 
726 					cache.font->draw(ci, Point2(cache.style_normal->get_margin(MARGIN_LEFT) + cache.breakpoint_gutter_width, ofs_y + cache.font->get_ascent()), fc, cache.line_number_color);
727 				}
728 				for (int j = 0; j < str.length(); j++) {
729 
730 					//look for keyword
731 
732 					if (deregion > 0) {
733 						deregion--;
734 						if (deregion == 0)
735 							in_region = -1;
736 					}
737 					if (syntax_coloring && deregion == 0) {
738 
739 						color = cache.font_color; //reset
740 						//find keyword
741 						bool is_char = _is_text_char(str[j]);
742 						bool is_symbol = _is_symbol(str[j]);
743 						bool is_number = _is_number(str[j]);
744 
745 						if (j == 0 && in_region >= 0 && color_regions[in_region].line_only) {
746 							in_region = -1; //reset regions that end at end of line
747 						}
748 
749 						// allow ABCDEF in hex notation
750 						if (is_hex_notation && (_is_hex_symbol(str[j]) || is_number)) {
751 							is_number = true;
752 						} else {
753 							is_hex_notation = false;
754 						}
755 
756 						// check for dot or 'x' for hex notation in floating point number
757 						if ((str[j] == '.' || str[j] == 'x') && !in_word && prev_is_number && !is_number) {
758 							is_number = true;
759 							is_symbol = false;
760 
761 							if (str[j] == 'x' && str[j - 1] == '0') {
762 								is_hex_notation = true;
763 							}
764 						}
765 
766 						if (!in_word && _is_char(str[j])) {
767 							in_word = true;
768 						}
769 
770 						if ((in_keyword || in_word) && !is_hex_notation) {
771 							is_number = false;
772 						}
773 
774 						if (is_symbol && str[j] != '.' && in_word) {
775 							in_word = false;
776 						}
777 
778 						if (is_symbol && cri_map.has(j)) {
779 
780 							const Text::ColorRegionInfo &cri = cri_map[j];
781 
782 							if (in_region == -1) {
783 
784 								if (!cri.end) {
785 
786 									in_region = cri.region;
787 								}
788 							} else if (in_region == cri.region && !color_regions[cri.region].line_only) { //ignore otherwise
789 
790 								if (cri.end || color_regions[cri.region].eq) {
791 
792 									deregion = color_regions[cri.region].eq ? color_regions[cri.region].begin_key.length() : color_regions[cri.region].end_key.length();
793 								}
794 							}
795 						}
796 
797 						if (!is_char)
798 							in_keyword = false;
799 
800 						if (in_region == -1 && !in_keyword && is_char && !prev_is_char) {
801 
802 							int to = j;
803 							while (to < str.length() && _is_text_char(str[to]))
804 								to++;
805 
806 							uint32_t hash = String::hash(&str[j], to - j);
807 							StrRange range(&str[j], to - j);
808 
809 							const Color *col = keywords.custom_getptr(range, hash);
810 
811 							if (col) {
812 
813 								in_keyword = true;
814 								keyword_color = *col;
815 							}
816 						}
817 
818 						if (!in_function_name && in_word && !in_keyword) {
819 
820 							int k = j;
821 							while (k < str.length() && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
822 								k++;
823 							}
824 
825 							// check for space between name and bracket
826 							while (k < str.length() && (str[k] == '\t' || str[k] == ' ')) {
827 								k++;
828 							}
829 
830 							if (str[k] == '(') {
831 								in_function_name = true;
832 							}
833 						}
834 
835 						if (!in_function_name && !in_member_variable && !in_keyword && !is_number && in_word) {
836 							int k = j;
837 							while (k > 0 && !_is_symbol(str[k]) && str[k] != '\t' && str[k] != ' ') {
838 								k--;
839 							}
840 
841 							if (str[k] == '.') {
842 								in_member_variable = true;
843 							}
844 						}
845 
846 						if (is_symbol) {
847 							in_function_name = false;
848 							in_member_variable = false;
849 						}
850 
851 						if (in_region >= 0)
852 							color = color_regions[in_region].color;
853 						else if (in_keyword)
854 							color = keyword_color;
855 						else if (in_member_variable)
856 							color = cache.member_variable_color;
857 						else if (in_function_name)
858 							color = cache.function_color;
859 						else if (is_symbol)
860 							color = symbol_color;
861 						else if (is_number)
862 							color = cache.number_color;
863 
864 						prev_is_char = is_char;
865 						prev_is_number = is_number;
866 					}
867 					int char_w;
868 
869 					//handle tabulator
870 
871 					if (str[j] == '\t') {
872 						int left = char_ofs % tab_w;
873 						if (left == 0)
874 							char_w = tab_w;
875 						else
876 							char_w = tab_w - char_ofs % tab_w; // is right...
877 
878 					} else {
879 						char_w = cache.font->get_char_size(str[j], str[j + 1]).width;
880 					}
881 
882 					if ((char_ofs + char_margin) < xmargin_beg) {
883 						char_ofs += char_w;
884 						continue;
885 					}
886 
887 					if ((char_ofs + char_margin + char_w) >= xmargin_end) {
888 						if (syntax_coloring)
889 							continue;
890 						else
891 							break;
892 					}
893 
894 					bool in_search_result = false;
895 
896 					if (search_text_col != -1) {
897 						// if we are at the end check for new search result on same line
898 						if (j >= search_text_col + search_text.length())
899 							search_text_col = _get_column_pos_of_word(search_text, str, search_flags, j);
900 
901 						in_search_result = j >= search_text_col && j < search_text_col + search_text.length();
902 
903 						if (in_search_result) {
904 							VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.search_result_color);
905 						}
906 					}
907 
908 					bool in_selection = (selection.active && line >= selection.from_line && line <= selection.to_line && (line > selection.from_line || j >= selection.from_column) && (line < selection.to_line || j < selection.to_column));
909 
910 					if (in_selection) {
911 						//inside selection!
912 						VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.selection_color);
913 					}
914 
915 					if (in_search_result) {
916 						Color border_color = (line == search_result_line && j >= search_result_col && j < search_result_col + search_text.length()) ? cache.font_color : cache.search_result_border_color;
917 
918 						VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, 1)), border_color);
919 						VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y + get_row_height() - 1), Size2i(char_w, 1)), border_color);
920 
921 						if (j == search_text_col)
922 							VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(1, get_row_height())), border_color);
923 						if (j == search_text_col + search_text.length() - 1)
924 							VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin + char_w - 1, ofs_y), Size2i(1, get_row_height())), border_color);
925 					}
926 
927 					if (highlight_all_occurrences) {
928 						if (highlighted_text_col != -1) {
929 
930 							// if we are at the end check for new word on same line
931 							if (j > highlighted_text_col + highlighted_text.length()) {
932 								highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, j);
933 							}
934 
935 							bool in_highlighted_word = (j >= highlighted_text_col && j < highlighted_text_col + highlighted_text.length());
936 
937 							/* if this is the original highlighted text we don't want to highlight it again */
938 							if (cursor.line == line && (cursor.column >= highlighted_text_col && cursor.column <= highlighted_text_col + highlighted_text.length())) {
939 								in_highlighted_word = false;
940 							}
941 
942 							if (in_highlighted_word) {
943 								VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2i(char_ofs + char_margin, ofs_y), Size2i(char_w, get_row_height())), cache.word_highlighted_color);
944 							}
945 						}
946 					}
947 
948 					if (brace_matching_enabled) {
949 						if ((brace_open_match_line == line && brace_open_match_column == j) ||
950 								(cursor.column == j && cursor.line == line && (brace_open_matching || brace_open_mismatch))) {
951 
952 							if (brace_open_mismatch)
953 								color = cache.brace_mismatch_color;
954 							cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), '_', str[j + 1], in_selection ? cache.font_selected_color : color);
955 						}
956 
957 						if (
958 								(brace_close_match_line == line && brace_close_match_column == j) ||
959 								(cursor.column == j + 1 && cursor.line == line && (brace_close_matching || brace_close_mismatch))) {
960 
961 							if (brace_close_mismatch)
962 								color = cache.brace_mismatch_color;
963 							cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), '_', str[j + 1], in_selection ? cache.font_selected_color : color);
964 						}
965 					}
966 
967 					if (cursor.column == j && cursor.line == line) {
968 
969 						cursor_pos = Point2i(char_ofs + char_margin, ofs_y);
970 
971 						if (insert_mode) {
972 							cursor_pos.y += (get_row_height() - 3);
973 						}
974 
975 						int caret_w = (str[j] == '\t') ? cache.font->get_char_size(' ').width : char_w;
976 						if (draw_caret) {
977 							if (insert_mode) {
978 								int caret_h = (block_caret) ? 4 : 1;
979 								VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, caret_h)), cache.caret_color);
980 							} else {
981 								caret_w = (block_caret) ? caret_w : 1;
982 								VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color);
983 							}
984 						}
985 					}
986 
987 					if (cursor.column == j && cursor.line == line && block_caret && draw_caret && !insert_mode) {
988 						color = cache.caret_background_color;
989 					}
990 
991 					if (str[j] >= 32)
992 						cache.font->draw_char(ci, Point2i(char_ofs + char_margin, ofs_y + ascent), str[j], str[j + 1], in_selection ? cache.font_selected_color : color);
993 
994 					else if (draw_tabs && str[j] == '\t') {
995 						int yofs = (get_row_height() - cache.tab_icon->get_height()) / 2;
996 						cache.tab_icon->draw(ci, Point2(char_ofs + char_margin, ofs_y + yofs), in_selection ? cache.font_selected_color : color);
997 					}
998 
999 					char_ofs += char_w;
1000 				}
1001 
1002 				if (cursor.column == str.length() && cursor.line == line && (char_ofs + char_margin) >= xmargin_beg) {
1003 
1004 					cursor_pos = Point2i(char_ofs + char_margin, ofs_y);
1005 
1006 					if (insert_mode) {
1007 						cursor_pos.y += (get_row_height() - 3);
1008 					}
1009 
1010 					if (draw_caret) {
1011 						if (insert_mode) {
1012 							int char_w = cache.font->get_char_size(' ').width;
1013 							int caret_h = (block_caret) ? 4 : 1;
1014 							VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(char_w, caret_h)), cache.caret_color);
1015 						} else {
1016 							int char_w = cache.font->get_char_size(' ').width;
1017 							int caret_w = (block_caret) ? char_w : 1;
1018 							VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor_pos, Size2i(caret_w, get_row_height())), cache.caret_color);
1019 						}
1020 					}
1021 				}
1022 			}
1023 
1024 			if (line_length_guideline) {
1025 				int x = xmargin_beg + cache.font->get_char_size('0').width * line_length_guideline_col - cursor.x_ofs;
1026 				if (x > xmargin_beg && x < xmargin_end) {
1027 					VisualServer::get_singleton()->canvas_item_add_line(ci, Point2(x, 0), Point2(x, cache.size.height), cache.line_length_guideline_color);
1028 				}
1029 			}
1030 
1031 			bool completion_below = false;
1032 			if (completion_active) {
1033 				// code completion box
1034 				Ref<StyleBox> csb = get_stylebox("completion");
1035 				int maxlines = get_constant("completion_lines");
1036 				int cmax_width = get_constant("completion_max_width") * cache.font->get_char_size('x').x;
1037 				int scrollw = get_constant("completion_scroll_width");
1038 				Color scrollc = get_color("completion_scroll_color");
1039 
1040 				int lines = MIN(completion_options.size(), maxlines);
1041 				int w = 0;
1042 				int h = lines * get_row_height();
1043 				int nofs = cache.font->get_string_size(completion_base).width;
1044 
1045 				if (completion_options.size() < 50) {
1046 					for (int i = 0; i < completion_options.size(); i++) {
1047 						int w2 = MIN(cache.font->get_string_size(completion_options[i]).x, cmax_width);
1048 						if (w2 > w)
1049 							w = w2;
1050 					}
1051 				} else {
1052 					w = cmax_width;
1053 				}
1054 
1055 				int th = h + csb->get_minimum_size().y;
1056 
1057 				if (cursor_pos.y + get_row_height() + th > get_size().height) {
1058 					completion_rect.pos.y = cursor_pos.y - th;
1059 				} else {
1060 					completion_rect.pos.y = cursor_pos.y + get_row_height() + csb->get_offset().y;
1061 					completion_below = true;
1062 				}
1063 
1064 				if (cursor_pos.x - nofs + w + scrollw > get_size().width) {
1065 					completion_rect.pos.x = get_size().width - w - scrollw;
1066 				} else {
1067 					completion_rect.pos.x = cursor_pos.x - nofs;
1068 				}
1069 
1070 				completion_rect.size.width = w + 2;
1071 				completion_rect.size.height = h;
1072 				if (completion_options.size() <= maxlines)
1073 					scrollw = 0;
1074 
1075 				draw_style_box(csb, Rect2(completion_rect.pos - csb->get_offset(), completion_rect.size + csb->get_minimum_size() + Size2(scrollw, 0)));
1076 
1077 				if (cache.completion_background_color.a > 0.01) {
1078 					VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.pos, completion_rect.size + Size2(scrollw, 0)), cache.completion_background_color);
1079 				}
1080 				int line_from = CLAMP(completion_index - lines / 2, 0, completion_options.size() - lines);
1081 				VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.pos.x, completion_rect.pos.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color);
1082 				draw_rect(Rect2(completion_rect.pos, Size2(nofs, completion_rect.size.height)), cache.completion_existing_color);
1083 
1084 				for (int i = 0; i < lines; i++) {
1085 
1086 					int l = line_from + i;
1087 					ERR_CONTINUE(l < 0 || l >= completion_options.size());
1088 					Color text_color = cache.completion_font_color;
1089 					for (int j = 0; j < color_regions.size(); j++) {
1090 						if (completion_options[l].begins_with(color_regions[j].begin_key)) {
1091 							text_color = color_regions[j].color;
1092 						}
1093 					}
1094 					draw_string(cache.font, Point2(completion_rect.pos.x, completion_rect.pos.y + i * get_row_height() + cache.font->get_ascent()), completion_options[l], text_color, completion_rect.size.width);
1095 				}
1096 
1097 				if (scrollw) {
1098 					//draw a small scroll rectangle to show a position in the options
1099 					float r = maxlines / (float)completion_options.size();
1100 					float o = line_from / (float)completion_options.size();
1101 					draw_rect(Rect2(completion_rect.pos.x + completion_rect.size.width, completion_rect.pos.y + o * completion_rect.size.y, scrollw, completion_rect.size.y * r), scrollc);
1102 				}
1103 
1104 				completion_line_ofs = line_from;
1105 			}
1106 
1107 			// check to see if the hint should be drawn
1108 			bool show_hint = false;
1109 			if (completion_hint != "") {
1110 				if (completion_active) {
1111 					if (completion_below && !callhint_below) {
1112 						show_hint = true;
1113 					} else if (!completion_below && callhint_below) {
1114 						show_hint = true;
1115 					}
1116 				} else {
1117 					show_hint = true;
1118 				}
1119 			}
1120 
1121 			if (show_hint) {
1122 
1123 				Ref<StyleBox> sb = get_stylebox("panel", "TooltipPanel");
1124 				Ref<Font> font = cache.font;
1125 				Color font_color = get_color("font_color", "TooltipLabel");
1126 
1127 				int max_w = 0;
1128 				int sc = completion_hint.get_slice_count("\n");
1129 				int offset = 0;
1130 				int spacing = 0;
1131 				for (int i = 0; i < sc; i++) {
1132 
1133 					String l = completion_hint.get_slice("\n", i);
1134 					int len = font->get_string_size(l).x;
1135 					max_w = MAX(len, max_w);
1136 					if (i == 0) {
1137 						offset = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
1138 					} else {
1139 						spacing += cache.line_spacing;
1140 					}
1141 				}
1142 
1143 				Size2 size = Size2(max_w, sc * font->get_height() + spacing);
1144 				Size2 minsize = size + sb->get_minimum_size();
1145 
1146 				if (completion_hint_offset == -0xFFFF) {
1147 					completion_hint_offset = cursor_pos.x - offset;
1148 				}
1149 
1150 				Point2 hint_ofs = Vector2(completion_hint_offset, cursor_pos.y) + callhint_offset;
1151 
1152 				if (callhint_below) {
1153 					hint_ofs.y += get_row_height() + sb->get_offset().y;
1154 				} else {
1155 					hint_ofs.y -= minsize.y + sb->get_offset().y;
1156 				}
1157 
1158 				draw_style_box(sb, Rect2(hint_ofs, minsize));
1159 
1160 				spacing = 0;
1161 				for (int i = 0; i < sc; i++) {
1162 					int begin = 0;
1163 					int end = 0;
1164 					String l = completion_hint.get_slice("\n", i);
1165 
1166 					if (l.find(String::chr(0xFFFF)) != -1) {
1167 						begin = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF)))).x;
1168 						end = font->get_string_size(l.substr(0, l.rfind(String::chr(0xFFFF)))).x;
1169 					}
1170 
1171 					draw_string(font, hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font->get_height() * i + spacing), l.replace(String::chr(0xFFFF), ""), font_color);
1172 					if (end > 0) {
1173 						Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font->get_height() + font->get_height() * i + spacing - 1);
1174 						draw_line(b, b + Vector2(end - begin, 0), font_color);
1175 					}
1176 					spacing += cache.line_spacing;
1177 				}
1178 			}
1179 
1180 		} break;
1181 		case NOTIFICATION_FOCUS_ENTER: {
1182 
1183 			if (!caret_blink_enabled) {
1184 				draw_caret = true;
1185 			}
1186 			if (OS::get_singleton()->has_virtual_keyboard())
1187 				OS::get_singleton()->show_virtual_keyboard(get_text(), get_global_rect());
1188 			if (raised_from_completion) {
1189 				VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
1190 			}
1191 
1192 		} break;
1193 		case NOTIFICATION_FOCUS_EXIT: {
1194 
1195 			if (OS::get_singleton()->has_virtual_keyboard())
1196 				OS::get_singleton()->hide_virtual_keyboard();
1197 			if (raised_from_completion) {
1198 				VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
1199 			}
1200 		} break;
1201 	}
1202 }
1203 
_consume_pair_symbol(CharType ch)1204 void TextEdit::_consume_pair_symbol(CharType ch) {
1205 
1206 	int cursor_position_to_move = cursor_get_column() + 1;
1207 
1208 	CharType ch_single[2] = { ch, 0 };
1209 	CharType ch_single_pair[2] = { _get_right_pair_symbol(ch), 0 };
1210 	CharType ch_pair[3] = { ch, _get_right_pair_symbol(ch), 0 };
1211 
1212 	if (is_selection_active()) {
1213 
1214 		int new_column, new_line;
1215 
1216 		begin_complex_operation();
1217 		_insert_text(get_selection_from_line(), get_selection_from_column(),
1218 				ch_single,
1219 				&new_line, &new_column);
1220 
1221 		int to_col_offset = 0;
1222 		if (get_selection_from_line() == get_selection_to_line())
1223 			to_col_offset = 1;
1224 
1225 		_insert_text(get_selection_to_line(),
1226 				get_selection_to_column() + to_col_offset,
1227 				ch_single_pair,
1228 				&new_line, &new_column);
1229 		end_complex_operation();
1230 
1231 		cursor_set_line(get_selection_to_line());
1232 		cursor_set_column(get_selection_to_column() + to_col_offset);
1233 
1234 		deselect();
1235 		update();
1236 		return;
1237 	}
1238 
1239 	if ((ch == '\'' || ch == '"') &&
1240 			cursor_get_column() > 0 &&
1241 			_is_text_char(text[cursor.line][cursor_get_column() - 1])) {
1242 		insert_text_at_cursor(ch_single);
1243 		cursor_set_column(cursor_position_to_move);
1244 		return;
1245 	}
1246 
1247 	if (cursor_get_column() < text[cursor.line].length()) {
1248 		if (_is_text_char(text[cursor.line][cursor_get_column()])) {
1249 			insert_text_at_cursor(ch_single);
1250 			cursor_set_column(cursor_position_to_move);
1251 			return;
1252 		}
1253 		if (_is_pair_right_symbol(ch) &&
1254 				text[cursor.line][cursor_get_column()] == ch) {
1255 			cursor_set_column(cursor_position_to_move);
1256 			return;
1257 		}
1258 	}
1259 
1260 	insert_text_at_cursor(ch_pair);
1261 	cursor_set_column(cursor_position_to_move);
1262 	return;
1263 }
1264 
_consume_backspace_for_pair_symbol(int prev_line,int prev_column)1265 void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
1266 
1267 	bool remove_right_symbol = false;
1268 
1269 	if (cursor.column < text[cursor.line].length() && cursor.column > 0) {
1270 
1271 		CharType left_char = text[cursor.line][cursor.column - 1];
1272 		CharType right_char = text[cursor.line][cursor.column];
1273 
1274 		if (right_char == _get_right_pair_symbol(left_char)) {
1275 			remove_right_symbol = true;
1276 		}
1277 	}
1278 	if (remove_right_symbol) {
1279 		_remove_text(prev_line, prev_column, cursor.line, cursor.column + 1);
1280 	} else {
1281 		_remove_text(prev_line, prev_column, cursor.line, cursor.column);
1282 	}
1283 }
1284 
backspace_at_cursor()1285 void TextEdit::backspace_at_cursor() {
1286 	if (readonly)
1287 		return;
1288 
1289 	if (cursor.column == 0 && cursor.line == 0)
1290 		return;
1291 
1292 	int prev_line = cursor.column ? cursor.line : cursor.line - 1;
1293 	int prev_column = cursor.column ? (cursor.column - 1) : (text[cursor.line - 1].length());
1294 	if (auto_brace_completion_enabled &&
1295 			cursor.column > 0 &&
1296 			_is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
1297 		_consume_backspace_for_pair_symbol(prev_line, prev_column);
1298 	} else {
1299 		_remove_text(prev_line, prev_column, cursor.line, cursor.column);
1300 	}
1301 
1302 	cursor_set_line(prev_line);
1303 	cursor_set_column(prev_column);
1304 }
1305 
indent_selection_right()1306 void TextEdit::indent_selection_right() {
1307 
1308 	if (!is_selection_active()) {
1309 		return;
1310 	}
1311 	begin_complex_operation();
1312 	int start_line = get_selection_from_line();
1313 	int end_line = get_selection_to_line();
1314 
1315 	// ignore if the cursor is not past the first column
1316 	if (get_selection_to_column() == 0) {
1317 		end_line--;
1318 	}
1319 
1320 	for (int i = start_line; i <= end_line; i++) {
1321 		String line_text = get_line(i);
1322 		line_text = '\t' + line_text;
1323 		set_line(i, line_text);
1324 	}
1325 
1326 	// fix selection being off by one on the last line
1327 	selection.to_column++;
1328 	end_complex_operation();
1329 	update();
1330 }
1331 
indent_selection_left()1332 void TextEdit::indent_selection_left() {
1333 
1334 	if (!is_selection_active()) {
1335 		return;
1336 	}
1337 	begin_complex_operation();
1338 	int start_line = get_selection_from_line();
1339 	int end_line = get_selection_to_line();
1340 
1341 	// ignore if the cursor is not past the first column
1342 	if (get_selection_to_column() == 0) {
1343 		end_line--;
1344 	}
1345 	String last_line_text = get_line(end_line);
1346 
1347 	for (int i = start_line; i <= end_line; i++) {
1348 		String line_text = get_line(i);
1349 
1350 		if (line_text.begins_with("\t")) {
1351 			line_text = line_text.substr(1, line_text.length());
1352 			set_line(i, line_text);
1353 		} else if (line_text.begins_with("    ")) {
1354 			line_text = line_text.substr(4, line_text.length());
1355 			set_line(i, line_text);
1356 		}
1357 	}
1358 
1359 	// fix selection being off by one on the last line
1360 	if (last_line_text != get_line(end_line) && selection.to_column > 0) {
1361 		selection.to_column--;
1362 	}
1363 	end_complex_operation();
1364 	update();
1365 }
1366 
_get_mouse_pos(const Point2i & p_mouse,int & r_row,int & r_col) const1367 void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const {
1368 
1369 	float rows = p_mouse.y;
1370 	rows -= cache.style_normal->get_margin(MARGIN_TOP);
1371 	rows /= get_row_height();
1372 	int row = cursor.line_ofs + rows;
1373 
1374 	if (row < 0)
1375 		row = 0;
1376 
1377 	int col = 0;
1378 
1379 	if (row >= text.size()) {
1380 
1381 		row = text.size() - 1;
1382 		col = text[row].size();
1383 	} else {
1384 
1385 		col = p_mouse.x - (cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width);
1386 		col += cursor.x_ofs;
1387 		col = get_char_pos_for(col, get_line(row));
1388 	}
1389 
1390 	r_row = row;
1391 	r_col = col;
1392 }
1393 
_input_event(const InputEvent & p_input_event)1394 void TextEdit::_input_event(const InputEvent &p_input_event) {
1395 
1396 	switch (p_input_event.type) {
1397 
1398 		case InputEvent::MOUSE_BUTTON: {
1399 
1400 			const InputEventMouseButton &mb = p_input_event.mouse_button;
1401 
1402 			if (completion_active && completion_rect.has_point(Point2(mb.x, mb.y))) {
1403 
1404 				if (!mb.pressed)
1405 					return;
1406 
1407 				if (mb.button_index == BUTTON_WHEEL_UP) {
1408 					if (completion_index > 0) {
1409 						completion_index--;
1410 						completion_current = completion_options[completion_index];
1411 						update();
1412 					}
1413 				}
1414 				if (mb.button_index == BUTTON_WHEEL_DOWN) {
1415 
1416 					if (completion_index < completion_options.size() - 1) {
1417 						completion_index++;
1418 						completion_current = completion_options[completion_index];
1419 						update();
1420 					}
1421 				}
1422 
1423 				if (mb.button_index == BUTTON_LEFT) {
1424 
1425 					completion_index = CLAMP(completion_line_ofs + (mb.y - completion_rect.pos.y) / get_row_height(), 0, completion_options.size() - 1);
1426 
1427 					completion_current = completion_options[completion_index];
1428 					update();
1429 					if (mb.doubleclick)
1430 						_confirm_completion();
1431 				}
1432 				return;
1433 			} else {
1434 				_cancel_completion();
1435 				_cancel_code_hint();
1436 			}
1437 
1438 			if (mb.pressed) {
1439 
1440 				if (mb.button_index == BUTTON_WHEEL_UP && !mb.mod.command) {
1441 					v_scroll->set_val(v_scroll->get_val() - (3 * mb.factor));
1442 				}
1443 				if (mb.button_index == BUTTON_WHEEL_DOWN && !mb.mod.command) {
1444 					v_scroll->set_val(v_scroll->get_val() + (3 * mb.factor));
1445 				}
1446 				if (mb.button_index == BUTTON_WHEEL_LEFT) {
1447 					h_scroll->set_val(h_scroll->get_val() - (100 * mb.factor));
1448 				}
1449 				if (mb.button_index == BUTTON_WHEEL_RIGHT) {
1450 					h_scroll->set_val(h_scroll->get_val() + (100 * mb.factor));
1451 				}
1452 				if (mb.button_index == BUTTON_LEFT) {
1453 
1454 					_reset_caret_blink_timer();
1455 
1456 					int row, col;
1457 					_get_mouse_pos(Point2i(mb.x, mb.y), row, col);
1458 
1459 					// toggle breakpoint on gutter click
1460 					if (draw_breakpoint_gutter) {
1461 						int gutter = cache.style_normal->get_margin(MARGIN_LEFT);
1462 						if (mb.x > gutter && mb.x <= gutter + cache.breakpoint_gutter_width + 3) {
1463 							set_line_as_breakpoint(row, !is_line_set_as_breakpoint(row));
1464 							emit_signal("breakpoint_toggled", row);
1465 							return;
1466 						}
1467 					}
1468 
1469 					int prev_col = cursor.column;
1470 					int prev_line = cursor.line;
1471 
1472 					cursor_set_line(row);
1473 					cursor_set_column(col);
1474 
1475 					if (mb.mod.shift && (cursor.column != prev_col || cursor.line != prev_line)) {
1476 
1477 						if (!selection.active) {
1478 							selection.active = true;
1479 							selection.selecting_mode = Selection::MODE_POINTER;
1480 							selection.from_column = prev_col;
1481 							selection.from_line = prev_line;
1482 							selection.to_column = cursor.column;
1483 							selection.to_line = cursor.line;
1484 
1485 							if (selection.from_line > selection.to_line || (selection.from_line == selection.to_line && selection.from_column > selection.to_column)) {
1486 								SWAP(selection.from_column, selection.to_column);
1487 								SWAP(selection.from_line, selection.to_line);
1488 								selection.shiftclick_left = false;
1489 							} else {
1490 								selection.shiftclick_left = true;
1491 							}
1492 							selection.selecting_line = prev_line;
1493 							selection.selecting_column = prev_col;
1494 							update();
1495 						} else {
1496 
1497 							if (cursor.line < selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column < selection.selecting_column)) {
1498 
1499 								if (selection.shiftclick_left) {
1500 									SWAP(selection.from_column, selection.to_column);
1501 									SWAP(selection.from_line, selection.to_line);
1502 									selection.shiftclick_left = !selection.shiftclick_left;
1503 								}
1504 								selection.from_column = cursor.column;
1505 								selection.from_line = cursor.line;
1506 
1507 							} else if (cursor.line > selection.selecting_line || (cursor.line == selection.selecting_line && cursor.column > selection.selecting_column)) {
1508 
1509 								if (!selection.shiftclick_left) {
1510 									SWAP(selection.from_column, selection.to_column);
1511 									SWAP(selection.from_line, selection.to_line);
1512 									selection.shiftclick_left = !selection.shiftclick_left;
1513 								}
1514 								selection.to_column = cursor.column;
1515 								selection.to_line = cursor.line;
1516 
1517 							} else {
1518 								selection.active = false;
1519 							}
1520 
1521 							update();
1522 						}
1523 
1524 					} else {
1525 
1526 						//if sel active and dblick last time < something
1527 
1528 						//else
1529 						selection.active = false;
1530 						selection.selecting_mode = Selection::MODE_POINTER;
1531 						selection.selecting_line = row;
1532 						selection.selecting_column = col;
1533 					}
1534 
1535 					if (!mb.doubleclick && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < 600 && cursor.line == prev_line) {
1536 						//tripleclick select line
1537 						select(cursor.line, 0, cursor.line, text[cursor.line].length());
1538 						selection.selecting_column = 0;
1539 						last_dblclk = 0;
1540 
1541 					} else if (mb.doubleclick && text[cursor.line].length()) {
1542 
1543 						//doubleclick select world
1544 						String s = text[cursor.line];
1545 						int beg = CLAMP(cursor.column, 0, s.length());
1546 						int end = beg;
1547 
1548 						if (s[beg] > 32 || beg == s.length()) {
1549 
1550 							bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
1551 
1552 							while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
1553 								beg--;
1554 							}
1555 							while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
1556 								end++;
1557 							}
1558 
1559 							if (end < s.length())
1560 								end += 1;
1561 
1562 							select(cursor.line, beg, cursor.line, end);
1563 
1564 							selection.selecting_column = beg;
1565 						}
1566 
1567 						last_dblclk = OS::get_singleton()->get_ticks_msec();
1568 					}
1569 
1570 					update();
1571 				}
1572 
1573 				if (mb.button_index == BUTTON_RIGHT) {
1574 
1575 					menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
1576 					menu->set_size(Vector2(1, 1));
1577 					menu->popup();
1578 					grab_focus();
1579 				}
1580 			} else {
1581 
1582 				if (mb.button_index == BUTTON_LEFT)
1583 					click_select_held->stop();
1584 
1585 				// notify to show soft keyboard
1586 				notification(NOTIFICATION_FOCUS_ENTER);
1587 			}
1588 
1589 		} break;
1590 		case InputEvent::MOUSE_MOTION: {
1591 
1592 			const InputEventMouseMotion &mm = p_input_event.mouse_motion;
1593 
1594 			if (mm.button_mask & BUTTON_MASK_LEFT) {
1595 
1596 				if (selection.selecting_mode != Selection::MODE_NONE) {
1597 
1598 					_reset_caret_blink_timer();
1599 
1600 					int row, col;
1601 					_get_mouse_pos(Point2i(mm.x, mm.y), row, col);
1602 
1603 					select(selection.selecting_line, selection.selecting_column, row, col);
1604 
1605 					cursor_set_line(row);
1606 					cursor_set_column(col);
1607 					update();
1608 
1609 					click_select_held->start();
1610 				}
1611 			}
1612 
1613 		} break;
1614 
1615 		case InputEvent::KEY: {
1616 
1617 			InputEventKey k = p_input_event.key;
1618 
1619 			if (!k.pressed)
1620 				return;
1621 
1622 			if (completion_active) {
1623 				if (readonly)
1624 					break;
1625 
1626 				bool valid = true;
1627 				if (k.mod.command || k.mod.meta)
1628 					valid = false;
1629 
1630 				if (valid) {
1631 
1632 					if (!k.mod.alt) {
1633 						if (k.scancode == KEY_UP) {
1634 
1635 							if (completion_index > 0) {
1636 								completion_index--;
1637 								completion_current = completion_options[completion_index];
1638 								update();
1639 							}
1640 							accept_event();
1641 							return;
1642 						}
1643 
1644 						if (k.scancode == KEY_DOWN) {
1645 
1646 							if (completion_index < completion_options.size() - 1) {
1647 								completion_index++;
1648 								completion_current = completion_options[completion_index];
1649 								update();
1650 							}
1651 							accept_event();
1652 							return;
1653 						}
1654 
1655 						if (k.scancode == KEY_PAGEUP) {
1656 
1657 							completion_index -= get_constant("completion_lines");
1658 							if (completion_index < 0)
1659 								completion_index = 0;
1660 							completion_current = completion_options[completion_index];
1661 							update();
1662 							accept_event();
1663 							return;
1664 						}
1665 
1666 						if (k.scancode == KEY_PAGEDOWN) {
1667 
1668 							completion_index += get_constant("completion_lines");
1669 							if (completion_index >= completion_options.size())
1670 								completion_index = completion_options.size() - 1;
1671 							completion_current = completion_options[completion_index];
1672 							update();
1673 							accept_event();
1674 							return;
1675 						}
1676 
1677 						if (k.scancode == KEY_HOME && completion_index > 0) {
1678 
1679 							completion_index = 0;
1680 							completion_current = completion_options[completion_index];
1681 							update();
1682 							accept_event();
1683 							return;
1684 						}
1685 
1686 						if (k.scancode == KEY_END && completion_index < completion_options.size() - 1) {
1687 
1688 							completion_index = completion_options.size() - 1;
1689 							completion_current = completion_options[completion_index];
1690 							update();
1691 							accept_event();
1692 							return;
1693 						}
1694 
1695 						if (k.scancode == KEY_DOWN) {
1696 
1697 							if (completion_index < completion_options.size() - 1) {
1698 								completion_index++;
1699 								completion_current = completion_options[completion_index];
1700 								update();
1701 							}
1702 							accept_event();
1703 							return;
1704 						}
1705 
1706 						if (k.scancode == KEY_ENTER || k.scancode == KEY_RETURN || k.scancode == KEY_TAB) {
1707 
1708 							_confirm_completion();
1709 							accept_event();
1710 							return;
1711 						}
1712 
1713 						if (k.scancode == KEY_BACKSPACE) {
1714 
1715 							_reset_caret_blink_timer();
1716 
1717 							backspace_at_cursor();
1718 							_update_completion_candidates();
1719 							accept_event();
1720 							return;
1721 						}
1722 
1723 						if (k.scancode == KEY_SHIFT) {
1724 							accept_event();
1725 							return;
1726 						}
1727 					}
1728 
1729 					if (k.unicode > 32) {
1730 
1731 						_reset_caret_blink_timer();
1732 
1733 						const CharType chr[2] = { (CharType)k.unicode, 0 };
1734 						if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
1735 							_consume_pair_symbol(chr[0]);
1736 						} else {
1737 
1738 							// remove the old character if in insert mode
1739 							if (insert_mode) {
1740 								begin_complex_operation();
1741 
1742 								// make sure we don't try and remove empty space
1743 								if (cursor.column < get_line(cursor.line).length()) {
1744 									_remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
1745 								}
1746 							}
1747 
1748 							_insert_text_at_cursor(chr);
1749 
1750 							if (insert_mode) {
1751 								end_complex_operation();
1752 							}
1753 						}
1754 						_update_completion_candidates();
1755 						accept_event();
1756 
1757 						return;
1758 					}
1759 				}
1760 
1761 				_cancel_completion();
1762 			}
1763 
1764 			/* TEST CONTROL FIRST!! */
1765 
1766 			// some remaps for duplicate functions..
1767 			if (k.mod.command && !k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode == KEY_INSERT) {
1768 
1769 				k.scancode = KEY_C;
1770 			}
1771 			if (!k.mod.command && k.mod.shift && !k.mod.alt && !k.mod.meta && k.scancode == KEY_INSERT) {
1772 
1773 				k.scancode = KEY_V;
1774 				k.mod.command = true;
1775 				k.mod.shift = false;
1776 			}
1777 
1778 			if (!k.mod.command) {
1779 				_reset_caret_blink_timer();
1780 			}
1781 			// save here for insert mode, just in case it is cleared in the following section
1782 			bool had_selection = selection.active;
1783 
1784 			// stuff to do when selection is active..
1785 			if (selection.active) {
1786 
1787 				if (readonly)
1788 					break;
1789 
1790 				bool clear = false;
1791 				bool unselect = false;
1792 				bool dobreak = false;
1793 
1794 				switch (k.scancode) {
1795 
1796 					case KEY_TAB: {
1797 						if (k.mod.shift) {
1798 							indent_selection_left();
1799 						} else {
1800 							indent_selection_right();
1801 						}
1802 						dobreak = true;
1803 						accept_event();
1804 					} break;
1805 					case KEY_X:
1806 					case KEY_C:
1807 						//special keys often used with control, wait...
1808 						clear = (!k.mod.command || k.mod.shift || k.mod.alt);
1809 						break;
1810 					case KEY_DELETE:
1811 						if (!k.mod.shift) {
1812 							accept_event();
1813 							clear = true;
1814 							dobreak = true;
1815 						} else if (k.mod.command || k.mod.alt) {
1816 							dobreak = true;
1817 						}
1818 						break;
1819 					case KEY_BACKSPACE:
1820 						accept_event();
1821 						clear = true;
1822 						dobreak = true;
1823 						break;
1824 					case KEY_LEFT:
1825 					case KEY_RIGHT:
1826 					case KEY_UP:
1827 					case KEY_DOWN:
1828 					case KEY_PAGEUP:
1829 					case KEY_PAGEDOWN:
1830 					case KEY_HOME:
1831 					case KEY_END:
1832 						// ignore arrows if any modifiers are held (shift = selecting, others may be used for editor hotkeys)
1833 						if (k.mod.command || k.mod.shift || k.mod.alt)
1834 							break;
1835 						unselect = true;
1836 						break;
1837 
1838 					default:
1839 						if (k.unicode >= 32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
1840 							clear = true;
1841 						if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode))
1842 							clear = false;
1843 				}
1844 
1845 				if (unselect) {
1846 					selection.active = false;
1847 					selection.selecting_mode = Selection::MODE_NONE;
1848 					update();
1849 				}
1850 				if (clear) {
1851 
1852 					if (!dobreak) {
1853 						begin_complex_operation();
1854 					}
1855 					selection.active = false;
1856 					update();
1857 					_remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
1858 					cursor_set_line(selection.from_line);
1859 					cursor_set_column(selection.from_column);
1860 					update();
1861 				}
1862 				if (dobreak)
1863 					break;
1864 			}
1865 
1866 			selection.selecting_text = false;
1867 
1868 			bool scancode_handled = true;
1869 
1870 			// special scancode test...
1871 
1872 			switch (k.scancode) {
1873 
1874 				case KEY_ENTER:
1875 				case KEY_RETURN: {
1876 
1877 					if (readonly)
1878 						break;
1879 
1880 					String ins = "\n";
1881 
1882 					//keep indentation
1883 					for (int i = 0; i < text[cursor.line].length(); i++) {
1884 						if (text[cursor.line][i] == '\t')
1885 							ins += "\t";
1886 						else
1887 							break;
1888 					}
1889 					if (auto_indent) {
1890 						// indent once again if previous line will end with ':'
1891 						// (i.e. colon precedes current cursor position)
1892 						if (cursor.column > 0 && text[cursor.line][cursor.column - 1] == ':') {
1893 							ins += "\t";
1894 						}
1895 					}
1896 
1897 					bool first_line = false;
1898 					if (k.mod.command) {
1899 						if (k.mod.shift) {
1900 							if (cursor.line > 0) {
1901 								cursor_set_line(cursor.line - 1);
1902 								cursor_set_column(text[cursor.line].length());
1903 							} else {
1904 								cursor_set_column(0);
1905 								first_line = true;
1906 							}
1907 						} else {
1908 							cursor_set_column(text[cursor.line].length());
1909 						}
1910 					}
1911 
1912 					_insert_text_at_cursor(ins);
1913 					_push_current_op();
1914 
1915 					if (first_line) {
1916 						cursor_set_line(0);
1917 					}
1918 
1919 				} break;
1920 				case KEY_ESCAPE: {
1921 					if (completion_hint != "") {
1922 						completion_hint = "";
1923 						update();
1924 					} else {
1925 						scancode_handled = false;
1926 					}
1927 				} break;
1928 				case KEY_TAB: {
1929 					if (k.mod.command) break; // avoid tab when command
1930 
1931 					if (readonly)
1932 						break;
1933 
1934 					if (selection.active) {
1935 
1936 					} else {
1937 						if (k.mod.shift) {
1938 
1939 							int cc = cursor.column;
1940 							if (cc > 0 && cc <= text[cursor.line].length() && text[cursor.line][cursor.column - 1] == '\t') {
1941 								//simple unindent
1942 
1943 								backspace_at_cursor();
1944 							}
1945 						} else {
1946 							//simple indent
1947 							_insert_text_at_cursor("\t");
1948 						}
1949 					}
1950 
1951 				} break;
1952 				case KEY_BACKSPACE: {
1953 					if (readonly)
1954 						break;
1955 
1956 #ifdef APPLE_STYLE_KEYS
1957 					if (k.mod.alt) {
1958 #else
1959 					if (k.mod.alt) {
1960 						scancode_handled = false;
1961 						break;
1962 					} else if (k.mod.command) {
1963 #endif
1964 						int line = cursor.line;
1965 						int column = cursor.column;
1966 
1967 						bool prev_char = false;
1968 						bool only_whitespace = true;
1969 
1970 						while (only_whitespace && line > 0) {
1971 
1972 							while (column > 0) {
1973 								CharType c = text[line][column - 1];
1974 
1975 								if (c != '\t' && c != ' ') {
1976 									only_whitespace = false;
1977 									break;
1978 								}
1979 
1980 								column--;
1981 							}
1982 
1983 							if (only_whitespace) {
1984 								line--;
1985 								column = text[line].length();
1986 							}
1987 						}
1988 
1989 						while (column > 0) {
1990 							bool ischar = _is_text_char(text[line][column - 1]);
1991 
1992 							if (prev_char && !ischar)
1993 								break;
1994 
1995 							prev_char = ischar;
1996 							column--;
1997 						}
1998 
1999 						_remove_text(line, column, cursor.line, cursor.column);
2000 
2001 						cursor_set_line(line);
2002 						cursor_set_column(column);
2003 
2004 					} else {
2005 						backspace_at_cursor();
2006 					}
2007 
2008 				} break;
2009 				case KEY_KP_4: {
2010 					if (k.unicode != 0) {
2011 						scancode_handled = false;
2012 						break;
2013 					}
2014 					// numlock disabled. fallthrough to key_left
2015 				}
2016 				case KEY_LEFT: {
2017 
2018 					if (k.mod.shift)
2019 						_pre_shift_selection();
2020 #ifdef APPLE_STYLE_KEYS
2021 					else
2022 #else
2023 					else if (!k.mod.alt)
2024 #endif
2025 						deselect();
2026 
2027 #ifdef APPLE_STYLE_KEYS
2028 					if (k.mod.command) {
2029 						cursor_set_column(0);
2030 					} else if (k.mod.alt) {
2031 
2032 #else
2033 					if (k.mod.alt) {
2034 						scancode_handled = false;
2035 						break;
2036 					} else if (k.mod.command) {
2037 #endif
2038 						bool prev_char = false;
2039 						int cc = cursor.column;
2040 
2041 						if (cc == 0 && cursor.line > 0) {
2042 							cursor_set_line(cursor.line - 1);
2043 							cursor_set_column(text[cursor.line].length());
2044 							break;
2045 						}
2046 
2047 						while (cc > 0) {
2048 
2049 							bool ischar = _is_text_char(text[cursor.line][cc - 1]);
2050 
2051 							if (prev_char && !ischar)
2052 								break;
2053 
2054 							prev_char = ischar;
2055 							cc--;
2056 						}
2057 
2058 						cursor_set_column(cc);
2059 
2060 					} else if (cursor.column == 0) {
2061 
2062 						if (cursor.line > 0) {
2063 							cursor_set_line(cursor.line - 1);
2064 							cursor_set_column(text[cursor.line].length());
2065 						}
2066 					} else {
2067 						cursor_set_column(cursor_get_column() - 1);
2068 					}
2069 
2070 					if (k.mod.shift)
2071 						_post_shift_selection();
2072 
2073 				} break;
2074 				case KEY_KP_6: {
2075 					if (k.unicode != 0) {
2076 						scancode_handled = false;
2077 						break;
2078 					}
2079 					// numlock disabled. fallthrough to key_right
2080 				}
2081 				case KEY_RIGHT: {
2082 
2083 					if (k.mod.shift)
2084 						_pre_shift_selection();
2085 #ifdef APPLE_STYLE_KEYS
2086 					else
2087 #else
2088 					else if (!k.mod.alt)
2089 #endif
2090 						deselect();
2091 
2092 #ifdef APPLE_STYLE_KEYS
2093 					if (k.mod.command) {
2094 						cursor_set_column(text[cursor.line].length());
2095 					} else if (k.mod.alt) {
2096 #else
2097 					if (k.mod.alt) {
2098 						scancode_handled = false;
2099 						break;
2100 					} else if (k.mod.command) {
2101 #endif
2102 						bool prev_char = false;
2103 						int cc = cursor.column;
2104 
2105 						if (cc == text[cursor.line].length() && cursor.line < text.size() - 1) {
2106 							cursor_set_line(cursor.line + 1);
2107 							cursor_set_column(0);
2108 							break;
2109 						}
2110 
2111 						while (cc < text[cursor.line].length()) {
2112 
2113 							bool ischar = _is_text_char(text[cursor.line][cc]);
2114 
2115 							if (prev_char && !ischar)
2116 								break;
2117 							prev_char = ischar;
2118 							cc++;
2119 						}
2120 
2121 						cursor_set_column(cc);
2122 
2123 					} else if (cursor.column == text[cursor.line].length()) {
2124 
2125 						if (cursor.line < text.size() - 1) {
2126 							cursor_set_line(cursor.line + 1);
2127 							cursor_set_column(0);
2128 						}
2129 					} else {
2130 						cursor_set_column(cursor_get_column() + 1);
2131 					}
2132 
2133 					if (k.mod.shift)
2134 						_post_shift_selection();
2135 
2136 				} break;
2137 				case KEY_KP_8: {
2138 					if (k.unicode != 0) {
2139 						scancode_handled = false;
2140 						break;
2141 					}
2142 					// numlock disabled. fallthrough to key_up
2143 				}
2144 				case KEY_UP: {
2145 
2146 					if (k.mod.shift)
2147 						_pre_shift_selection();
2148 					if (k.mod.alt) {
2149 						scancode_handled = false;
2150 						break;
2151 					}
2152 #ifndef APPLE_STYLE_KEYS
2153 					if (k.mod.command) {
2154 						_scroll_lines_up();
2155 						break;
2156 					}
2157 #else
2158 					if (k.mod.command && k.mod.alt) {
2159 						_scroll_lines_up();
2160 						break;
2161 					}
2162 
2163 					if (k.mod.command)
2164 						cursor_set_line(0);
2165 					else
2166 #endif
2167 					cursor_set_line(cursor_get_line() - 1);
2168 
2169 					if (k.mod.shift)
2170 						_post_shift_selection();
2171 					_cancel_code_hint();
2172 
2173 				} break;
2174 				case KEY_KP_2: {
2175 					if (k.unicode != 0) {
2176 						scancode_handled = false;
2177 						break;
2178 					}
2179 					// numlock disabled. fallthrough to key_down
2180 				}
2181 				case KEY_DOWN: {
2182 
2183 					if (k.mod.shift)
2184 						_pre_shift_selection();
2185 					if (k.mod.alt) {
2186 						scancode_handled = false;
2187 						break;
2188 					}
2189 #ifndef APPLE_STYLE_KEYS
2190 					if (k.mod.command) {
2191 						_scroll_lines_down();
2192 						break;
2193 					}
2194 #else
2195 					if (k.mod.command && k.mod.alt) {
2196 						_scroll_lines_down();
2197 						break;
2198 					}
2199 
2200 					if (k.mod.command)
2201 						cursor_set_line(text.size() - 1);
2202 					else
2203 #endif
2204 					cursor_set_line(cursor_get_line() + 1);
2205 
2206 					if (k.mod.shift)
2207 						_post_shift_selection();
2208 					_cancel_code_hint();
2209 
2210 				} break;
2211 
2212 				case KEY_DELETE: {
2213 
2214 					if (readonly)
2215 						break;
2216 
2217 					if (k.mod.shift && !k.mod.command && !k.mod.alt) {
2218 						cut();
2219 						break;
2220 					}
2221 
2222 					int curline_len = text[cursor.line].length();
2223 
2224 					if (cursor.line == text.size() - 1 && cursor.column == curline_len)
2225 						break; //nothing to do
2226 
2227 					int next_line = cursor.column < curline_len ? cursor.line : cursor.line + 1;
2228 					int next_column;
2229 
2230 #ifdef APPLE_STYLE_KEYS
2231 					if (k.mod.alt) {
2232 #else
2233 					if (k.mod.alt) {
2234 						scancode_handled = false;
2235 						break;
2236 					} else if (k.mod.command) {
2237 #endif
2238 						int last_line = text.size() - 1;
2239 
2240 						int line = cursor.line;
2241 						int column = cursor.column;
2242 
2243 						bool prev_char = false;
2244 						bool only_whitespace = true;
2245 
2246 						while (only_whitespace && line < last_line) {
2247 
2248 							while (column < text[line].length()) {
2249 								CharType c = text[line][column];
2250 
2251 								if (c != '\t' && c != ' ') {
2252 									only_whitespace = false;
2253 									break;
2254 								}
2255 
2256 								column++;
2257 							}
2258 
2259 							if (only_whitespace) {
2260 								line++;
2261 								column = 0;
2262 							}
2263 						}
2264 
2265 						while (column < text[line].length()) {
2266 
2267 							bool ischar = _is_text_char(text[line][column]);
2268 
2269 							if (prev_char && !ischar)
2270 								break;
2271 							prev_char = ischar;
2272 							column++;
2273 						}
2274 
2275 						next_line = line;
2276 						next_column = column;
2277 					} else {
2278 						next_column = cursor.column < curline_len ? (cursor.column + 1) : 0;
2279 					}
2280 
2281 					_remove_text(cursor.line, cursor.column, next_line, next_column);
2282 					update();
2283 
2284 				} break;
2285 				case KEY_KP_7: {
2286 					if (k.unicode != 0) {
2287 						scancode_handled = false;
2288 						break;
2289 					}
2290 					// numlock disabled. fallthrough to key_home
2291 				}
2292 #ifdef APPLE_STYLE_KEYS
2293 				case KEY_HOME: {
2294 
2295 					if (k.mod.shift)
2296 						_pre_shift_selection();
2297 
2298 					cursor_set_line(0);
2299 
2300 					if (k.mod.shift)
2301 						_post_shift_selection();
2302 					else if (k.mod.command || k.mod.control)
2303 						deselect();
2304 
2305 				} break;
2306 #else
2307 				case KEY_HOME: {
2308 
2309 					if (k.mod.shift)
2310 						_pre_shift_selection();
2311 
2312 					if (k.mod.command) {
2313 						cursor_set_line(0);
2314 						cursor_set_column(0);
2315 					} else {
2316 						// compute whitespace symbols seq length
2317 						int current_line_whitespace_len = 0;
2318 						while (current_line_whitespace_len < text[cursor.line].length()) {
2319 							CharType c = text[cursor.line][current_line_whitespace_len];
2320 							if (c != '\t' && c != ' ')
2321 								break;
2322 							current_line_whitespace_len++;
2323 						}
2324 
2325 						if (cursor_get_column() == current_line_whitespace_len)
2326 							cursor_set_column(0);
2327 						else
2328 							cursor_set_column(current_line_whitespace_len);
2329 					}
2330 
2331 					if (k.mod.shift)
2332 						_post_shift_selection();
2333 					else if (k.mod.command || k.mod.control)
2334 						deselect();
2335 					_cancel_completion();
2336 					completion_hint = "";
2337 
2338 				} break;
2339 #endif
2340 				case KEY_KP_1: {
2341 					if (k.unicode != 0) {
2342 						scancode_handled = false;
2343 						break;
2344 					}
2345 					// numlock disabled. fallthrough to key_end
2346 				}
2347 #ifdef APPLE_STYLE_KEYS
2348 				case KEY_END: {
2349 
2350 					if (k.mod.shift)
2351 						_pre_shift_selection();
2352 
2353 					cursor_set_line(text.size() - 1);
2354 
2355 					if (k.mod.shift)
2356 						_post_shift_selection();
2357 					else if (k.mod.command || k.mod.control)
2358 						deselect();
2359 
2360 				} break;
2361 #else
2362 				case KEY_END: {
2363 
2364 					if (k.mod.shift)
2365 						_pre_shift_selection();
2366 
2367 					if (k.mod.command)
2368 						cursor_set_line(text.size() - 1);
2369 					cursor_set_column(text[cursor.line].length());
2370 
2371 					if (k.mod.shift)
2372 						_post_shift_selection();
2373 					else if (k.mod.command || k.mod.control)
2374 						deselect();
2375 
2376 					_cancel_completion();
2377 					completion_hint = "";
2378 
2379 				} break;
2380 #endif
2381 				case KEY_KP_9: {
2382 					if (k.unicode != 0) {
2383 						scancode_handled = false;
2384 						break;
2385 					}
2386 					// numlock disabled. fallthrough to key_pageup
2387 				}
2388 				case KEY_PAGEUP: {
2389 
2390 					if (k.mod.shift)
2391 						_pre_shift_selection();
2392 
2393 					cursor_set_line(cursor_get_line() - get_visible_rows());
2394 
2395 					if (k.mod.shift)
2396 						_post_shift_selection();
2397 
2398 					_cancel_completion();
2399 					completion_hint = "";
2400 
2401 				} break;
2402 				case KEY_KP_3: {
2403 					if (k.unicode != 0) {
2404 						scancode_handled = false;
2405 						break;
2406 					}
2407 					// numlock disabled. fallthrough to key_pageup
2408 				}
2409 				case KEY_PAGEDOWN: {
2410 
2411 					if (k.mod.shift)
2412 						_pre_shift_selection();
2413 
2414 					cursor_set_line(cursor_get_line() + get_visible_rows());
2415 
2416 					if (k.mod.shift)
2417 						_post_shift_selection();
2418 
2419 					_cancel_completion();
2420 					completion_hint = "";
2421 
2422 				} break;
2423 				case KEY_A: {
2424 
2425 					if (!k.mod.command || k.mod.shift || k.mod.alt) {
2426 						scancode_handled = false;
2427 						break;
2428 					}
2429 
2430 					select_all();
2431 
2432 				} break;
2433 				case KEY_X: {
2434 					if (readonly) {
2435 						break;
2436 					}
2437 					if (!k.mod.command || k.mod.shift || k.mod.alt) {
2438 						scancode_handled = false;
2439 						break;
2440 					}
2441 
2442 					cut();
2443 
2444 				} break;
2445 				case KEY_C: {
2446 
2447 					if (!k.mod.command || k.mod.shift || k.mod.alt) {
2448 						scancode_handled = false;
2449 						break;
2450 					}
2451 
2452 					copy();
2453 
2454 				} break;
2455 				case KEY_Z: {
2456 
2457 					if (!k.mod.command) {
2458 						scancode_handled = false;
2459 						break;
2460 					}
2461 
2462 					if (k.mod.shift)
2463 						redo();
2464 					else
2465 						undo();
2466 				} break;
2467 				case KEY_V: {
2468 					if (readonly) {
2469 						break;
2470 					}
2471 					if (!k.mod.command || k.mod.shift || k.mod.alt) {
2472 						scancode_handled = false;
2473 						break;
2474 					}
2475 
2476 					paste();
2477 
2478 				} break;
2479 				case KEY_SPACE: {
2480 #ifdef OSX_ENABLED
2481 					if (completion_enabled && k.mod.meta) { //cmd-space is spotlight shortcut in OSX
2482 #else
2483 					if (completion_enabled && k.mod.command) {
2484 #endif
2485 
2486 						query_code_comple();
2487 						scancode_handled = true;
2488 					} else {
2489 						scancode_handled = false;
2490 					}
2491 
2492 				} break;
2493 
2494 				case KEY_U: {
2495 					if (!k.mod.command || k.mod.shift) {
2496 						scancode_handled = false;
2497 						break;
2498 					} else {
2499 						if (selection.active) {
2500 							int ini = selection.from_line;
2501 							int end = selection.to_line;
2502 							for (int i = ini; i <= end; i++) {
2503 								if (text[i][0] == '#')
2504 									_remove_text(i, 0, i, 1);
2505 							}
2506 						} else {
2507 							if (text[cursor.line][0] == '#')
2508 								_remove_text(cursor.line, 0, cursor.line, 1);
2509 						}
2510 						update();
2511 					}
2512 					break;
2513 				}
2514 
2515 				default: {
2516 
2517 					scancode_handled = false;
2518 				} break;
2519 			}
2520 
2521 			if (scancode_handled)
2522 				accept_event();
2523 			/*
2524 	    if (!scancode_handled && !k.mod.command && !k.mod.alt) {
2525 
2526 		if (k.unicode>=32) {
2527 
2528 		    if (readonly)
2529 			break;
2530 
2531 		    accept_event();
2532 		} else {
2533 
2534 		    break;
2535 		}
2536 	    }
2537 */
2538 			if (k.scancode == KEY_INSERT) {
2539 				set_insert_mode(!insert_mode);
2540 				accept_event();
2541 				return;
2542 			}
2543 
2544 			if (!scancode_handled && !k.mod.command) { //for german kbds
2545 
2546 				if (k.unicode >= 32) {
2547 
2548 					if (readonly)
2549 						break;
2550 
2551 					// remove the old character if in insert mode and no selection
2552 					if (insert_mode && !had_selection) {
2553 						begin_complex_operation();
2554 
2555 						// make sure we don't try and remove empty space
2556 						if (cursor.column < get_line(cursor.line).length()) {
2557 							_remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1);
2558 						}
2559 					}
2560 
2561 					const CharType chr[2] = { (CharType)k.unicode, 0 };
2562 
2563 					if (completion_hint != "" && k.unicode == ')') {
2564 						completion_hint = "";
2565 					}
2566 					if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
2567 						_consume_pair_symbol(chr[0]);
2568 					} else {
2569 						_insert_text_at_cursor(chr);
2570 					}
2571 
2572 					if (insert_mode && !had_selection) {
2573 						end_complex_operation();
2574 					}
2575 
2576 					if (selection.active != had_selection) {
2577 						end_complex_operation();
2578 					}
2579 					accept_event();
2580 				} else {
2581 
2582 					break;
2583 				}
2584 			}
2585 
2586 			return;
2587 		} break;
2588 	}
2589 }
2590 
2591 void TextEdit::_pre_shift_selection() {
2592 
2593 	if (!selection.active || selection.selecting_mode == Selection::MODE_NONE) {
2594 
2595 		selection.selecting_line = cursor.line;
2596 		selection.selecting_column = cursor.column;
2597 		selection.active = true;
2598 	}
2599 
2600 	selection.selecting_mode = Selection::MODE_SHIFT;
2601 }
2602 
2603 void TextEdit::_post_shift_selection() {
2604 
2605 	if (selection.active && selection.selecting_mode == Selection::MODE_SHIFT) {
2606 
2607 		select(selection.selecting_line, selection.selecting_column, cursor.line, cursor.column);
2608 		update();
2609 	}
2610 
2611 	selection.selecting_text = true;
2612 }
2613 
2614 void TextEdit::_scroll_lines_up() {
2615 	// adjust the vertical scroll
2616 	if (get_v_scroll() > 0) {
2617 		set_v_scroll(get_v_scroll() - 1);
2618 	}
2619 
2620 	// adjust the cursor
2621 	if (cursor_get_line() >= (get_visible_rows() + get_v_scroll()) && !selection.active) {
2622 		cursor_set_line((get_visible_rows() + get_v_scroll()) - 1, false);
2623 	}
2624 }
2625 
2626 void TextEdit::_scroll_lines_down() {
2627 	// calculate the maximum vertical scroll position
2628 	int max_v_scroll = get_line_count() - 1;
2629 	if (!scroll_past_end_of_file_enabled) {
2630 		max_v_scroll -= get_visible_rows() - 1;
2631 	}
2632 
2633 	// adjust the vertical scroll
2634 	if (get_v_scroll() < max_v_scroll) {
2635 		set_v_scroll(get_v_scroll() + 1);
2636 	}
2637 
2638 	// adjust the cursor
2639 	if ((cursor_get_line()) <= get_v_scroll() - 1 && !selection.active) {
2640 		cursor_set_line(get_v_scroll(), false);
2641 	}
2642 }
2643 
2644 /**** TEXT EDIT CORE API ****/
2645 
2646 void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) {
2647 
2648 	//save for undo...
2649 	ERR_FAIL_INDEX(p_line, text.size());
2650 	ERR_FAIL_COND(p_char < 0);
2651 
2652 	/* STEP 1 add spaces if the char is greater than the end of the line */
2653 	while (p_char > text[p_line].length()) {
2654 
2655 		text.set(p_line, text[p_line] + String::chr(' '));
2656 	}
2657 
2658 	/* STEP 2 separate dest string in pre and post text */
2659 
2660 	String preinsert_text = text[p_line].substr(0, p_char);
2661 	String postinsert_text = text[p_line].substr(p_char, text[p_line].size());
2662 
2663 	/* STEP 3 remove \r from source text and separate in substrings */
2664 
2665 	//buh bye \r and split
2666 	Vector<String> substrings = p_text.replace("\r", "").split("\n");
2667 
2668 	for (int i = 0; i < substrings.size(); i++) {
2669 		//insert the substrings
2670 
2671 		if (i == 0) {
2672 
2673 			text.set(p_line, preinsert_text + substrings[i]);
2674 		} else {
2675 
2676 			text.insert(p_line + i, substrings[i]);
2677 		}
2678 
2679 		if (i == substrings.size() - 1) {
2680 
2681 			text.set(p_line + i, text[p_line + i] + postinsert_text);
2682 		}
2683 	}
2684 
2685 	r_end_line = p_line + substrings.size() - 1;
2686 	r_end_column = text[r_end_line].length() - postinsert_text.length();
2687 
2688 	if (!text_changed_dirty && !setting_text) {
2689 		if (is_inside_tree())
2690 			MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
2691 		text_changed_dirty = true;
2692 	}
2693 }
2694 
2695 String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const {
2696 
2697 	ERR_FAIL_INDEX_V(p_from_line, text.size(), String());
2698 	ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String());
2699 	ERR_FAIL_INDEX_V(p_to_line, text.size(), String());
2700 	ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String());
2701 	ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // from > to
2702 	ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // from > to
2703 
2704 	String ret;
2705 
2706 	for (int i = p_from_line; i <= p_to_line; i++) {
2707 
2708 		int begin = (i == p_from_line) ? p_from_column : 0;
2709 		int end = (i == p_to_line) ? p_to_column : text[i].length();
2710 
2711 		if (i > p_from_line)
2712 			ret += "\n";
2713 		ret += text[i].substr(begin, end - begin);
2714 	}
2715 
2716 	return ret;
2717 }
2718 
2719 void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
2720 
2721 	ERR_FAIL_INDEX(p_from_line, text.size());
2722 	ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1);
2723 	ERR_FAIL_INDEX(p_to_line, text.size());
2724 	ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1);
2725 	ERR_FAIL_COND(p_to_line < p_from_line); // from > to
2726 	ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // from > to
2727 
2728 	String pre_text = text[p_from_line].substr(0, p_from_column);
2729 	String post_text = text[p_to_line].substr(p_to_column, text[p_to_line].length());
2730 
2731 	for (int i = p_from_line; i < p_to_line; i++) {
2732 
2733 		text.remove(p_from_line + 1);
2734 	}
2735 
2736 	text.set(p_from_line, pre_text + post_text);
2737 
2738 	if (!text_changed_dirty && !setting_text) {
2739 		if (is_inside_tree())
2740 			MessageQueue::get_singleton()->push_call(this, "_text_changed_emit");
2741 		text_changed_dirty = true;
2742 	}
2743 }
2744 
2745 void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_column) {
2746 
2747 	if (!setting_text)
2748 		idle_detect->start();
2749 
2750 	if (undo_enabled) {
2751 		_clear_redo();
2752 	}
2753 
2754 	int retline, retchar;
2755 	_base_insert_text(p_line, p_char, p_text, retline, retchar);
2756 	if (r_end_line)
2757 		*r_end_line = retline;
2758 	if (r_end_column)
2759 		*r_end_column = retchar;
2760 
2761 	if (!undo_enabled)
2762 		return;
2763 
2764 	/* UNDO!! */
2765 	TextOperation op;
2766 	op.type = TextOperation::TYPE_INSERT;
2767 	op.from_line = p_line;
2768 	op.from_column = p_char;
2769 	op.to_line = retline;
2770 	op.to_column = retchar;
2771 	op.text = p_text;
2772 	op.version = ++version;
2773 	op.chain_forward = false;
2774 	op.chain_backward = false;
2775 
2776 	//see if it should just be set as current op
2777 	if (current_op.type != op.type) {
2778 		op.prev_version = get_version();
2779 		_push_current_op();
2780 		current_op = op;
2781 
2782 		return; //set as current op, return
2783 	}
2784 	//see if it can be merged
2785 	if (current_op.to_line != p_line || current_op.to_column != p_char) {
2786 		op.prev_version = get_version();
2787 		_push_current_op();
2788 		current_op = op;
2789 		return; //set as current op, return
2790 	}
2791 	//merge current op
2792 
2793 	current_op.text += p_text;
2794 	current_op.to_column = retchar;
2795 	current_op.to_line = retline;
2796 	current_op.version = op.version;
2797 }
2798 
2799 void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
2800 
2801 	if (!setting_text)
2802 		idle_detect->start();
2803 
2804 	String text;
2805 	if (undo_enabled) {
2806 		_clear_redo();
2807 		text = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column);
2808 	}
2809 
2810 	_base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column);
2811 
2812 	if (!undo_enabled)
2813 		return;
2814 
2815 	/* UNDO!! */
2816 	TextOperation op;
2817 	op.type = TextOperation::TYPE_REMOVE;
2818 	op.from_line = p_from_line;
2819 	op.from_column = p_from_column;
2820 	op.to_line = p_to_line;
2821 	op.to_column = p_to_column;
2822 	op.text = text;
2823 	op.version = ++version;
2824 	op.chain_forward = false;
2825 	op.chain_backward = false;
2826 
2827 	//see if it should just be set as current op
2828 	if (current_op.type != op.type) {
2829 		op.prev_version = get_version();
2830 		_push_current_op();
2831 		current_op = op;
2832 		return; //set as current op, return
2833 	}
2834 	//see if it can be merged
2835 	if (current_op.from_line == p_to_line && current_op.from_column == p_to_column) {
2836 		//basckace or similar
2837 		current_op.text = text + current_op.text;
2838 		current_op.from_line = p_from_line;
2839 		current_op.from_column = p_from_column;
2840 		return; //update current op
2841 	}
2842 
2843 	op.prev_version = get_version();
2844 	_push_current_op();
2845 	current_op = op;
2846 }
2847 
2848 void TextEdit::_insert_text_at_cursor(const String &p_text) {
2849 
2850 	int new_column, new_line;
2851 	_insert_text(cursor.line, cursor.column, p_text, &new_line, &new_column);
2852 	cursor_set_line(new_line);
2853 	cursor_set_column(new_column);
2854 
2855 	update();
2856 }
2857 
2858 int TextEdit::get_char_count() {
2859 
2860 	int totalsize = 0;
2861 
2862 	for (int i = 0; i < text.size(); i++) {
2863 
2864 		if (i > 0)
2865 			totalsize++; // incliude \n
2866 		totalsize += text[i].length();
2867 	}
2868 
2869 	return totalsize; // omit last \n
2870 }
2871 
2872 Size2 TextEdit::get_minimum_size() const {
2873 
2874 	return cache.style_normal->get_minimum_size();
2875 }
2876 int TextEdit::get_visible_rows() const {
2877 
2878 	int total = cache.size.height;
2879 	total -= cache.style_normal->get_minimum_size().height;
2880 	total /= get_row_height();
2881 	return total;
2882 }
2883 void TextEdit::adjust_viewport_to_cursor() {
2884 
2885 	if (cursor.line_ofs > cursor.line)
2886 		cursor.line_ofs = cursor.line;
2887 
2888 	int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width;
2889 	if (v_scroll->is_visible())
2890 		visible_width -= v_scroll->get_combined_minimum_size().width;
2891 	visible_width -= 20; // give it a little more space
2892 
2893 	//printf("rowofs %i, visrows %i, cursor.line %i\n",cursor.line_ofs,get_visible_rows(),cursor.line);
2894 
2895 	int visible_rows = get_visible_rows();
2896 	if (h_scroll->is_visible())
2897 		visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
2898 
2899 	if (cursor.line >= (cursor.line_ofs + visible_rows))
2900 		cursor.line_ofs = cursor.line - visible_rows + 1;
2901 	if (cursor.line < cursor.line_ofs)
2902 		cursor.line_ofs = cursor.line;
2903 
2904 	int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
2905 
2906 	if (cursor_x > (cursor.x_ofs + visible_width))
2907 		cursor.x_ofs = cursor_x - visible_width + 1;
2908 
2909 	if (cursor_x < cursor.x_ofs)
2910 		cursor.x_ofs = cursor_x;
2911 
2912 	update();
2913 	/*
2914     get_range()->set_max(text.size());
2915 
2916     get_range()->set_page(get_visible_rows());
2917 
2918     get_range()->set((int)cursor.line_ofs);
2919 */
2920 }
2921 
2922 void TextEdit::center_viewport_to_cursor() {
2923 
2924 	if (cursor.line_ofs > cursor.line)
2925 		cursor.line_ofs = cursor.line;
2926 
2927 	int visible_width = cache.size.width - cache.style_normal->get_minimum_size().width - cache.line_number_w - cache.breakpoint_gutter_width;
2928 	if (v_scroll->is_visible())
2929 		visible_width -= v_scroll->get_combined_minimum_size().width;
2930 	visible_width -= 20; // give it a little more space
2931 
2932 	int visible_rows = get_visible_rows();
2933 	if (h_scroll->is_visible())
2934 		visible_rows -= ((h_scroll->get_combined_minimum_size().height - 1) / get_row_height());
2935 
2936 	int max_ofs = text.size() - (scroll_past_end_of_file_enabled ? 1 : visible_rows);
2937 	cursor.line_ofs = CLAMP(cursor.line - (visible_rows / 2), 0, max_ofs);
2938 
2939 	int cursor_x = get_column_x_offset(cursor.column, text[cursor.line]);
2940 
2941 	if (cursor_x > (cursor.x_ofs + visible_width))
2942 		cursor.x_ofs = cursor_x - visible_width + 1;
2943 
2944 	if (cursor_x < cursor.x_ofs)
2945 		cursor.x_ofs = cursor_x;
2946 
2947 	update();
2948 }
2949 
2950 void TextEdit::cursor_set_column(int p_col, bool p_adjust_viewport) {
2951 
2952 	if (p_col < 0)
2953 		p_col = 0;
2954 
2955 	cursor.column = p_col;
2956 	if (cursor.column > get_line(cursor.line).length())
2957 		cursor.column = get_line(cursor.line).length();
2958 
2959 	cursor.last_fit_x = get_column_x_offset(cursor.column, get_line(cursor.line));
2960 
2961 	if (p_adjust_viewport)
2962 		adjust_viewport_to_cursor();
2963 
2964 	if (!cursor_changed_dirty) {
2965 		if (is_inside_tree())
2966 			MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
2967 		cursor_changed_dirty = true;
2968 	}
2969 }
2970 
2971 void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport) {
2972 
2973 	if (setting_row)
2974 		return;
2975 
2976 	setting_row = true;
2977 	if (p_row < 0)
2978 		p_row = 0;
2979 
2980 	if (p_row >= (int)text.size())
2981 		p_row = (int)text.size() - 1;
2982 
2983 	cursor.line = p_row;
2984 	cursor.column = get_char_pos_for(cursor.last_fit_x, get_line(cursor.line));
2985 
2986 	if (p_adjust_viewport)
2987 		adjust_viewport_to_cursor();
2988 
2989 	setting_row = false;
2990 
2991 	if (!cursor_changed_dirty) {
2992 		if (is_inside_tree())
2993 			MessageQueue::get_singleton()->push_call(this, "_cursor_changed_emit");
2994 		cursor_changed_dirty = true;
2995 	}
2996 }
2997 
2998 int TextEdit::cursor_get_column() const {
2999 
3000 	return cursor.column;
3001 }
3002 
3003 int TextEdit::cursor_get_line() const {
3004 
3005 	return cursor.line;
3006 }
3007 
3008 bool TextEdit::cursor_get_blink_enabled() const {
3009 	return caret_blink_enabled;
3010 }
3011 
3012 void TextEdit::cursor_set_blink_enabled(const bool p_enabled) {
3013 	caret_blink_enabled = p_enabled;
3014 
3015 	if (p_enabled) {
3016 		caret_blink_timer->start();
3017 	} else {
3018 		caret_blink_timer->stop();
3019 	}
3020 	draw_caret = true;
3021 }
3022 
3023 float TextEdit::cursor_get_blink_speed() const {
3024 	return caret_blink_timer->get_wait_time();
3025 }
3026 
3027 void TextEdit::cursor_set_blink_speed(const float p_speed) {
3028 	ERR_FAIL_COND(p_speed <= 0);
3029 	caret_blink_timer->set_wait_time(p_speed);
3030 }
3031 
3032 void TextEdit::cursor_set_block_mode(const bool p_enable) {
3033 	block_caret = p_enable;
3034 	update();
3035 }
3036 
3037 bool TextEdit::cursor_is_block_mode() const {
3038 	return block_caret;
3039 }
3040 
3041 void TextEdit::_scroll_moved(double p_to_val) {
3042 
3043 	if (updating_scrolls)
3044 		return;
3045 
3046 	if (h_scroll->is_visible())
3047 		cursor.x_ofs = h_scroll->get_val();
3048 	if (v_scroll->is_visible())
3049 		cursor.line_ofs = v_scroll->get_val();
3050 	update();
3051 }
3052 
3053 int TextEdit::get_row_height() const {
3054 
3055 	return cache.font->get_height() + cache.line_spacing;
3056 }
3057 
3058 int TextEdit::get_char_pos_for(int p_px, String p_str) const {
3059 
3060 	int px = 0;
3061 	int c = 0;
3062 
3063 	int tab_w = cache.font->get_char_size(' ').width * tab_size;
3064 
3065 	while (c < p_str.length()) {
3066 
3067 		int w = 0;
3068 
3069 		if (p_str[c] == '\t') {
3070 
3071 			int left = px % tab_w;
3072 			if (left == 0)
3073 				w = tab_w;
3074 			else
3075 				w = tab_w - px % tab_w; // is right...
3076 
3077 		} else {
3078 
3079 			w = cache.font->get_char_size(p_str[c], p_str[c + 1]).width;
3080 		}
3081 
3082 		if (p_px < (px + w / 2))
3083 			break;
3084 		px += w;
3085 		c++;
3086 	}
3087 
3088 	return c;
3089 }
3090 
3091 int TextEdit::get_column_x_offset(int p_char, String p_str) {
3092 
3093 	int px = 0;
3094 
3095 	int tab_w = cache.font->get_char_size(' ').width * tab_size;
3096 
3097 	for (int i = 0; i < p_char; i++) {
3098 
3099 		if (i >= p_str.length())
3100 			break;
3101 
3102 		if (p_str[i] == '\t') {
3103 
3104 			int left = px % tab_w;
3105 			if (left == 0)
3106 				px += tab_w;
3107 			else
3108 				px += tab_w - px % tab_w; // is right...
3109 
3110 		} else {
3111 			px += cache.font->get_char_size(p_str[i], p_str[i + 1]).width;
3112 		}
3113 	}
3114 
3115 	return px;
3116 }
3117 
3118 void TextEdit::insert_text_at_cursor(const String &p_text) {
3119 
3120 	if (selection.active) {
3121 
3122 		cursor_set_line(selection.from_line);
3123 		cursor_set_column(selection.from_column);
3124 
3125 		_remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3126 		selection.active = false;
3127 		selection.selecting_mode = Selection::MODE_NONE;
3128 	}
3129 
3130 	_insert_text_at_cursor(p_text);
3131 	update();
3132 }
3133 
3134 Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const {
3135 	int gutter = cache.style_normal->get_margin(MARGIN_LEFT) + cache.line_number_w + cache.breakpoint_gutter_width;
3136 	if ((completion_active && completion_rect.has_point(p_pos)) || p_pos.x < gutter) {
3137 		return CURSOR_ARROW;
3138 	}
3139 	return CURSOR_IBEAM;
3140 }
3141 
3142 void TextEdit::set_text(String p_text) {
3143 
3144 	setting_text = true;
3145 	clear();
3146 	_insert_text_at_cursor(p_text);
3147 	clear_undo_history();
3148 	cursor.column = 0;
3149 	cursor.line = 0;
3150 	cursor.x_ofs = 0;
3151 	cursor.line_ofs = 0;
3152 	cursor.last_fit_x = 0;
3153 	cursor_set_line(0);
3154 	cursor_set_column(0);
3155 	update();
3156 	setting_text = false;
3157 
3158 	//get_range()->set(0);
3159 };
3160 
3161 String TextEdit::get_text() {
3162 	String longthing;
3163 	int len = text.size();
3164 	for (int i = 0; i < len; i++) {
3165 
3166 		longthing += text[i];
3167 		if (i != len - 1)
3168 			longthing += "\n";
3169 	}
3170 
3171 	return longthing;
3172 };
3173 
3174 String TextEdit::get_text_for_completion() {
3175 
3176 	String longthing;
3177 	int len = text.size();
3178 	for (int i = 0; i < len; i++) {
3179 
3180 		if (i == cursor.line) {
3181 			longthing += text[i].substr(0, cursor.column);
3182 			longthing += String::chr(0xFFFF); //not unicode, represents the cursor
3183 			longthing += text[i].substr(cursor.column, text[i].size());
3184 		} else {
3185 
3186 			longthing += text[i];
3187 		}
3188 
3189 		if (i != len - 1)
3190 			longthing += "\n";
3191 	}
3192 
3193 	return longthing;
3194 };
3195 
3196 String TextEdit::get_line(int line) const {
3197 
3198 	if (line < 0 || line >= text.size())
3199 		return "";
3200 
3201 	return text[line];
3202 };
3203 
3204 void TextEdit::_clear() {
3205 
3206 	if (undo_enabled) {
3207 		String undo_text = get_text();
3208 
3209 		if (undo_text.length() > 0) {
3210 			_clear_redo();
3211 
3212 			/* UNDO!! */
3213 			TextOperation op;
3214 			op.type = TextOperation::TYPE_CLEAR;
3215 			op.from_line = 0;
3216 			op.from_column = 0;
3217 			op.to_line = MAX(0, get_line_count() - 1);
3218 			op.to_column = get_line(op.to_line).length();
3219 			op.text = undo_text;
3220 			op.version = ++version;
3221 			op.chain_forward = false;
3222 			op.chain_backward = false;
3223 
3224 			op.prev_version = get_version();
3225 			_push_current_op();
3226 			current_op = op;
3227 		}
3228 	} else {
3229 		clear_undo_history();
3230 	}
3231 
3232 	text.clear();
3233 	cursor.column = 0;
3234 	cursor.line = 0;
3235 	cursor.x_ofs = 0;
3236 	cursor.line_ofs = 0;
3237 	cursor.last_fit_x = 0;
3238 }
3239 
3240 void TextEdit::clear() {
3241 
3242 	setting_text = true;
3243 	_clear();
3244 	setting_text = false;
3245 };
3246 
3247 void TextEdit::set_readonly(bool p_readonly) {
3248 
3249 	readonly = p_readonly;
3250 }
3251 
3252 void TextEdit::set_wrap(bool p_wrap) {
3253 
3254 	wrap = p_wrap;
3255 }
3256 
3257 void TextEdit::set_max_chars(int p_max_chars) {
3258 
3259 	max_chars = p_max_chars;
3260 }
3261 
3262 void TextEdit::_reset_caret_blink_timer() {
3263 	if (caret_blink_enabled) {
3264 		caret_blink_timer->stop();
3265 		caret_blink_timer->start();
3266 		draw_caret = true;
3267 		update();
3268 	}
3269 }
3270 
3271 void TextEdit::_toggle_draw_caret() {
3272 	draw_caret = !draw_caret;
3273 	if (is_visible() && has_focus() && window_has_focus) {
3274 		update();
3275 	}
3276 }
3277 
3278 void TextEdit::_update_caches() {
3279 
3280 	cache.style_normal = get_stylebox("normal");
3281 	cache.style_focus = get_stylebox("focus");
3282 	cache.completion_background_color = get_color("completion_background_color");
3283 	cache.completion_selected_color = get_color("completion_selected_color");
3284 	cache.completion_existing_color = get_color("completion_existing_color");
3285 	cache.completion_font_color = get_color("completion_font_color");
3286 	cache.font = get_font("font");
3287 	cache.caret_color = get_color("caret_color");
3288 	cache.caret_background_color = get_color("caret_background_color");
3289 	cache.line_number_color = get_color("line_number_color");
3290 	cache.font_color = get_color("font_color");
3291 	cache.font_selected_color = get_color("font_selected_color");
3292 	cache.keyword_color = get_color("keyword_color");
3293 	cache.function_color = get_color("function_color");
3294 	cache.member_variable_color = get_color("member_variable_color");
3295 	cache.number_color = get_color("number_color");
3296 	cache.selection_color = get_color("selection_color");
3297 	cache.mark_color = get_color("mark_color");
3298 	cache.current_line_color = get_color("current_line_color");
3299 	cache.line_length_guideline_color = get_color("line_length_guideline_color");
3300 	cache.breakpoint_color = get_color("breakpoint_color");
3301 	cache.brace_mismatch_color = get_color("brace_mismatch_color");
3302 	cache.word_highlighted_color = get_color("word_highlighted_color");
3303 	cache.search_result_color = get_color("search_result_color");
3304 	cache.search_result_border_color = get_color("search_result_border_color");
3305 	cache.line_spacing = get_constant("line_spacing");
3306 	cache.row_height = cache.font->get_height() + cache.line_spacing;
3307 	cache.tab_icon = get_icon("tab");
3308 	text.set_font(cache.font);
3309 }
3310 
3311 void TextEdit::clear_colors() {
3312 
3313 	keywords.clear();
3314 	color_regions.clear();
3315 	;
3316 	text.clear_caches();
3317 	custom_bg_color = Color(0, 0, 0, 0);
3318 }
3319 
3320 void TextEdit::set_custom_bg_color(const Color &p_color) {
3321 
3322 	custom_bg_color = p_color;
3323 	update();
3324 }
3325 
3326 void TextEdit::add_keyword_color(const String &p_keyword, const Color &p_color) {
3327 
3328 	keywords[p_keyword] = p_color;
3329 	update();
3330 }
3331 
3332 void TextEdit::add_color_region(const String &p_begin_key, const String &p_end_key, const Color &p_color, bool p_line_only) {
3333 
3334 	color_regions.push_back(ColorRegion(p_begin_key, p_end_key, p_color, p_line_only));
3335 	text.clear_caches();
3336 	update();
3337 }
3338 
3339 void TextEdit::set_symbol_color(const Color &p_color) {
3340 
3341 	symbol_color = p_color;
3342 	update();
3343 }
3344 
3345 void TextEdit::set_syntax_coloring(bool p_enabled) {
3346 
3347 	syntax_coloring = p_enabled;
3348 	update();
3349 }
3350 
3351 bool TextEdit::is_syntax_coloring_enabled() const {
3352 
3353 	return syntax_coloring;
3354 }
3355 
3356 void TextEdit::set_auto_indent(bool p_auto_indent) {
3357 	auto_indent = p_auto_indent;
3358 }
3359 
3360 void TextEdit::cut() {
3361 
3362 	if (!selection.active) {
3363 
3364 		String clipboard = text[cursor.line];
3365 		OS::get_singleton()->set_clipboard(clipboard);
3366 		cursor_set_line(cursor.line);
3367 		cursor_set_column(0);
3368 		_remove_text(cursor.line, 0, cursor.line, text[cursor.line].length());
3369 
3370 		backspace_at_cursor();
3371 		update();
3372 		cursor_set_line(cursor.line + 1);
3373 		cut_copy_line = true;
3374 
3375 	} else {
3376 
3377 		String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3378 		OS::get_singleton()->set_clipboard(clipboard);
3379 
3380 		cursor_set_line(selection.from_line);
3381 		cursor_set_column(selection.from_column);
3382 
3383 		_remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3384 		selection.active = false;
3385 		selection.selecting_mode = Selection::MODE_NONE;
3386 		update();
3387 		cut_copy_line = false;
3388 	}
3389 }
3390 
3391 void TextEdit::convert_case(int p_case) {
3392 	if (selection.active) {
3393 		String text = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3394 		selection.active = false;
3395 		_remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3396 		cursor_set_line(selection.from_line);
3397 		cursor_set_column(selection.from_column);
3398 		if (p_case == UPPERCASE) {
3399 			_insert_text_at_cursor(text.to_upper());
3400 		} else if (p_case == LOWERCASE) {
3401 			_insert_text_at_cursor(text.to_lower());
3402 		}
3403 	}
3404 }
3405 
3406 void TextEdit::copy() {
3407 
3408 	if (!selection.active) {
3409 		String clipboard = _base_get_text(cursor.line, 0, cursor.line, text[cursor.line].length());
3410 		OS::get_singleton()->set_clipboard(clipboard);
3411 		cut_copy_line = true;
3412 	} else {
3413 		String clipboard = _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3414 		OS::get_singleton()->set_clipboard(clipboard);
3415 		cut_copy_line = false;
3416 	}
3417 }
3418 
3419 void TextEdit::paste() {
3420 
3421 	String clipboard = OS::get_singleton()->get_clipboard();
3422 
3423 	if (selection.active) {
3424 
3425 		selection.active = false;
3426 		selection.selecting_mode = Selection::MODE_NONE;
3427 		_remove_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3428 		cursor_set_line(selection.from_line);
3429 		cursor_set_column(selection.from_column);
3430 
3431 	} else if (cut_copy_line) {
3432 
3433 		cursor_set_column(0);
3434 		String ins = "\n";
3435 		clipboard += ins;
3436 	}
3437 
3438 	_insert_text_at_cursor(clipboard);
3439 	update();
3440 }
3441 
3442 void TextEdit::select_all() {
3443 
3444 	if (text.size() == 1 && text[0].length() == 0)
3445 		return;
3446 	selection.active = true;
3447 	selection.from_line = 0;
3448 	selection.from_column = 0;
3449 	selection.selecting_line = 0;
3450 	selection.selecting_column = 0;
3451 	selection.to_line = text.size() - 1;
3452 	selection.to_column = text[selection.to_line].length();
3453 	selection.selecting_mode = Selection::MODE_SHIFT;
3454 	selection.shiftclick_left = true;
3455 	cursor_set_line(selection.to_line, false);
3456 	cursor_set_column(selection.to_column, false);
3457 	update();
3458 }
3459 
3460 void TextEdit::deselect() {
3461 
3462 	selection.active = false;
3463 	update();
3464 }
3465 
3466 void TextEdit::select(int p_from_line, int p_from_column, int p_to_line, int p_to_column) {
3467 
3468 	if (p_from_line >= text.size())
3469 		p_from_line = text.size() - 1;
3470 	if (p_from_column >= text[p_from_line].length())
3471 		p_from_column = text[p_from_line].length();
3472 
3473 	if (p_to_line >= text.size())
3474 		p_to_line = text.size() - 1;
3475 	if (p_to_column >= text[p_to_line].length())
3476 		p_to_column = text[p_to_line].length();
3477 
3478 	selection.from_line = p_from_line;
3479 	selection.from_column = p_from_column;
3480 	selection.to_line = p_to_line;
3481 	selection.to_column = p_to_column;
3482 
3483 	selection.active = true;
3484 
3485 	if (selection.from_line == selection.to_line) {
3486 
3487 		if (selection.from_column == selection.to_column) {
3488 
3489 			selection.active = false;
3490 
3491 		} else if (selection.from_column > selection.to_column) {
3492 
3493 			selection.shiftclick_left = false;
3494 			SWAP(selection.from_column, selection.to_column);
3495 		} else {
3496 
3497 			selection.shiftclick_left = true;
3498 		}
3499 	} else if (selection.from_line > selection.to_line) {
3500 
3501 		selection.shiftclick_left = false;
3502 		SWAP(selection.from_line, selection.to_line);
3503 		SWAP(selection.from_column, selection.to_column);
3504 	} else {
3505 
3506 		selection.shiftclick_left = true;
3507 	}
3508 
3509 	update();
3510 }
3511 
3512 bool TextEdit::is_selection_active() const {
3513 
3514 	return selection.active;
3515 }
3516 int TextEdit::get_selection_from_line() const {
3517 
3518 	ERR_FAIL_COND_V(!selection.active, -1);
3519 	return selection.from_line;
3520 }
3521 int TextEdit::get_selection_from_column() const {
3522 
3523 	ERR_FAIL_COND_V(!selection.active, -1);
3524 	return selection.from_column;
3525 }
3526 int TextEdit::get_selection_to_line() const {
3527 
3528 	ERR_FAIL_COND_V(!selection.active, -1);
3529 	return selection.to_line;
3530 }
3531 int TextEdit::get_selection_to_column() const {
3532 
3533 	ERR_FAIL_COND_V(!selection.active, -1);
3534 	return selection.to_column;
3535 }
3536 
3537 String TextEdit::get_selection_text() const {
3538 
3539 	if (!selection.active)
3540 		return "";
3541 
3542 	return _base_get_text(selection.from_line, selection.from_column, selection.to_line, selection.to_column);
3543 }
3544 
3545 String TextEdit::get_word_under_cursor() const {
3546 
3547 	int prev_cc = cursor.column;
3548 	while (prev_cc > 0) {
3549 		bool is_char = _is_text_char(text[cursor.line][prev_cc - 1]);
3550 		if (!is_char)
3551 			break;
3552 		--prev_cc;
3553 	}
3554 
3555 	int next_cc = cursor.column;
3556 	while (next_cc < text[cursor.line].length()) {
3557 		bool is_char = _is_text_char(text[cursor.line][next_cc]);
3558 		if (!is_char)
3559 			break;
3560 		++next_cc;
3561 	}
3562 	if (prev_cc == cursor.column || next_cc == cursor.column)
3563 		return "";
3564 	return text[cursor.line].substr(prev_cc, next_cc - prev_cc);
3565 }
3566 
3567 void TextEdit::set_search_text(const String &p_search_text) {
3568 	search_text = p_search_text;
3569 }
3570 
3571 void TextEdit::set_search_flags(uint32_t p_flags) {
3572 	search_flags = p_flags;
3573 }
3574 
3575 void TextEdit::set_current_search_result(int line, int col) {
3576 	search_result_line = line;
3577 	search_result_col = col;
3578 	update();
3579 }
3580 
3581 void TextEdit::set_highlight_all_occurrences(const bool p_enabled) {
3582 	highlight_all_occurrences = p_enabled;
3583 	update();
3584 }
3585 
3586 bool TextEdit::is_highlight_all_occurrences_enabled() const {
3587 	return highlight_all_occurrences;
3588 }
3589 
3590 int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) {
3591 	int col = -1;
3592 
3593 	if (p_key.length() > 0 && p_search.length() > 0) {
3594 		if (p_from_column < 0 || p_from_column > p_search.length()) {
3595 			p_from_column = 0;
3596 		}
3597 
3598 		while (col == -1 && p_from_column <= p_search.length()) {
3599 			if (p_search_flags & SEARCH_MATCH_CASE) {
3600 				col = p_search.find(p_key, p_from_column);
3601 			} else {
3602 				col = p_search.findn(p_key, p_from_column);
3603 			}
3604 
3605 			// whole words only
3606 			if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) {
3607 				p_from_column = col;
3608 
3609 				if (col > 0 && _is_text_char(p_search[col - 1])) {
3610 					col = -1;
3611 				} else if ((col + p_key.length()) < p_search.length() && _is_text_char(p_search[col + p_key.length()])) {
3612 					col = -1;
3613 				}
3614 			}
3615 
3616 			p_from_column += 1;
3617 		}
3618 	}
3619 	return col;
3620 }
3621 
3622 DVector<int> TextEdit::_search_bind(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const {
3623 
3624 	int col, line;
3625 	if (search(p_key, p_search_flags, p_from_line, p_from_column, col, line)) {
3626 		DVector<int> result;
3627 		result.resize(2);
3628 		result.set(0, line);
3629 		result.set(1, col);
3630 		return result;
3631 
3632 	} else {
3633 
3634 		return DVector<int>();
3635 	}
3636 }
3637 
3638 bool TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column, int &r_line, int &r_column) const {
3639 
3640 	if (p_key.length() == 0)
3641 		return false;
3642 	ERR_FAIL_INDEX_V(p_from_line, text.size(), false);
3643 	ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, false);
3644 
3645 	//search through the whole documment, but start by current line
3646 
3647 	int line = -1;
3648 	int pos = -1;
3649 
3650 	line = p_from_line;
3651 
3652 	for (int i = 0; i < text.size() + 1; i++) {
3653 		//backwards is broken...
3654 		//int idx=(p_search_flags&SEARCH_BACKWARDS)?(text.size()-i):i; //do backwards seearch
3655 
3656 		if (line < 0) {
3657 			line = text.size() - 1;
3658 		}
3659 		if (line == text.size()) {
3660 			line = 0;
3661 		}
3662 
3663 		String text_line = text[line];
3664 		int from_column = 0;
3665 		if (line == p_from_line) {
3666 
3667 			if (i == text.size()) {
3668 				//wrapped
3669 
3670 				if (p_search_flags & SEARCH_BACKWARDS) {
3671 					from_column = text_line.length();
3672 				} else {
3673 					from_column = 0;
3674 				}
3675 
3676 			} else {
3677 
3678 				from_column = p_from_column;
3679 			}
3680 
3681 		} else {
3682 			if (p_search_flags & SEARCH_BACKWARDS)
3683 				from_column = text_line.length() - 1;
3684 			else
3685 				from_column = 0;
3686 		}
3687 
3688 		pos = -1;
3689 
3690 		int pos_from = 0;
3691 		int last_pos = -1;
3692 		while ((last_pos = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, pos_from) : text_line.findn(p_key, pos_from)) != -1) {
3693 
3694 			if (p_search_flags & SEARCH_BACKWARDS) {
3695 
3696 				if (last_pos > from_column)
3697 					break;
3698 				pos = last_pos;
3699 
3700 			} else {
3701 
3702 				if (last_pos >= from_column) {
3703 					pos = last_pos;
3704 					break;
3705 				}
3706 			}
3707 
3708 			pos_from = last_pos + p_key.length();
3709 		}
3710 
3711 		if (pos != -1 && (p_search_flags & SEARCH_WHOLE_WORDS)) {
3712 			//validate for whole words
3713 			if (pos > 0 && _is_text_char(text_line[pos - 1]))
3714 				pos = -1;
3715 			else if (_is_text_char(text_line[pos + p_key.length()]))
3716 				pos = -1;
3717 		}
3718 
3719 		if (pos != -1)
3720 			break;
3721 
3722 		if (p_search_flags & SEARCH_BACKWARDS)
3723 			line--;
3724 		else
3725 			line++;
3726 	}
3727 
3728 	if (pos == -1) {
3729 		r_line = -1;
3730 		r_column = -1;
3731 		return false;
3732 	}
3733 
3734 	r_line = line;
3735 	r_column = pos;
3736 
3737 	return true;
3738 }
3739 
3740 void TextEdit::_cursor_changed_emit() {
3741 
3742 	emit_signal("cursor_changed");
3743 	cursor_changed_dirty = false;
3744 }
3745 
3746 void TextEdit::_text_changed_emit() {
3747 
3748 	emit_signal("text_changed");
3749 	text_changed_dirty = false;
3750 }
3751 
3752 void TextEdit::set_line_as_marked(int p_line, bool p_marked) {
3753 
3754 	ERR_FAIL_INDEX(p_line, text.size());
3755 	text.set_marked(p_line, p_marked);
3756 	update();
3757 }
3758 
3759 bool TextEdit::is_line_set_as_breakpoint(int p_line) const {
3760 
3761 	ERR_FAIL_INDEX_V(p_line, text.size(), false);
3762 	return text.is_breakpoint(p_line);
3763 }
3764 
3765 void TextEdit::set_line_as_breakpoint(int p_line, bool p_breakpoint) {
3766 
3767 	ERR_FAIL_INDEX(p_line, text.size());
3768 	text.set_breakpoint(p_line, p_breakpoint);
3769 	update();
3770 }
3771 
3772 void TextEdit::get_breakpoints(List<int> *p_breakpoints) const {
3773 
3774 	for (int i = 0; i < text.size(); i++) {
3775 		if (text.is_breakpoint(i))
3776 			p_breakpoints->push_back(i);
3777 	}
3778 }
3779 
3780 int TextEdit::get_line_count() const {
3781 
3782 	return text.size();
3783 }
3784 
3785 void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) {
3786 
3787 	ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE);
3788 
3789 	bool insert = p_op.type == TextOperation::TYPE_INSERT;
3790 	if (p_reverse)
3791 		insert = !insert;
3792 
3793 	if (insert) {
3794 
3795 		int check_line;
3796 		int check_column;
3797 		_base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column);
3798 		ERR_FAIL_COND(check_line != p_op.to_line); // BUG
3799 		ERR_FAIL_COND(check_column != p_op.to_column); // BUG
3800 	} else {
3801 
3802 		_base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column);
3803 	}
3804 }
3805 
3806 void TextEdit::_clear_redo() {
3807 
3808 	if (undo_stack_pos == NULL)
3809 		return; //nothing to clear
3810 
3811 	_push_current_op();
3812 
3813 	while (undo_stack_pos) {
3814 		List<TextOperation>::Element *elem = undo_stack_pos;
3815 		undo_stack_pos = undo_stack_pos->next();
3816 		undo_stack.erase(elem);
3817 	}
3818 }
3819 
3820 void TextEdit::undo() {
3821 
3822 	_push_current_op();
3823 
3824 	if (undo_stack_pos == NULL) {
3825 
3826 		if (!undo_stack.size())
3827 			return; //nothing to undo
3828 
3829 		undo_stack_pos = undo_stack.back();
3830 
3831 	} else if (undo_stack_pos == undo_stack.front())
3832 		return; // at the bottom of the undo stack
3833 	else
3834 		undo_stack_pos = undo_stack_pos->prev();
3835 
3836 	TextOperation op = undo_stack_pos->get();
3837 	_do_text_op(op, true);
3838 	current_op.version = op.prev_version;
3839 	if (undo_stack_pos->get().chain_backward) {
3840 		while (true) {
3841 			ERR_BREAK(!undo_stack_pos->prev());
3842 			undo_stack_pos = undo_stack_pos->prev();
3843 			op = undo_stack_pos->get();
3844 			_do_text_op(op, true);
3845 			current_op.version = op.prev_version;
3846 			if (undo_stack_pos->get().chain_forward) {
3847 				break;
3848 			}
3849 		}
3850 	}
3851 
3852 	if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE || undo_stack_pos->get().type == TextOperation::TYPE_CLEAR) {
3853 		cursor_set_line(undo_stack_pos->get().to_line);
3854 		cursor_set_column(undo_stack_pos->get().to_column);
3855 		_cancel_code_hint();
3856 	} else {
3857 		cursor_set_line(undo_stack_pos->get().from_line);
3858 		cursor_set_column(undo_stack_pos->get().from_column);
3859 	}
3860 	update();
3861 }
3862 
3863 void TextEdit::redo() {
3864 
3865 	_push_current_op();
3866 
3867 	if (undo_stack_pos == NULL)
3868 		return; //nothing to do.
3869 
3870 	TextOperation op = undo_stack_pos->get();
3871 	_do_text_op(op, false);
3872 	current_op.version = op.version;
3873 	if (undo_stack_pos->get().chain_forward) {
3874 
3875 		while (true) {
3876 			ERR_BREAK(!undo_stack_pos->next());
3877 			undo_stack_pos = undo_stack_pos->next();
3878 			op = undo_stack_pos->get();
3879 			_do_text_op(op, false);
3880 			current_op.version = op.version;
3881 			if (undo_stack_pos->get().chain_backward)
3882 				break;
3883 		}
3884 	}
3885 	cursor_set_line(undo_stack_pos->get().to_line);
3886 	cursor_set_column(undo_stack_pos->get().to_column);
3887 	undo_stack_pos = undo_stack_pos->next();
3888 	update();
3889 }
3890 
3891 void TextEdit::clear_undo_history() {
3892 
3893 	saved_version = 0;
3894 	current_op.type = TextOperation::TYPE_NONE;
3895 	undo_stack_pos = NULL;
3896 	undo_stack.clear();
3897 }
3898 
3899 void TextEdit::begin_complex_operation() {
3900 	_push_current_op();
3901 	next_operation_is_complex = true;
3902 }
3903 
3904 void TextEdit::end_complex_operation() {
3905 
3906 	_push_current_op();
3907 	ERR_FAIL_COND(undo_stack.size() == 0);
3908 
3909 	if (undo_stack.back()->get().chain_forward) {
3910 		undo_stack.back()->get().chain_forward = false;
3911 		return;
3912 	}
3913 
3914 	undo_stack.back()->get().chain_backward = true;
3915 }
3916 
3917 void TextEdit::_push_current_op() {
3918 
3919 	if (current_op.type == TextOperation::TYPE_NONE)
3920 		return; // do nothing
3921 
3922 	if (next_operation_is_complex) {
3923 		current_op.chain_forward = true;
3924 		next_operation_is_complex = false;
3925 	}
3926 
3927 	undo_stack.push_back(current_op);
3928 	current_op.type = TextOperation::TYPE_NONE;
3929 	current_op.text = "";
3930 	current_op.chain_forward = false;
3931 }
3932 
3933 void TextEdit::set_tab_size(const int p_size) {
3934 	ERR_FAIL_COND(p_size <= 0);
3935 	tab_size = p_size;
3936 	text.set_tab_size(p_size);
3937 	update();
3938 }
3939 
3940 void TextEdit::set_draw_tabs(bool p_draw) {
3941 
3942 	draw_tabs = p_draw;
3943 }
3944 
3945 bool TextEdit::is_drawing_tabs() const {
3946 
3947 	return draw_tabs;
3948 }
3949 
3950 void TextEdit::set_insert_mode(bool p_enabled) {
3951 	insert_mode = p_enabled;
3952 	update();
3953 }
3954 
3955 bool TextEdit::is_insert_mode() const {
3956 	return insert_mode;
3957 }
3958 
3959 uint32_t TextEdit::get_version() const {
3960 	return current_op.version;
3961 }
3962 uint32_t TextEdit::get_saved_version() const {
3963 
3964 	return saved_version;
3965 }
3966 void TextEdit::tag_saved_version() {
3967 
3968 	saved_version = get_version();
3969 }
3970 
3971 int TextEdit::get_v_scroll() const {
3972 
3973 	return v_scroll->get_val();
3974 }
3975 void TextEdit::set_v_scroll(int p_scroll) {
3976 
3977 	if (!scroll_past_end_of_file_enabled) {
3978 		if (p_scroll + get_visible_rows() > get_line_count()) {
3979 			p_scroll = get_line_count() - get_visible_rows();
3980 		}
3981 	}
3982 	v_scroll->set_val(p_scroll);
3983 	cursor.line_ofs = p_scroll;
3984 }
3985 
3986 int TextEdit::get_h_scroll() const {
3987 
3988 	return h_scroll->get_val();
3989 }
3990 void TextEdit::set_h_scroll(int p_scroll) {
3991 	h_scroll->set_val(p_scroll);
3992 }
3993 
3994 void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) {
3995 
3996 	completion_prefixes.clear();
3997 	completion_enabled = p_enabled;
3998 	for (int i = 0; i < p_prefixes.size(); i++)
3999 		completion_prefixes.insert(p_prefixes[i]);
4000 }
4001 
4002 void TextEdit::_confirm_completion() {
4003 
4004 	begin_complex_operation();
4005 
4006 	_remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column);
4007 	cursor_set_column(cursor.column - completion_base.length(), false);
4008 	insert_text_at_cursor(completion_current);
4009 
4010 	if (completion_current.ends_with("(") && auto_brace_completion_enabled) {
4011 		insert_text_at_cursor(")");
4012 		cursor.column--;
4013 	}
4014 
4015 	end_complex_operation();
4016 
4017 	_cancel_completion();
4018 }
4019 
4020 void TextEdit::_cancel_code_hint() {
4021 
4022 	VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
4023 	raised_from_completion = false;
4024 	completion_hint = "";
4025 	update();
4026 }
4027 
4028 void TextEdit::_cancel_completion() {
4029 
4030 	VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 0);
4031 	raised_from_completion = false;
4032 	if (!completion_active)
4033 		return;
4034 
4035 	completion_active = false;
4036 	update();
4037 }
4038 
4039 static bool _is_completable(CharType c) {
4040 
4041 	return !_is_symbol(c) || c == '"' || c == '\'';
4042 }
4043 
4044 void TextEdit::_update_completion_candidates() {
4045 
4046 	String l = text[cursor.line];
4047 	int cofs = CLAMP(cursor.column, 0, l.length());
4048 
4049 	String s;
4050 
4051 	//look for keywords first
4052 
4053 	bool inquote = false;
4054 	int first_quote = -1;
4055 
4056 	int c = cofs - 1;
4057 	while (c >= 0) {
4058 		if (l[c] == '"' || l[c] == '\'') {
4059 			inquote = !inquote;
4060 			if (first_quote == -1)
4061 				first_quote = c;
4062 		}
4063 		c--;
4064 	}
4065 
4066 	bool pre_keyword = false;
4067 	bool cancel = false;
4068 
4069 	//print_line("inquote: "+itos(inquote)+"first quote "+itos(first_quote)+" cofs-1 "+itos(cofs-1));
4070 	if (!inquote && first_quote == cofs - 1) {
4071 		//no completion here
4072 		//print_line("cancel!");
4073 		cancel = true;
4074 	}
4075 	if (inquote && first_quote != -1) {
4076 
4077 		s = l.substr(first_quote, cofs - first_quote);
4078 		//print_line("s: 1"+s);
4079 	} else if (cofs > 0 && l[cofs - 1] == ' ') {
4080 		int kofs = cofs - 1;
4081 		String kw;
4082 		while (kofs >= 0 && l[kofs] == ' ')
4083 			kofs--;
4084 
4085 		while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) {
4086 			kw = String::chr(l[kofs]) + kw;
4087 			kofs--;
4088 		}
4089 
4090 		pre_keyword = keywords.has(kw);
4091 		//print_line("KW "+kw+"? "+itos(pre_keyword));
4092 
4093 	} else {
4094 
4095 		while (cofs > 0 && l[cofs - 1] > 32 && _is_completable(l[cofs - 1])) {
4096 			s = String::chr(l[cofs - 1]) + s;
4097 			if (l[cofs - 1] == '\'' || l[cofs - 1] == '"')
4098 				break;
4099 
4100 			cofs--;
4101 		}
4102 	}
4103 
4104 	if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_strings[0].begins_with("\"")) {
4105 		cancel = true;
4106 	}
4107 
4108 	update();
4109 
4110 	if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !completion_prefixes.has(String::chr(l[cofs - 1]))))) {
4111 		//none to complete, cancel
4112 		_cancel_completion();
4113 		return;
4114 	}
4115 
4116 	completion_options.clear();
4117 	completion_index = 0;
4118 	completion_base = s;
4119 	Vector<float> sim_cache;
4120 	for (int i = 0; i < completion_strings.size(); i++) {
4121 		if (s == completion_strings[i]) {
4122 			// A perfect match, stop completion
4123 			_cancel_completion();
4124 			return;
4125 		}
4126 		if (s.is_subsequence_ofi(completion_strings[i])) {
4127 			// don't remove duplicates if no input is provided
4128 			if (s != "" && completion_options.find(completion_strings[i]) != -1) {
4129 				continue;
4130 			}
4131 			// Calculate the similarity to keep completions in good order
4132 			float similarity;
4133 			if (completion_strings[i].to_lower().begins_with(s.to_lower())) {
4134 				// Substrings are the best candidates
4135 				similarity = 1.1;
4136 			} else {
4137 				// Otherwise compute the similarity
4138 				similarity = s.to_lower().similarity(completion_strings[i].to_lower());
4139 			}
4140 
4141 			int comp_size = completion_options.size();
4142 			if (comp_size == 0) {
4143 				completion_options.push_back(completion_strings[i]);
4144 				sim_cache.push_back(similarity);
4145 			} else {
4146 				float comp_sim;
4147 				int pos = 0;
4148 				do {
4149 					comp_sim = sim_cache[pos++];
4150 				} while (pos < comp_size && similarity < comp_sim);
4151 				pos = similarity > comp_sim ? pos - 1 : pos; // Pos will be off by one
4152 				completion_options.insert(pos, completion_strings[i]);
4153 				sim_cache.insert(pos, similarity);
4154 			}
4155 		}
4156 	}
4157 
4158 	if (completion_options.size() == 0) {
4159 		//no options to complete, cancel
4160 		_cancel_completion();
4161 		return;
4162 	}
4163 
4164 	// The top of the list is the best match
4165 	completion_current = completion_options[0];
4166 
4167 #if 0 // even there's only one option, user still get the chance to choose using it or not
4168 	if (completion_options.size()==1) {
4169 		//one option to complete, just complete it automagically
4170 		_confirm_completion();
4171 		//		insert_text_at_cursor(completion_options[0].substr(s.length(),completion_options[0].length()-s.length()));
4172 		_cancel_completion();
4173 		return;
4174 
4175 	}
4176 #endif
4177 
4178 	completion_enabled = true;
4179 }
4180 
4181 void TextEdit::query_code_comple() {
4182 
4183 	String l = text[cursor.line];
4184 	int ofs = CLAMP(cursor.column, 0, l.length());
4185 
4186 	bool inquote = false;
4187 
4188 	int c = ofs - 1;
4189 	while (c >= 0) {
4190 		if (l[c] == '"' || l[c] == '\'')
4191 			inquote = !inquote;
4192 		c--;
4193 	}
4194 
4195 	if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1]))))
4196 		emit_signal("request_completion");
4197 }
4198 
4199 void TextEdit::set_code_hint(const String &p_hint) {
4200 
4201 	VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
4202 	raised_from_completion = true;
4203 	completion_hint = p_hint;
4204 	completion_hint_offset = -0xFFFF;
4205 	update();
4206 }
4207 
4208 void TextEdit::code_complete(const Vector<String> &p_strings) {
4209 
4210 	VisualServer::get_singleton()->canvas_item_set_z(get_canvas_item(), 1);
4211 	raised_from_completion = true;
4212 	completion_strings = p_strings;
4213 	completion_active = true;
4214 	completion_current = "";
4215 	completion_index = 0;
4216 	_update_completion_candidates();
4217 	//
4218 }
4219 
4220 String TextEdit::get_tooltip(const Point2 &p_pos) const {
4221 
4222 	if (!tooltip_obj)
4223 		return Control::get_tooltip(p_pos);
4224 	int row, col;
4225 	_get_mouse_pos(p_pos, row, col);
4226 
4227 	String s = text[row];
4228 	if (s.length() == 0)
4229 		return Control::get_tooltip(p_pos);
4230 	int beg = CLAMP(col, 0, s.length());
4231 	int end = beg;
4232 
4233 	if (s[beg] > 32 || beg == s.length()) {
4234 
4235 		bool symbol = beg < s.length() && _is_symbol(s[beg]); //not sure if right but most editors behave like this
4236 
4237 		while (beg > 0 && s[beg - 1] > 32 && (symbol == _is_symbol(s[beg - 1]))) {
4238 			beg--;
4239 		}
4240 		while (end < s.length() && s[end + 1] > 32 && (symbol == _is_symbol(s[end + 1]))) {
4241 			end++;
4242 		}
4243 
4244 		if (end < s.length())
4245 			end += 1;
4246 
4247 		String tt = tooltip_obj->call(tooltip_func, s.substr(beg, end - beg), tooltip_ud);
4248 
4249 		return tt;
4250 	}
4251 
4252 	return Control::get_tooltip(p_pos);
4253 }
4254 
4255 void TextEdit::set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata) {
4256 
4257 	tooltip_obj = p_obj;
4258 	tooltip_func = p_function;
4259 	tooltip_ud = p_udata;
4260 }
4261 
4262 void TextEdit::set_line(int line, String new_text) {
4263 	if (line < 0 || line > text.size())
4264 		return;
4265 	_remove_text(line, 0, line, text[line].length());
4266 	_insert_text(line, 0, new_text);
4267 	if (cursor.line == line) {
4268 		cursor.column = MIN(cursor.column, new_text.length());
4269 	}
4270 }
4271 
4272 void TextEdit::insert_at(const String &p_text, int at) {
4273 	cursor_set_column(0);
4274 	cursor_set_line(at);
4275 	_insert_text(at, 0, p_text + "\n");
4276 }
4277 
4278 void TextEdit::set_show_line_numbers(bool p_show) {
4279 
4280 	line_numbers = p_show;
4281 	update();
4282 }
4283 
4284 void TextEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
4285 
4286 	line_numbers_zero_padded = p_zero_padded;
4287 	update();
4288 }
4289 
4290 bool TextEdit::is_show_line_numbers_enabled() const {
4291 	return line_numbers;
4292 }
4293 
4294 void TextEdit::set_show_line_length_guideline(bool p_show) {
4295 	line_length_guideline = p_show;
4296 	update();
4297 }
4298 
4299 void TextEdit::set_line_length_guideline_column(int p_column) {
4300 	line_length_guideline_col = p_column;
4301 	update();
4302 }
4303 
4304 void TextEdit::set_draw_breakpoint_gutter(bool p_draw) {
4305 	draw_breakpoint_gutter = p_draw;
4306 	update();
4307 }
4308 
4309 bool TextEdit::is_drawing_breakpoint_gutter() const {
4310 	return draw_breakpoint_gutter;
4311 }
4312 
4313 void TextEdit::set_breakpoint_gutter_width(int p_gutter_width) {
4314 	breakpoint_gutter_width = p_gutter_width;
4315 	update();
4316 }
4317 
4318 int TextEdit::get_breakpoint_gutter_width() const {
4319 	return cache.breakpoint_gutter_width;
4320 }
4321 
4322 bool TextEdit::is_text_field() const {
4323 
4324 	return true;
4325 }
4326 
4327 void TextEdit::menu_option(int p_option) {
4328 
4329 	switch (p_option) {
4330 		case MENU_CUT: {
4331 			if (!readonly) {
4332 				cut();
4333 			}
4334 		} break;
4335 		case MENU_COPY: {
4336 			copy();
4337 		} break;
4338 		case MENU_PASTE: {
4339 			if (!readonly) {
4340 				paste();
4341 			}
4342 		} break;
4343 		case MENU_CLEAR: {
4344 			if (!readonly) {
4345 				clear();
4346 			}
4347 		} break;
4348 		case MENU_UPPERCASE: {
4349 			if (!readonly) {
4350 				convert_case(UPPERCASE);
4351 			}
4352 		} break;
4353 		case MENU_LOWERCASE: {
4354 			if (!readonly) {
4355 				convert_case(LOWERCASE);
4356 			}
4357 		} break;
4358 		case MENU_SELECT_ALL: {
4359 			select_all();
4360 		} break;
4361 		case MENU_UNDO: {
4362 			undo();
4363 		} break;
4364 	};
4365 }
4366 
4367 PopupMenu *TextEdit::get_menu() const {
4368 	return menu;
4369 }
4370 
4371 void TextEdit::_bind_methods() {
4372 
4373 	ObjectTypeDB::bind_method(_MD("_input_event"), &TextEdit::_input_event);
4374 	ObjectTypeDB::bind_method(_MD("_scroll_moved"), &TextEdit::_scroll_moved);
4375 	ObjectTypeDB::bind_method(_MD("_cursor_changed_emit"), &TextEdit::_cursor_changed_emit);
4376 	ObjectTypeDB::bind_method(_MD("_text_changed_emit"), &TextEdit::_text_changed_emit);
4377 	ObjectTypeDB::bind_method(_MD("_push_current_op"), &TextEdit::_push_current_op);
4378 	ObjectTypeDB::bind_method(_MD("_click_selection_held"), &TextEdit::_click_selection_held);
4379 	ObjectTypeDB::bind_method(_MD("_toggle_draw_caret"), &TextEdit::_toggle_draw_caret);
4380 
4381 	BIND_CONSTANT(SEARCH_MATCH_CASE);
4382 	BIND_CONSTANT(SEARCH_WHOLE_WORDS);
4383 	BIND_CONSTANT(SEARCH_BACKWARDS);
4384 
4385 	/*
4386     ObjectTypeDB::bind_method(_MD("delete_char"),&TextEdit::delete_char);
4387     ObjectTypeDB::bind_method(_MD("delete_line"),&TextEdit::delete_line);
4388 */
4389 
4390 	ObjectTypeDB::bind_method(_MD("set_text", "text"), &TextEdit::set_text);
4391 	ObjectTypeDB::bind_method(_MD("insert_text_at_cursor", "text"), &TextEdit::insert_text_at_cursor);
4392 
4393 	ObjectTypeDB::bind_method(_MD("get_line_count"), &TextEdit::get_line_count);
4394 	ObjectTypeDB::bind_method(_MD("get_text"), &TextEdit::get_text);
4395 	ObjectTypeDB::bind_method(_MD("get_line", "line"), &TextEdit::get_line);
4396 
4397 	ObjectTypeDB::bind_method(_MD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(false));
4398 	ObjectTypeDB::bind_method(_MD("cursor_set_line", "line", "adjust_viewport"), &TextEdit::cursor_set_line, DEFVAL(false));
4399 
4400 	ObjectTypeDB::bind_method(_MD("cursor_get_column"), &TextEdit::cursor_get_column);
4401 	ObjectTypeDB::bind_method(_MD("cursor_get_line"), &TextEdit::cursor_get_line);
4402 	ObjectTypeDB::bind_method(_MD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled);
4403 	ObjectTypeDB::bind_method(_MD("cursor_get_blink_enabled"), &TextEdit::cursor_get_blink_enabled);
4404 	ObjectTypeDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"), &TextEdit::cursor_set_blink_speed);
4405 	ObjectTypeDB::bind_method(_MD("cursor_get_blink_speed"), &TextEdit::cursor_get_blink_speed);
4406 	ObjectTypeDB::bind_method(_MD("cursor_set_block_mode", "enable"), &TextEdit::cursor_set_block_mode);
4407 	ObjectTypeDB::bind_method(_MD("cursor_is_block_mode"), &TextEdit::cursor_is_block_mode);
4408 
4409 	ObjectTypeDB::bind_method(_MD("set_readonly", "enable"), &TextEdit::set_readonly);
4410 	ObjectTypeDB::bind_method(_MD("set_wrap", "enable"), &TextEdit::set_wrap);
4411 	ObjectTypeDB::bind_method(_MD("set_max_chars", "amount"), &TextEdit::set_max_chars);
4412 
4413 	ObjectTypeDB::bind_method(_MD("cut"), &TextEdit::cut);
4414 	ObjectTypeDB::bind_method(_MD("copy"), &TextEdit::copy);
4415 	ObjectTypeDB::bind_method(_MD("paste"), &TextEdit::paste);
4416 	ObjectTypeDB::bind_method(_MD("select_all"), &TextEdit::select_all);
4417 	ObjectTypeDB::bind_method(_MD("select", "from_line", "from_column", "to_line", "to_column"), &TextEdit::select);
4418 	ObjectTypeDB::bind_method(_MD("convert_case", "case"), &TextEdit::convert_case);
4419 
4420 	ObjectTypeDB::bind_method(_MD("is_selection_active"), &TextEdit::is_selection_active);
4421 	ObjectTypeDB::bind_method(_MD("get_selection_from_line"), &TextEdit::get_selection_from_line);
4422 	ObjectTypeDB::bind_method(_MD("get_selection_from_column"), &TextEdit::get_selection_from_column);
4423 	ObjectTypeDB::bind_method(_MD("get_selection_to_line"), &TextEdit::get_selection_to_line);
4424 	ObjectTypeDB::bind_method(_MD("get_selection_to_column"), &TextEdit::get_selection_to_column);
4425 	ObjectTypeDB::bind_method(_MD("get_selection_text"), &TextEdit::get_selection_text);
4426 	ObjectTypeDB::bind_method(_MD("get_word_under_cursor"), &TextEdit::get_word_under_cursor);
4427 	ObjectTypeDB::bind_method(_MD("search", "flags", "from_line", "from_column", "to_line", "to_column"), &TextEdit::_search_bind);
4428 
4429 	ObjectTypeDB::bind_method(_MD("undo"), &TextEdit::undo);
4430 	ObjectTypeDB::bind_method(_MD("redo"), &TextEdit::redo);
4431 	ObjectTypeDB::bind_method(_MD("clear_undo_history"), &TextEdit::clear_undo_history);
4432 
4433 	ObjectTypeDB::bind_method(_MD("set_show_line_numbers", "enable"), &TextEdit::set_show_line_numbers);
4434 	ObjectTypeDB::bind_method(_MD("is_show_line_numbers_enabled"), &TextEdit::is_show_line_numbers_enabled);
4435 
4436 	ObjectTypeDB::bind_method(_MD("set_highlight_all_occurrences", "enable"), &TextEdit::set_highlight_all_occurrences);
4437 	ObjectTypeDB::bind_method(_MD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled);
4438 
4439 	ObjectTypeDB::bind_method(_MD("set_syntax_coloring", "enable"), &TextEdit::set_syntax_coloring);
4440 	ObjectTypeDB::bind_method(_MD("is_syntax_coloring_enabled"), &TextEdit::is_syntax_coloring_enabled);
4441 
4442 	ObjectTypeDB::bind_method(_MD("add_keyword_color", "keyword", "color"), &TextEdit::add_keyword_color);
4443 	ObjectTypeDB::bind_method(_MD("add_color_region", "begin_key", "end_key", "color", "line_only"), &TextEdit::add_color_region, DEFVAL(false));
4444 	ObjectTypeDB::bind_method(_MD("set_symbol_color", "color"), &TextEdit::set_symbol_color);
4445 	ObjectTypeDB::bind_method(_MD("set_custom_bg_color", "color"), &TextEdit::set_custom_bg_color);
4446 	ObjectTypeDB::bind_method(_MD("clear_colors"), &TextEdit::clear_colors);
4447 	ObjectTypeDB::bind_method(_MD("menu_option"), &TextEdit::menu_option);
4448 	ObjectTypeDB::bind_method(_MD("get_menu:PopupMenu"), &TextEdit::get_menu);
4449 
4450 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "syntax_highlighting"), _SCS("set_syntax_coloring"), _SCS("is_syntax_coloring_enabled"));
4451 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_line_numbers"), _SCS("set_show_line_numbers"), _SCS("is_show_line_numbers_enabled"));
4452 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), _SCS("set_highlight_all_occurrences"), _SCS("is_highlight_all_occurrences_enabled"));
4453 
4454 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/block_caret"), _SCS("cursor_set_block_mode"), _SCS("cursor_is_block_mode"));
4455 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret/caret_blink"), _SCS("cursor_set_blink_enabled"), _SCS("cursor_get_blink_enabled"));
4456 	ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret/caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), _SCS("cursor_set_blink_speed"), _SCS("cursor_get_blink_speed"));
4457 
4458 	ADD_SIGNAL(MethodInfo("cursor_changed"));
4459 	ADD_SIGNAL(MethodInfo("text_changed"));
4460 	ADD_SIGNAL(MethodInfo("request_completion"));
4461 	ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "row")));
4462 
4463 	BIND_CONSTANT(MENU_CUT);
4464 	BIND_CONSTANT(MENU_COPY);
4465 	BIND_CONSTANT(MENU_PASTE);
4466 	BIND_CONSTANT(MENU_CLEAR);
4467 	BIND_CONSTANT(MENU_SELECT_ALL);
4468 	BIND_CONSTANT(MENU_UNDO);
4469 	BIND_CONSTANT(MENU_MAX);
4470 }
4471 
4472 TextEdit::TextEdit() {
4473 
4474 	readonly = false;
4475 	setting_row = false;
4476 	draw_tabs = false;
4477 	draw_caret = true;
4478 	max_chars = 0;
4479 	clear();
4480 	wrap = false;
4481 	set_focus_mode(FOCUS_ALL);
4482 	_update_caches();
4483 	cache.size = Size2(1, 1);
4484 	cache.row_height = 1;
4485 	cache.line_spacing = 1;
4486 	cache.line_number_w = 1;
4487 	cache.breakpoint_gutter_width = 0;
4488 	breakpoint_gutter_width = 0;
4489 
4490 	tab_size = 4;
4491 	text.set_tab_size(tab_size);
4492 	text.clear();
4493 	//	text.insert(1,"Mongolia..");
4494 	//	text.insert(2,"PAIS GENEROSO!!");
4495 	text.set_color_regions(&color_regions);
4496 
4497 	h_scroll = memnew(HScrollBar);
4498 	v_scroll = memnew(VScrollBar);
4499 
4500 	add_child(h_scroll);
4501 	add_child(v_scroll);
4502 
4503 	updating_scrolls = false;
4504 	selection.active = false;
4505 
4506 	h_scroll->connect("value_changed", this, "_scroll_moved");
4507 	v_scroll->connect("value_changed", this, "_scroll_moved");
4508 
4509 	cursor_changed_dirty = false;
4510 	text_changed_dirty = false;
4511 
4512 	selection.selecting_mode = Selection::MODE_NONE;
4513 	selection.selecting_line = 0;
4514 	selection.selecting_column = 0;
4515 	selection.selecting_text = false;
4516 	selection.active = false;
4517 	syntax_coloring = false;
4518 
4519 	block_caret = false;
4520 	caret_blink_enabled = false;
4521 	caret_blink_timer = memnew(Timer);
4522 	add_child(caret_blink_timer);
4523 	caret_blink_timer->set_wait_time(0.65);
4524 	caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
4525 	cursor_set_blink_enabled(false);
4526 
4527 	custom_bg_color = Color(0, 0, 0, 0);
4528 	idle_detect = memnew(Timer);
4529 	add_child(idle_detect);
4530 	idle_detect->set_one_shot(true);
4531 	idle_detect->set_wait_time(GLOBAL_DEF("display/text_edit_idle_detect_sec", 3));
4532 	idle_detect->connect("timeout", this, "_push_current_op");
4533 
4534 	click_select_held = memnew(Timer);
4535 	add_child(click_select_held);
4536 	click_select_held->set_wait_time(0.05);
4537 	click_select_held->connect("timeout", this, "_click_selection_held");
4538 
4539 #if 0
4540 	syntax_coloring=true;
4541 	keywords["void"]=Color(0.3,0.0,0.1);
4542 	keywords["int"]=Color(0.3,0.0,0.1);
4543 	keywords["function"]=Color(0.3,0.0,0.1);
4544 	keywords["class"]=Color(0.3,0.0,0.1);
4545 	keywords["extends"]=Color(0.3,0.0,0.1);
4546 	keywords["constructor"]=Color(0.3,0.0,0.1);
4547 	symbol_color=Color(0.1,0.0,0.3,1.0);
4548 
4549 	color_regions.push_back(ColorRegion("/*","*/",Color(0.4,0.6,0,4)));
4550 	color_regions.push_back(ColorRegion("//","",Color(0.6,0.6,0.4)));
4551 	color_regions.push_back(ColorRegion("\"","\"",Color(0.4,0.7,0.7)));
4552 	color_regions.push_back(ColorRegion("'","'",Color(0.4,0.8,0.8)));
4553 	color_regions.push_back(ColorRegion("#","",Color(0.2,1.0,0.2)));
4554 
4555 #endif
4556 
4557 	current_op.type = TextOperation::TYPE_NONE;
4558 	undo_enabled = true;
4559 	undo_stack_pos = NULL;
4560 	setting_text = false;
4561 	last_dblclk = 0;
4562 	current_op.version = 0;
4563 	version = 0;
4564 	saved_version = 0;
4565 
4566 	completion_enabled = false;
4567 	completion_active = false;
4568 	completion_line_ofs = 0;
4569 	tooltip_obj = NULL;
4570 	line_numbers = false;
4571 	line_numbers_zero_padded = false;
4572 	line_length_guideline = false;
4573 	line_length_guideline_col = 80;
4574 	draw_breakpoint_gutter = false;
4575 	next_operation_is_complex = false;
4576 	scroll_past_end_of_file_enabled = false;
4577 	auto_brace_completion_enabled = false;
4578 	brace_matching_enabled = false;
4579 	highlight_all_occurrences = false;
4580 	auto_indent = false;
4581 	insert_mode = false;
4582 	window_has_focus = true;
4583 
4584 	raised_from_completion = false;
4585 
4586 	menu = memnew(PopupMenu);
4587 	add_child(menu);
4588 	menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
4589 	menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
4590 	menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
4591 	menu->add_separator();
4592 	menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
4593 	menu->add_item(TTR("Clear"), MENU_CLEAR);
4594 	menu->add_separator();
4595 	menu->add_item(TTR("UpperCase"), MENU_UPPERCASE);
4596 	menu->add_item(TTR("LowerCase"), MENU_LOWERCASE);
4597 	menu->add_separator();
4598 	menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
4599 	menu->connect("item_pressed", this, "menu_option");
4600 }
4601 
4602 TextEdit::~TextEdit() {
4603 }
4604