diff options
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 173 |
1 files changed, 73 insertions, 100 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 76716f01b7..3ea970a0f0 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -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,7 +166,6 @@ bool FindReplaceBar::_search(uint32_t p_flags, int p_from_line, int p_from_col) } void FindReplaceBar::_replace() { - if (result_line != -1 && result_col != -1) { text_edit->begin_complex_operation(); @@ -199,7 +184,6 @@ void FindReplaceBar::_replace() { } 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 +273,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,14 +286,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()) + if (searched.empty()) { return; + } String full_text = text_edit->get_text(); @@ -318,15 +303,18 @@ 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) + if (pos == -1) { break; + } 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])) + if (pos > 0 && !is_symbol(full_text[pos - 1])) { continue; - if (pos + searched.length() < full_text.length() && !is_symbol(full_text[pos + searched.length()])) + } + if (pos + searched.length() < full_text.length() && !is_symbol(full_text[pos + searched.length()])) { continue; + } } results_count++; @@ -335,7 +323,6 @@ void FindReplaceBar::_update_results_count() { } void FindReplaceBar::_update_matches_label() { - if (search_text->get_text().empty() || results_count == -1) { matches_label->hide(); } else { @@ -347,13 +334,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); @@ -362,30 +350,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(); } @@ -393,21 +385,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); @@ -416,8 +411,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; } } @@ -426,9 +422,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; @@ -437,10 +433,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(); @@ -470,7 +466,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(); @@ -479,7 +474,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(); @@ -492,13 +486,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; @@ -508,13 +500,11 @@ 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 (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) { search_prev(); } else { @@ -523,7 +513,6 @@ 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(); @@ -536,44 +525,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); @@ -583,7 +564,6 @@ void FindReplaceBar::_bind_methods() { } FindReplaceBar::FindReplaceBar() { - results_count = -1; replace_all_mode = false; preserve_cursor = false; @@ -676,10 +656,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()) { return; + } if (ED_IS_SHORTCUT("script_text_editor/move_up", key_event)) { move_lines_up(); @@ -704,13 +684,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) { @@ -721,7 +698,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()) { @@ -739,7 +715,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(); @@ -765,8 +740,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() { @@ -779,7 +755,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; @@ -802,7 +777,6 @@ void CodeTextEditor::_line_col_changed() { } void CodeTextEditor::_text_changed() { - if (text_editor->is_insert_text_operation()) { code_complete_timer->start(); } @@ -811,13 +785,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); @@ -825,8 +799,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()); @@ -879,14 +854,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()) { @@ -904,7 +877,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")); @@ -940,7 +912,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; @@ -1088,10 +1059,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) { @@ -1129,8 +1102,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); @@ -1145,8 +1119,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); @@ -1170,8 +1145,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); @@ -1186,8 +1162,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); @@ -1281,8 +1258,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(); @@ -1314,14 +1292,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); @@ -1440,7 +1421,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); @@ -1463,7 +1443,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")); @@ -1474,13 +1453,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"); @@ -1537,7 +1516,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(); @@ -1568,18 +1546,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) { @@ -1605,7 +1582,6 @@ void CodeTextEditor::goto_next_bookmark() { } void CodeTextEditor::goto_prev_bookmark() { - List<int> bmarks; text_editor->get_bookmarks(&bmarks); if (bmarks.size() <= 0) { @@ -1631,7 +1607,6 @@ void CodeTextEditor::goto_prev_bookmark() { } void CodeTextEditor::remove_all_bookmarks() { - List<int> bmarks; text_editor->get_bookmarks(&bmarks); @@ -1641,7 +1616,6 @@ void CodeTextEditor::remove_all_bookmarks() { } void CodeTextEditor::_bind_methods() { - ClassDB::bind_method(D_METHOD("_input"), &CodeTextEditor::_input); ADD_SIGNAL(MethodInfo("validate_script")); @@ -1665,7 +1639,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); |