Skip to content

Commit

Permalink
Merge pull request #49238 from Paulb23/code_edit_code_folding
Browse files Browse the repository at this point in the history
Move code folding into CodeEdit and hide line hiding API
  • Loading branch information
akien-mga authored Jun 16, 2021
2 parents 48fe9c9 + 5c618dd commit 341cb8d
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 306 deletions.
70 changes: 70 additions & 0 deletions doc/classes/CodeEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@
Line only denotes if the region should continue until the end of the line or carry over on to the next line. If the end key is blank this is automatically set to [code]true[/code].
</description>
</method>
<method name="can_fold_line" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Returns if the given line is foldable, that is, it has indented lines right below it or a comment / string block.
</description>
</method>
<method name="cancel_code_completion">
<return type="void">
</return>
Expand Down Expand Up @@ -134,6 +143,22 @@
Inserts the selected entry into the text. If [code]replace[/code] is true, any existing text is replaced rather then merged.
</description>
</method>
<method name="fold_all_lines">
<return type="void">
</return>
<description>
Folds all lines that are possible to be folded (see [method can_fold_line]).
</description>
</method>
<method name="fold_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Folds the given line, if possible (see [method can_fold_line]).
</description>
</method>
<method name="get_bookmarked_lines" qualifiers="const">
<return type="Array">
</return>
Expand Down Expand Up @@ -221,6 +246,13 @@
<description>
</description>
</method>
<method name="get_folded_lines" qualifiers="const">
<return type="int[]">
</return>
<description>
Return all lines that are current folded.
</description>
</method>
<method name="get_text_for_code_completion" qualifiers="const">
<return type="String">
</return>
Expand Down Expand Up @@ -292,6 +324,15 @@
<description>
</description>
</method>
<method name="is_line_folded" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Returns whether the line at the specified index is folded or not.
</description>
</method>
<method name="remove_comment_delimiter">
<return type="void">
</return>
Expand Down Expand Up @@ -376,6 +417,30 @@
<description>
</description>
</method>
<method name="toggle_foldable_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Toggle the folding of the code block at the given line.
</description>
</method>
<method name="unfold_all_lines">
<return type="void">
</return>
<description>
</description>
</method>
<method name="unfold_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Unfolds all lines that were previously folded.
</description>
</method>
<method name="update_code_completion_options">
<return type="void">
</return>
Expand Down Expand Up @@ -411,6 +476,9 @@
<member name="draw_line_numbers" type="bool" setter="set_draw_line_numbers" getter="is_draw_line_numbers_enabled" default="false">
</member>
<member name="layout_direction" type="int" setter="set_layout_direction" getter="get_layout_direction" override="true" enum="Control.LayoutDirection" default="2" />
<member name="line_folding" type="bool" setter="set_line_folding_enabled" getter="is_line_folding_enabled" default="true">
Sets whether line folding is allowed.
</member>
<member name="structured_text_bidi_override_options" type="Array" setter="set_structured_text_bidi_override_options" getter="get_structured_text_bidi_override_options" override="true" default="[ ]" />
<member name="text_direction" type="int" setter="set_text_direction" getter="get_text_direction" override="true" enum="Control.TextDirection" default="1" />
<member name="zero_pad_line_numbers" type="bool" setter="set_line_numbers_zero_padded" getter="is_line_numbers_zero_padded" default="false">
Expand Down Expand Up @@ -500,6 +568,8 @@
</theme_item>
<theme_item name="folded" type="Texture2D">
</theme_item>
<theme_item name="folded_eol_icon" type="Texture2D">
</theme_item>
<theme_item name="font" type="Font">
</theme_item>
<theme_item name="font_color" type="Color" default="Color( 0.88, 0.88, 0.88, 1 )">
Expand Down
86 changes: 1 addition & 85 deletions doc/classes/TextEdit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
<description>
</description>
</method>
<method name="can_fold" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Returns if the given line is foldable, that is, it has indented lines right below it.
</description>
</method>
<method name="center_viewport_to_cursor">
<return type="void">
</return>
Expand Down Expand Up @@ -95,7 +86,7 @@
<description>
Moves the cursor at the specified [code]line[/code] index.
If [code]adjust_viewport[/code] is set to [code]true[/code], the viewport will center at the cursor position after the move occurs.
If [code]can_be_hidden[/code] is set to [code]true[/code], the specified [code]line[/code] can be hidden using [method set_line_as_hidden].
If [code]can_be_hidden[/code] is set to [code]true[/code], the specified [code]line[/code] can be hidden.
</description>
</method>
<method name="cut">
Expand All @@ -112,22 +103,6 @@
Deselects the current selection.
</description>
</method>
<method name="fold_all_lines">
<return type="void">
</return>
<description>
Folds all lines that are possible to be folded (see [method can_fold]).
</description>
</method>
<method name="fold_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Folds the given line, if possible (see [method can_fold]).
</description>
</method>
<method name="get_caret_draw_pos" qualifiers="const">
<return type="Vector2">
</return>
Expand Down Expand Up @@ -329,15 +304,6 @@
Returns [code]true[/code] if the caret is visible on the screen.
</description>
</method>
<method name="is_folded" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Returns whether the line at the specified index is folded or not.
</description>
</method>
<method name="is_gutter_clickable" qualifiers="const">
<return type="bool">
</return>
Expand Down Expand Up @@ -372,15 +338,6 @@
<description>
</description>
</method>
<method name="is_line_hidden" qualifiers="const">
<return type="bool">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Returns whether the line at the specified index is hidden or not.
</description>
</method>
<method name="is_selection_active" qualifiers="const">
<return type="bool">
</return>
Expand Down Expand Up @@ -560,17 +517,6 @@
Sets the text for a specific line.
</description>
</method>
<method name="set_line_as_hidden">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<argument index="1" name="enable" type="bool">
</argument>
<description>
If [code]true[/code], hides the line of the specified index.
</description>
</method>
<method name="set_line_background_color">
<return type="void">
</return>
Expand Down Expand Up @@ -665,38 +611,13 @@
<description>
</description>
</method>
<method name="toggle_fold_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Toggle the folding of the code block at the given line.
</description>
</method>
<method name="undo">
<return type="void">
</return>
<description>
Perform undo operation.
</description>
</method>
<method name="unfold_line">
<return type="void">
</return>
<argument index="0" name="line" type="int">
</argument>
<description>
Unfolds the given line, if folded.
</description>
</method>
<method name="unhide_all_lines">
<return type="void">
</return>
<description>
Unhide all lines that were previously set to hidden by [method set_line_as_hidden].
</description>
</method>
</methods>
<members>
<member name="caret_blink" type="bool" setter="cursor_set_blink_enabled" getter="cursor_get_blink_enabled" default="false">
Expand Down Expand Up @@ -730,9 +651,6 @@
If [code]true[/code], the "tab" character will have a visible representation.
</member>
<member name="focus_mode" type="int" setter="set_focus_mode" getter="get_focus_mode" override="true" enum="Control.FocusMode" default="2" />
<member name="hiding_enabled" type="bool" setter="set_hiding_enabled" getter="is_hiding_enabled" default="false">
If [code]true[/code], all lines that have been set to hidden by [method set_line_as_hidden], will not be visible.
</member>
<member name="highlight_all_occurrences" type="bool" setter="set_highlight_all_occurrences" getter="is_highlight_all_occurrences_enabled" default="false">
If [code]true[/code], all occurrences of the selected text will be highlighted.
</member>
Expand Down Expand Up @@ -972,8 +890,6 @@
</theme_item>
<theme_item name="caret_color" type="Color" default="Color( 0.88, 0.88, 0.88, 1 )">
</theme_item>
<theme_item name="code_folding_color" type="Color" default="Color( 0.8, 0.8, 0.8, 0.8 )">
</theme_item>
<theme_item name="current_line_color" type="Color" default="Color( 0.25, 0.25, 0.26, 0.8 )">
Sets the [Color] of the breakpoints. [member breakpoint_gutter] has to be enabled.
</theme_item>
Expand Down
3 changes: 2 additions & 1 deletion editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ void CodeTextEditor::update_editor_settings() {
text_editor->set_draw_line_numbers(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_numbers"));
text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/appearance/line_numbers_zero_padded"));
text_editor->set_draw_bookmarks_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/show_bookmark_gutter"));
text_editor->set_hiding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
text_editor->set_line_folding_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
text_editor->set_draw_fold_gutter(EditorSettings::get_singleton()->get("text_editor/appearance/code_folding"));
text_editor->set_wrap_enabled(EditorSettings::get_singleton()->get("text_editor/appearance/word_wrap"));
text_editor->set_show_line_length_guidelines(EditorSettings::get_singleton()->get("text_editor/appearance/show_line_length_guidelines"));
Expand Down Expand Up @@ -1502,6 +1502,7 @@ void CodeTextEditor::set_error_pos(int p_line, int p_column) {

void CodeTextEditor::goto_error() {
if (error->get_text() != "") {
text_editor->unfold_line(error_line);
text_editor->cursor_set_line(error_line);
text_editor->cursor_set_column(error_column);
text_editor->center_viewport_to_cursor();
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ void ScriptTextEditor::_edit_option(int p_op) {
code_editor->clone_lines_down();
} break;
case EDIT_TOGGLE_FOLD_LINE: {
tx->toggle_fold_line(tx->cursor_get_line());
tx->toggle_foldable_line(tx->cursor_get_line());
tx->update();
} break;
case EDIT_FOLD_ALL_LINES: {
Expand Down Expand Up @@ -1549,7 +1549,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
}

bool has_color = (word_at_pos == "Color");
bool foldable = tx->can_fold(row) || tx->is_folded(row);
bool foldable = tx->can_fold_line(row) || tx->is_line_folded(row);
bool open_docs = false;
bool goto_definition = false;

Expand Down
8 changes: 4 additions & 4 deletions editor/plugins/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void TextEditor::_edit_option(int p_op) {
code_editor->clone_lines_down();
} break;
case EDIT_TOGGLE_FOLD_LINE: {
tx->toggle_fold_line(tx->cursor_get_line());
tx->toggle_foldable_line(tx->cursor_get_line());
tx->update();
} break;
case EDIT_FOLD_ALL_LINES: {
Expand Down Expand Up @@ -432,8 +432,8 @@ void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);

tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
bool can_fold = tx->can_fold(row);
bool is_folded = tx->is_folded(row);
bool can_fold = tx->can_fold_line(row);
bool is_folded = tx->is_line_folded(row);

if (tx->is_right_click_moving_caret()) {
if (tx->is_selection_active()) {
Expand Down Expand Up @@ -463,7 +463,7 @@ void TextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
if (k.is_valid() && k->is_pressed() && k->get_keycode() == KEY_MENU) {
CodeEdit *tx = code_editor->get_text_editor();
int line = tx->cursor_get_line();
_make_context_menu(tx->is_selection_active(), tx->can_fold(line), tx->is_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->_get_cursor_pixel_pos()));
_make_context_menu(tx->is_selection_active(), tx->can_fold_line(line), tx->is_line_folded(line), (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->_get_cursor_pixel_pos()));
context_menu->grab_focus();
}
}
Expand Down
Loading

0 comments on commit 341cb8d

Please sign in to comment.