Skip to content

Commit 4d294ca

Browse files
committed
Add script editor join line functionality
1 parent 7dac0bc commit 4d294ca

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

doc/classes/CodeEdit.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@
391391
Returns [code]true[/code] if the given line is folded. See [method fold_line].
392392
</description>
393393
</method>
394+
<method name="join_lines">
395+
<return type="void" />
396+
<param index="0" name="line_ending" type="String" default="&quot; &quot;" />
397+
<description>
398+
Joins all selected lines or lines containing a caret with their next line. Whitespace in between will be removed. If the next line has content, the [param line_ending] will be inserted in between.
399+
</description>
400+
</method>
394401
<method name="move_lines_down">
395402
<return type="void" />
396403
<description>

editor/gui/code_editor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,11 @@ void CodeTextEditor::input(const Ref<InputEvent> &event) {
910910
accept_event();
911911
return;
912912
}
913+
if (ED_IS_SHORTCUT("script_text_editor/join_lines", key_event)) {
914+
text_editor->join_lines();
915+
accept_event();
916+
return;
917+
}
913918
if (ED_IS_SHORTCUT("script_text_editor/duplicate_selection", key_event)) {
914919
text_editor->duplicate_selection();
915920
accept_event();

editor/script/script_editor_base.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ TextEditorBase::EditMenus::EditMenus() {
200200
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
201201
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
202202
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
203+
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/join_lines"), EDIT_JOIN_LINES);
203204
edit_menu_line->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
204205
edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), edit_menu_line);
205206
}
@@ -412,6 +413,9 @@ bool TextEditorBase::_edit_option(int p_op) {
412413
case EDIT_DELETE_LINE: {
413414
tx->delete_lines();
414415
} break;
416+
case EDIT_JOIN_LINES: {
417+
tx->join_lines();
418+
} break;
415419
case EDIT_DUPLICATE_SELECTION: {
416420
tx->duplicate_selection();
417421
} break;

editor/script/script_editor_base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class TextEditorBase : public ScriptEditorBase {
119119
BOOKMARK_GOTO_PREV,
120120
BOOKMARK_REMOVE_ALL,
121121

122+
EDIT_JOIN_LINES,
123+
122124
BASE_ENUM_COUNT,
123125
};
124126

editor/script/script_text_editor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,7 @@ void ScriptTextEditor::register_editor() {
25282528
ED_SHORTCUT("script_text_editor/move_up", TTRC("Move Up"), KeyModifierMask::ALT | Key::UP);
25292529
ED_SHORTCUT("script_text_editor/move_down", TTRC("Move Down"), KeyModifierMask::ALT | Key::DOWN);
25302530
ED_SHORTCUT("script_text_editor/delete_line", TTRC("Delete Line"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K);
2531+
ED_SHORTCUT("script_text_editor/join_lines", TTRC("Join Lines"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::J);
25312532

25322533
// Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
25332534
// The next/previous in history shortcut in this case makes a lot more sense.

editor/shader/text_shader_editor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,9 @@ void TextShaderEditor::_menu_option(int p_option) {
779779
case EDIT_EMOJI_AND_SYMBOL: {
780780
code_editor->get_text_editor()->show_emoji_and_symbol_picker();
781781
} break;
782+
case EDIT_JOIN_LINES: {
783+
code_editor->get_text_editor()->join_lines();
784+
} break;
782785
}
783786
if (p_option != SEARCH_FIND && p_option != SEARCH_REPLACE && p_option != SEARCH_GOTO_LINE) {
784787
callable_mp((Control *)code_editor->get_text_editor(), &Control::grab_focus).call_deferred(false);
@@ -1228,6 +1231,7 @@ TextShaderEditor::TextShaderEditor() {
12281231
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
12291232
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
12301233
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
1234+
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/join_lines"), EDIT_JOIN_LINES);
12311235
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
12321236
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
12331237
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);

editor/shader/text_shader_editor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class TextShaderEditor : public ShaderEditor {
134134
BOOKMARK_REMOVE_ALL,
135135
HELP_DOCS,
136136
EDIT_EMOJI_AND_SYMBOL,
137+
EDIT_JOIN_LINES,
137138
};
138139

139140
HBoxContainer *menu_bar_hbox = nullptr;

scene/gui/code_edit.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
27022740
void 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

scene/gui/code_edit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ class CodeEdit : public TextEdit {
523523
void move_lines_up();
524524
void move_lines_down();
525525
void delete_lines();
526+
void join_lines(const String &p_line_ending = " ");
526527
void duplicate_selection();
527528
void duplicate_lines();
528529

0 commit comments

Comments
 (0)