Skip to content

Commit 213f0a7

Browse files
committed
Ensure chunks are nested in sections
1 parent c367795 commit 213f0a7

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

crates/ark/src/lsp/folding_range.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,18 @@ fn cell_processor(
362362
line_idx: usize,
363363
line_text: &str,
364364
) {
365-
let cell_pattern: Regex = Regex::new(r"^#+( %%|\+) (.*)").unwrap();
365+
// Check if the line is a comment section
366+
if RE_COMMENT_SECTION.is_match(line_text) {
367+
if let Some(start_line) = cell_marker.take() {
368+
let folding_range = comment_range(start_line, line_idx - 1);
369+
folding_ranges.push(folding_range);
370+
}
371+
return; // Stop processing as this is a comment section
372+
}
366373

367-
if !cell_pattern.is_match(line_text) {
368-
} else {
374+
// Check if the line is a chunk delimiter
375+
let cell_pattern: Regex = Regex::new(r"^#+( %%|\+) (.*)").unwrap();
376+
if cell_pattern.is_match(line_text) {
369377
let Some(start_line) = cell_marker else {
370378
cell_marker.replace(line_idx);
371379
return;
@@ -466,24 +474,6 @@ mod tests {
466474
);
467475
}
468476

469-
#[test]
470-
fn test_folding_section_chunks_with_section_in_middle() {
471-
// FIXME: First chunk overlaps section, and section overlaps second
472-
// chunk. Causes section folding to not appear in Positron.
473-
insta::assert_debug_snapshot!(test_folding_range(
474-
"
475-
#+ Cell
476-
a
477-
478-
# Section ----
479-
b
480-
481-
#+ Other cell
482-
c
483-
"
484-
));
485-
}
486-
487477
#[test]
488478
fn test_folding_section_comments_basic() {
489479
// Note the chunks are nested in comment sections
@@ -548,6 +538,23 @@ a"
548538
));
549539
}
550540

541+
#[test]
542+
fn test_folding_section_chunks_with_section_in_middle() {
543+
// Chunks should be nested in sections
544+
insta::assert_debug_snapshot!(test_folding_range(
545+
"
546+
#+ Cell
547+
a
548+
549+
# Section ----
550+
b
551+
552+
#+ Other cell
553+
c
554+
"
555+
));
556+
}
557+
551558
// Test for VS Code region markers
552559
#[test]
553560
fn test_folding_regions() {

crates/ark/src/lsp/snapshots/ark__lsp__folding_range__tests__folding_mixed.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ expression: "test_folding_range(\"\n# First section ----\nfunction() {\n # #reg
4848
FoldingRange {
4949
start_line: 9,
5050
start_character: None,
51-
end_line: 13,
51+
end_line: 11,
5252
end_character: None,
5353
kind: Some(
5454
Region,

crates/ark/src/lsp/snapshots/ark__lsp__folding_range__tests__folding_section_chunks_with_section_in_middle.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ expression: "test_folding_range(\"\n#+ Cell\na\n\n# Section ----\nb\n\n#+ Other
66
FoldingRange {
77
start_line: 1,
88
start_character: None,
9-
end_line: 6,
9+
end_line: 3,
1010
end_character: None,
1111
kind: Some(
1212
Region,

0 commit comments

Comments
 (0)