Skip to content

Commit 38effc6

Browse files
committed
handle new lines as well as tabs
1 parent 66e71fa commit 38effc6

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
lines changed

crates/language/src/buffer.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,7 @@ impl BufferSnapshot {
28832883
while let Some(mat) = matches.peek() {
28842884
let mut start: Option<Point> = None;
28852885
let mut end: Option<Point> = None;
2886+
let mut outdent: Option<Point> = None;
28862887

28872888
let config = &indent_configs[mat.grammar_index];
28882889
for capture in mat.captures {
@@ -2894,11 +2895,19 @@ impl BufferSnapshot {
28942895
} else if Some(capture.index) == config.end_capture_ix {
28952896
end = Some(Point::from_ts_point(capture.node.start_position()));
28962897
} else if Some(capture.index) == config.outdent_capture_ix {
2897-
outdent_positions.push(Point::from_ts_point(capture.node.start_position()));
2898+
let point = Point::from_ts_point(capture.node.start_position());
2899+
outdent.get_or_insert(point);
2900+
outdent_positions.push(point);
28982901
}
28992902
}
29002903

29012904
matches.advance();
2905+
// in case of significant indentation expand end to outdent position
2906+
let end = if significant_indentation {
2907+
outdent.or(end)
2908+
} else {
2909+
end
2910+
};
29022911
if let Some((start, end)) = start.zip(end) {
29032912
if start.row == end.row && !significant_indentation {
29042913
continue;
@@ -2938,16 +2947,20 @@ impl BufferSnapshot {
29382947
matches.advance();
29392948
}
29402949

2941-
outdent_positions.sort();
2942-
for outdent_position in outdent_positions {
2943-
// find the innermost indent range containing this outdent_position
2944-
// set its end to the outdent position
2945-
if let Some(range_to_truncate) = indent_ranges
2946-
.iter_mut()
2947-
.filter(|indent_range| indent_range.contains(&outdent_position))
2948-
.next_back()
2949-
{
2950-
range_to_truncate.end = outdent_position;
2950+
// we don't use outdent positions to truncate in case of significant indentation
2951+
// rather we use them to expand (handled above)
2952+
if !significant_indentation {
2953+
outdent_positions.sort();
2954+
for outdent_position in outdent_positions {
2955+
// find the innermost indent range containing this outdent_position
2956+
// set its end to the outdent position
2957+
if let Some(range_to_truncate) = indent_ranges
2958+
.iter_mut()
2959+
.filter(|indent_range| indent_range.contains(&outdent_position))
2960+
.next_back()
2961+
{
2962+
range_to_truncate.end = outdent_position;
2963+
}
29512964
}
29522965
}
29532966

@@ -3011,23 +3024,17 @@ impl BufferSnapshot {
30113024
}
30123025

30133026
for range in &indent_ranges {
3014-
if significant_indentation {
3015-
if range.start.row > row {
3016-
break;
3017-
}
3018-
if range.start == row_start {
3019-
indent_from_prev_row = true;
3020-
}
3021-
if range.end > prev_row_start && range.end <= row_start {
3022-
outdent_to_row = outdent_to_row.min(range.start.row.saturating_sub(1));
3023-
}
3024-
} else {
3025-
if range.start.row >= row {
3026-
break;
3027-
}
3028-
if range.start.row == prev_row && range.end > row_start {
3029-
indent_from_prev_row = true;
3030-
}
3027+
if range.start.row >= row {
3028+
break;
3029+
}
3030+
if range.start.row == prev_row && range.end > row_start {
3031+
indent_from_prev_row = true;
3032+
}
3033+
if significant_indentation && self.is_line_blank(row) && range.start.row == prev_row
3034+
{
3035+
indent_from_prev_row = true;
3036+
}
3037+
if !significant_indentation || !self.is_line_blank(row) {
30313038
if range.end > prev_row_start && range.end <= row_start {
30323039
outdent_to_row = outdent_to_row.min(range.start.row);
30333040
}
@@ -3038,8 +3045,8 @@ impl BufferSnapshot {
30383045
.iter()
30393046
.any(|e| e.start.row < row && e.end > row_start);
30403047

3041-
let suggestion = if !significant_indentation
3042-
&& (outdent_to_row == prev_row || (outdent_from_prev_row && indent_from_prev_row))
3048+
let suggestion = if outdent_to_row == prev_row
3049+
|| (outdent_from_prev_row && indent_from_prev_row)
30433050
{
30443051
Some(IndentSuggestion {
30453052
basis_row: prev_row,
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
(_ "[" "]" @end) @indent
2-
(_ "{" "}" @end) @indent
3-
(_ "(" ")" @end) @indent
1+
(function_definition
2+
":" @start
3+
(block) @indent
4+
)
45

5-
(function_definition (block) @indent)
6-
(if_statement (block) @indent)
7-
(else_clause (block) @indent)
8-
(elif_clause (block) @indent)
6+
(if_statement
7+
":" @start
8+
consequence: (block) @indent
9+
alternative: (_)? @outdent
10+
)
11+
12+
(else_clause
13+
":" @start
14+
(block) @indent
15+
)
16+
17+
(elif_clause
18+
":" @start
19+
(block) @indent
20+
)

0 commit comments

Comments
 (0)