Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 67dce30

Browse files
committedSep 11, 2023
Add code region folding to CodeEdit
1 parent 221884e commit 67dce30

16 files changed

+506
-27
lines changed
 

‎doc/classes/CodeEdit.xml

+52
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@
137137
Values of [code]-1[/code] convert the entire text.
138138
</description>
139139
</method>
140+
<method name="create_code_region">
141+
<return type="void" />
142+
<description>
143+
Creates a new code region with the selection. At least one single line comment delimiter have to be defined (see [method add_comment_delimiter]).
144+
A code region is a part of code that is highlighted when folded and can help organize your script.
145+
Code region start and end tags can be customized (see [method set_code_region_tags]).
146+
Code regions are delimited using start and end tags (respectively [code]region[/code] and [code]endregion[/code] by default) preceded by one line comment delimiter. (eg. [code]#region[/code] and [code]#endregion[/code])
147+
</description>
148+
</method>
140149
<method name="do_indent">
141150
<return type="void" />
142151
<description>
@@ -200,6 +209,18 @@
200209
Gets the index of the current selected completion option.
201210
</description>
202211
</method>
212+
<method name="get_code_region_end_tag" qualifiers="const">
213+
<return type="String" />
214+
<description>
215+
Returns the code region end tag (without comment delimiter).
216+
</description>
217+
</method>
218+
<method name="get_code_region_start_tag" qualifiers="const">
219+
<return type="String" />
220+
<description>
221+
Returns the code region start tag (without comment delimiter).
222+
</description>
223+
</method>
203224
<method name="get_delimiter_end_key" qualifiers="const">
204225
<return type="String" />
205226
<param index="0" name="delimiter_index" type="int" />
@@ -326,6 +347,20 @@
326347
Returns whether the line at the specified index is breakpointed or not.
327348
</description>
328349
</method>
350+
<method name="is_line_code_region_end" qualifiers="const">
351+
<return type="bool" />
352+
<param index="0" name="line" type="int" />
353+
<description>
354+
Returns whether the line at the specified index is a code region end.
355+
</description>
356+
</method>
357+
<method name="is_line_code_region_start" qualifiers="const">
358+
<return type="bool" />
359+
<param index="0" name="line" type="int" />
360+
<description>
361+
Returns whether the line at the specified index is a code region start.
362+
</description>
363+
</method>
329364
<method name="is_line_executing" qualifiers="const">
330365
<return type="bool" />
331366
<param index="0" name="line" type="int" />
@@ -382,6 +417,14 @@
382417
Sets if the code hint should draw below the text.
383418
</description>
384419
</method>
420+
<method name="set_code_region_tags">
421+
<return type="void" />
422+
<param index="0" name="start" type="String" default="&quot;region&quot;" />
423+
<param index="1" name="end" type="String" default="&quot;endregion&quot;" />
424+
<description>
425+
Sets the code region start and end tags (without comment delimiter).
426+
</description>
427+
</method>
385428
<method name="set_line_as_bookmarked">
386429
<return type="void" />
387430
<param index="0" name="line" type="int" />
@@ -629,6 +672,9 @@
629672
<theme_item name="executing_line_color" data_type="color" type="Color" default="Color(0.98, 0.89, 0.27, 1)">
630673
[Color] of the executing icon for executing lines.
631674
</theme_item>
675+
<theme_item name="folded_code_region_color" data_type="color" type="Color" default="Color(0.68, 0.46, 0.77, 0.2)">
676+
[Color] of background line highlight for folded code region.
677+
</theme_item>
632678
<theme_item name="font_color" data_type="color" type="Color" default="Color(0.875, 0.875, 0.875, 1)">
633679
Sets the font [Color].
634680
</theme_item>
@@ -693,12 +739,18 @@
693739
<theme_item name="can_fold" data_type="icon" type="Texture2D">
694740
Sets a custom [Texture2D] to draw in the line folding gutter when a line can be folded.
695741
</theme_item>
742+
<theme_item name="can_fold_code_region" data_type="icon" type="Texture2D">
743+
Sets a custom [Texture2D] to draw in the line folding gutter when a code region can be folded.
744+
</theme_item>
696745
<theme_item name="executing_line" data_type="icon" type="Texture2D">
697746
Icon to draw in the executing gutter for executing lines.
698747
</theme_item>
699748
<theme_item name="folded" data_type="icon" type="Texture2D">
700749
Sets a custom [Texture2D] to draw in the line folding gutter when a line is folded and can be unfolded.
701750
</theme_item>
751+
<theme_item name="folded_code_region" data_type="icon" type="Texture2D">
752+
Sets a custom [Texture2D] to draw in the line folding gutter when a code region is folded and can be unfolded.
753+
</theme_item>
702754
<theme_item name="folded_eol_icon" data_type="icon" type="Texture2D">
703755
Sets a custom [Texture2D] to draw at the end of a folded line.
704756
</theme_item>

‎doc/classes/EditorSettings.xml

+3
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,9 @@
959959
<member name="text_editor/theme/highlighting/executing_line_color" type="Color" setter="" getter="">
960960
The script editor's color for the debugger's executing line icon (displayed in the gutter).
961961
</member>
962+
<member name="text_editor/theme/highlighting/folded_code_region_color" type="Color" setter="" getter="">
963+
The script editor's background line highlighting color for folded code region.
964+
</member>
962965
<member name="text_editor/theme/highlighting/function_color" type="Color" setter="" getter="">
963966
The script editor's function call color.
964967
[b]Note:[/b] When using the GDScript syntax highlighter, this is replaced by the function definition color configured in the syntax theme for function definitions (e.g. [code]func _ready():[/code]).

‎editor/editor_settings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,7 @@ void EditorSettings::_load_godot2_text_editor_theme() {
848848
_initial_set("text_editor/theme/highlighting/breakpoint_color", Color(0.9, 0.29, 0.3));
849849
_initial_set("text_editor/theme/highlighting/executing_line_color", Color(0.98, 0.89, 0.27));
850850
_initial_set("text_editor/theme/highlighting/code_folding_color", Color(0.8, 0.8, 0.8, 0.8));
851+
_initial_set("text_editor/theme/highlighting/folded_code_region_color", Color(0.68, 0.46, 0.77, 0.2));
851852
_initial_set("text_editor/theme/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1));
852853
_initial_set("text_editor/theme/highlighting/search_result_border_color", Color(0.41, 0.61, 0.91, 0.38));
853854
}

‎editor/editor_themes.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ void EditorColorMap::create() {
208208
add_conversion_exception("GuiSpace");
209209
add_conversion_exception("CodeFoldedRightArrow");
210210
add_conversion_exception("CodeFoldDownArrow");
211+
add_conversion_exception("CodeRegionFoldedRightArrow");
212+
add_conversion_exception("CodeRegionFoldDownArrow");
211213
add_conversion_exception("TextEditorPlay");
212214
add_conversion_exception("Breakpoint");
213215
}
@@ -2088,6 +2090,7 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
20882090
const Color breakpoint_color = dark_theme ? error_color : Color(1, 0.27, 0.2, 1);
20892091
const Color executing_line_color = Color(0.98, 0.89, 0.27);
20902092
const Color code_folding_color = alpha3;
2093+
const Color folded_code_region_color = Color(0.68, 0.46, 0.77, 0.2);
20912094
const Color search_result_color = alpha1;
20922095
const Color search_result_border_color = dark_theme ? Color(0.41, 0.61, 0.91, 0.38) : Color(0, 0.4, 1, 0.38);
20932096

@@ -2128,6 +2131,7 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
21282131
setting->set_initial_value("text_editor/theme/highlighting/breakpoint_color", breakpoint_color, true);
21292132
setting->set_initial_value("text_editor/theme/highlighting/executing_line_color", executing_line_color, true);
21302133
setting->set_initial_value("text_editor/theme/highlighting/code_folding_color", code_folding_color, true);
2134+
setting->set_initial_value("text_editor/theme/highlighting/folded_code_region_color", folded_code_region_color, true);
21312135
setting->set_initial_value("text_editor/theme/highlighting/search_result_color", search_result_color, true);
21322136
setting->set_initial_value("text_editor/theme/highlighting/search_result_border_color", search_result_border_color, true);
21332137
} else if (text_editor_color_theme == "Godot 2") {
@@ -2147,6 +2151,8 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
21472151
theme->set_icon("space", "CodeEdit", theme->get_icon(SNAME("GuiSpace"), EditorStringName(EditorIcons)));
21482152
theme->set_icon("folded", "CodeEdit", theme->get_icon(SNAME("CodeFoldedRightArrow"), EditorStringName(EditorIcons)));
21492153
theme->set_icon("can_fold", "CodeEdit", theme->get_icon(SNAME("CodeFoldDownArrow"), EditorStringName(EditorIcons)));
2154+
theme->set_icon("folded_code_region", "CodeEdit", theme->get_icon(SNAME("CodeRegionFoldedRightArrow"), EditorStringName(EditorIcons)));
2155+
theme->set_icon("can_fold_code_region", "CodeEdit", theme->get_icon(SNAME("CodeRegionFoldDownArrow"), EditorStringName(EditorIcons)));
21502156
theme->set_icon("executing_line", "CodeEdit", theme->get_icon(SNAME("TextEditorPlay"), EditorStringName(EditorIcons)));
21512157
theme->set_icon("breakpoint", "CodeEdit", theme->get_icon(SNAME("Breakpoint"), EditorStringName(EditorIcons)));
21522158

@@ -2172,6 +2178,7 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
21722178
theme->set_color("breakpoint_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/breakpoint_color"));
21732179
theme->set_color("executing_line_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/executing_line_color"));
21742180
theme->set_color("code_folding_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/code_folding_color"));
2181+
theme->set_color("folded_code_region_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color"));
21752182
theme->set_color("search_result_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/search_result_color"));
21762183
theme->set_color("search_result_border_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/search_result_border_color"));
21772184

