diff options
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 375 |
1 files changed, 182 insertions, 193 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index b217cd57bf..91ccede26d 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -42,7 +42,8 @@ void GotoLineDialog::popup_find_line(CodeEdit *p_edit) { text_editor = p_edit; - line->set_text(itos(text_editor->get_caret_line())); + // Add 1 because text_editor->get_caret_line() starts from 0, but the editor user interface starts from 1. + line->set_text(itos(text_editor->get_caret_line() + 1)); line->select_all(); popup_centered(Size2(180, 80) * EDSCALE); line->grab_focus(); @@ -53,12 +54,14 @@ int GotoLineDialog::get_line() const { } void GotoLineDialog::ok_pressed() { - if (get_line() < 1 || get_line() > text_editor->get_line_count()) { + // Subtract 1 because the editor user interface starts from 1, but text_editor->set_caret_line(n) starts from 0. + const int line_number = get_line() - 1; + if (line_number < 0 || line_number >= text_editor->get_line_count()) { return; } text_editor->remove_secondary_carets(); - text_editor->unfold_line(get_line() - 1); - text_editor->set_caret_line(get_line() - 1); + text_editor->unfold_line(line_number); + text_editor->set_caret_line(line_number); hide(); } @@ -193,7 +196,8 @@ void FindReplaceBar::_replace() { int search_text_len = get_search_text().length(); text_editor->begin_complex_operation(); - if (selection_enabled && is_selection_only()) { // To restrict search_current() to selected region + if (selection_enabled && is_selection_only()) { + // Restrict search_current() to selected region. text_editor->set_caret_line(selection_begin.width, false, true, 0, 0); text_editor->set_caret_column(selection_begin.height, true, 0); } @@ -207,7 +211,8 @@ void FindReplaceBar::_replace() { Point2i match_to(result_line, result_col + search_text_len); if (!(match_from < selection_begin || match_to > selection_end)) { text_editor->insert_text_at_caret(repl_text, 0); - if (match_to.x == selection_end.x) { // Adjust selection bounds if necessary + if (match_to.x == selection_end.x) { + // Adjust selection bounds if necessary. selection_end.y += repl_text.length() - search_text_len; } } @@ -221,7 +226,7 @@ void FindReplaceBar::_replace() { needs_to_count_results = true; if (selection_enabled && is_selection_only()) { - // Reselect in order to keep 'Replace' restricted to selection + // Reselect in order to keep 'Replace' restricted to selection. text_editor->select(selection_begin.x, selection_begin.y, selection_end.x, selection_end.y, 0); } else { text_editor->deselect(0); @@ -271,7 +276,7 @@ void FindReplaceBar::_replace_all() { if (search_current()) { do { - // replace area + // Replace area. Point2i match_from(result_line, result_col); Point2i match_to(result_line, result_col + search_text_len); @@ -695,7 +700,7 @@ FindReplaceBar::FindReplaceBar() { hbc_option_replace = memnew(HBoxContainer); vbc_option->add_child(hbc_option_replace); - // search toolbar + // Search toolbar search_text = memnew(LineEdit); vbc_lineedit->add_child(search_text); search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); @@ -730,7 +735,7 @@ FindReplaceBar::FindReplaceBar() { whole_words->set_focus_mode(FOCUS_NONE); whole_words->connect("toggled", callable_mp(this, &FindReplaceBar::_search_options_changed)); - // replace toolbar + // Replace toolbar replace_text = memnew(LineEdit); vbc_lineedit->add_child(replace_text); replace_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0)); @@ -886,7 +891,7 @@ void CodeTextEditor::_line_col_changed() { int positional_column = 0; for (int i = 0; i < text_editor->get_caret_column(); i++) { if (line[i] == '\t') { - positional_column += text_editor->get_indent_size(); //tab size + positional_column += text_editor->get_indent_size(); // Tab size } else { positional_column += 1; } @@ -1044,6 +1049,7 @@ void CodeTextEditor::update_editor_settings() { // Appearance: Whitespace text_editor->set_draw_tabs(EDITOR_GET("text_editor/appearance/whitespace/draw_tabs")); text_editor->set_draw_spaces(EDITOR_GET("text_editor/appearance/whitespace/draw_spaces")); + text_editor->add_theme_constant_override("line_spacing", EDITOR_GET("text_editor/appearance/whitespace/line_spacing")); // Behavior: Navigation text_editor->set_scroll_past_end_of_file_enabled(EDITOR_GET("text_editor/behavior/navigation/scroll_past_end_of_file")); @@ -1092,13 +1098,13 @@ void CodeTextEditor::remove_find_replace_bar() { } void CodeTextEditor::trim_trailing_whitespace() { - bool trimed_whitespace = false; + bool trimmed_whitespace = false; 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) { + if (!trimmed_whitespace) { text_editor->begin_complex_operation(); - trimed_whitespace = true; + trimmed_whitespace = true; } int end = 0; @@ -1112,7 +1118,7 @@ void CodeTextEditor::trim_trailing_whitespace() { } } - if (trimed_whitespace) { + if (trimmed_whitespace) { text_editor->merge_overlapping_carets(); text_editor->end_complex_operation(); text_editor->queue_redraw(); @@ -1124,8 +1130,7 @@ void CodeTextEditor::insert_final_newline() { String line = text_editor->get_line(final_line); - //length 0 means it's already an empty line, - //no need to add a newline + // Length 0 means it's already an empty line, no need to add a newline. if (line.length() > 0 && !line.ends_with("\n")) { text_editor->begin_complex_operation(); @@ -1139,11 +1144,7 @@ void CodeTextEditor::insert_final_newline() { void CodeTextEditor::convert_indent_to_spaces() { int indent_size = EDITOR_GET("text_editor/behavior/indent/size"); - String indent = ""; - - for (int i = 0; i < indent_size; i++) { - indent += " "; - } + String indent = String(" ").repeat(indent_size); Vector<int> cursor_columns; cursor_columns.resize(text_editor->get_caret_count()); @@ -1302,30 +1303,25 @@ void CodeTextEditor::move_lines_up() { Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); - // Lists of carets representing each group + // Lists of carets representing each group. Vector<Vector<int>> caret_groups; Vector<Pair<int, int>> group_borders; - // Search for groups of carets and their selections residing on the same lines + // Search for groups of carets and their selections residing on the same lines. for (int i = 0; i < caret_edit_order.size(); i++) { int c = caret_edit_order[i]; Vector<int> new_group{ c }; Pair<int, int> group_border; - if (text_editor->has_selection(c)) { - group_border.first = text_editor->get_selection_from_line(c); - group_border.second = text_editor->get_selection_to_line(c); - } else { - group_border.first = text_editor->get_caret_line(c); - group_border.second = text_editor->get_caret_line(c); - } + group_border.first = _get_affected_lines_from(c); + group_border.second = _get_affected_lines_to(c); for (int j = i; j < caret_edit_order.size() - 1; j++) { int c_current = caret_edit_order[j]; int c_next = caret_edit_order[j + 1]; - int next_start_pos = text_editor->has_selection(c_next) ? text_editor->get_selection_from_line(c_next) : text_editor->get_caret_line(c_next); - int next_end_pos = text_editor->has_selection(c_next) ? text_editor->get_selection_to_line(c_next) : text_editor->get_caret_line(c_next); + int next_start_pos = _get_affected_lines_from(c_next); + int next_end_pos = _get_affected_lines_to(c_next); int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current); @@ -1335,7 +1331,7 @@ void CodeTextEditor::move_lines_up() { } group_border.first = next_start_pos; new_group.push_back(c_next); - // If the last caret is added to the current group there is no need to process it again + // If the last caret is added to the current group there is no need to process it again. if (j + 1 == caret_edit_order.size() - 1) { i++; } @@ -1349,12 +1345,12 @@ void CodeTextEditor::move_lines_up() { continue; } - // If the group starts overlapping with the upper group don't move it + // If the group starts overlapping with the upper group don't move it. if (i < group_borders.size() - 1 && group_borders[i].first - 1 <= group_borders[i + 1].second) { continue; } - // We have to remember caret positions and selections prior to line swapping + // We have to remember caret positions and selections prior to line swapping. Vector<Vector<int>> caret_group_parameters; for (int j = 0; j < caret_groups[i].size(); j++) { @@ -1402,30 +1398,25 @@ void CodeTextEditor::move_lines_down() { Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); - // Lists of carets representing each group + // Lists of carets representing each group. Vector<Vector<int>> caret_groups; Vector<Pair<int, int>> group_borders; - - // Search for groups of carets and their selections residing on the same lines + Vector<int> group_border_ends; + // Search for groups of carets and their selections residing on the same lines. for (int i = 0; i < caret_edit_order.size(); i++) { int c = caret_edit_order[i]; Vector<int> new_group{ c }; Pair<int, int> group_border; - if (text_editor->has_selection(c)) { - group_border.first = text_editor->get_selection_from_line(c); - group_border.second = text_editor->get_selection_to_line(c); - } else { - group_border.first = text_editor->get_caret_line(c); - group_border.second = text_editor->get_caret_line(c); - } + group_border.first = _get_affected_lines_from(c); + group_border.second = _get_affected_lines_to(c); for (int j = i; j < caret_edit_order.size() - 1; j++) { int c_current = caret_edit_order[j]; int c_next = caret_edit_order[j + 1]; - int next_start_pos = text_editor->has_selection(c_next) ? text_editor->get_selection_from_line(c_next) : text_editor->get_caret_line(c_next); - int next_end_pos = text_editor->has_selection(c_next) ? text_editor->get_selection_to_line(c_next) : text_editor->get_caret_line(c_next); + int next_start_pos = _get_affected_lines_from(c_next); + int next_end_pos = _get_affected_lines_to(c_next); int current_start_pos = text_editor->has_selection(c_current) ? text_editor->get_selection_from_line(c_current) : text_editor->get_caret_line(c_current); @@ -1433,7 +1424,7 @@ void CodeTextEditor::move_lines_down() { if (next_end_pos == current_start_pos || next_end_pos + 1 == current_start_pos) { group_border.first = next_start_pos; new_group.push_back(c_next); - // If the last caret is added to the current group there is no need to process it again + // If the last caret is added to the current group there is no need to process it again. if (j + 1 == caret_edit_order.size() - 1) { i++; } @@ -1442,20 +1433,21 @@ void CodeTextEditor::move_lines_down() { } } group_borders.push_back(group_border); + group_border_ends.push_back(text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c)); caret_groups.push_back(new_group); } for (int i = 0; i < group_borders.size(); i++) { - if (group_borders[i].second + 1 > text_editor->get_line_count() - 1) { + if (group_border_ends[i] + 1 > text_editor->get_line_count() - 1) { continue; } - // If the group starts overlapping with the upper group don't move it - if (i > 0 && group_borders[i].second + 1 >= group_borders[i - 1].first) { + // If the group starts overlapping with the upper group don't move it. + if (i > 0 && group_border_ends[i] + 1 >= group_borders[i - 1].first) { continue; } - // We have to remember caret positions and selections prior to line swapping + // We have to remember caret positions and selections prior to line swapping. Vector<Vector<int>> caret_group_parameters; for (int j = 0; j < caret_groups[i].size(); j++) { @@ -1463,15 +1455,15 @@ void CodeTextEditor::move_lines_down() { int cursor_line = text_editor->get_caret_line(c); int cursor_column = text_editor->get_caret_column(c); - if (text_editor->has_selection(c)) { - int from_line = text_editor->get_selection_from_line(c); - int from_col = text_editor->get_selection_from_column(c); - int to_line = text_editor->get_selection_to_line(c); - int to_column = text_editor->get_selection_to_column(c); - caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column }); - } else { + if (!text_editor->has_selection(c)) { caret_group_parameters.push_back(Vector<int>{ -1, -1, -1, -1, cursor_line, cursor_column }); + continue; } + int from_line = text_editor->get_selection_from_line(c); + int from_col = text_editor->get_selection_from_column(c); + int to_line = text_editor->get_selection_to_line(c); + int to_column = text_editor->get_selection_to_column(c); + caret_group_parameters.push_back(Vector<int>{ from_line, from_col, to_line, to_column, cursor_line, cursor_column }); } for (int line_id = group_borders[i].second; line_id >= group_borders[i].first; line_id--) { @@ -1498,87 +1490,44 @@ void CodeTextEditor::move_lines_down() { text_editor->queue_redraw(); } -void CodeTextEditor::_delete_line(int p_line, int p_caret) { - // this is currently intended to be called within delete_lines() - // so `begin_complex_operation` is omitted here - text_editor->set_line(p_line, ""); - if (p_line == 0 && text_editor->get_line_count() > 1) { - text_editor->set_caret_line(1, p_caret == 0, true, 0, p_caret); - text_editor->set_caret_column(0, p_caret == 0, p_caret); - } - text_editor->backspace(p_caret); - if (p_line < text_editor->get_line_count()) { - text_editor->unfold_line(p_line); - } - text_editor->set_caret_line(p_line, p_caret == 0, true, 0, p_caret); -} - void CodeTextEditor::delete_lines() { text_editor->begin_complex_operation(); - Vector<int> carets_to_remove; - Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); - for (int i = 0; i < caret_edit_order.size(); i++) { - int c = caret_edit_order[i]; - int cl = text_editor->get_caret_line(c); - - bool swaped_caret = false; - for (int j = i + 1; j < caret_edit_order.size(); j++) { - if (text_editor->has_selection(caret_edit_order[j])) { - if (text_editor->get_selection_from_line() == cl) { - carets_to_remove.push_back(caret_edit_order[j]); - continue; - } - - if (text_editor->get_selection_to_line() == cl) { - if (text_editor->has_selection(c)) { - if (text_editor->get_selection_to_line(c) != cl) { - text_editor->select(cl + 1, 0, text_editor->get_selection_to_line(c), text_editor->get_selection_to_column(c), c); - break; - } - } - - carets_to_remove.push_back(c); - i = j - 1; - swaped_caret = true; - break; - } - break; - } - - if (text_editor->get_caret_line(caret_edit_order[j]) == cl) { - carets_to_remove.push_back(caret_edit_order[j]); - i = j; + Vector<int> lines; + int last_line = INT_MAX; + for (const int &c : caret_edit_order) { + for (int line = _get_affected_lines_to(c); line >= _get_affected_lines_from(c); line--) { + if (line >= last_line) { continue; } - break; + last_line = line; + lines.append(line); } + } - if (swaped_caret) { - continue; + for (const int &line : lines) { + if (line != text_editor->get_line_count() - 1) { + text_editor->remove_text(line, 0, line + 1, 0); + } else { + text_editor->remove_text(line - 1, text_editor->get_line(line - 1).length(), line, text_editor->get_line(line).length()); } - - if (text_editor->has_selection(c)) { - int to_line = text_editor->get_selection_to_line(c); - int from_line = text_editor->get_selection_from_line(c); - int count = Math::abs(to_line - from_line) + 1; - - text_editor->set_caret_line(from_line, false, true, 0, c); - text_editor->deselect(c); - for (int j = 0; j < count; j++) { - _delete_line(from_line, c); + // Readjust carets. + int new_line = MIN(line, text_editor->get_line_count() - 1); + text_editor->unfold_line(new_line); + for (const int &c : caret_edit_order) { + if (text_editor->get_caret_line(c) == line || (text_editor->get_caret_line(c) == line + 1 && text_editor->get_caret_column(c) == 0)) { + text_editor->deselect(c); + text_editor->set_caret_line(new_line, c == 0, true, 0, c); + continue; } - } else { - _delete_line(text_editor->get_caret_line(c), c); + if (text_editor->get_caret_line(c) > line) { + text_editor->set_caret_line(text_editor->get_caret_line(c) - 1, c == 0, true, 0, c); + continue; + } + break; } } - - // Sort and remove backwards to preserve indexes. - carets_to_remove.sort(); - for (int i = carets_to_remove.size() - 1; i >= 0; i--) { - text_editor->remove_caret(carets_to_remove[i]); - } text_editor->merge_overlapping_carets(); text_editor->end_complex_operation(); } @@ -1634,77 +1583,74 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { text_editor->begin_complex_operation(); Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); - for (const int &c : caret_edit_order) { - if (text_editor->has_selection(c)) { - int begin = text_editor->get_selection_from_line(c); - int end = text_editor->get_selection_to_line(c); - - // End of selection ends on the first column of the last line, ignore it. - if (text_editor->get_selection_to_column(c) == 0) { - end -= 1; + caret_edit_order.reverse(); + int last_line = -1; + int folded_to = 0; + for (const int &c1 : caret_edit_order) { + int from = _get_affected_lines_from(c1); + from += from == last_line ? 1 + folded_to : 0; + int to = _get_affected_lines_to(c1); + last_line = to; + // If last line is folded, extends to the end of the folded section + if (text_editor->is_line_folded(to)) { + folded_to = text_editor->get_next_visible_line_offset_from(to + 1, 1) - 1; + to += folded_to; + } + // Check first if there's any uncommented lines in selection. + bool is_commented = true; + for (int line = from; line <= to; line++) { + if (!text_editor->get_line(line).begins_with(delimiter)) { + is_commented = false; + break; } + } - int col_to = text_editor->get_selection_to_column(c); - int cursor_pos = text_editor->get_caret_column(c); - - // Check if all lines in the selected block are commented. - bool is_commented = true; - for (int i = begin; i <= end; i++) { - if (!text_editor->get_line(i).begins_with(delimiter)) { - is_commented = false; - break; - } + // Caret positions need to be saved since they could be moved at the eol. + Vector<int> caret_cols; + Vector<int> selection_to_cols; + for (const int &c2 : caret_edit_order) { + if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) { + caret_cols.append(text_editor->get_caret_column(c2)); } - for (int i = begin; i <= end; i++) { - String line_text = text_editor->get_line(i); - - if (line_text.strip_edges().is_empty()) { - line_text = delimiter; - } else { - if (is_commented) { - line_text = line_text.substr(delimiter.length(), line_text.length()); - } else { - line_text = delimiter + line_text; - } - } - text_editor->set_line(i, line_text); + if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) { + selection_to_cols.append(text_editor->get_selection_to_column(c2)); } + } - // Adjust selection & cursor position. - int offset = (is_commented ? -1 : 1) * delimiter.length(); - int col_from = text_editor->get_selection_from_column(c) > 0 ? text_editor->get_selection_from_column(c) + offset : 0; - - if (is_commented && text_editor->get_caret_column(c) == text_editor->get_line(text_editor->get_caret_line(c)).length() + 1) { - cursor_pos += 1; + // Comment/uncomment. + for (int line = from; line <= to; line++) { + String line_text = text_editor->get_line(line); + if (line_text.strip_edges().is_empty()) { + text_editor->set_line(line, delimiter); + continue; } - - if (text_editor->get_selection_to_column(c) != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line(c)).length() + 1) { - col_to += offset; + if (is_commented) { + text_editor->set_line(line, line_text.substr(delimiter.length(), line_text.length())); + continue; } + text_editor->set_line(line, delimiter + line_text); + } - if (text_editor->get_caret_column(c) != 0) { - cursor_pos += offset; + // Readjust carets and selections. + int caret_i = 0; + int selection_i = 0; + int offset = (is_commented ? -1 : 1) * delimiter.length(); + for (const int &c2 : caret_edit_order) { + bool is_line_selection = text_editor->has_selection(c2) && text_editor->get_selection_from_line(c2) < text_editor->get_selection_to_line(c2); + if (text_editor->get_caret_line(c2) >= from && text_editor->get_caret_line(c2) <= to) { + int caret_col = caret_cols[caret_i++]; + caret_col += (is_line_selection && caret_col == 0) ? 0 : offset; + text_editor->set_caret_column(caret_col, c2 == 0, c2); } - - text_editor->select(begin, col_from, text_editor->get_selection_to_line(c), col_to, c); - text_editor->set_caret_column(cursor_pos, c == 0, c); - - } else { - int begin = text_editor->get_caret_line(c); - String line_text = text_editor->get_line(begin); - int delimiter_length = delimiter.length(); - - int col = text_editor->get_caret_column(c); - if (line_text.begins_with(delimiter)) { - line_text = line_text.substr(delimiter_length, line_text.length()); - col -= delimiter_length; - } else { - line_text = delimiter + line_text; - col += delimiter_length; + if (text_editor->has_selection(c2) && text_editor->get_selection_to_line(c2) >= from && text_editor->get_selection_to_line(c2) <= to) { + int from_col = text_editor->get_selection_from_column(c2); + from_col += (is_line_selection && from_col == 0) ? 0 : offset; + int to_col = selection_to_cols[selection_i++]; + to_col += (to_col == 0) ? 0 : offset; + text_editor->select( + text_editor->get_selection_from_line(c2), from_col, + text_editor->get_selection_to_line(c2), to_col, c2); } - - text_editor->set_line(begin, line_text); - text_editor->set_caret_column(col, c == 0, c); } } text_editor->merge_overlapping_carets(); @@ -1942,6 +1888,22 @@ void CodeTextEditor::_toggle_scripts_pressed() { update_toggle_scripts_button(); } +int CodeTextEditor::_get_affected_lines_from(int p_caret) { + return text_editor->has_selection(p_caret) ? text_editor->get_selection_from_line(p_caret) : text_editor->get_caret_line(p_caret); +} + +int CodeTextEditor::_get_affected_lines_to(int p_caret) { + if (!text_editor->has_selection(p_caret)) { + return text_editor->get_caret_line(p_caret); + } + int line = text_editor->get_selection_to_line(p_caret); + // Don't affect a line with no selected characters. + if (text_editor->get_selection_to_column(p_caret) == 0) { + line--; + } + return line; +} + void CodeTextEditor::_error_pressed(const Ref<InputEvent> &p_event) { Ref<InputEventMouseButton> mb = p_event; if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { @@ -2010,9 +1972,36 @@ void CodeTextEditor::set_warning_count(int p_warning_count) { } void CodeTextEditor::toggle_bookmark() { - for (int i = 0; i < text_editor->get_caret_count(); i++) { - int line = text_editor->get_caret_line(i); - text_editor->set_line_as_bookmarked(line, !text_editor->is_line_bookmarked(line)); + Vector<int> caret_edit_order = text_editor->get_caret_index_edit_order(); + caret_edit_order.reverse(); + int last_line = -1; + for (const int &c : caret_edit_order) { + int from = text_editor->has_selection(c) ? text_editor->get_selection_from_line(c) : text_editor->get_caret_line(c); + from += from == last_line ? 1 : 0; + int to = text_editor->has_selection(c) ? text_editor->get_selection_to_line(c) : text_editor->get_caret_line(c); + if (to < from) { + continue; + } + // Check first if there's any bookmarked lines in the selection. + bool selection_has_bookmarks = false; + for (int line = from; line <= to; line++) { + if (text_editor->is_line_bookmarked(line)) { + selection_has_bookmarks = true; + break; + } + } + + // Set bookmark on caret or remove all bookmarks from the selection. + if (!selection_has_bookmarks) { + if (text_editor->get_caret_line(c) != last_line) { + text_editor->set_line_as_bookmarked(text_editor->get_caret_line(c), true); + } + } else { + for (int line = from; line <= to; line++) { + text_editor->set_line_as_bookmarked(line, false); + } + } + last_line = to; } } |