1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
//! Auto-indentation.
//!
//! # Architecture
//!
//! For files with a tree-sitter grammar, the language's `indents.scm` is the
//! source of truth. Captures are interpreted via `QueryCursor`/`Node` APIs
//! (no ad-hoc parsing of the query file's literal strings).
//!
//! When tree-sitter cannot decide (typically because the parsed window
//! contains incomplete syntax — e.g. the user has typed `{` but not the
//! matching `}`), the fallback depends on the language family:
//!
//! - **Keyword-delimited languages** (Lua, Ruby, Bash, Pascal): copy the
//! current line's indent. Layering byte heuristics on top would mis-indent
//! them — `(` opens a function call, not a block. See `calculate_indent`.
//! - **C-family languages** (Rust, JS/TS, C/C++, Java, Go, Python, JSON,
//! HTML, CSS, …): consult [`IndentCalculator::calculate_indent_pattern`] as
//! a pragmatic last-resort heuristic, since its `{`/`[`/`(`/`:` triggers
//! line up with those languages' block openers.
//!
//! For files **without** any tree-sitter grammar (`.txt`, `.ini`, `Dockerfile`,
//! `Makefile`, …), [`IndentCalculator::calculate_indent_no_language`] uses
//! the C-family pattern heuristic directly — without an AST there is nothing
//! better to do.
//!
//! # Performance
//! - Parses up to 2000 bytes before cursor (balances accuracy vs speed).
//! - Pattern matching is O(n) where n = lines scanned (typically < 100).
//! - Tree-sitter queries cached per-language.
//!
//! # Query Captures
//! - `@indent`: Increase indent after this node (e.g., `block`).
//! - `@dedent`: Decrease indent for this node (e.g., closing `}`, `end`,
//! `fi`, `done`).
//!
//! # History
//! Issue #1425 generalised auto-indent's leading-whitespace handling. PR #1819
//! revealed that the previous "tree-sitter then unconditional C-family pattern
//! matching" pipeline cross-contaminated keyword-delimited languages. This
//! module's current shape — tree-sitter as the source of truth, pattern
//! fallback gated by language family — is the resolution.
use crate::model::buffer::Buffer;
use crate::primitives::highlighter::Language;
use fresh_languages::tree_sitter::{Parser, Query, QueryCursor, StreamingIterator};
use std::collections::HashMap;
/// Maximum bytes to parse before cursor for indent calculation
const MAX_PARSE_BYTES: usize = 2000;
/// Indent calculator using tree-sitter queries
pub struct IndentCalculator {
/// Map of language to (parser, query)
configs: HashMap<&'static str, (Parser, Query)>,
}
impl IndentCalculator {
/// Create a new indent calculator
pub fn new() -> Self {
Self {
configs: HashMap::new(),
}
}
/// Get or create parser and query for a language
fn get_config(&mut self, language: &Language) -> Option<(&mut Parser, &Query)> {
let (lang_name, ts_language, query_str) = match language {
Language::Rust => (
"rust",
fresh_languages::tree_sitter_rust::LANGUAGE.into(),
include_str!("../../queries/rust/indents.scm"),
),
Language::Python => (
"python",
fresh_languages::tree_sitter_python::LANGUAGE.into(),
include_str!("../../queries/python/indents.scm"),
),
Language::JavaScript => (
"javascript",
fresh_languages::tree_sitter_javascript::LANGUAGE.into(),
include_str!("../../queries/javascript/indents.scm"),
),
Language::TypeScript => (
"typescript",
fresh_languages::tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
include_str!("../../queries/typescript/indents.scm"),
),
Language::C => (
"c",
fresh_languages::tree_sitter_c::LANGUAGE.into(),
include_str!("../../queries/c/indents.scm"),
),
Language::Cpp => (
"cpp",
fresh_languages::tree_sitter_cpp::LANGUAGE.into(),
include_str!("../../queries/cpp/indents.scm"),
),
Language::Go => (
"go",
fresh_languages::tree_sitter_go::LANGUAGE.into(),
include_str!("../../queries/go/indents.scm"),
),
Language::Java => (
"java",
fresh_languages::tree_sitter_java::LANGUAGE.into(),
include_str!("../../queries/java/indents.scm"),
),
Language::HTML => (
"html",
fresh_languages::tree_sitter_html::LANGUAGE.into(),
include_str!("../../queries/html/indents.scm"),
),
Language::CSS => (
"css",
fresh_languages::tree_sitter_css::LANGUAGE.into(),
include_str!("../../queries/css/indents.scm"),
),
Language::Bash => (
"bash",
fresh_languages::tree_sitter_bash::LANGUAGE.into(),
include_str!("../../queries/bash/indents.scm"),
),
Language::Json => (
"json",
fresh_languages::tree_sitter_json::LANGUAGE.into(),
include_str!("../../queries/json/indents.scm"),
),
Language::Jsonc => (
"jsonc",
fresh_languages::tree_sitter_json::LANGUAGE.into(),
include_str!("../../queries/json/indents.scm"),
),
Language::Ruby => (
"ruby",
fresh_languages::tree_sitter_ruby::LANGUAGE.into(),
include_str!("../../queries/ruby/indents.scm"),
),
Language::Php => (
"php",
fresh_languages::tree_sitter_php::LANGUAGE_PHP.into(),
include_str!("../../queries/php/indents.scm"),
),
Language::Lua => (
"lua",
fresh_languages::tree_sitter_lua::LANGUAGE.into(),
include_str!("../../queries/lua/indents.scm"),
),
Language::CSharp => (
"csharp",
fresh_languages::tree_sitter_c_sharp::LANGUAGE.into(),
include_str!("../../queries/csharp/indents.scm"),
),
Language::Pascal => (
"pascal",
fresh_languages::tree_sitter_pascal::LANGUAGE.into(),
include_str!("../../queries/pascal/indents.scm"),
),
Language::Odin => (
"odin",
fresh_languages::tree_sitter_odin::LANGUAGE.into(),
include_str!("../../queries/odin/indents.scm"),
),
};
// Check if we already have this config
if !self.configs.contains_key(lang_name) {
// Create parser
let mut parser = Parser::new();
if parser.set_language(&ts_language).is_err() {
tracing::error!("Failed to set language for {}", lang_name);
return None;
}
// Create query
let query = match Query::new(&ts_language, query_str) {
Ok(q) => q,
Err(e) => {
tracing::error!("Failed to create query for {}: {:?}", lang_name, e);
return None;
}
};
self.configs.insert(lang_name, (parser, query));
}
// Return mutable references
let (parser, query) = self.configs.get_mut(lang_name)?;
Some((parser, query))
}
/// Calculate indent for a new line at the given position
///
/// Returns the number of spaces to indent, or None if auto-indent should be disabled.
///
/// # Fallback policy by language family
///
/// When `language` has tree-sitter support, tree-sitter (via the language's
/// `indents.scm`) is the source of truth. If tree-sitter cannot decide
/// (e.g. it returns `None` because the parsed window is incomplete), the
/// fallback depends on whether the language uses keyword-delimited blocks
/// (see [`uses_keyword_delimited_blocks`]):
///
/// - **Keyword-delimited (Lua, Ruby, Bash, Pascal):** copy the current
/// line's indent and stop. The C-family byte heuristics in
/// [`calculate_indent_pattern`] would mis-indent these languages —
/// their `(` opens a function call, not a block, and their blocks are
/// opened by words (`function`, `def`, `do`, `then`, `begin`) that the
/// pattern matcher cannot recognise. See issue #1425 and PR #1819.
/// - **C-family (Rust, JavaScript, TypeScript, C, C++, Java, Go, Python,
/// JSON, HTML, CSS, …):** consult [`calculate_indent_pattern`] as a
/// pragmatic last-resort heuristic. This keeps "user typed `{` and
/// pressed Enter before completing the closing `}`" working in the
/// common case where tree-sitter cannot parse the half-written buffer.
pub fn calculate_indent(
&mut self,
buffer: &Buffer,
position: usize,
language: &Language,
tab_size: usize,
) -> Option<usize> {
// When the cursor is inside (or at the boundary of) an existing
// non-empty line's leading whitespace, the auto-indent must equal
// the cursor's column so that pressing Enter does not displace the
// existing content. See #1425.
if let Some(indent) = Self::indent_for_cursor_in_leading_ws(buffer, position, tab_size) {
return Some(indent);
}
// Try tree-sitter-based indent
if let Some(indent) =
self.calculate_indent_tree_sitter(buffer, position, language, tab_size)
{
return Some(indent);
}
// Tree-sitter could not decide. For keyword-delimited languages, copy
// the current line's indent and stop — running `calculate_indent_pattern`
// would mis-indent them (its `(`/`:` triggers don't match those
// languages' grammars). For C-family languages, fall back to pattern
// matching as a pragmatic last resort for the common
// "buffer is mid-edit" case where tree-sitter has no useful structure.
if Self::uses_keyword_delimited_blocks(language) {
return Some(Self::get_current_line_indent(buffer, position, tab_size));
}
if let Some(indent) = Self::calculate_indent_pattern(buffer, position, tab_size) {
return Some(indent);
}
Some(Self::get_current_line_indent(buffer, position, tab_size))
}
/// Whether the language opens blocks with keywords (e.g. `function … end`,
/// `def … end`, `if … fi`, `begin … end`) rather than C-style braces.
///
/// Used by [`calculate_indent`] to decide whether the C-family byte
/// heuristics in [`calculate_indent_pattern`] are a safe last-resort
/// fallback when tree-sitter cannot decide. For keyword-delimited
/// languages, those heuristics produce wrong answers (notably treating
/// `(` as an indent trigger when it opens a function call rather than a
/// block) and must not be consulted.
fn uses_keyword_delimited_blocks(language: &Language) -> bool {
matches!(
language,
Language::Lua | Language::Ruby | Language::Bash | Language::Pascal
)
}
/// Calculate indent without language/tree-sitter support
/// Uses pattern matching and current line copying as fallback
/// This is used for files without syntax highlighting (e.g., .txt files)
pub fn calculate_indent_no_language(
buffer: &Buffer,
position: usize,
tab_size: usize,
) -> usize {
// See `calculate_indent` for the rationale (#1425).
if let Some(indent) = Self::indent_for_cursor_in_leading_ws(buffer, position, tab_size) {
return indent;
}
// Pattern-based indent (for incomplete syntax)
if let Some(indent) = Self::calculate_indent_pattern(buffer, position, tab_size) {
return indent;
}
// Final fallback: copy current line's indent
Self::get_current_line_indent(buffer, position, tab_size)
}
/// If `position` is inside (or at the boundary of) the leading whitespace
/// of a line that has non-whitespace content, return the cursor's column
/// measured in indent units.
///
/// At such a position, pressing Enter splits the line before (or in the
/// middle of) the existing leading whitespace. To preserve the existing
/// content's column on the new line below, the auto-indent inserted
/// between the new `\n` and the remainder of the line must equal the
/// cursor's column. Concretely:
///
/// - cursor at col 0 of `unindented line` → 0 (no displacement)
/// - cursor at col 0 of ` indented_target` → 0 (the 4 spaces ride
/// over with the content untouched)
/// - cursor at col 2 of ` indented_target` → 2 (line A keeps 2
/// spaces; 2 more spaces remain in front of `indented_target`)
/// - cursor at col 4 of ` foo()` (just before `f`) → 4
///
/// The rule is language-agnostic — it does not look at what character
/// starts the content (word, `}`, `end`, `</tag>`, `fi`, …) — and it
/// matches the behaviour of VS Code, Sublime Text, and similar editors.
/// Returns `None` when the cursor is past any non-whitespace character on
/// the line, or when the line has no content (empty / whitespace-only);
/// in those cases the regular smart-indent logic takes over.
fn indent_for_cursor_in_leading_ws(
buffer: &Buffer,
position: usize,
tab_size: usize,
) -> Option<usize> {
// Find start of the current line.
let mut line_start = position;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Verify everything from line_start to position is whitespace and
// accumulate the cursor's column in indent units.
let mut col = 0;
let mut pos = line_start;
while pos < position {
match Self::byte_at(buffer, pos) {
Some(b' ') => col += 1,
Some(b'\t') => col += tab_size,
Some(b'\r') => {}
_ => return None, // cursor is past content on this line
}
pos += 1;
}
// Require at least one non-whitespace character at or after the
// cursor; otherwise this is a blank/whitespace-only line and the
// existing logic already handles it correctly.
let mut pos = position;
while pos < buffer.len() {
match Self::byte_at(buffer, pos) {
Some(b'\n') => return None,
Some(b' ') | Some(b'\t') | Some(b'\r') => pos += 1,
Some(_) => return Some(col),
None => return None,
}
}
None
}
/// Calculate the correct indent for a closing delimiter being typed.
///
/// # C-family limitation
///
/// The pattern-matching fallback used here ([`calculate_dedent_pattern`])
/// only understands C-family bracket nesting (`{}`, `[]`, `()`). It does
/// **not** know about keyword-delimited blocks like Lua `function … end`,
/// Ruby `def … end`, or Bash `if … fi`. Generalising the dedent algorithm
/// to those languages requires tracking opener/closer pairs that are words
/// rather than single characters — a separate, larger change. For now the
/// public callers (auto-dedent on typing a closing delimiter) only fire on
/// C-family delimiters anyway, so the limitation is contained.
///
/// # Strategy: Tree-sitter with Pattern Fallback
///
/// This function attempts to use tree-sitter first, but falls back to pattern matching
/// when the syntax is incomplete (which is the common case during typing).
///
/// ## Tree-sitter Path
/// 1. Parse buffer content before cursor (up to 2000 bytes)
/// 2. Count @indent nodes at cursor position vs reference line
/// 3. Calculate dedent based on nesting level difference
/// 4. **Problem**: Fails when syntax is incomplete (e.g., missing closing brace)
///
/// ## Pattern Matching Fallback (see calculate_dedent_pattern)
/// 1. Scan backwards line by line
/// 2. Track nesting depth (closing delimiters increment, opening decrement)
/// 3. Find first unmatched opening delimiter
/// 4. Dedent to its indentation level
///
/// # Example
/// ```text
/// if (1) {
/// if (2) {
/// hi
/// } // inner closing at depth 1
/// more
/// <cursor typing }> // should dedent to column 0, not 4
/// ```
///
/// Pattern matching correctly skips the matched inner block and finds the outer `if (1) {`.
pub fn calculate_dedent_for_delimiter(
&mut self,
buffer: &Buffer,
position: usize,
_delimiter: char,
language: &Language,
tab_size: usize,
) -> Option<usize> {
// Get parser and query for this language
let (parser, query) = self.get_config(language)?;
// Extract context before cursor (for parsing)
let parse_start = position.saturating_sub(MAX_PARSE_BYTES);
let parse_range = parse_start..position;
if parse_range.is_empty() {
return Some(0);
}
let source = buffer.slice_bytes(parse_range.clone());
// Parse the source
let tree = parser.parse(&source, None)?;
let root = tree.root_node();
// Find capture index for @indent
let mut indent_capture_idx = None;
for (i, name) in query.capture_names().iter().enumerate() {
if *name == "indent" {
indent_capture_idx = Some(i);
break;
}
}
let indent_capture_idx = indent_capture_idx?;
let cursor_offset = position - parse_start;
// Hybrid heuristic: find previous non-empty line as reference
// This is the same approach used in calculate_indent_tree_sitter
let (reference_line_indent, reference_line_offset) = {
let mut search_pos = position;
let mut reference_indent = 0;
let mut reference_offset = cursor_offset;
// Scan backwards through the buffer to find a non-empty line
while search_pos > 0 {
// Find start of current line
let mut line_start = search_pos;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Check if this line has non-whitespace content
let mut has_content = false;
let mut line_indent = 0;
let mut content_pos = line_start;
let mut pos = line_start;
while pos < search_pos {
match Self::byte_at(buffer, pos) {
Some(b' ') => line_indent += 1,
Some(b'\t') => line_indent += tab_size,
Some(b'\n') => break,
Some(_) => {
has_content = true;
content_pos = pos; // Remember where we found content
break;
}
None => break,
}
pos += 1;
}
if has_content {
// Found a non-empty line, use it as reference
reference_indent = line_indent;
// Use position of first non-whitespace character as reference
if content_pos >= parse_start {
reference_offset = content_pos - parse_start;
} else {
// Reference line is before parse window - use start of parse window
reference_offset = 0;
}
break;
}
// Move to previous line
if line_start == 0 {
break;
}
search_pos = line_start.saturating_sub(1);
}
(reference_indent, reference_offset)
};
// Count @indent nodes at reference and cursor positions
let mut reference_indent_count: i32 = 0;
let mut cursor_indent_count: i32 = 0;
let mut query_cursor = QueryCursor::new();
let mut captures = query_cursor.captures(query, root, source.as_slice());
while let Some((match_result, _)) = captures.next() {
for capture in match_result.captures {
if capture.index == indent_capture_idx as u32 {
let node = capture.node;
let node_start = node.start_byte();
let node_end = node.end_byte();
// Count @indent nodes at reference position
if node_start < reference_line_offset && reference_line_offset <= node_end {
reference_indent_count += 1;
}
// Count @indent nodes at cursor position
if node_start < cursor_offset && cursor_offset <= node_end {
cursor_indent_count += 1;
}
}
}
}
// Tree-sitter fallback: incomplete syntax produces ERROR nodes with no structure
// This is the common case when typing (e.g., "if (true) {\n hi\n " is incomplete)
// Pattern matching handles this gracefully by tracking delimiter nesting
if cursor_indent_count == 0 && reference_indent_count == 0 {
tracing::debug!("No @indent nodes found (incomplete syntax), using pattern fallback");
return Self::calculate_dedent_pattern(buffer, position, tab_size);
}
// Tree-sitter path: Calculate relative indent based on @indent node counts
// The closing delimiter should be at one level less than current nesting
// Formula: reference_indent + (cursor_depth - reference_depth - 1) * tab_size
// The -1 accounts for the closing delimiter dedenting one level
let indent_delta = cursor_indent_count - reference_indent_count - 1;
let final_indent =
(reference_line_indent as i32 + (indent_delta * tab_size as i32)).max(0) as usize;
tracing::debug!(
"Tree-sitter dedent: reference_indent={}, cursor_depth={}, reference_depth={}, delta={}, final_indent={}",
reference_line_indent,
cursor_indent_count,
reference_indent_count,
indent_delta,
final_indent
);
Some(final_indent)
}
/// Calculate dedent using pattern matching (fallback for incomplete syntax)
///
/// This is the **primary dedent algorithm** used during typing, since tree-sitter
/// cannot handle incomplete syntax.
///
/// # Algorithm: Nesting Depth Tracking
///
/// Scans backwards line by line, tracking nesting depth to skip over already-matched
/// delimiter pairs. This ensures we find the **matching** opening delimiter, not just
/// any opening delimiter.
///
/// ## Depth Counter Logic
/// - **Closing delimiter** (`}`, `]`, `)`) → increment depth
/// - Reason: We need to skip its matching opening delimiter
/// - **Opening delimiter** (`{`, `[`, `(`) → check depth:
/// - If depth > 0: decrement and continue (this open is matched)
/// - If depth == 0: **found it!** This is the unmatched opening we're looking for
///
/// ## Example Walkthrough
/// ```text
/// if (1) { // ← target: we want to find this
/// if (2) {
/// hi
/// } // matched pair
/// more
/// <cursor> // typing } here
/// ```
///
/// Search backwards:
/// 1. Line " more" → not a delimiter, continue
/// 2. Line " }" → closing delimiter, depth = 1 (skip next opening)
/// 3. Line " hi" → not a delimiter, continue
/// 4. Line " if (2) {" → opening delimiter, but depth = 1, so decrement to 0, continue
/// 5. Line "if (1) {" → opening delimiter, depth = 0, **match found!** Return indent = 0
///
/// # Language Agnostic
/// Works for any language using C-style delimiters: { } [ ] ( )
fn calculate_dedent_pattern(
buffer: &Buffer,
position: usize,
tab_size: usize,
) -> Option<usize> {
let mut depth = 0;
let mut search_pos = position;
while search_pos > 0 {
// Find start of line
let mut line_start = search_pos;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Get line content
let line_bytes = buffer.slice_bytes(line_start..search_pos + 1);
let last_non_ws = line_bytes
.iter()
.rev()
.find(|&&b| b != b' ' && b != b'\t' && b != b'\r' && b != b'\n');
if let Some(&last_char) = last_non_ws {
// Calculate this line's indentation (count leading spaces/tabs)
let mut line_indent = 0;
let mut pos = line_start;
while pos < search_pos {
match Self::byte_at(buffer, pos) {
Some(b' ') => line_indent += 1,
Some(b'\t') => line_indent += tab_size,
Some(b'\n') => break,
Some(_) => break, // Hit non-whitespace
None => break,
}
pos += 1;
}
// Apply nesting depth tracking based on last character
match last_char {
// Closing delimiter: increment depth to skip its matching opening
b'}' | b']' | b')' => {
depth += 1;
tracing::debug!(
"Pattern dedent: found closing '{}', depth now {}",
last_char as char,
depth
);
}
// Opening delimiter: check if it's matched or unmatched
b'{' | b'[' | b'(' => {
if depth > 0 {
// Already matched by a closing delimiter we saw earlier
depth -= 1;
tracing::debug!(
"Pattern dedent: skipping matched '{}' (depth {}→{})",
last_char as char,
depth + 1,
depth
);
} else {
// Unmatched! This is the opening delimiter we're closing
tracing::debug!(
"Pattern dedent: found unmatched '{}' at indent {}",
last_char as char,
line_indent
);
return Some(line_indent);
}
}
// Content line: continue searching
_ => {
tracing::debug!(
"Pattern dedent: line ends with '{}', continuing",
last_char as char
);
}
}
}
// Move to previous line
if line_start == 0 {
break;
}
search_pos = line_start.saturating_sub(1);
}
// No matching opening delimiter found - dedent to column 0
Some(0)
}
/// Calculate indent using simple byte-level pattern matching.
///
/// # Scope
///
/// This function is the heuristic used:
///
/// 1. By [`calculate_indent_no_language`] — files without a tree-sitter
/// grammar (`.txt`, `.ini`, `Dockerfile`, `Makefile`, …). Without an
/// AST there is nothing better to do.
/// 2. By [`calculate_indent`] **only** for tree-sitter-backed C-family
/// languages (Rust, JS/TS, C/C++, Java, Go, Python, JSON, HTML, CSS,
/// PHP, C#, Odin) when tree-sitter cannot decide — typically because
/// the parsed window contains incomplete syntax (e.g. user just typed
/// `{` and has not yet typed the matching `}`).
///
/// It is **not** consulted for keyword-delimited languages (Lua, Ruby,
/// Bash, Pascal). For those, layering this byte heuristic on top of
/// tree-sitter would produce wrong answers — most notably treating `(` as
/// an indent trigger when in those languages `(` opens a function call,
/// not a block.
///
/// # C-family bias (intentional)
///
/// The triggers `{`, `[`, `(`, `:` are baked in. Without a grammar there
/// is nothing better to do — these are the most common delimiters across
/// programming and structured-text formats. They line up with C-family
/// languages' own block openers, which is why we still use this as a
/// last-resort fallback for those. They are wrong for keyword-delimited
/// languages, which is why [`calculate_indent`] suppresses this path for
/// Lua/Ruby/Bash/Pascal. See issue #1425 and PR #1819.
fn calculate_indent_pattern(
buffer: &Buffer,
position: usize,
tab_size: usize,
) -> Option<usize> {
if position == 0 {
return None;
}
// Find start of the line we're currently on (before pressing Enter)
let mut line_start = position;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Get the content of the current line (the one we're leaving)
let line_bytes = buffer.slice_bytes(line_start..position);
// Find the last non-whitespace character on current line
let last_non_whitespace = line_bytes
.iter()
.rev()
.find(|&&b| b != b' ' && b != b'\t' && b != b'\r');
// Check if current line is empty (only whitespace)
let current_line_is_empty = last_non_whitespace.is_none();
// Hybrid heuristic: find previous non-empty line for reference
let reference_indent = if !current_line_is_empty {
// Current line has content - use its indent as reference
Self::get_current_line_indent(buffer, position, tab_size)
} else {
// Current line is empty - find previous non-empty line and check for indent triggers
let mut search_pos = if line_start > 0 {
line_start - 1 // Position of \n before current line
} else {
0
};
let mut found_reference_indent = 0;
while search_pos > 0 {
// Find start of line
let mut ref_line_start = search_pos;
while ref_line_start > 0 {
if Self::byte_at(buffer, ref_line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
ref_line_start = ref_line_start.saturating_sub(1);
}
// Check if this line has non-whitespace content
let ref_line_bytes = buffer.slice_bytes(ref_line_start..search_pos + 1);
let ref_last_non_ws = ref_line_bytes
.iter()
.rev()
.find(|&&b| b != b' ' && b != b'\t' && b != b'\r' && b != b'\n');
if ref_last_non_ws.is_some() {
// Found a non-empty reference line - calculate its indent
let mut line_indent = 0;
let mut pos = ref_line_start;
while pos <= search_pos {
let byte_opt = Self::byte_at(buffer, pos);
match byte_opt {
Some(b' ') => line_indent += 1,
Some(b'\t') => line_indent += tab_size,
Some(b'\n') => break,
Some(_) => break, // Hit non-whitespace, done counting indent
None => break,
}
pos += 1;
}
found_reference_indent = line_indent;
// Check if reference line ends with indent trigger
if let Some(&last_char) = ref_last_non_ws {
match last_char {
b'{' | b'[' | b'(' => {
tracing::debug!(
"Pattern match: reference line ends with '{}'",
last_char as char
);
return Some(found_reference_indent + tab_size);
}
b':' => {
tracing::debug!("Pattern match: reference line ends with colon");
return Some(found_reference_indent + tab_size);
}
_ => {}
}
}
break;
}
// Move to previous line
if ref_line_start == 0 {
break;
}
search_pos = ref_line_start.saturating_sub(1);
}
// Return the reference indent we found (or 0 if no non-empty line was found)
found_reference_indent
};
// If current line ends with indent trigger, add to reference
if let Some(&last_char) = last_non_whitespace {
tracing::debug!("Pattern match: last char = '{}'", last_char as char);
match last_char {
b'{' | b'[' | b'(' => {
// Opening braces/brackets/parens: increase indent
tracing::debug!("Pattern match: found opening brace/bracket at end of line");
return Some(reference_indent + tab_size);
}
b':' => {
// Colon (for Python, YAML, etc.): increase indent
tracing::debug!("Pattern match: found colon at end of line");
return Some(reference_indent + tab_size);
}
_ => {
tracing::debug!("Pattern match: no indent trigger found");
}
}
}
// Current line is empty and has no indent trigger - use reference indent
Some(reference_indent)
}
/// Calculate indent using tree-sitter queries
fn calculate_indent_tree_sitter(
&mut self,
buffer: &Buffer,
position: usize,
language: &Language,
tab_size: usize,
) -> Option<usize> {
// Get parser and query
let (parser, query) = self.get_config(language)?;
// Extract context before cursor (for parsing)
let parse_start = position.saturating_sub(MAX_PARSE_BYTES);
let parse_range = parse_start..position;
if parse_range.is_empty() {
return None;
}
let source = buffer.slice_bytes(parse_range.clone());
// Parse the source
let tree = parser.parse(&source, None)?;
let root = tree.root_node();
// Find capture indices for @indent and @dedent
let mut indent_capture_idx = None;
let mut dedent_capture_idx = None;
for (i, name) in query.capture_names().iter().enumerate() {
if *name == "indent" {
indent_capture_idx = Some(i);
} else if *name == "dedent" {
dedent_capture_idx = Some(i);
}
}
// Query for indent/dedent captures
let mut query_cursor = QueryCursor::new();
// Count indent/dedent at cursor position
// The cursor position in the parsed text is (position - parse_start)
let cursor_offset = position - parse_start;
let mut indent_delta = 0i32;
let mut found_any_captures = false;
// Find the line start to get the base column offset
let mut line_start_offset = cursor_offset;
while line_start_offset > 0 {
if source.get(line_start_offset.saturating_sub(1)) == Some(&b'\n') {
break;
}
line_start_offset = line_start_offset.saturating_sub(1);
}
// Find the previous non-empty line in the buffer to use as reference
// This is the "hybrid heuristic" approach: calculate indent delta relative to actual code
let (reference_line_indent, reference_line_offset) = {
let mut search_pos = position;
let mut reference_indent = 0;
let mut reference_offset = cursor_offset;
// Scan backwards through the buffer to find a non-empty line
while search_pos > 0 {
// Find start of current line
let mut line_start = search_pos;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Check if this line has non-whitespace content
let mut has_content = false;
let mut line_indent = 0;
let mut content_pos = line_start;
let mut pos = line_start;
while pos < search_pos {
match Self::byte_at(buffer, pos) {
Some(b' ') => line_indent += 1,
Some(b'\t') => line_indent += tab_size,
Some(b'\n') => break,
Some(_) => {
has_content = true;
content_pos = pos; // Remember where we found content
break;
}
None => break,
}
pos += 1;
}
if has_content {
// Found a non-empty line, use it as reference
reference_indent = line_indent;
// Use position of first non-whitespace character as reference
// This ensures we're measuring from inside the content, not at line boundaries
if content_pos >= parse_start {
reference_offset = content_pos - parse_start;
} else {
// Reference line is before parse window - use start of parse window
reference_offset = 0;
}
break;
}
// Move to previous line
if line_start == 0 {
break;
}
search_pos = line_start.saturating_sub(1);
}
(reference_indent, reference_offset)
};
// Locate the last non-whitespace byte on the cursor's line (used below
// to ask tree-sitter what kind of token sits at the line's end —
// structural replacement for the old "is the byte `}`?" check).
let last_nonws_offset = {
let mut pos = cursor_offset;
let mut found = None;
while pos > line_start_offset {
pos -= 1;
match source.get(pos) {
Some(b' ') | Some(b'\t') | Some(b'\r') => continue,
Some(_) => {
found = Some(pos);
break;
}
None => break,
}
}
found
};
// Calculate indent delta using hybrid heuristic:
// Count @indent nodes at reference line and at cursor, then compute the difference.
let mut reference_indent_count: i32 = 0;
let mut cursor_indent_count: i32 = 0;
// Tree-sitter analogue of the old "line ends with `{`/`:`/..." byte
// rescue: an @indent node that opens on the current line and contains
// the cursor means a block has just been opened — pressing Enter
// should go one level deeper.
let mut indent_opens_on_cursor_line = false;
// Tree-sitter analogue of the old "line ends with `}`" byte rescue:
// if the last token on the cursor's line is itself captured as @dedent
// (any closing delimiter the language declares — `}`, `end`, `fi`,
// `done`, `until`, `</tag>`, …), the new line should match the
// line's existing indent rather than re-counting nesting (which
// produces asymmetric results when the `@dedent` token sits on the
// boundary of an `@indent` node).
let mut last_nonws_is_dedent_capture = false;
// Manually iterate through matches to count indent/dedent captures
let mut captures = query_cursor.captures(query, root, source.as_slice());
while let Some((match_result, _)) = captures.next() {
for capture in match_result.captures {
let node = capture.node;
let node_start = node.start_byte();
let node_end = node.end_byte();
// Count @indent nodes at reference position
if let Some(idx) = indent_capture_idx {
if capture.index == idx as u32 {
// Reference line: count if reference position is inside this node
if node_start < reference_line_offset && reference_line_offset <= node_end {
reference_indent_count += 1;
}
// Cursor position: count if cursor is inside this node
// Also check: node must start on a previous line (not current line)
let node_on_previous_line = node_start < line_start_offset;
let cursor_inside_node =
node_start < cursor_offset && cursor_offset <= node_end;
if cursor_inside_node && node_on_previous_line {
cursor_indent_count += 1;
found_any_captures = true;
}
if cursor_inside_node && !node_on_previous_line {
indent_opens_on_cursor_line = true;
}
}
}
// Handle @dedent captures
if let Some(idx) = dedent_capture_idx {
if capture.index == idx as u32 {
// Existing: dedent that begins exactly at the cursor position.
if cursor_offset == node_start && node_end > node_start {
indent_delta -= 1;
found_any_captures = true;
}
// Structural check for "line ends with closing token":
// does this @dedent capture cover the last non-ws byte
// on the cursor's line?
if let Some(last_pos) = last_nonws_offset {
if node_start <= last_pos && last_pos < node_end {
last_nonws_is_dedent_capture = true;
}
}
}
}
}
}
// When the current line ends with a token captured as @dedent (`}`,
// `end`, `fi`, `done`, …), keep the new line at the same indent as
// the closing token. The grammar has already placed that token at the
// correct column matching its opener; the next line continues at the
// enclosing scope's indent.
if last_nonws_is_dedent_capture {
let line_indent = Self::get_current_line_indent(buffer, position, tab_size);
tracing::debug!(
"Cursor line ends with @dedent token, maintaining indent level: {}",
line_indent
);
return Some(line_indent);
}
// Calculate delta: how many more @indent levels are we at cursor vs reference
indent_delta += cursor_indent_count - reference_indent_count;
// When the cursor is at the end of a line that has just opened an
// @indent block (e.g. `def foo():` in Python, `function foo()` in Lua,
// `if true; then` in Bash, `fn main() {` in Rust), the regular
// counting under-counts at the cursor because the new node's
// `node_start` is on the current line and the `node_on_previous_line`
// filter excludes it. Detect this structurally: if a `@indent` node
// opens on the cursor's line and contains the cursor, treat it as one
// additional level of nesting.
if indent_delta == 0 && indent_opens_on_cursor_line {
indent_delta = 1;
found_any_captures = true;
}
// If no captures were found, return None to trigger pattern-based fallback
if !found_any_captures {
tracing::debug!("No tree-sitter captures found, falling back to pattern matching");
return None;
}
// Calculate final indent: reference line indent + delta
let final_indent =
(reference_line_indent as i32 + (indent_delta * tab_size as i32)).max(0) as usize;
tracing::debug!(
"Indent calculation: reference={}, delta={}, final={}",
reference_line_indent,
indent_delta,
final_indent
);
Some(final_indent)
}
/// Get a single byte at a position
fn byte_at(buffer: &Buffer, pos: usize) -> Option<u8> {
if pos >= buffer.len() {
return None;
}
buffer.slice_bytes(pos..pos + 1).first().copied()
}
/// Get the indent of the current line (the line cursor is on)
fn get_current_line_indent(buffer: &Buffer, position: usize, tab_size: usize) -> usize {
// Find start of current line
let mut line_start = position;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Count leading whitespace on current line
let mut indent = 0;
let mut pos = line_start;
while pos < position {
match Self::byte_at(buffer, pos) {
Some(b' ') => indent += 1,
Some(b'\t') => indent += tab_size,
Some(_) => break, // Hit non-whitespace
None => break,
}
pos += 1;
}
indent
}
/// Get the indent of the line containing the given position
/// This is a public API used for bracket expansion
pub fn get_line_indent_at_position(buffer: &Buffer, position: usize, tab_size: usize) -> usize {
// Find start of the line containing position
let mut line_start = position;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Find end of line or buffer
let mut line_end = position;
while line_end < buffer.len() {
if Self::byte_at(buffer, line_end) == Some(b'\n') {
break;
}
line_end += 1;
}
// Count leading whitespace on the line
let mut indent = 0;
let mut pos = line_start;
while pos < line_end {
match Self::byte_at(buffer, pos) {
Some(b' ') => indent += 1,
Some(b'\t') => indent += tab_size,
Some(_) => break, // Hit non-whitespace
None => break,
}
pos += 1;
}
indent
}
/// Get the indent of the previous line (line before cursor's line)
#[cfg(test)]
fn get_previous_line_indent(buffer: &Buffer, position: usize, tab_size: usize) -> usize {
// Find start of current line
let mut line_start = position;
while line_start > 0 {
if Self::byte_at(buffer, line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
line_start = line_start.saturating_sub(1);
}
// Find start of previous line
if line_start == 0 {
return 0;
}
let mut prev_line_start = line_start - 1;
while prev_line_start > 0 {
if Self::byte_at(buffer, prev_line_start.saturating_sub(1)) == Some(b'\n') {
break;
}
prev_line_start = prev_line_start.saturating_sub(1);
}
// Count leading whitespace on previous line
let mut indent = 0;
let mut pos = prev_line_start;
while pos < line_start - 1 {
match Self::byte_at(buffer, pos) {
Some(b' ') => indent += 1,
Some(b'\t') => indent += tab_size,
Some(_) => break, // Hit non-whitespace
None => break,
}
pos += 1;
}
indent
}
}
impl Default for IndentCalculator {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::buffer::Buffer;
#[test]
fn test_current_and_previous_line_indent() {
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;");
let tab_size = 4;
// At end of buffer (end of line 2)
let current_indent =
IndentCalculator::get_current_line_indent(&buffer, buffer.len(), tab_size);
assert_eq!(current_indent, 4, "Current line (line 2) has 4 spaces");
let prev_indent =
IndentCalculator::get_previous_line_indent(&buffer, buffer.len(), tab_size);
assert_eq!(prev_indent, 0, "Previous line (line 1) has 0 spaces");
}
#[test]
fn test_pattern_matching_basic() {
let buffer = Buffer::from_str_test("fn main() {");
let position = buffer.len();
let result = IndentCalculator::calculate_indent_pattern(&buffer, position, 4);
println!("Pattern result for 'fn main() {{': {:?}", result);
assert_eq!(
result,
Some(4),
"Should detect {{ and return 4 space indent"
);
}
#[test]
fn test_rust_indent_after_brace_debug() {
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {");
let position = buffer.len(); // After the {
// Test pattern matching directly first
let pattern_result = IndentCalculator::calculate_indent_pattern(&buffer, position, 4);
println!("Pattern matching result: {:?}", pattern_result);
// This should trigger tree-sitter parsing
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
println!("Test buffer: {:?}", buffer.to_string().unwrap());
println!("Position: {}", position);
println!("Result indent: {:?}", indent);
assert!(indent.is_some(), "Should return Some indent");
let indent_val = indent.unwrap();
println!("Indent value: {}", indent_val);
// Should suggest indenting (4 spaces)
assert_eq!(
indent_val, 4,
"Should indent by 4 spaces after opening brace"
);
}
#[test]
fn test_python_indent_after_colon() {
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("def foo():");
let position = buffer.len(); // After the :
let indent = calc.calculate_indent(&buffer, position, &Language::Python, 4);
assert!(indent.is_some());
// Should suggest indenting
assert!(indent.unwrap() >= 4);
}
#[test]
fn test_tree_sitter_used_for_complete_block() {
// Test that tree-sitter is used when we have a complete block with context
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;\n}");
// Position after the closing }
let position = buffer.len();
// Tree-sitter should recognize this is a complete block
// Pattern matching would see '}' and not indent, but tree-sitter context should work
let ts_result = calc.calculate_indent_tree_sitter(&buffer, position, &Language::Rust, 4);
// Tree-sitter should return Some (even if it's 0 indent)
assert!(
ts_result.is_some(),
"Tree-sitter should handle complete blocks"
);
}
#[test]
fn test_nested_indent_maintained() {
// Test that we maintain nested indentation correctly
let mut calc = IndentCalculator::new();
// Create nested structure - position at end of line with just whitespace
let buffer = Buffer::from_str_test("fn main() {\n if true {\n ");
let position = buffer.len();
// This should be 8 spaces (maintaining nested indent from current line)
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
assert_eq!(
indent,
Some(8),
"Should maintain nested indent level (got {:?})",
indent
);
}
#[test]
fn test_pattern_fallback_for_incomplete_syntax() {
// Verify pattern matching kicks in when tree-sitter can't help
let buffer = Buffer::from_str_test("fn main() {");
let position = buffer.len();
// Pattern matching should detect the '{'
let pattern_result = IndentCalculator::calculate_indent_pattern(&buffer, position, 4);
assert_eq!(
pattern_result,
Some(4),
"Pattern matching should detect opening brace"
);
}
#[test]
fn test_fallback_to_previous_line() {
let mut calc = IndentCalculator::new();
// C# not supported, should fall back
let buffer = Buffer::from_str_test(" var x = 1;");
let position = buffer.len();
let indent = calc.calculate_indent(&buffer, position, &Language::CSharp, 4);
// Should fall back to previous line indent (4 spaces)
assert_eq!(indent, Some(4));
}
#[test]
fn test_typescript_interface_indent() {
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("interface User {");
let position = buffer.len(); // Position after the {
let indent = calc.calculate_indent(&buffer, position, &Language::TypeScript, 4);
assert!(indent.is_some(), "TypeScript interface should get indent");
assert_eq!(
indent.unwrap(),
4,
"Should indent 4 spaces after opening brace"
);
}
#[test]
fn test_no_language_fallback_copies_indent() {
// Test that files without language support (like .txt) copy current line indent
let buffer = Buffer::from_str_test(" indented text");
let position = buffer.len();
let indent = IndentCalculator::calculate_indent_no_language(&buffer, position, 4);
assert_eq!(indent, 4, "Should copy 4-space indent from current line");
}
#[test]
fn test_no_language_fallback_with_brace() {
// Test that pattern matching works for files without language support
let buffer = Buffer::from_str_test("some text {");
let position = buffer.len();
let indent = IndentCalculator::calculate_indent_no_language(&buffer, position, 4);
assert_eq!(
indent, 4,
"Should indent 4 spaces after brace even without language"
);
}
#[test]
fn test_tree_sitter_enter_after_close_brace_returns_zero() {
// Verify tree-sitter correctly handles Enter after closing brace
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;\n}");
let position = buffer.len(); // Position right after the }
// Tree-sitter should recognize we're outside the block and return 0 indent
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
assert_eq!(
indent,
Some(0),
"Should return 0 indent after closing brace"
);
// Verify tree-sitter is being used (not just pattern fallback)
let ts_result = calc.calculate_indent_tree_sitter(&buffer, position, &Language::Rust, 4);
assert!(ts_result.is_some(), "Tree-sitter should handle this case");
}
#[test]
fn test_tree_sitter_auto_dedent_on_close_brace() {
// Verify tree-sitter correctly calculates dedent for closing delimiter
let mut calc = IndentCalculator::new();
// Simulate typing } on an indented line
let buffer = Buffer::from_str_test("fn main() {\n ");
let position = buffer.len(); // Cursor after 4 spaces
// Calculate where the } should be placed using tree-sitter
let correct_indent =
calc.calculate_dedent_for_delimiter(&buffer, position, '}', &Language::Rust, 4);
// Should dedent to column 0 (same level as fn main)
assert_eq!(
correct_indent,
Some(0),
"Closing brace should dedent to column 0"
);
// Verify this uses tree-sitter by checking it works
let nested_buffer = Buffer::from_str_test("fn main() {\n if true {\n ");
let nested_pos = nested_buffer.len();
let nested_indent = calc.calculate_dedent_for_delimiter(
&nested_buffer,
nested_pos,
'}',
&Language::Rust,
4,
);
// Should return a valid indent level
assert!(
nested_indent.is_some(),
"Nested closing brace should get valid indent"
);
}
#[test]
fn test_tree_sitter_handles_multiple_languages() {
// Verify tree-sitter-based auto-dedent works across languages
let mut calc = IndentCalculator::new();
// Python
let py_buffer = Buffer::from_str_test("def foo():\n ");
let py_indent = calc.calculate_indent(&py_buffer, py_buffer.len(), &Language::Python, 4);
assert_eq!(py_indent, Some(4), "Python should indent after colon");
// JavaScript
let js_buffer = Buffer::from_str_test("function foo() {\n ");
let js_dedent = calc.calculate_dedent_for_delimiter(
&js_buffer,
js_buffer.len(),
'}',
&Language::JavaScript,
4,
);
assert_eq!(js_dedent, Some(0), "JavaScript closing brace should dedent");
// C++
let cpp_buffer = Buffer::from_str_test("class Foo {\n ");
let cpp_dedent = calc.calculate_dedent_for_delimiter(
&cpp_buffer,
cpp_buffer.len(),
'}',
&Language::Cpp,
4,
);
assert_eq!(cpp_dedent, Some(0), "C++ closing brace should dedent");
}
#[test]
fn test_indent_on_empty_line_uses_reference() {
// Hybrid heuristic: when the cursor is on a truly empty line between
// code lines, indent calculation should use the previous non-empty
// line as reference.
let mut calc = IndentCalculator::new();
// "fn main() {\n let x = 1;\n\n}" — cursor on the empty line.
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;\n\n}");
let position = 27; // start of the empty line (after the second '\n')
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
assert_eq!(
indent,
Some(4),
"On an empty line inside a function body, should indent to match the reference line"
);
}
#[test]
fn test_indent_after_empty_line_incomplete_syntax() {
// Test with incomplete syntax (no closing brace) - this is the real-world case
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;\n");
let position = buffer.len(); // After the second \n, start of empty line
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
tracing::trace!("TEST: Without closing brace, indent = {:?}", indent);
// With incomplete syntax, tree-sitter returns ERROR nodes
// We should fall back to pattern matching or reference line heuristic
assert_eq!(
indent,
Some(4),
"After empty line in function body (incomplete syntax), should indent to 4 spaces using reference line"
);
}
#[test]
fn test_enter_at_start_of_unindented_line_after_blank_does_not_indent() {
// Regression test for #1425: pressing Enter at the start of an
// unindented line that follows blank lines after indented content
// should not pull in the previous block's indent and displace the
// existing line content.
//
// ····line1
// ····line2
// ········line3
// ········line4
// <empty>
// unindented line <- cursor at column 0, press Enter
//
// Expected: the existing "unindented line" stays at column 0.
let buffer = Buffer::from_str_test(
" line1\n line2\n line3\n line4\n\nunindented line",
);
let position = buffer
.to_string()
.unwrap()
.find("unindented line")
.expect("test fixture should contain marker");
let indent = IndentCalculator::calculate_indent_no_language(&buffer, position, 4);
assert_eq!(
indent, 0,
"Enter at column 0 of an existing non-empty line must not insert indentation"
);
}
#[test]
fn test_enter_at_start_of_indented_line_does_not_displace_content() {
// Even when the existing line is itself indented, pressing Enter at
// the very start of that line should not add extra indent (which
// would push the existing leading whitespace further right).
let buffer = Buffer::from_str_test(" line1\n target");
let position = buffer
.to_string()
.unwrap()
.find(" target")
.expect("test fixture should contain marker");
let indent = IndentCalculator::calculate_indent_no_language(&buffer, position, 4);
assert_eq!(
indent, 0,
"Enter at column 0 of an indented line must not add indent on top of the existing leading whitespace"
);
}
#[test]
fn test_enter_at_start_of_unindented_line_python() {
// Same regression as #1425 but for a tree-sitter language: the
// pattern fallback or tree-sitter logic must not inject indent that
// displaces existing content on the line.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("def foo():\n pass\n\nunindented");
let position = buffer
.to_string()
.unwrap()
.find("unindented")
.expect("test fixture should contain marker");
let indent = calc.calculate_indent(&buffer, position, &Language::Python, 4);
assert_eq!(
indent,
Some(0),
"Enter at column 0 of an unindented Python line must not be auto-indented"
);
}
#[test]
fn test_enter_in_middle_of_leading_ws_preserves_content_column() {
// Cursor at column 2 of " indented_target" (in the middle of the
// 4-space leading whitespace). Pressing Enter splits the indent: 2
// spaces stay on line A, 2 spaces remain in front of `indented_target`
// on line B. Auto-indent must equal the cursor's column (2), giving
// line B a total of 2 + 2 = 4 leading spaces — preserving the
// original column of `indented_target`.
let buffer = Buffer::from_str_test(" line1\n indented_target");
let target = buffer
.to_string()
.unwrap()
.find(" indented_target")
.unwrap();
let position = target + 2; // mid-indent
let indent = IndentCalculator::calculate_indent_no_language(&buffer, position, 4);
assert_eq!(
indent, 2,
"Splitting in the middle of leading whitespace must preserve the content column"
);
}
#[test]
fn test_enter_at_start_of_closing_brace_line_does_not_displace() {
// Language-agnostic: at column 0 of a `}` line we must not insert
// indent that pushes `}` rightward. Other editors (VS Code, Sublime)
// create the empty line above and leave the `}` at column 0; the
// user can press Tab to indent if they want to type code in front
// of the close. The same rule applies uniformly to `end` (Lua,
// Ruby), `</tag>` (HTML), `fi`/`done` (Bash) — there is no
// language-specific list of closing tokens to maintain.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {\n let x = 1;\n}");
let position = 27; // start of the `}` line
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
assert_eq!(
indent,
Some(0),
"Enter at column 0 of a `}}` line must not displace the closing delimiter"
);
}
#[test]
fn test_enter_inside_content_still_uses_smart_indent() {
// Sanity check: when the cursor is past the leading whitespace, the
// generalised fix must NOT short-circuit — smart auto-indent is still
// expected to fire and indent inside an opened block.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("fn main() {");
let position = buffer.len(); // end of the opening brace line
let indent = calc.calculate_indent(&buffer, position, &Language::Rust, 4);
assert_eq!(
indent,
Some(4),
"Pressing Enter at the end of `fn main() {{` should still indent the new line"
);
}
// ============================================================================
// Cross-contamination regression tests for keyword-delimited languages.
//
// Before this change, the tree-sitter path fell through to
// `calculate_indent_pattern` when it had no captures to apply. That fallback
// hardcodes `{`, `[`, `(`, `:` as universal indent triggers, which is wrong
// for Lua, Ruby, Bash, and Pascal: in those languages `(` opens a function
// call (or subshell, or condition) — never a block.
//
// These tests pin down the corrected behaviour: for keyword-delimited
// languages, when tree-sitter cannot decide, we copy the current line's
// indent rather than asking the C-family pattern matcher. See issue #1425
// and PR #1819.
// ============================================================================
#[test]
fn test_lua_open_paren_on_function_call_does_not_trigger_indent() {
// Lua: `foo(` is a function call, not a block opener. Tree-sitter does
// not capture an @indent here. The C-family pattern fallback would
// wrongly read `(` as an indent trigger and return +tab_size; after
// this change it must not.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("foo(");
let indent = calc.calculate_indent(&buffer, buffer.len(), &Language::Lua, 4);
assert_eq!(
indent,
Some(0),
"Lua: open paren on a function call must not deepen indent"
);
}
#[test]
fn test_ruby_open_paren_on_function_call_does_not_trigger_indent() {
// Ruby analogue: `foo(` is a method call, not a block opener.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("foo(");
let indent = calc.calculate_indent(&buffer, buffer.len(), &Language::Ruby, 4);
assert_eq!(
indent,
Some(0),
"Ruby: open paren on a method call must not deepen indent"
);
}
#[test]
fn test_bash_open_paren_on_subshell_does_not_trigger_indent() {
// Bash: `result=$(` opens a subshell expansion, which Bash's
// indents.scm does not capture as @indent. The pattern fallback would
// wrongly read `(` as a trigger; after this change it must not.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("result=$(");
let indent = calc.calculate_indent(&buffer, buffer.len(), &Language::Bash, 4);
assert_eq!(
indent,
Some(0),
"Bash: open paren on a subshell must not deepen indent"
);
}
#[test]
fn test_lua_paren_in_block_does_not_cross_contaminate() {
// Lua: cursor sits one indent level inside a function body and the
// previous content line happens to end with `(` (a function call).
// The C-family pattern fallback would have wrongly added an extra
// tab_size on top of the body's indent. Tree-sitter is the source of
// truth: it should keep the new line at the body's indent.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("function bar()\n do_thing(\n");
let position = buffer.len();
let indent = calc.calculate_indent(&buffer, position, &Language::Lua, 4);
// Tree-sitter has no useful capture here; the language is keyword-
// delimited so the pattern fallback is suppressed. Expected: copy the
// current line's indent (the line we just left was indented 4 spaces;
// the new line is empty so its current indent is 0). Critically, it
// must NOT be 8 (which is what the old `(`-trigger heuristic produced).
assert!(
matches!(indent, Some(0) | Some(4)),
"Lua: `(` at end of a body line must not push past 4-space body indent (got {:?})",
indent
);
assert_ne!(
indent,
Some(8),
"Lua: previous-line `(` must not add C-family indent on top of body indent"
);
}
#[test]
fn test_ruby_def_opens_block_via_tree_sitter_structural_rescue() {
// Verifiable improvement: Ruby `def foo` opens a method block.
// Master with the old pattern fallback returned 0 (last char `o`,
// no C-family trigger). With tree-sitter as source of truth and the
// structural "opens-on-cursor-line" rescue, when tree-sitter does
// produce a `(method)` capture this should yield +tab_size. When the
// input is so short tree-sitter can't recover (just an ERROR node),
// we accept "stay at current line indent" as the safe default.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("def foo");
let indent = calc.calculate_indent(&buffer, buffer.len(), &Language::Ruby, 4);
// The result must NOT come from the C-family pattern matcher. With
// pattern fallback engaged the answer would still be 0 here (last
// char `o`, no trigger), so the meaningful assertion is that this
// is an answer we can justify structurally — copy current line indent.
assert_eq!(
indent,
Some(0),
"Ruby: short `def foo` (incomplete syntax) falls back to current line indent"
);
}
#[test]
fn test_bash_then_opens_block_via_tree_sitter_structural_rescue() {
// Bash: `if true; then` produces a tree-sitter `if_statement` node
// (with a MISSING `fi`). The structural rescue inside the tree-sitter
// path detects that an @indent block opens on the cursor's line and
// requests one extra level of indent — without consulting the
// C-family pattern matcher.
let mut calc = IndentCalculator::new();
let buffer = Buffer::from_str_test("if true; then");
let indent = calc.calculate_indent(&buffer, buffer.len(), &Language::Bash, 4);
assert_eq!(
indent,
Some(4),
"Bash: `if true; then` opens a block (tree-sitter sees if_statement) — should indent +4"
);
}
}