+1
Loading
Loading

‎editor/plugins/script_text_editor.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,12 @@ void ScriptTextEditor::_load_theme_settings() {
181181

182182
Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");
183183
Color updated_safe_line_number_color = EDITOR_GET("text_editor/theme/highlighting/safe_line_number_color");
184+
Color updated_folded_code_region_color = EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color");
184185

185186
bool safe_line_number_color_updated = updated_safe_line_number_color != safe_line_number_color;
186187
bool marked_line_color_updated = updated_marked_line_color != marked_line_color;
187-
if (safe_line_number_color_updated || marked_line_color_updated) {
188+
bool folded_code_region_color_updated = updated_folded_code_region_color != folded_code_region_color;
189+
if (safe_line_number_color_updated || marked_line_color_updated || folded_code_region_color_updated) {
188190
safe_line_number_color = updated_safe_line_number_color;
189191
for (int i = 0; i < text_edit->get_line_count(); i++) {
190192
if (marked_line_color_updated && text_edit->get_line_background_color(i) == marked_line_color) {
@@ -194,8 +196,13 @@ void ScriptTextEditor::_load_theme_settings() {
194196
if (safe_line_number_color_updated && text_edit->get_line_gutter_item_color(i, line_number_gutter) != default_line_number_color) {
195197
text_edit->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
196198
}
199+
200+
if (folded_code_region_color_updated && text_edit->get_line_background_color(i) == folded_code_region_color) {
201+
text_edit->set_line_background_color(i, updated_folded_code_region_color);
202+
}
197203
}
198204
marked_line_color = updated_marked_line_color;
205+
folded_code_region_color = updated_folded_code_region_color;
199206
}
200207

201208
theme_loaded = true;
@@ -647,7 +654,8 @@ void ScriptTextEditor::_update_errors() {
647654
bool last_is_safe = false;
648655
for (int i = 0; i < te->get_line_count(); i++) {
649656
if (errors.is_empty()) {
650-
te->set_line_background_color(i, Color(0, 0, 0, 0));
657+
bool is_folded_code_region = te->is_line_code_region_start(i) && te->is_line_folded(i);
658+
te->set_line_background_color(i, is_folded_code_region ? folded_code_region_color : Color(0, 0, 0, 0));
651659
} else {
652660
for (const ScriptLanguage::ScriptError &E : errors) {
653661
bool error_line = i == E.line - 1;
@@ -1312,6 +1320,9 @@ void ScriptTextEditor::_edit_option(int p_op) {
13121320
tx->unfold_all_lines();
13131321
tx->queue_redraw();
13141322
} break;
1323+
case EDIT_CREATE_CODE_REGION: {
1324+
tx->create_code_region();
1325+
} break;
13151326
case EDIT_TOGGLE_COMMENT: {
13161327
_edit_option_toggle_inline_comment();
13171328
} break;
@@ -2064,6 +2075,7 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p
20642075
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
20652076
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
20662077
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
2078+
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
20672079
}
20682080
if (p_foldable) {
20692081
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
@@ -2178,6 +2190,7 @@ void ScriptTextEditor::_enable_code_editor() {
21782190
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
21792191
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
21802192
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
2193+
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
21812194
sub_menu->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
21822195
edit_menu->get_popup()->add_child(sub_menu);
21832196
edit_menu->get_popup()->add_submenu_item(TTR("Folding"), "folding_menu");
@@ -2373,6 +2386,7 @@ void ScriptTextEditor::register_editor() {
23732386
ED_SHORTCUT("script_text_editor/toggle_fold_line", TTR("Fold/Unfold Line"), KeyModifierMask::ALT | Key::F);
23742387
ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_fold_line", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::F);
23752388
ED_SHORTCUT("script_text_editor/fold_all_lines", TTR("Fold All Lines"), Key::NONE);
2389+
ED_SHORTCUT("script_text_editor/create_code_region", TTR("Create Code Region"), KeyModifierMask::ALT | Key::R);
23762390
ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), Key::NONE);
23772391
ED_SHORTCUT("script_text_editor/duplicate_selection", TTR("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D);
23782392
ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C);

‎editor/plugins/script_text_editor.h

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ScriptTextEditor : public ScriptEditorBase {
9898
Color safe_line_number_color = Color(1, 1, 1);
9999

100100
Color marked_line_color = Color(1, 1, 1);
101+
Color folded_code_region_color = Color(1, 1, 1);
101102

102103
PopupPanel *color_panel = nullptr;
103104
ColorPicker *color_picker = nullptr;
@@ -133,6 +134,7 @@ class ScriptTextEditor : public ScriptEditorBase {
133134
EDIT_TOGGLE_WORD_WRAP,
134135
EDIT_TOGGLE_FOLD_LINE,
135136
EDIT_FOLD_ALL_LINES,
137+
EDIT_CREATE_CODE_REGION,
136138
EDIT_UNFOLD_ALL_LINES,
137139
SEARCH_FIND,
138140
SEARCH_FIND_NEXT,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Please sign in to comment.