Skip to content

Commit 74d3f1d

Browse files
authored
Fix regression in region detection (#857)
1 parent 60e0173 commit 74d3f1d

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

crates/ark/src/lsp/folding_range.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ fn region_processor(
282282
}
283283

284284
fn parse_region_type(line_text: &str) -> Option<RegionType> {
285-
let region_start = Regex::new(r"^\s*#+ #region\b").unwrap();
286-
let region_end = Regex::new(r"^\s*#+ #endregion\b").unwrap();
285+
// Source: https://github.com/microsoft/vscode/blob/d6d5034f/extensions/python/language-configuration.json#L45-L48
286+
let region_start = Regex::new(r"^\s*#\s*region\b").unwrap();
287+
let region_end = Regex::new(r"^\s*#\s*endregion\b").unwrap();
287288

288289
if region_start.is_match(line_text) {
289290
Some(RegionType::Start)
@@ -391,17 +392,17 @@ mod tests {
391392
assert_eq!(parse_region_type("# #endregionsomething"), None);
392393

393394
// Valid regions
394-
assert_eq!(parse_region_type("# #region"), Some(RegionType::Start));
395-
assert_eq!(parse_region_type("## #region "), Some(RegionType::Start));
395+
assert_eq!(parse_region_type("#region"), Some(RegionType::Start));
396+
assert_eq!(parse_region_type("# region "), Some(RegionType::Start));
396397
assert_eq!(
397-
parse_region_type("# #region my special area"),
398+
parse_region_type("#region my special area"),
398399
Some(RegionType::Start)
399400
);
400401

401-
assert_eq!(parse_region_type("# #endregion"), Some(RegionType::End));
402-
assert_eq!(parse_region_type("## #endregion "), Some(RegionType::End));
402+
assert_eq!(parse_region_type("#endregion"), Some(RegionType::End));
403+
assert_eq!(parse_region_type("# endregion "), Some(RegionType::End));
403404
assert_eq!(
404-
parse_region_type("# #endregion end of my special area"),
405+
parse_region_type("#endregion end of my special area"),
405406
Some(RegionType::End)
406407
);
407408
}
@@ -540,15 +541,15 @@ d
540541
fn test_folding_regions() {
541542
insta::assert_debug_snapshot!(test_folding_range(
542543
"
543-
# #region Important code
544+
#region Important code
544545
a
545546
b
546547
c
547-
# #endregion
548+
#endregion
548549
549-
# #region Another section
550+
#region Another section
550551
d
551-
# #endregion"
552+
#endregion"
552553
));
553554
}
554555

@@ -597,9 +598,9 @@ list <- list(
597598
"
598599
# First section ----
599600
function() {
600-
# #region nested region
601+
#region nested region
601602
a
602-
# #endregion
603+
#endregion
603604
}
604605
605606
## Subsection ----
@@ -683,7 +684,7 @@ bar)
683684
"
684685
# Complex example ----
685686
function(a, b, c) {
686-
# #region inner calculations
687+
#region inner calculations
687688
x <- a + b
688689
y <- b + c
689690
@@ -693,7 +694,7 @@ function(a, b, c) {
693694
} else {
694695
result <- x / y
695696
}
696-
# #endregion
697+
#endregion
697698
698699
result
699700
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/ark/src/lsp/folding_range.rs
3-
expression: "test_folding_range(\"\n# Complex example ----\nfunction(a, b, c) {\n # #region inner calculations\n x <- a + b\n y <- b + c\n\n if (x > y) {\n # %% cell inside function\n result <- x * y\n } else {\n result <- x / y\n }\n # #endregion\n\n result\n}\n\n## Subsection ----\n# This is a regular comment, not a section or region\")"
3+
expression: "test_folding_range(\"\n# Complex example ----\nfunction(a, b, c) {\n #region inner calculations\n x <- a + b\n y <- b + c\n\n if (x > y) {\n # %% cell inside function\n result <- x * y\n } else {\n result <- x / y\n }\n #endregion\n\n result\n}\n\n## Subsection ----\n# This is a regular comment, not a section or region\")"
44
---
55
[
66
FoldingRange {

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
@@ -1,6 +1,6 @@
11
---
22
source: crates/ark/src/lsp/folding_range.rs
3-
expression: "test_folding_range(\"\n# First section ----\nfunction() {\n # #region nested region\n a\n # #endregion\n}\n\n## Subsection ----\n# %% Cell in subsection\nb\n\n# Another section ----\nc\")"
3+
expression: "test_folding_range(\"\n# First section ----\nfunction() {\n #region nested region\n a\n #endregion\n}\n\n## Subsection ----\n# %% Cell in subsection\nb\n\n# Another section ----\nc\")"
44
---
55
[
66
FoldingRange {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/ark/src/lsp/folding_range.rs
3-
expression: "test_folding_range(\"\n# #region Important code\na\nb\nc\n# #endregion\n\n# #region Another section\nd\n# #endregion\")"
3+
expression: "test_folding_range(\"\n#region Important code\na\nb\nc\n#endregion\n\n#region Another section\nd\n#endregion\")"
44
---
55
[
66
FoldingRange {

0 commit comments

Comments
 (0)