diff options
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 66 |
1 files changed, 49 insertions, 17 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 9888013ce6..c5df947d64 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -166,20 +166,47 @@ 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(); + 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()); + } - 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()); + String replace_text = get_replace_text(); + int search_text_len = get_search_text().length(); - text_edit->end_complex_operation(); + 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); + } - results_count = -1; + if (search_current()) { + text_edit->unfold_line(result_line); + text_edit->select(result_line, result_col, result_line, result_col + search_text_len); + + 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(); } } @@ -307,18 +334,19 @@ void FindReplaceBar::_update_results_count() { 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; } } @@ -600,12 +628,14 @@ FindReplaceBar::FindReplaceBar() { hbc_button_search->add_child(matches_label); matches_label->hide(); - find_prev = memnew(ToolButton); + find_prev = memnew(Button); + find_prev->set_flat(true); hbc_button_search->add_child(find_prev); find_prev->set_focus_mode(FOCUS_NONE); find_prev->connect("pressed", callable_mp(this, &FindReplaceBar::search_prev)); - find_next = memnew(ToolButton); + find_next = memnew(Button); + find_next->set_flat(true); hbc_button_search->add_child(find_next); find_next->set_focus_mode(FOCUS_NONE); find_next->connect("pressed", callable_mp(this, &FindReplaceBar::search_next)); @@ -1678,7 +1708,8 @@ CodeTextEditor::CodeTextEditor() { error_line = 0; error_column = 0; - toggle_scripts_button = memnew(ToolButton); + toggle_scripts_button = memnew(Button); + toggle_scripts_button->set_flat(true); toggle_scripts_button->connect("pressed", callable_mp(this, &CodeTextEditor::_toggle_scripts_pressed)); status_bar->add_child(toggle_scripts_button); toggle_scripts_button->hide(); @@ -1698,7 +1729,8 @@ CodeTextEditor::CodeTextEditor() { find_replace_bar->connect("error", callable_mp(error, &Label::set_text)); // Warnings - warning_button = memnew(ToolButton); + warning_button = memnew(Button); + warning_button->set_flat(true); status_bar->add_child(warning_button); warning_button->set_v_size_flags(SIZE_EXPAND | SIZE_SHRINK_CENTER); warning_button->set_default_cursor_shape(CURSOR_POINTING_HAND); |