Skip to content

Commit

Permalink
Merge pull request #74623 from MewPurPur/edit-text-with-style
Browse files Browse the repository at this point in the history
Code style improvements to text_edit and related
  • Loading branch information
YuriSizov authored Apr 17, 2023
2 parents e349cd8 + f587a21 commit de416c5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 126 deletions.
4 changes: 2 additions & 2 deletions doc/classes/TextEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@
<return type="String" />
<param index="0" name="caret_index" type="int" default="-1" />
<description>
Returns the text inside the selection.
Returns the text inside the selection of a caret, or all the carets if [param caret_index] is its default value [code]-1[/code].
</description>
</method>
<method name="get_selection_column" qualifiers="const">
Expand Down Expand Up @@ -1378,7 +1378,7 @@
Select whole words as if the user double clicked.
</constant>
<constant name="SELECTION_MODE_LINE" value="4" enum="SelectionMode">
Select whole lines as if the user tripped clicked.
Select whole lines as if the user triple clicked.
</constant>
<constant name="LINE_WRAPPING_NONE" value="0" enum="LineWrappingMode">
Line wrapping is disabled.
Expand Down
55 changes: 28 additions & 27 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,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);
}
Expand All @@ -210,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;
}
}
Expand All @@ -224,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);
Expand Down Expand Up @@ -274,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);

Expand Down Expand Up @@ -698,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));
Expand Down Expand Up @@ -733,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));
Expand Down Expand Up @@ -889,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;
}
Expand Down Expand Up @@ -1127,8 +1129,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();

Expand Down Expand Up @@ -1305,11 +1306,11 @@ 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];

Expand All @@ -1333,7 +1334,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++;
}
Expand All @@ -1347,12 +1348,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++) {
Expand Down Expand Up @@ -1400,11 +1401,11 @@ 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;
Vector<int> group_border_ends;
// 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];

Expand All @@ -1426,7 +1427,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++;
}
Expand All @@ -1444,28 +1445,28 @@ void CodeTextEditor::move_lines_down() {
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 > 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++) {
int c = caret_groups[i][j];
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--) {
Expand Down Expand Up @@ -1899,7 +1900,7 @@ int CodeTextEditor::_get_affected_lines_to(int 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
// Don't affect a line with no selected characters.
if (text_editor->get_selection_to_column(p_caret) == 0) {
line--;
}
Expand Down
13 changes: 6 additions & 7 deletions scene/gui/code_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
return;
}

// Override new line actions, for auto indent
// Override new line actions, for auto indent.
if (k->is_action("ui_text_newline_above", true)) {
_new_line(false, true);
accept_event();
Expand All @@ -601,7 +601,7 @@ void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
return;
}

/* Remove shift otherwise actions will not match. */
// Remove shift, otherwise actions will not match.
k = k->duplicate();
k->set_shift_pressed(false);

Expand Down Expand Up @@ -774,8 +774,7 @@ void CodeEdit::_backspace_internal(int p_caret) {
}
}

// For space indentation we need to do a simple unindent if there are no chars to the left, acting in the
// same way as tabs.
// For space indentation we need to do a basic unindent if there are no chars to the left, acting the same way as tabs.
if (indent_using_spaces && cc != 0) {
if (get_first_non_whitespace_column(cl) >= cc) {
prev_column = cc - _calculate_spaces_till_next_left_indent(cc);
Expand Down Expand Up @@ -1369,7 +1368,7 @@ PackedInt32Array CodeEdit::get_bookmarked_lines() const {
return ret;
}

// executing lines
// Executing lines
void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
int mask = get_line_gutter_metadata(p_line, main_gutter);
set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
Expand Down Expand Up @@ -2061,7 +2060,7 @@ void CodeEdit::confirm_code_completion(bool p_replace) {
insert_text_at_caret(insert_text.substr(matching_chars), i);
}

//* Handle merging of symbols eg strings, brackets.
// Handle merging of symbols eg strings, brackets.
const String line = get_line(caret_line);
char32_t next_char = line[get_caret_column(i)];
char32_t last_completion_char = insert_text[insert_text.length() - 1];
Expand Down Expand Up @@ -2236,7 +2235,7 @@ void CodeEdit::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);

// executing lines
// Executing lines
ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
Expand Down
14 changes: 7 additions & 7 deletions scene/gui/line_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {

_reset_caret_blink_timer();
if (b->is_pressed()) {
accept_event(); // don't pass event further when clicked on text field
accept_event(); // Don't pass event further when clicked on text field.
if (!text.is_empty() && is_editable() && _is_over_clear_button(b->get_position())) {
clear_button_status.press_attempt = true;
clear_button_status.pressing_inside = true;
Expand Down Expand Up @@ -429,7 +429,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
return;
}

// Alt+ Unicode input:
// Alt + Unicode input:
if (k->is_alt_pressed()) {
if (!alt_start) {
if (k->get_keycode() == Key::KP_ADD) {
Expand Down Expand Up @@ -470,7 +470,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
}
}

// Default is ENTER and KP_ENTER. Cannot use ui_accept as default includes SPACE
// Default is ENTER and KP_ENTER. Cannot use ui_accept as default includes SPACE.
if (k->is_action("ui_text_submit", false)) {
emit_signal(SNAME("text_submitted"), text);
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
Expand Down Expand Up @@ -608,7 +608,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
bool allow_unicode_handling = !(k->is_command_or_control_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());

if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
// Handle Unicode (if no modifiers active)
// Handle Unicode if no modifiers are active.
selection_delete();
char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
int prev_len = text.length();
Expand Down Expand Up @@ -1116,7 +1116,7 @@ void LineEdit::_notification(int p_what) {
ime_text = "";
ime_selection = Point2();
_shape();
set_caret_column(caret_column); // Update scroll_offset
set_caret_column(caret_column); // Update scroll_offset.

if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
DisplayServer::get_singleton()->virtual_keyboard_hide();
Expand All @@ -1137,7 +1137,7 @@ void LineEdit::_notification(int p_what) {
}

_shape();
set_caret_column(caret_column); // Update scroll_offset
set_caret_column(caret_column); // Update scroll_offset.

queue_redraw();
}
Expand Down Expand Up @@ -2360,7 +2360,7 @@ Key LineEdit::_get_menu_action_accelerator(const String &p_action) {
return Key::NONE;
}

// Use physical keycode if non-zero
// Use physical keycode if non-zero.
if (event->get_physical_keycode() != Key::NONE) {
return event->get_physical_keycode_with_modifiers();
} else {
Expand Down
2 changes: 1 addition & 1 deletion scene/gui/spin_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void SpinBox::_text_submitted(const String &p_string) {
expr.instantiate();

String num = TS->parse_number(p_string);
// Ignore the prefix and suffix in the expression
// Ignore the prefix and suffix in the expression.
Error err = expr->parse(num.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
if (err != OK) {
return;
Expand Down
Loading

0 comments on commit de416c5

Please sign in to comment.