summaryrefslogtreecommitdiff
path: root/scene/gui/text_edit.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/text_edit.cpp')
-rw-r--r--scene/gui/text_edit.cpp162
1 files changed, 157 insertions, 5 deletions
diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp
index a6207c5611..87900306d4 100644
--- a/scene/gui/text_edit.cpp
+++ b/scene/gui/text_edit.cpp
@@ -46,7 +46,6 @@
#define TAB_PIXELS
-
static bool _is_text_char(CharType c) {
return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
@@ -57,6 +56,42 @@ static bool _is_symbol(CharType c) {
return c!='_' && ((c>='!' && c<='/') || (c>=':' && c<='@') || (c>='[' && c<='`') || (c>='{' && c<='~') || c=='\t');
}
+static bool _is_pair_right_symbol(CharType c) {
+ return
+ c == '"' ||
+ c == '\'' ||
+ c == ')' ||
+ c == ']' ||
+ c == '}';
+}
+
+static bool _is_pair_left_symbol(CharType c) {
+ return
+ c == '"' ||
+ c == '\'' ||
+ c == '(' ||
+ c == '[' ||
+ c == '{';
+}
+
+static bool _is_pair_symbol(CharType c) {
+ return _is_pair_left_symbol(c) || _is_pair_right_symbol(c);
+}
+
+static CharType _get_right_pair_symbol(CharType c) {
+ if(c == '"')
+ return '"';
+ if(c == '\'')
+ return '\'';
+ if(c == '(')
+ return ')';
+ if(c == '[')
+ return ']';
+ if(c == '{')
+ return '}';
+ return 0;
+}
+
void TextEdit::Text::set_font(const Ref<Font>& p_font) {
font=p_font;
@@ -707,6 +742,94 @@ void TextEdit::_notification(int p_what) {
}
}
+void TextEdit::_consume_pair_symbol(CharType ch) {
+
+ int cursor_position_to_move = cursor_get_column() + 1;
+
+ CharType ch_single[2] = {ch, 0};
+ CharType ch_single_pair[2] = {_get_right_pair_symbol(ch), 0};
+ CharType ch_pair[3] = {ch, _get_right_pair_symbol(ch), 0};
+
+ printf("Selectin if active, %d\n", is_selection_active());
+ if(is_selection_active()) {
+
+ int new_column,new_line;
+
+ _begin_compex_operation();
+ _insert_text(get_selection_from_line(), get_selection_from_column(),
+ ch_single,
+ &new_line, &new_column);
+
+ int to_col_offset = 0;
+ if(get_selection_from_line() == get_selection_to_line())
+ to_col_offset = 1;
+
+ _insert_text(get_selection_to_line(),
+ get_selection_to_column() + to_col_offset,
+ ch_single_pair,
+ &new_line,&new_column);
+ _end_compex_operation();
+
+ cursor_set_line(get_selection_to_line());
+ cursor_set_column(get_selection_to_column() + to_col_offset);
+
+ deselect();
+ update();
+ return;
+ }
+
+ if( (ch == '\'' || ch == '"') &&
+ cursor_get_column() > 0 &&
+ _is_text_char(text[cursor.line][cursor_get_column() - 1])
+ ) {
+ insert_text_at_cursor(ch_single);
+ cursor_set_column(cursor_position_to_move);
+ return;
+ }
+
+ if(cursor_get_column() < text[cursor.line].length()) {
+ if(_is_text_char(text[cursor.line][cursor_get_column()])) {
+ insert_text_at_cursor(ch_single);
+ cursor_set_column(cursor_position_to_move);
+ return;
+ }
+ if( _is_pair_right_symbol(ch) &&
+ text[cursor.line][cursor_get_column()] == ch
+ ) {
+ cursor_set_column(cursor_position_to_move);
+ return;
+ }
+ }
+
+
+ insert_text_at_cursor(ch_pair);
+ cursor_set_column(cursor_position_to_move);
+ return;
+
+}
+
+void TextEdit::_consume_backspace_for_pair_symbol(int prev_line, int prev_column) {
+
+ bool remove_right_symbol = false;
+
+ if(cursor.column < text[cursor.line].length() && cursor.column > 0) {
+
+ CharType left_char = text[cursor.line][cursor.column - 1];
+ CharType right_char = text[cursor.line][cursor.column];
+
+ if(right_char == _get_right_pair_symbol(left_char)) {
+ remove_right_symbol = true;
+ }
+
+ }
+ if(remove_right_symbol) {
+ _remove_text(prev_line,prev_column,cursor.line,cursor.column + 1);
+ } else {
+ _remove_text(prev_line,prev_column,cursor.line,cursor.column);
+ }
+
+}
+
void TextEdit::backspace_at_cursor() {
if (cursor.column==0 && cursor.line==0)
@@ -714,7 +837,14 @@ void TextEdit::backspace_at_cursor() {
int prev_line = cursor.column?cursor.line:cursor.line-1;
int prev_column = cursor.column?(cursor.column-1):(text[cursor.line-1].length());
- _remove_text(prev_line,prev_column,cursor.line,cursor.column);
+ if(auto_brace_completion_enabled &&
+ cursor.column > 0 &&
+ _is_pair_left_symbol(text[cursor.line][cursor.column - 1])) {
+ _consume_backspace_for_pair_symbol(prev_line, prev_column);
+ } else {
+ _remove_text(prev_line,prev_column,cursor.line,cursor.column);
+ }
+
cursor_set_line(prev_line);
cursor_set_column(prev_column);
@@ -1113,7 +1243,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
default:
if (k.unicode>=32 && !k.mod.command && !k.mod.alt && !k.mod.meta)
clear=true;
-
+ if (auto_brace_completion_enabled && _is_pair_left_symbol(k.unicode))
+ clear=false;
}
if (unselect) {
@@ -1522,14 +1653,35 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
if (readonly)
break;
+ accept_event();
+ } else {
+
+ break;
+ }
+ }
+
+ if (!scancode_handled && !k.mod.command && !k.mod.alt) {
+
+ if (k.unicode>=32) {
+
+ if (readonly)
+ break;
+
const CharType chr[2] = {k.unicode, 0};
- _insert_text_at_cursor(chr);
+
+ if(auto_brace_completion_enabled && _is_pair_symbol(chr[0])) {
+ _consume_pair_symbol(chr[0]);
+ } else {
+ _insert_text_at_cursor(chr);
+ }
+
accept_event();
} else {
break;
}
}
+
if (!selection.selecting_test) {
@@ -1866,7 +2018,6 @@ void TextEdit::adjust_viewport_to_cursor() {
}
-
void TextEdit::cursor_set_column(int p_col) {
if (p_col<0)
@@ -2989,6 +3140,7 @@ TextEdit::TextEdit() {
tooltip_obj=NULL;
line_numbers=false;
next_operation_is_complex=false;
+ auto_brace_completion_enabled=false;
}
TextEdit::~TextEdit(){