Repository: godotengine/godot
File: scene/gui/code_edit.cpp (C++)

Code before:

void CodeEdit::join_lines(const String &p_line_ending) {
	begin_complex_operation();
	begin_multicaret_edit();

	Vector<Point2i> line_ranges = get_line_ranges_from_carets();
	int line_offset = 0;
	for (Point2i line_range : line_ranges) {
		for (int32_t line_index = line_range.x; line_index <= line_range.y; line_index++) {
			int32_t real_line = line_index + line_offset;
			if (real_line + 1 >= get_line_count()) {
				break;
			}
			unfold_line(real_line);
			String line = get_line(real_line);
			int line_length = line.length();
			int next_line_leading_whitespace_length = get_first_non_whitespace_column(real_line + 1);
			int next_line_length = get_line(real_line + 1).length();
			int corrected_line_length = line_length - 1;
			if (line_length != 0) {
				for (; corrected_line_length > 1; corrected_line_length--) {
					if (!is_whitespace(line[corrected_line_length])) {
						break;
					}
				}
			}
			corrected_line_length++;
			remove_text(real_line, corrected_line_length, real_line + 1, next_line_leading_whitespace_length);
			if (next_line_leading_whitespace_length != next_line_length && corrected_line_length != 0) {
				insert_text(p_line_ending, real_line, corrected_line_length);
			}
			line_offset--;
		}
	}

	end_multicaret_edit();
	end_complex_operation();
}

void CodeEdit::duplicate_selection() {
	begin_complex_operation();
	begin_multicaret_edit();

	// Duplicate lines from carets without selections first.
	for (int i = 0; i < get_caret_count(); i++) {
		if (multicaret_edit_ignore_caret(i)) {
			continue;
		}
		for (int l = get_selection_from_line(i); l <= get_selection_to_line(i); l++) {
			unfold_line(l);
		}

Review comment (suggestion):
```suggestion
			for (; corrected_line_length >= 0; corrected_line_length--) {
				if (!is_whitespace(line[corrected_line_length])) {
					break;
				}
			}
```
Can be simplified, the for loop condition handles it.
Also I believe it needs to be >= 0 instead of > 1, otherwise whitespace at the beginning or after just 1 character won't be caught.

Code after:
		}
		// Fix selection if the last one ends at column 0, since it won't be moved.
		bool selection_to_line_at_end = false;
		for (int i = 0; i < get_caret_count(); i++) {
			if (has_selection(i) && get_selection_to_column(i) == 0 && get_selection_to_line(i) == line_range.y + 1) {
				selection_to_line_at_end = get_selection_to_line(i) == get_line_count() - 1;
				if (selection_to_line_at_end) {
					break;
				}
				if (is_caret_after_selection_origin(i)) {
					set_caret_line(get_caret_line(i) + 1, false, true, -1, i);
				} else {
					set_selection_origin_line(get_selection_origin_line(i) + 1, true, -1, i);
				}
				break;
			}
		}
		if (selection_to_line_at_end) {
			continue;
		}

		unfold_line(line_range.y + 1);
		for (int line = line_range.y; line >= line_range.x; line--) {
			unfold_line(line);
			swap_lines(line + 1, line);
		}
	}
	adjust_viewport_to_caret();

	end_multicaret_edit();
	end_complex_operation();
}

void CodeEdit::delete_lines() {
	begin_complex_operation();
	begin_multicaret_edit();

	Vector<Point2i> line_ranges = get_line_ranges_from_carets();
	int line_offset = 0;
	for (Point2i line_range : line_ranges) {
		// Remove last line of range separately to preserve carets.
		unfold_line(line_range.y + line_offset);
		remove_line_at(line_range.y + line_offset);
		if (line_range.x != line_range.y) {
			remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0);
		}
		line_offset += line_range.x - line_range.y - 1;
	}

	// Deselect all.
	deselect();