Skip to content

Commit fc3c9e7

Browse files
committed
Fix nested ifs folding ranges
1 parent f4a2df8 commit fc3c9e7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

crates/parsa_python_cst/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,13 @@ impl Tree {
386386

387387
pub fn folding_blocks<'x>(&'x self) -> impl Iterator<Item = (CodeIndex, CodeIndex)> {
388388
self.0.nodes().filter_map(|n| {
389-
let end = || n.last_leaf_in_subtree().previous_leaf().unwrap().end() - 1;
389+
let end = || {
390+
let mut leaf = n.last_leaf_in_subtree();
391+
while leaf.is_type(Terminal(TerminalType::Dedent)) {
392+
leaf = leaf.previous_leaf().unwrap();
393+
}
394+
leaf.end() - 1
395+
};
390396
if n.is_type(Nonterminal(match_stmt)) {
391397
return Some((n.nth_child(3).start(), end()));
392398
}

crates/zuban_python/tests/mypylike/tests/folding_ranges.test

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def f(something, something_else) -> None:
103103

104104
[out]
105105
__main__.py:2: Folding Ranges:
106-
- 3:41-18:0 Region
106+
- 3:41-17:17 Region
107107
- 5:8-6:11 Region
108108
- 8:26-11:11 Region
109109
- 12:31-13:11 Region
@@ -127,8 +127,8 @@ def f(something) -> None:
127127
[out]
128128
__main__.py:2: Folding Ranges:
129129
- 1:0-2:13 Imports
130-
- 4:25-13:8 Region
131-
- 5:20-13:8 Region
130+
- 4:25-12:21 Region
131+
- 5:20-12:21 Region
132132
- 6:27-7:15 Region
133133
- 8:20-9:15 Region
134134
- 11:17-12:21 Region
@@ -156,7 +156,7 @@ async def bar():
156156

157157
[out]
158158
__main__.py:2: Folding Ranges:
159-
- 2:25-16:0 Region
159+
- 2:25-15:16 Region
160160
- 4:29-5:11 Region
161161
- 6:29-7:11 Region
162162
- 8:9-9:24 Region
@@ -179,3 +179,18 @@ x = 1
179179
[out]
180180
__main__.py:2: Folding Ranges:
181181
- 6:20-10:7 Region
182+
183+
[case if_nested_folding_range]
184+
#? folding-ranges
185+
def f() -> None:
186+
if bool():
187+
if bool():
188+
...
189+
else:
190+
...
191+
[out]
192+
__main__.py:2: Folding Ranges:
193+
- 2:16-7:11 Region
194+
- 3:14-5:15 Region
195+
- 4:18-5:15 Region
196+
- 6:9-7:11 Region

0 commit comments

Comments
 (0)