Skip to content

Commit 6fce829

Browse files
committed
Merge pull request godotengine#105007 from Aziroshin/dev/aziroshin/comment-after-region-not-folding-102382
[CodeEdit] Fix folding for comments mixed with code region tags.
2 parents 722ddf6 + 40b7931 commit 6fce829

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
@@ -1644,12 +1644,12 @@ bool CodeEdit::can_fold_line(int p_line) const {
16441644
if (delimiter_end_line == p_line) {
16451645
/* Check we are the start of the block. */
16461646
if (p_line - 1 >= 0) {
1647-
if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1)) {
1647+
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))) {
16481648
return false;
16491649
}
16501650
}
16511651
/* Check it continues for at least one line. */
1652-
return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1));
1652+
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)));
16531653
}
16541654
return ((in_string != -1 && is_in_string(delimiter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimiter_end_line) != -1));
16551655
}
@@ -1704,6 +1704,10 @@ void CodeEdit::fold_line(int p_line) {
17041704
if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
17051705
break;
17061706
}
1707+
if (in_comment != -1 && (is_line_code_region_start(i) || is_line_code_region_end(i))) {
1708+
// A code region tag should split a comment block, ending it early.
1709+
break;
1710+
}
17071711
end_line = i;
17081712
}
17091713
}

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)