@@ -2699,6 +2699,44 @@ void CodeEdit::delete_lines() {
26992699 end_complex_operation ();
27002700}
27012701
2702+ void CodeEdit::join_lines (const String &p_line_ending) {
2703+ ERR_FAIL_COND_MSG (p_line_ending.contains_char (' \n ' ), " Cannot join lines with a newline." );
2704+
2705+ begin_complex_operation ();
2706+ begin_multicaret_edit ();
2707+
2708+ Vector<Point2i> line_ranges = get_line_ranges_from_carets ();
2709+ int line_offset = 0 ;
2710+ for (const Point2i &line_range : line_ranges) {
2711+ for (int32_t line_index = line_range.x ; line_index <= line_range.y ; line_index++) {
2712+ int32_t real_line = line_index + line_offset;
2713+ if (real_line + 1 >= get_line_count ()) {
2714+ break ;
2715+ }
2716+ unfold_line (real_line);
2717+ String line = get_line (real_line);
2718+ int line_length = line.length ();
2719+ int next_line_leading_whitespace_length = get_first_non_whitespace_column (real_line + 1 );
2720+ int next_line_length = get_line (real_line + 1 ).length ();
2721+ int corrected_line_length = line_length - 1 ;
2722+ for (; corrected_line_length >= 0 ; corrected_line_length--) {
2723+ if (!is_whitespace (line[corrected_line_length])) {
2724+ break ;
2725+ }
2726+ }
2727+ corrected_line_length++;
2728+ remove_text (real_line, corrected_line_length, real_line + 1 , next_line_leading_whitespace_length);
2729+ if (next_line_leading_whitespace_length != next_line_length && corrected_line_length != 0 ) {
2730+ insert_text (p_line_ending, real_line, corrected_line_length);
2731+ }
2732+ line_offset--;
2733+ }
2734+ }
2735+
2736+ end_multicaret_edit ();
2737+ end_complex_operation ();
2738+ }
2739+
27022740void CodeEdit::duplicate_selection () {
27032741 begin_complex_operation ();
27042742 begin_multicaret_edit ();
@@ -2971,6 +3009,7 @@ void CodeEdit::_bind_methods() {
29713009 ClassDB::bind_method (D_METHOD (" move_lines_up" ), &CodeEdit::move_lines_up);
29723010 ClassDB::bind_method (D_METHOD (" move_lines_down" ), &CodeEdit::move_lines_down);
29733011 ClassDB::bind_method (D_METHOD (" delete_lines" ), &CodeEdit::delete_lines);
3012+ ClassDB::bind_method (D_METHOD (" join_lines" , " line_ending" ), &CodeEdit::join_lines, DEFVAL (" " ));
29743013 ClassDB::bind_method (D_METHOD (" duplicate_selection" ), &CodeEdit::duplicate_selection);
29753014 ClassDB::bind_method (D_METHOD (" duplicate_lines" ), &CodeEdit::duplicate_lines);
29763015
0 commit comments