diff options
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 229 |
1 files changed, 116 insertions, 113 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 77e20b971c..717c1406c8 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -30,7 +30,7 @@ #include "code_editor.h" -#include "core/input/input_filter.h" +#include "core/input/input.h" #include "core/os/keyboard.h" #include "core/string_builder.h" #include "editor/editor_scale.h" @@ -41,7 +41,6 @@ #include "scene/resources/dynamic_font.h" void GotoLineDialog::popup_find_line(TextEdit *p_edit) { - text_editor = p_edit; line->set_text(itos(text_editor->cursor_get_line())); @@ -51,21 +50,19 @@ void GotoLineDialog::popup_find_line(TextEdit *p_edit) { } int GotoLineDialog::get_line() const { - return line->get_text().to_int(); } void GotoLineDialog::ok_pressed() { - - if (get_line() < 1 || get_line() > text_editor->get_line_count()) + if (get_line() < 1 || get_line() > text_editor->get_line_count()) { return; + } text_editor->unfold_line(get_line() - 1); text_editor->cursor_set_line(get_line() - 1); hide(); } GotoLineDialog::GotoLineDialog() { - set_title(TTR("Go to Line")); VBoxContainer *vbc = memnew(VBoxContainer); @@ -88,9 +85,7 @@ GotoLineDialog::GotoLineDialog() { } void FindReplaceBar::_notification(int p_what) { - if (p_what == NOTIFICATION_READY) { - find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons")); find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons")); hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons")); @@ -98,10 +93,8 @@ void FindReplaceBar::_notification(int p_what) { hide_button->set_pressed_texture(get_theme_icon("Close", "EditorIcons")); hide_button->set_custom_minimum_size(hide_button->get_normal_texture()->get_size()); } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) { - set_process_unhandled_input(is_visible_in_tree()); } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { - find_prev->set_icon(get_theme_icon("MoveUp", "EditorIcons")); find_next->set_icon(get_theme_icon("MoveDown", "EditorIcons")); hide_button->set_normal_texture(get_theme_icon("Close", "EditorIcons")); @@ -114,22 +107,16 @@ void FindReplaceBar::_notification(int p_what) { } void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) { - Ref<InputEventKey> k = p_event; if (k.is_valid()) { - if (k->is_pressed() && (text_edit->has_focus() || vbc_lineedit->is_a_parent_of(get_focus_owner()))) { - bool accepted = true; switch (k->get_keycode()) { - case KEY_ESCAPE: { - _hide_bar(); } break; default: { - accepted = false; } break; } @@ -142,7 +129,6 @@ void FindReplaceBar::_unhandled_input(const Ref<InputEvent> &p_event) { } bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) { - int line, col; String text = get_search_text(); @@ -180,26 +166,51 @@ bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) } void FindReplaceBar::_replace() { + bool selection_enabled = text_edit->is_selection_active(); + Point2i selection_begin, selection_end; + if (selection_enabled) { + selection_begin = Point2i(text_edit->get_selection_from_line(), text_edit->get_selection_from_column()); + selection_end = Point2i(text_edit->get_selection_to_line(), text_edit->get_selection_to_column()); + } - if (result_line != -1 && result_col != -1) { - text_edit->begin_complex_operation(); + String replace_text = get_replace_text(); + int search_text_len = get_search_text().length(); - text_edit->unfold_line(result_line); - text_edit->select(result_line, result_col, result_line, result_col + get_search_text().length()); - text_edit->insert_text_at_cursor(get_replace_text()); + text_edit->begin_complex_operation(); + if (selection_enabled && is_selection_only()) { // To restrict search_current() to selected region + text_edit->cursor_set_line(selection_begin.width); + text_edit->cursor_set_column(selection_begin.height); + } - text_edit->end_complex_operation(); + if (search_current()) { + text_edit->unfold_line(result_line); + text_edit->select(result_line, result_col, result_line, result_col + search_text_len); - results_count = -1; + if (selection_enabled && is_selection_only()) { + Point2i match_from(result_line, result_col); + Point2i match_to(result_line, result_col + search_text_len); + if (!(match_from < selection_begin || match_to > selection_end)) { + text_edit->insert_text_at_cursor(replace_text); + if (match_to.x == selection_end.x) { // Adjust selection bounds if necessary + selection_end.y += replace_text.length() - search_text_len; + } + } + } else { + text_edit->insert_text_at_cursor(replace_text); + } } + text_edit->end_complex_operation(); + results_count = -1; - if (!search_current()) { - search_next(); + if (selection_enabled && is_selection_only()) { + // Reselect in order to keep 'Replace' restricted to selection + text_edit->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y); + } else { + text_edit->deselect(); } } void FindReplaceBar::_replace_all() { - text_edit->disconnect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed)); // Line as x so it gets priority in comparison, column as y. Point2i orig_cursor(text_edit->cursor_get_line(), text_edit->cursor_get_column()); @@ -289,7 +300,6 @@ void FindReplaceBar::_replace_all() { } void FindReplaceBar::_get_search_from(int &r_line, int &r_col) { - r_line = text_edit->cursor_get_line(); r_col = text_edit->cursor_get_column(); @@ -303,13 +313,16 @@ void FindReplaceBar::_get_search_from(int &r_line, int &r_col) { } void FindReplaceBar::_update_results_count() { - if (results_count != -1) + if (results_count != -1) { return; + } results_count = 0; String searched = get_search_text(); - if (searched.empty()) return; + if (searched.empty()) { + return; + } String full_text = text_edit->get_text(); @@ -317,23 +330,27 @@ void FindReplaceBar::_update_results_count() { while (true) { int pos = is_case_sensitive() ? full_text.find(searched, from_pos) : full_text.findn(searched, from_pos); - if (pos == -1) break; + if (pos == -1) { + break; + } + int pos_subsequent = pos + searched.length(); if (is_whole_words()) { - from_pos++; // Making sure we won't hit the same match next time, if we get out via a continue. - if (pos > 0 && !is_symbol(full_text[pos - 1])) + from_pos = pos + 1; // Making sure we won't hit the same match next time, if we get out via a continue. + if (pos > 0 && !(is_symbol(full_text[pos - 1]) || full_text[pos - 1] == '\n')) { continue; - if (pos + searched.length() < full_text.length() && !is_symbol(full_text[pos + searched.length()])) + } + if (pos_subsequent < full_text.length() && !(is_symbol(full_text[pos_subsequent]) || full_text[pos_subsequent] == '\n')) { continue; + } } results_count++; - from_pos = pos + searched.length(); + from_pos = pos_subsequent; } } void FindReplaceBar::_update_matches_label() { - if (search_text->get_text().empty() || results_count == -1) { matches_label->hide(); } else { @@ -345,13 +362,14 @@ void FindReplaceBar::_update_matches_label() { } bool FindReplaceBar::search_current() { - uint32_t flags = 0; - if (is_whole_words()) + if (is_whole_words()) { flags |= TextEdit::SEARCH_WHOLE_WORDS; - if (is_case_sensitive()) + } + if (is_case_sensitive()) { flags |= TextEdit::SEARCH_MATCH_CASE; + } int line, col; _get_search_from(line, col); @@ -360,30 +378,34 @@ bool FindReplaceBar::search_current() { } bool FindReplaceBar::search_prev() { - - if (!is_visible()) + if (!is_visible()) { popup_search(true); + } uint32_t flags = 0; String text = get_search_text(); - if (is_whole_words()) + if (is_whole_words()) { flags |= TextEdit::SEARCH_WHOLE_WORDS; - if (is_case_sensitive()) + } + if (is_case_sensitive()) { flags |= TextEdit::SEARCH_MATCH_CASE; + } flags |= TextEdit::SEARCH_BACKWARDS; int line, col; _get_search_from(line, col); - if (text_edit->is_selection_active()) + if (text_edit->is_selection_active()) { col--; // Skip currently selected word. + } col -= text.length(); if (col < 0) { line -= 1; - if (line < 0) + if (line < 0) { line = text_edit->get_line_count() - 1; + } col = text_edit->get_line(line).length(); } @@ -391,21 +413,24 @@ bool FindReplaceBar::search_prev() { } bool FindReplaceBar::search_next() { - - if (!is_visible()) + if (!is_visible()) { popup_search(true); + } uint32_t flags = 0; String text; - if (replace_all_mode) + if (replace_all_mode) { text = get_replace_text(); - else + } else { text = get_search_text(); + } - if (is_whole_words()) + if (is_whole_words()) { flags |= TextEdit::SEARCH_WHOLE_WORDS; - if (is_case_sensitive()) + } + if (is_case_sensitive()) { flags |= TextEdit::SEARCH_MATCH_CASE; + } int line, col; _get_search_from(line, col); @@ -414,8 +439,9 @@ bool FindReplaceBar::search_next() { col += text.length(); if (col > text_edit->get_line(line).length()) { line += 1; - if (line >= text_edit->get_line_count()) + if (line >= text_edit->get_line_count()) { line = 0; + } col = 0; } } @@ -424,9 +450,9 @@ bool FindReplaceBar::search_next() { } void FindReplaceBar::_hide_bar() { - - if (replace_text->has_focus() || search_text->has_focus()) + if (replace_text->has_focus() || search_text->has_focus()) { text_edit->grab_focus(); + } text_edit->set_search_text(""); result_line = -1; @@ -435,10 +461,10 @@ void FindReplaceBar::_hide_bar() { } void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) { - show(); - if (p_show_only) + if (p_show_only) { return; + } if (p_focus_replace) { search_text->deselect(); @@ -468,7 +494,6 @@ void FindReplaceBar::_show_search(bool p_focus_replace, bool p_show_only) { } void FindReplaceBar::popup_search(bool p_show_only) { - replace_text->hide(); hbc_button_replace->hide(); hbc_option_replace->hide(); @@ -477,7 +502,6 @@ void FindReplaceBar::popup_search(bool p_show_only) { } void FindReplaceBar::popup_replace() { - if (!replace_text->is_visible_in_tree()) { replace_text->show(); hbc_button_replace->show(); @@ -490,13 +514,11 @@ void FindReplaceBar::popup_replace() { } void FindReplaceBar::_search_options_changed(bool p_pressed) { - results_count = -1; search_current(); } void FindReplaceBar::_editor_text_changed() { - results_count = -1; if (is_visible_in_tree()) { preserve_cursor = true; @@ -506,14 +528,12 @@ void FindReplaceBar::_editor_text_changed() { } void FindReplaceBar::_search_text_changed(const String &p_text) { - results_count = -1; search_current(); } void FindReplaceBar::_search_text_entered(const String &p_text) { - - if (InputFilter::get_singleton()->is_key_pressed(KEY_SHIFT)) { + if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) { search_prev(); } else { search_next(); @@ -521,11 +541,10 @@ void FindReplaceBar::_search_text_entered(const String &p_text) { } void FindReplaceBar::_replace_text_entered(const String &p_text) { - if (selection_only->is_pressed() && text_edit->is_selection_active()) { _replace_all(); _hide_bar(); - } else if (InputFilter::get_singleton()->is_key_pressed(KEY_SHIFT)) { + } else if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) { _replace(); search_prev(); } else { @@ -534,44 +553,36 @@ void FindReplaceBar::_replace_text_entered(const String &p_text) { } String FindReplaceBar::get_search_text() const { - return search_text->get_text(); } String FindReplaceBar::get_replace_text() const { - return replace_text->get_text(); } bool FindReplaceBar::is_case_sensitive() const { - return case_sensitive->is_pressed(); } bool FindReplaceBar::is_whole_words() const { - return whole_words->is_pressed(); } bool FindReplaceBar::is_selection_only() const { - return selection_only->is_pressed(); } void FindReplaceBar::set_error(const String &p_label) { - emit_signal("error", p_label); } void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) { - results_count = -1; text_edit = p_text_edit; text_edit->connect("text_changed", callable_mp(this, &FindReplaceBar::_editor_text_changed)); } void FindReplaceBar::_bind_methods() { - ClassDB::bind_method("_unhandled_input", &FindReplaceBar::_unhandled_input); ClassDB::bind_method("_search_current", &FindReplaceBar::search_current); @@ -581,7 +592,6 @@ void FindReplaceBar::_bind_methods() { } FindReplaceBar::FindReplaceBar() { - results_count = -1; replace_all_mode = false; preserve_cursor = false; @@ -674,10 +684,10 @@ FindReplaceBar::FindReplaceBar() { // This function should be used to handle shortcuts that could otherwise // be handled too late if they weren't handled here. void CodeTextEditor::_input(const Ref<InputEvent> &event) { - const Ref<InputEventKey> key_event = event; - if (!key_event.is_valid() || !key_event->is_pressed()) + if (!key_event.is_valid() || !key_event->is_pressed() || !text_editor->has_focus()) { return; + } if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) { move_lines_up(); @@ -702,13 +712,10 @@ void CodeTextEditor::_input(const Ref<InputEvent> &event) { } void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) { - Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid()) { - if (mb->is_pressed() && mb->get_command()) { - if (mb->get_button_index() == BUTTON_WHEEL_UP) { _zoom_in(); } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) { @@ -719,7 +726,6 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMagnifyGesture> magnify_gesture = p_event; if (magnify_gesture.is_valid()) { - Ref<DynamicFont> font = text_editor->get_theme_font("font"); if (font.is_valid()) { @@ -737,7 +743,6 @@ void CodeTextEditor::_text_editor_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventKey> k = p_event; if (k.is_valid()) { - if (k->is_pressed()) { if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) { _zoom_in(); @@ -763,8 +768,9 @@ void CodeTextEditor::_zoom_out() { } void CodeTextEditor::_zoom_changed() { - if (font_resize_timer->get_time_left() == 0) + if (font_resize_timer->get_time_left() == 0) { font_resize_timer->start(); + } } void CodeTextEditor::_reset_zoom() { @@ -777,7 +783,6 @@ void CodeTextEditor::_reset_zoom() { } void CodeTextEditor::_line_col_changed() { - String line = text_editor->get_line(text_editor->cursor_get_line()); int positional_column = 0; @@ -800,7 +805,6 @@ void CodeTextEditor::_line_col_changed() { } void CodeTextEditor::_text_changed() { - if (text_editor->is_insert_text_operation()) { code_complete_timer->start(); } @@ -809,13 +813,13 @@ void CodeTextEditor::_text_changed() { } void CodeTextEditor::_code_complete_timer_timeout() { - if (!is_visible_in_tree()) + if (!is_visible_in_tree()) { return; + } text_editor->query_code_comple(); } void CodeTextEditor::_complete_request() { - List<ScriptCodeCompletionOption> entries; String ctext = text_editor->get_text_for_completion(); _code_complete_script(ctext, &entries); @@ -823,8 +827,9 @@ void CodeTextEditor::_complete_request() { if (code_complete_func) { code_complete_func(code_complete_ud, ctext, &entries, forced); } - if (entries.size() == 0) + if (entries.size() == 0) { return; + } for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) { E->get().icon = _get_completion_icon(E->get()); @@ -877,14 +882,12 @@ Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOp } void CodeTextEditor::_font_resize_timeout() { - if (_add_font_size(font_resize_val)) { font_resize_val = 0; } } bool CodeTextEditor::_add_font_size(int p_delta) { - Ref<DynamicFont> font = text_editor->get_theme_font("font"); if (font.is_valid()) { @@ -902,7 +905,6 @@ bool CodeTextEditor::_add_font_size(int p_delta) { } void CodeTextEditor::update_editor_settings() { - text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/highlighting/syntax_highlighting")); text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_all_occurrences")); text_editor->set_highlight_current_line(EditorSettings::get_singleton()->get("text_editor/highlighting/highlight_current_line")); @@ -938,7 +940,6 @@ void CodeTextEditor::trim_trailing_whitespace() { for (int i = 0; i < text_editor->get_line_count(); i++) { String line = text_editor->get_line(i); if (line.ends_with(" ") || line.ends_with("\t")) { - if (!trimed_whitespace) { text_editor->begin_complex_operation(); trimed_whitespace = true; @@ -1086,10 +1087,12 @@ void CodeTextEditor::convert_case(CaseStyle p_case) { for (int i = begin; i <= end; i++) { int len = text_editor->get_line(i).length(); - if (i == end) + if (i == end) { len = end_col; - if (i == begin) + } + if (i == begin) { len -= begin_col; + } String new_line = text_editor->get_line(i).substr(i == begin ? begin_col : 0, len); switch (p_case) { @@ -1127,8 +1130,9 @@ void CodeTextEditor::move_lines_up() { int line_id = i; int next_id = i - 1; - if (line_id == 0 || next_id < 0) + if (line_id == 0 || next_id < 0) { return; + } text_editor->unfold_line(line_id); text_editor->unfold_line(next_id); @@ -1143,8 +1147,9 @@ void CodeTextEditor::move_lines_up() { int line_id = text_editor->cursor_get_line(); int next_id = line_id - 1; - if (line_id == 0 || next_id < 0) + if (line_id == 0 || next_id < 0) { return; + } text_editor->unfold_line(line_id); text_editor->unfold_line(next_id); @@ -1168,8 +1173,9 @@ void CodeTextEditor::move_lines_down() { int line_id = i; int next_id = i + 1; - if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) + if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) { return; + } text_editor->unfold_line(line_id); text_editor->unfold_line(next_id); @@ -1184,8 +1190,9 @@ void CodeTextEditor::move_lines_down() { int line_id = text_editor->cursor_get_line(); int next_id = line_id + 1; - if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) + if (line_id == text_editor->get_line_count() - 1 || next_id > text_editor->get_line_count()) { return; + } text_editor->unfold_line(line_id); text_editor->unfold_line(next_id); @@ -1279,8 +1286,9 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { int end = text_editor->get_selection_to_line(); // End of selection ends on the first column of the last line, ignore it. - if (text_editor->get_selection_to_column() == 0) + if (text_editor->get_selection_to_column() == 0) { end -= 1; + } int col_to = text_editor->get_selection_to_column(); int cursor_pos = text_editor->cursor_get_column(); @@ -1312,14 +1320,17 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { int offset = (is_commented ? -1 : 1) * delimiter.length(); int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0; - if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) + if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) { cursor_pos += 1; + } - if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) + if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) { col_to += offset; + } - if (text_editor->cursor_get_column() != 0) + if (text_editor->cursor_get_column() != 0) { cursor_pos += offset; + } text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to); text_editor->cursor_set_column(cursor_pos); @@ -1438,7 +1449,6 @@ void CodeTextEditor::set_edit_state(const Variant &p_state) { } void CodeTextEditor::set_error(const String &p_error) { - error->set_text(p_error); if (p_error != "") { error->set_default_cursor_shape(CURSOR_POINTING_HAND); @@ -1461,7 +1471,6 @@ void CodeTextEditor::goto_error() { } void CodeTextEditor::_update_font() { - text_editor->add_theme_font_override("font", get_theme_font("source", "EditorFonts")); error->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts")); @@ -1472,13 +1481,13 @@ void CodeTextEditor::_update_font() { int count = status_bar->get_child_count(); for (int i = 0; i < count; i++) { Control *n = Object::cast_to<Control>(status_bar->get_child(i)); - if (n) + if (n) { n->add_theme_font_override("font", status_bar_font); + } } } void CodeTextEditor::_on_settings_change() { - _update_font(); font_size = EditorSettings::get_singleton()->get("interface/editor/code_font_size"); @@ -1535,7 +1544,6 @@ void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) { } void CodeTextEditor::_notification(int p_what) { - switch (p_what) { case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { _load_theme_settings(); @@ -1566,18 +1574,17 @@ void CodeTextEditor::set_warning_nb(int p_warning_nb) { warning_count_label->set_text(itos(p_warning_nb)); warning_count_label->set_visible(p_warning_nb > 0); warning_button->set_visible(p_warning_nb > 0); - if (!p_warning_nb) + if (!p_warning_nb) { _set_show_warnings_panel(false); + } } void CodeTextEditor::toggle_bookmark() { - int line = text_editor->cursor_get_line(); text_editor->set_line_as_bookmark(line, !text_editor->is_line_set_as_bookmark(line)); } void CodeTextEditor::goto_next_bookmark() { - List<int> bmarks; text_editor->get_bookmarks(&bmarks); if (bmarks.size() <= 0) { @@ -1603,7 +1610,6 @@ void CodeTextEditor::goto_next_bookmark() { } void CodeTextEditor::goto_prev_bookmark() { - List<int> bmarks; text_editor->get_bookmarks(&bmarks); if (bmarks.size() <= 0) { @@ -1629,7 +1635,6 @@ void CodeTextEditor::goto_prev_bookmark() { } void CodeTextEditor::remove_all_bookmarks() { - List<int> bmarks; text_editor->get_bookmarks(&bmarks); @@ -1639,7 +1644,6 @@ void CodeTextEditor::remove_all_bookmarks() { } void CodeTextEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input); ADD_SIGNAL(MethodInfo("validate_script")); @@ -1663,7 +1667,6 @@ void CodeTextEditor::update_toggle_scripts_button() { } CodeTextEditor::CodeTextEditor() { - code_complete_func = nullptr; ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD | KEY_EQUAL); ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD | KEY_MINUS); |