Skip to content

Commit 40b7931

Browse files
Aziroshinkitbdev
andcommitted
[CodeEdit] Fix folding for comments mixed with code region tags.
Co-authored-by: Kit Bishop <[email protected]>
1 parent 8bd9cde commit 40b7931

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

scene/gui/code_edit.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,12 +1645,12 @@ bool CodeEdit::can_fold_line(int p_line) const {
16451645
if (delimiter_end_line == p_line) {
16461646
/* Check we are the start of the block. */
16471647
if (p_line - 1 >= 0) {
1648-
if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
1648+
if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1 && !is_line_code_region_start(p_line - 1) && !is_line_code_region_end(p_line - 1))) {
16491649
return false;
16501650
}
16511651
}
16521652
/* Check it continues for at least one line. */
1653-
return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
1653+
return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1 && !is_line_code_region_start(p_line + 1) && !is_line_code_region_end(p_line + 1)));
16541654
}
16551655
return ((in_string != -1 && is_in_string(delimiter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimiter_end_line) != -1));
16561656
}
@@ -1705,6 +1705,10 @@ void CodeEdit::fold_line(int p_line) {
17051705
if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
17061706
break;
17071707
}
1708+
if (in_comment != -1 && (is_line_code_region_start(i) || is_line_code_region_end(i))) {
1709+
// A code region tag should split a comment block, ending it early.
1710+
break;
1711+
}
17081712
end_line = i;
17091713
}
17101714
}

tests/scene/test_code_edit.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,47 @@ TEST_CASE("[SceneTree][CodeEdit] folding") {
32693269
CHECK(code_edit->get_next_visible_line_offset_from(1, 1) == 4);
32703270
}
32713271

3272+
SUBCASE("[CodeEdit] folding comments including and/or adjacent to code regions") {
3273+
code_edit->add_comment_delimiter("#", "", true);
3274+
3275+
// Single line comment directly above a code region tag is not foldable.
3276+
code_edit->set_text("#line0\n#region a\nnothing\n#line3\n#endregion");
3277+
CHECK_FALSE(code_edit->can_fold_line(0));
3278+
CHECK_FALSE(code_edit->can_fold_line(3));
3279+
3280+
// Comment blocks.
3281+
// Foldable even when directly below a code region start tag.
3282+
code_edit->set_text("#line0\n#line1\n#region a\n#line3\n#line4\nnothing\n#endregion");
3283+
CHECK(code_edit->can_fold_line(3));
3284+
3285+
// Doesn't fold beyond region start tag.
3286+
code_edit->fold_line(0);
3287+
CHECK(code_edit->is_line_folded(0));
3288+
CHECK_EQ(code_edit->get_visible_line_count_in_range(0, 1), 1);
3289+
CHECK_EQ(code_edit->get_visible_line_count_in_range(2, 2), 1);
3290+
3291+
// Foldable even when directly below a code region end tag.
3292+
code_edit->set_text("#region a\nnothing\n#line2\n#line3\n#endregion\n#line5\n#line6");
3293+
CHECK(code_edit->can_fold_line(5));
3294+
3295+
// Doesn't fold beyond region end tag.
3296+
code_edit->fold_line(2);
3297+
CHECK(code_edit->is_line_folded(2));
3298+
CHECK_EQ(code_edit->get_visible_line_count_in_range(2, 3), 1);
3299+
CHECK_EQ(code_edit->get_visible_line_count_in_range(4, 4), 1);
3300+
3301+
code_edit->add_comment_delimiter("/*", "*/", false);
3302+
3303+
// Multiline comments.
3304+
// Folds a region tag inside it.
3305+
code_edit->set_text("/*\nnothing\n#region a\n*/\n#endregion");
3306+
CHECK(code_edit->can_fold_line(0));
3307+
code_edit->fold_line(0);
3308+
CHECK(code_edit->is_line_folded(0));
3309+
CHECK_EQ(code_edit->get_visible_line_count_in_range(0, 3), 1);
3310+
CHECK_EQ(code_edit->get_visible_line_count_in_range(4, 4), 1);
3311+
}
3312+
32723313
SUBCASE("[CodeEdit] folding carets") {
32733314
// Folding a line moves all carets that would be hidden.
32743315
code_edit->set_text("test\n\tline1\n\t\tline 2\n");

0 commit comments

Comments
 (0)