cpp_code_analysis 0.2.0

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
Documentation
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
//! Turning filesystem events into work: which of them the index cares about, and what each one implies.
//!
//! gives the watcher three jobs, and this module is all three of them and nothing else:
//!
//! ```text
//! filter      ignore what churns and what the index wrote itself
//! coalesce    many events in one path become one thing that happened
//! respond     say what the index must now do: forget, re-read, or start over
//! ```
//!
//! # What is deliberately not here
//!
//! **No OS watcher, no threads, and no clock.** A `notify`-style adapter produces [`FileEvent`]s and someone has
//! to decide when a burst of them is over; both of those are a shell around this module rather than part of it,
//! and the split is the same one [`crate::index::store`] makes about parsing: the decisions are testable on
//! strings, and the shell has nothing in it worth testing. Adding `notify` to the workspace is a dependency
//! decision this layer does not force.
//!
//! # Modified is the boring one, and that is the point
//!
//! A file whose text changed needs its summary rebuilt, and **nothing else does**. Its includers' summaries do not
//! depend on its contents: a summary is a function of a file's own text, its directory and the configuration —
//! which is what [`crate::SummaryKey`] records, and why the macro environment left the key. So an edited header is
//! one file to re-read, not the fifty that include it. (Their *queries* do change, and queries walk the live
//! index, so the new declarations are visible the moment the header's summary is replaced.)
//!
//! That is worth stating because the opposite is the intuitive answer, and the code that implements the intuitive
//! answer — [`SummaryStore::invalidate`] and its transitive walk — is still here and still right for the two cases
//! that do invalidate a summary without its text changing: a file **appearing** and a file **disappearing**. A
//! summary records where its includes *resolved*, and that is a fact about the filesystem:
//!
//! ```text
//! a.cpp:  #include "generated.h"     ->  resolved: None          (nothing there yet)
//! …      generated.h is created      ->  a.cpp's summary is now wrong, and its text never changed
//! ```
//!
//! A third case belongs with those two and is the one that catches people out: a stored summary is **only ever a
//! candidate**. Its key names the text, the configuration and the directory — everything except which files exist
//! — so a hit is re-checked against the filesystem before it is used. [`SummaryStore::get`] says why, and what
//! that costs.
//!
//! # Why a created file can be repaired exactly
//!
//! To know which files a creation affects, the index needs to know which files *looked* for that path and did not
//! find it. It does not store the candidate lists, and it does not need to: an include's spelling and the
//! directory it was written in determine every candidate it could have tried, and both are known — the spelling
//! is a fact in the summary, and the directories are the file's own plus the configuration's include paths. So the
//! question is answered by enumerating, from each file's side, the paths it would try — the resolver's own
//! `join_normalized` of a directory and a spelling — and looking each one up in the set of paths that appeared.
//! Enumerating from the file's side is also what makes a batch of a thousand creations one pass over the index
//! rather than a thousand.
//!
//! The same test answers the other half, which is the hole this layer closes: a
//! file created **earlier in the search order** than the one that resolution currently points at takes over. The
//! rank of a candidate is the rank the resolver itself computes, so "earlier" is the resolver's own order rather
//! than a guess about it.
//!
//! # This layer is promptness, not correctness
//!
//! Everything above describes what a watcher *should* do about an event — and none of it is what makes the index
//! right. A stored summary whose `resolved` values have gone stale is caught by [`SummaryStore::get`] itself,
//! which re-checks every include against the filesystem before it believes the entry. That check has to exist
//! here regardless, because a watcher can always miss an event: a queue overflows, a network filesystem reports
//! nothing, a file appears while the process was not running, an editor writes in a way nobody anticipated.
//!
//! What this layer buys on top of that is *when* the repair happens. Without it, a file that lost an include
//! keeps answering from a summary that describes the search as it used to be, until something asks about that file
//! again — which for a header the user is not looking at may be never. With it, the answer changes as soon as the
//! event does. That is a real difference for a diagnostics feature and no difference at all for correctness, and
//! keeping the two apart is what stops the watcher from becoming load-bearing.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::file::paths::{FileProvider, join_normalized, normalize_path};
use crate::include::config::CompilerConfig;
use crate::index::ProjectIndex;
use crate::index::store::SummaryStore;
use crate::preprocess::directive::IncludeForm;
use crate::summary::{FileSummary, IncludeFact};

/// The name a compile database is conventionally found under, relative to the project root.
const COMPILE_DATABASE: &str = "compile_commands.json";

/// How many changed paths make a batch not worth asking about one at a time.
///
/// A branch switch arrives as thousands of events. Asking the project for its file list and running a
/// [`crate::index::Worklist`] over it is then *cheaper* than thousands of individual lookups — and not much
/// cheaper in correctness terms either way: the files that did not change are cache hits, which the measurement in
/// `examples/measure.rs` puts at a fraction of a millisecond.
const OVERWHELMING_BATCH: usize = 256;

/// What happened to one path.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventKind {
    /// The path is there and was not, as far as this batch knows.
    Created,
    /// The path is there and its contents may have changed.
    Modified,
    /// The path is gone.
    Removed,
}

/// One filesystem event, as an OS adapter would report it.
///
/// A rename is two events — a removal and a creation — and deliberately so: they are the two things the index can
/// act on, and the pair is *free* to handle, because a summary is keyed by content. A file renamed and not
/// otherwise changed has the text the cache was built from, so its new path is a hit and nothing is re-parsed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileEvent {
    pub path: PathBuf,
    pub kind: EventKind,
}

impl FileEvent {
    pub fn created(path: impl Into<PathBuf>) -> Self {
        FileEvent {
            path: path.into(),
            kind: EventKind::Created,
        }
    }

    pub fn modified(path: impl Into<PathBuf>) -> Self {
        FileEvent {
            path: path.into(),
            kind: EventKind::Modified,
        }
    }

    pub fn removed(path: impl Into<PathBuf>) -> Self {
        FileEvent {
            path: path.into(),
            kind: EventKind::Removed,
        }
    }
}

/// Which events a project cares about.
///
/// # What is filtered, and what that filtering is worth
///
/// Two things are excluded for correctness and both are about *this* program writing files:
///
/// * **the cache directory**, because the index writes summaries into it — an unfiltered watcher would take its
///   own writes as input and re-index them for ever;
/// * **`compile_commands.json`**, which is not a source file at all but the signal that the *configuration*
///   changed. See [`ChangeBatch::configuration_changed`].
///
/// Everything else here is an **optimisation**, and it is worth being precise about that: a path nothing includes
/// and the index has never held is a no-op whatever happens to it, so a deny-list of editor temporary files would
/// buy nothing. What the directory list buys is the *batch*: a `git checkout` produces thousands of events under
/// `.git/`, and filtering them where they arrive keeps a thousand no-ops from being sorted through later.
///
/// # What is deliberately not filtered
///
/// **File extensions.** A header is included by *name*, and `<vector>` has no extension at all — so an extension
/// allow-list would drop exactly the files an index most needs. The narrower question "does this file deserve a
/// summary?" is not answered by looking at its name: it is answered by whether the index already holds it, or
/// something includes it. See [`SummaryStore::respond`].
#[derive(Debug, Clone)]
pub struct WatchFilter {
    root: PathBuf,
    cache: PathBuf,
    configuration: PathBuf,
    ignored: Vec<PathBuf>,
    /// Ignored by **pattern**, for the exclusions a caller has as a glob rather than as a directory.
    ignored_patterns: Vec<PathPattern>,
}

impl WatchFilter {
    /// A filter for a project rooted at `root`.
    pub fn new(root: impl AsRef<Path>) -> Self {
        let root = root.as_ref();

        WatchFilter {
            root: root.to_path_buf(),
            cache: root.join(crate::CACHE_DIRECTORY),
            configuration: root.join(COMPILE_DATABASE),
            ignored: Vec::new(),
            ignored_patterns: Vec::new(),
        }
    }

    /// Ignore everything under a directory — a build tree, a vendored dependency, a scratch directory.
    ///
    /// There is no way to guess these: `build`, `out` and `target` are the ones that are, and the next project all conventions, and a
    /// project is entitled to call its build tree `tmp`. So the caller says, and this layer does not invent.
    pub fn ignore(mut self, directory: impl Into<PathBuf>) -> Self {
        self.ignored.push(directory.into());
        self
    }

    /// Ignore every path matching a pattern — `**/build/**`, `*.generated.h`, `third_party/**`.
    ///
    /// The other half of [`WatchFilter::ignore`], for the caller that has a *configuration file*: an editor's
    /// `exclude` list is written as globs, and turning each one into a directory would be guessing. The pattern is
    /// parsed by the caller ([`PathPattern`]), so a typo is reported where the user can see it rather than
    /// swallowed here.
    ///
    /// A pattern is tried against the whole path **and** against the path relative to this filter's root, because
    /// a settings file writes both: `**/build/**` names any `build` directory, and `build/*.h` names one from the
    /// root.
    pub fn ignore_pattern(mut self, pattern: PathPattern) -> Self {
        self.ignored_patterns.push(pattern);
        self
    }

    /// Where the compile database is, for a caller that read it from somewhere unusual.
    pub fn with_configuration(mut self, path: impl Into<PathBuf>) -> Self {
        self.configuration = path.into();
        self
    }

    /// The cache directory's **name**, when the project renamed it (`index.cache_dir` in `.cppls.toml`).
    ///
    /// A name rather than a path: the cache lives under the project root or it is not the project's cache, and the
    /// rule that keeps a watcher from re-indexing the index's own writes has to be the same one the store writes
    /// under. The module documentation gives the reason the cache is inside the checkout at all.
    pub fn with_cache_directory(mut self, name: &str) -> Self {
        if !name.is_empty() {
            self.cache = self.root.join(name);
        }
        self
    }

    /// Is this event one the index should not even look at?
    pub fn is_ignored(&self, path: &Path) -> bool {
        let path = normalize(path);

        if under(&path, &normalize(&self.cache)) {
            return true;
        }

        // `.git` anywhere in the path rather than only at the root: a checkout of a submodule, a worktree, or a
        // nested repository all churn the same way.
        if path.split('/').any(|segment| segment == ".git") {
            return true;
        }

        if self
            .ignored
            .iter()
            .any(|directory| under(&path, &normalize(directory)))
        {
            return true;
        }

        if self.ignored_patterns.is_empty() {
            return false;
        }

        // Two spellings, because a settings file uses both: `**/build/**` names a directory anywhere in the tree,
        // and `build/*.h` names one from the project root. Matching only the whole path would silently ignore
        // nothing for the second, which is the failure mode nobody notices.
        let root = normalize(&self.root);
        let relative = path
            .strip_prefix(&root)
            .map(|rest| rest.trim_start_matches('/'));

        self.ignored_patterns.iter().any(|pattern| {
            pattern.matches(Path::new(&path))
                || relative.is_some_and(|relative| pattern.matches(Path::new(relative)))
        })
    }

    /// Is this path the compile database?
    pub fn is_configuration(&self, path: &Path) -> bool {
        self.is_configuration_file(path)
    }

    /// Is this path one of the files that **configure** the analysis?
    ///
    /// Three questions have one answer here, which is why the rule is a method rather than a comparison at each
    /// call site: an event about a configuration file is not an edit to index, it is a reason to *reconfigure*; a
    /// project's configuration lives in files whose names are conventions; and the same name anywhere in the tree
    /// means the same thing — a build directory's `compile_commands.json` configures the build as much as the
    /// root's does, and a vendored subproject's `CMakeCache.txt` describes a compilation this analysis may well be
    /// reading.
    ///
    /// The explicit path is kept as well, because a caller can point the database somewhere no convention
    /// predicts (`compile.database` in `.cppls.toml`).
    pub fn is_configuration_file(&self, path: &Path) -> bool {
        if normalize(path) == normalize(&self.configuration) {
            return true;
        }

        path.file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| {
                crate::project::CONFIGURATION_FILE_NAMES
                    .iter()
                    .any(|known| known.eq_ignore_ascii_case(name))
            })
    }
}

/// How an ignored-by-pattern path is matched.
///
/// `require_literal_separator` is the one that matters: with glob's default, `*` happily crosses a `/`, so
/// `build/*` would ignore `build/generated/deep/file.cpp` — more than the pattern says, and an exclusion that
/// silently swallows more than it names is the wrong way to be wrong. Here `*` stays inside one path segment and
/// `**` is how a pattern reaches across them.
///
/// Case sensitivity follows the filesystem, which is the same rule [`FileProvider::is_case_insensitive`] states:
/// `SPEED.H` and `speed.h` are one file on Windows and two on Linux, so an exclusion has to mean the same thing.
const PATTERN_OPTIONS: glob::MatchOptions = glob::MatchOptions {
    case_sensitive: !cfg!(windows),
    require_literal_separator: true,
    require_literal_leading_dot: false,
};

/// A path pattern, as a caller's configuration writes one: `**/build/**`, `*.generated.h`.
///
/// `glob` is the implementation rather than the API. This crate defines its own types at its boundary — a
/// [`cpp_parser::SourceRange`] rather than rowan's range, a [`crate::FoundIn`] rather than a boolean pair — and a
/// pattern is no different: a caller hands over the string a settings file held and gets back either a pattern or
/// a sentence saying why it is not one. Reporting that sentence is the caller's job, because the layer with a user
/// in front of it is the caller's (the same division `CompileCommands::malformed` makes).
///
/// What a pattern means here:
///
/// ```text
/// build/*.h        the headers directly in `build` — `*` does not cross a separator
/// **/build/**      every `build` directory at any depth, and everything under it
/// *.generated.h    a file whose name ends that way, in any directory (`*` is name-only)
/// ```
///
/// It is matched against a **normalized** path, so a pattern is written with `/` on every platform, and against
/// both the whole path and the path as written from the project root (see [`WatchFilter::ignore_pattern`]).
#[derive(Debug, Clone)]
pub struct PathPattern(glob::Pattern);

impl PathPattern {
    /// Parse a pattern, or say what is wrong with it.
    pub fn new(pattern: &str) -> Result<PathPattern, String> {
        glob::Pattern::new(pattern)
            .map(PathPattern)
            .map_err(|error| error.to_string())
    }

    /// Does this path match?
    ///
    /// The path is normalized the way resolution normalizes one (`\` and `/` are one separator, and case is folded
    /// where the filesystem folds it), because a pattern from a settings file is written against paths as that
    /// filesystem spells them.
    pub fn matches(&self, path: &Path) -> bool {
        let normalized = normalize(path);
        self.0
            .matches_path_with(Path::new(&normalized), PATTERN_OPTIONS)
    }

    /// The pattern as it was written.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl std::str::FromStr for PathPattern {
    type Err = String;

    fn from_str(pattern: &str) -> Result<Self, Self::Err> {
        PathPattern::new(pattern)
    }
}

/// A batch of events, reduced to the paths the index has to think about.
///
/// The batch is where several events on one path become one thing that happened, and the rule is the conservative
/// one: **the last event decides whether the file is there, and any creation in the batch decides that it might be
/// new.** Both halves earn their keep on the shapes an editor actually produces:
///
/// ```text
/// remove + create   a save that writes a temporary file and renames it over the target
///                   -> the file is there, and it may be a file that did not exist before
/// create + modify   a save into a path that was missing -> the same conclusion
/// create + remove   a temporary file that came and went   -> it is not there
/// ```
///
/// # What this does not do
///
/// **It does not decide when the batch ends.** That is a clock question — an editor writes a file several times a
/// second, a `git checkout` is over in a burst — and a clock is exactly what this module does not have. A caller
/// with a timer pushes until it fires and responds once.
#[derive(Debug, Clone)]
pub struct ChangeBatch {
    filter: WatchFilter,
    /// The paths, and what happened to them, in the order they were first seen — so that a response is
    /// deterministic rather than `HashMap`-ordered.
    changes: Vec<(PathBuf, EventKind)>,
    position: HashMap<String, usize>,
    configuration: bool,
}

impl ChangeBatch {
    pub fn new(filter: WatchFilter) -> Self {
        ChangeBatch {
            filter,
            changes: Vec::new(),
            position: HashMap::new(),
            configuration: false,
        }
    }

    /// Add an event. Returns whether the index has to think about it.
    ///
    /// A `false` answer means the event is one of the ignored ones or the configuration — both of which are
    /// *reported* rather than swallowed, which is why the return value matters to a caller deciding whether to do
    /// anything at all.
    pub fn push(&mut self, event: FileEvent) -> bool {
        if self.filter.is_configuration(&event.path) {
            self.configuration = true;
            return false;
        }

        if self.filter.is_ignored(&event.path) {
            return false;
        }

        match self.position.get(&normalize(&event.path)) {
            Some(index) => {
                let (path, kind) = &mut self.changes[*index];
                *kind = merge(*kind, event.kind);
                // The new spelling wins: a rename reports the path the event was about, and that is the one the
                // caller will ask about next.
                *path = event.path;
            }
            None => {
                self.position
                    .insert(normalize(&event.path), self.changes.len());
                self.changes.push((event.path, event.kind));
            }
        }

        true
    }

    /// Every event of a batch, in one call. Returns how many of them the index has to think about.
    pub fn extend(&mut self, events: impl IntoIterator<Item = FileEvent>) -> usize {
        let mut kept = 0;
        for event in events {
            if self.push(event) {
                kept += 1;
            }
        }
        kept
    }

    /// The paths that changed, and what happened to each — one entry per path.
    pub fn changed(&self) -> impl Iterator<Item = (&Path, EventKind)> {
        self.changes
            .iter()
            .map(|(path, kind)| (path.as_path(), *kind))
    }

    pub fn len(&self) -> usize {
        self.changes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.changes.is_empty() && !self.configuration
    }

    /// Did the compile database change?
    ///
    /// A separate answer from "these files changed", because it is a different *size* of change: the configuration
    /// is part of every summary's key, so a single `-I` added to the database makes every summary in the project a
    /// miss. Nothing is wrong with the stored summaries — they describe a compilation that is no longer the one
    /// being asked about — and a caller responds by rebuilding from its file list rather than by asking about the
    /// paths here.
    pub fn configuration_changed(&self) -> bool {
        self.configuration
    }
}

/// What a batch means for the index.
///
/// The two lists are not alternatives and the order between them matters: `forgotten` has to be applied before
/// anything is re-read, so that a query during the work does not find declarations in a file that is gone.
/// [`SummaryStore::respond`] applies it in that order.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Response {
    /// Summaries dropped, because the files are gone.
    pub forgotten: Vec<PathBuf>,
    /// Files to read again, in the order to read them.
    pub reindex: Vec<PathBuf>,
    /// Start over instead: the configuration changed, or the batch was too large to be worth asking about one path
    /// at a time. `reindex` is empty when this is set, and the caller rebuilds from its own file list.
    pub everything: bool,
}

impl Response {
    /// Is there nothing to do?
    pub fn is_empty(&self) -> bool {
        self.forgotten.is_empty() && self.reindex.is_empty() && !self.everything
    }
}

impl<F: FileProvider> SummaryStore<F> {
    /// What the index has to do about a batch of events.
    ///
    /// Three answers, and the reasoning for each is the module documentation:
    ///
    /// * **a file changed** — re-read it, if the index holds it. A path the index has never held and that nothing
    ///   includes is not indexed here: that is the project scan's job, not an event's, and doing it here would mean
    ///   parsing every `.md` file the editor touches;
    /// * **a file is gone** — forget it, and re-read the files that resolved to it, because their summaries record
    ///   a search that would now fail;
    /// * **a file appeared** — re-read it if the index holds it, and re-read the files that *searched* for it and
    ///   did not find it, which is every file whose include would now resolve to it (including one whose include
    ///   resolved to something the new file outranks).
    pub fn respond(&mut self, batch: &ChangeBatch) -> Response {
        if batch.configuration_changed() {
            return Response {
                everything: true,
                ..Response::default()
            };
        }

        if batch.len() > OVERWHELMING_BATCH {
            return Response {
                everything: true,
                ..Response::default()
            };
        }

        let mut forgotten = Vec::new();
        let mut reindex: Vec<PathBuf> = Vec::new();
        let mut queued: HashSet<String> = HashSet::new();

        // The creations are collected first and answered in one pass over the index, because that pass costs the
        // same whether it is looking for one path or a thousand. See `naming_files`.
        let created: HashSet<String> = batch
            .changed()
            .filter(|(_, kind)| *kind == EventKind::Created)
            .map(|(path, _)| normalize(path))
            .collect();

        for (path, kind) in batch.changed() {
            match kind {
                EventKind::Removed => {
                    if self.forget(path) {
                        forgotten.push(path.to_path_buf());
                    }
                    // The files whose include resolved *to* this path: their summaries are now describing a search
                    // that would not happen the same way twice.
                    for includer in self.index().includers_of(path) {
                        push_once(&mut reindex, &mut queued, includer);
                    }
                }
                EventKind::Modified => {
                    if self.index().summary(path).is_some() {
                        push_once(&mut reindex, &mut queued, path.to_path_buf());
                    }
                }
                EventKind::Created => {
                    if self.index().summary(path).is_some() {
                        push_once(&mut reindex, &mut queued, path.to_path_buf());
                    }
                    for file in naming_files(self.index(), self.config(), &created) {
                        push_once(&mut reindex, &mut queued, file);
                    }
                }
            }
        }

        // A file that is gone is not read again. This happens when a header and one of the files that included it
        // are deleted in the same batch: the includer is named by the header's removal, and it is not there to be
        // read. Reading it would be a step whose only outcome is `Missing`.
        if !forgotten.is_empty() {
            let gone: std::collections::HashSet<String> =
                forgotten.iter().map(|path| normalize(path)).collect();
            reindex.retain(|path| !gone.contains(&normalize(path)));
        }

        Response {
            forgotten,
            reindex,
            everything: false,
        }
    }
}

/// Add a path to the work list if it is not already there.
fn push_once(
    work: &mut Vec<PathBuf>,
    queued: &mut std::collections::HashSet<String>,
    path: PathBuf,
) {
    if queued.insert(normalize(&path)) {
        work.push(path);
    }
}

/// The files whose includes could find one of the paths that just appeared.
///
/// The candidates are enumerated **from each file's side** — the directories it would search, joined with each of
/// its spellings — and looked up in the set of paths that appeared. Enumerating this way is what keeps a batch of
/// many creations one pass over the index instead of one pass *per created path*, which is the shape a branch
/// switch has.
fn naming_files(
    index: &ProjectIndex,
    config: &CompilerConfig,
    created: &std::collections::HashSet<String>,
) -> Vec<PathBuf> {
    if created.is_empty() {
        return Vec::new();
    }

    let mut found = Vec::new();

    for summary in index.summaries() {
        if names_one_of(summary, config, created) {
            found.push(summary.path.clone());
        }
    }

    found
}

/// Does this file write an include that would find one of these paths, where it did not find it before?
fn names_one_of(
    summary: &FileSummary,
    config: &CompilerConfig,
    created: &std::collections::HashSet<String>,
) -> bool {
    let including = summary.path.parent().unwrap_or(Path::new("."));

    summary.includes.iter().any(|include| {
        candidates_of(include, including, config)
            .into_iter()
            .any(|(candidate, rank)| {
                if !created.contains(&candidate) {
                    return false;
                }

                match &include.resolved {
                    // Nothing was found, so anything the resolver would try is an improvement.
                    None => true,
                    // Something was found: the new file has to be *earlier* in the search order to take over.
                    Some(resolved) => {
                        // A resolution whose directory cannot be ranked at all — which no search the resolver
                        // performs can produce — counts as "the new file wins", because an extra rebuild is the
                        // cheap mistake and a missed one is the expensive mistake.
                        let held = resolved.parent().and_then(|directory| {
                            rank_of(directory, including, include.form, config)
                        });

                        held.is_none_or(|held| rank < held)
                    }
                }
            })
    })
}

/// Every path this include would try, in the order the resolver tries them, each with the resolver's rank.
///
/// Built with the resolver's own [`join_normalized`], so the candidates are the ones it would actually form
/// rather than a second opinion about how to join a directory and a spelling.
///
/// `#include_next` is the one approximation: the resolver starts *after* the directory the including file was
/// found in, and that origin is not stored, so this enumerates from the beginning. That can over-include — naming
/// a file as affected when the search would have skipped that candidate — which costs one rebuild, where the
/// opposite mistake would leave a summary stale. `#include_next` lives in wrapper headers around a system
/// installation, which is not what a project index is usually made of.
fn candidates_of(
    include: &IncludeFact,
    including: &Path,
    config: &CompilerConfig,
) -> Vec<(String, usize)> {
    let spelling = Path::new(&include.spelling);

    // An absolute spelling skips the search entirely, so there is one candidate and it is the spelling.
    if spelling.is_absolute() {
        return vec![(
            normalize(&join_normalized(Path::new(""), spelling, false)),
            0,
        )];
    }

    let mut candidates = Vec::new();

    // The local directory is searched only for a quoted include, which is the whole difference between the two
    // spellings — and it comes first, which is why a file created beside its includer takes over.
    if include.form == IncludeForm::Quote {
        candidates.push((
            normalize(&join_normalized(including, spelling, cfg!(windows))),
            0,
        ));
    }

    for (index, include_path) in config.include_paths.iter().enumerate() {
        let directory = config.resolve_against_working_directory(&include_path.directory);
        candidates.push((
            normalize(&join_normalized(&directory, spelling, cfg!(windows))),
            index + 1,
        ));
    }

    candidates
}

/// Where in an include's candidate list a directory is.
///
/// The numbers are the resolver's own: the including file's own directory first, then the configured paths in
/// order. Getting this wrong would not crash — it would rebuild a file that did not need it, or fail to rebuild
/// one that did, which is the failure this whole module exists to avoid.
fn rank_of(
    directory: &Path,
    including: &Path,
    form: IncludeForm,
    config: &CompilerConfig,
) -> Option<usize> {
    if form == IncludeForm::Quote && normalize(directory) == normalize(including) {
        return Some(0);
    }

    config
        .include_paths
        .iter()
        .position(|include_path| {
            let searched = config.resolve_against_working_directory(&include_path.directory);
            normalize(&searched) == normalize(directory)
        })
        .map(|index| index + 1)
}

/// How two events on one path become one.
///
/// See [`ChangeBatch`] for why a creation is remembered rather than overwritten: an editor's save is a removal and
/// a creation, and the file that comes back may be one that did not exist before.
fn merge(held: EventKind, arriving: EventKind) -> EventKind {
    if arriving == EventKind::Removed {
        return EventKind::Removed;
    }

    if held == EventKind::Created || arriving == EventKind::Created {
        return EventKind::Created;
    }

    EventKind::Modified
}

/// Is `path` the same as, or inside, `directory`?
fn under(path: &str, directory: &str) -> bool {
    path == directory
        || path
            .strip_prefix(directory)
            .is_some_and(|rest| rest.starts_with('/'))
}

/// A path as this module compares them.
fn normalize(path: &Path) -> String {
    // Borrowed from the index rather than invented: a filter that disagrees with the index about when two paths
    // are one path would filter the wrong events, or miss its own writes.
    normalize_path(path, cfg!(windows))
}

#[cfg(test)]
mod tests {
    use super::{ChangeBatch, EventKind, FileEvent, PathPattern, WatchFilter};
    use crate::file::paths::{DiskFiles, MemoryFiles};
    use crate::include::config::CompilerConfig;
    use crate::index::store::SummaryStore;
    use std::path::{Path, PathBuf};

    /// A batch over a filter with the cache and `.git` rules, and nothing else.
    fn batch(root: &str) -> ChangeBatch {
        ChangeBatch::new(WatchFilter::new(root))
    }

    /// A pattern the test wrote, which therefore parses.
    fn pattern(text: &str) -> PathPattern {
        PathPattern::new(text).expect("the test's own pattern is a pattern")
    }

    fn kinds(batch: &ChangeBatch) -> Vec<(String, EventKind)> {
        batch
            .changed()
            .map(|(path, kind)| (path.to_string_lossy().replace('\\', "/"), kind))
            .collect()
    }

    // -------------------------------------------------------------------------------------------
    // Filtering
    // -------------------------------------------------------------------------------------------

    #[test]
    fn the_cache_directory_is_ignored() {
        // Not an optimisation: the index writes summaries there, so an unfiltered watcher would take its own
        // writes as input and re-index them for ever.
        let mut batch = batch("/p");

        assert!(!batch.push(FileEvent::created("/p/.cppls/summaries/ab/abcdef.bin")));
        assert!(!batch.push(FileEvent::modified("/p/.cppls/summaries/ab/abcdef.bin.tmp")));
        assert!(batch.is_empty());
    }

    #[test]
    fn a_path_that_merely_starts_like_the_cache_is_not_ignored() {
        // The boundary the rule above is written with a separator for: `.cpplsfoo` is a directory somebody may
        // well have, and filtering it would silently drop a project's files.
        let mut batch = batch("/p");

        assert!(batch.push(FileEvent::modified("/p/.cpplsfoo/widget.h")));
        assert_eq!(batch.len(), 1);
    }

    #[test]
    fn git_internals_are_ignored_wherever_they_are() {
        let mut batch = batch("/p");

        assert!(!batch.push(FileEvent::modified("/p/.git/index.lock")));
        assert!(!batch.push(FileEvent::created("/p/sub/.git/objects/ab/cdef")));
        assert!(!batch.push(FileEvent::modified("/p/.git")));
        assert!(batch.is_empty());
    }

    #[test]
    fn a_directory_the_caller_names_is_ignored() {
        // There is no way to guess this one: a project is entitled to call its build tree `tmp`.
        let mut batch = ChangeBatch::new(WatchFilter::new("/p").ignore("/p/build"));

        assert!(!batch.push(FileEvent::modified("/p/build/generated.h")));
        assert!(batch.push(FileEvent::modified("/p/src/generated.h")));
    }

    #[test]
    fn a_pattern_the_caller_names_is_ignored() {
        // An editor's exclusion list is globs, not directories: `**/build/**` is how a client says "every build
        // tree, wherever it is", and a filter that only understood directories would index them all.
        let mut batch =
            ChangeBatch::new(WatchFilter::new("/p").ignore_pattern(pattern("**/build/**")));

        assert!(!batch.push(FileEvent::modified("/p/build/generated.h")));
        assert!(!batch.push(FileEvent::created("/p/sub/deep/build/generated.cpp")));
        assert!(batch.push(FileEvent::modified("/p/src/generated.h")));
        assert!(
            batch.push(FileEvent::modified("/p/src/build.h")),
            "a file that merely shares the name is not inside the ignored directory"
        );
    }

    #[test]
    fn a_star_stays_inside_one_directory_and_two_of_them_do_not() {
        // The rule `PATTERN_OPTIONS` records: `build/*.h` is the headers directly in `build`, and reaching deeper
        // takes `**`. A pattern that ignored more than it says would silently drop a project's sources.
        let mut batch = ChangeBatch::new(
            WatchFilter::new("/p")
                .ignore_pattern(pattern("build/*.h"))
                .ignore_pattern(pattern("gen/**/*.cpp")),
        );

        assert!(!batch.push(FileEvent::modified("/p/build/one.h")));
        assert!(batch.push(FileEvent::modified("/p/build/deep/two.h")));
        assert!(!batch.push(FileEvent::modified("/p/gen/a/b/three.cpp")));
        assert!(!batch.push(FileEvent::modified("/p/gen/four.cpp")));
    }

    #[test]
    fn a_pattern_that_is_not_one_says_so() {
        // The typo in a client's settings is reported where the user can see it, which is why parsing is the
        // caller's step rather than something a builder swallows.
        let error =
            PathPattern::new("**/[unclosed").expect_err("an unclosed class is not a pattern");
        assert!(
            error.contains("Pattern syntax error"),
            "the error should say what is wrong with the pattern, got {error:?}"
        );
    }

    #[test]
    fn a_file_with_no_extension_is_not_ignored() {
        // The reason there is no extension allow-list: `#include <vector>` has no extension, so a list of "C++
        // extensions" would drop exactly the files an index most needs.
        let mut batch = batch("/p");

        assert!(batch.push(FileEvent::modified("/usr/include/vector")));
    }

    #[test]
    fn the_compile_database_is_reported_rather_than_indexed() {
        let mut batch = batch("/p");

        assert!(
            !batch.push(FileEvent::modified("/p/compile_commands.json")),
            "it is not a source file and must not be queued as one"
        );
        assert!(batch.configuration_changed());
        assert_eq!(batch.len(), 0);
        assert!(
            !batch.is_empty(),
            "a configuration change is not nothing, even with no paths in the batch"
        );
    }

    // -------------------------------------------------------------------------------------------
    // Coalescing
    // -------------------------------------------------------------------------------------------

    #[test]
    fn several_events_on_one_path_become_one() {
        let mut batch = batch("/p");

        for _ in 0..5 {
            batch.push(FileEvent::modified("/p/widget.h"));
        }

        assert_eq!(
            kinds(&batch),
            [("/p/widget.h".to_string(), EventKind::Modified)]
        );
    }

    #[test]
    fn a_save_that_replaces_a_file_is_a_creation() {
        // The shape an editor produces: write a temporary file, rename it over the target. The file that comes
        // back may be one that did not exist before, so the batch has to remember the creation.
        let mut batch = batch("/p");

        batch.push(FileEvent::removed("/p/widget.h"));
        batch.push(FileEvent::created("/p/widget.h"));

        assert_eq!(
            kinds(&batch),
            [("/p/widget.h".to_string(), EventKind::Created)]
        );
    }

    #[test]
    fn a_temporary_file_that_comes_and_goes_is_gone() {
        let mut batch = batch("/p");

        batch.push(FileEvent::created("/p/widget.h"));
        batch.push(FileEvent::removed("/p/widget.h"));

        assert_eq!(
            kinds(&batch),
            [("/p/widget.h".to_string(), EventKind::Removed)]
        );
    }

    #[test]
    fn a_creation_anywhere_in_the_batch_upgrades_a_modification() {
        let mut batch = batch("/p");

        batch.push(FileEvent::created("/p/new.h"));
        batch.push(FileEvent::modified("/p/new.h"));

        assert_eq!(
            kinds(&batch),
            [("/p/new.h".to_string(), EventKind::Created)]
        );
    }

    #[test]
    fn a_batch_keeps_the_order_it_first_saw_paths_in() {
        // So that a response is deterministic: two runs over the same events do the same work in the same order.
        let mut batch = batch("/p");

        batch.push(FileEvent::modified("/p/b.h"));
        batch.push(FileEvent::modified("/p/a.h"));
        batch.push(FileEvent::modified("/p/b.h"));

        assert_eq!(
            kinds(&batch),
            [
                ("/p/b.h".to_string(), EventKind::Modified),
                ("/p/a.h".to_string(), EventKind::Modified),
            ]
        );
    }

    // -------------------------------------------------------------------------------------------
    // Responding, with files in memory
    //
    // `respond` reads the index and the configuration and touches no filesystem, so these tests are about *which*
    // paths it names. What happens when those paths are actually read is the next section's business.
    // -------------------------------------------------------------------------------------------

    fn memory_store(name: &str, files: &MemoryFiles) -> (SummaryStore<MemoryFiles>, PathBuf) {
        let root = std::env::temp_dir().join("cppls-watch-tests").join(name);
        let _ = std::fs::remove_dir_all(&root);

        let store = SummaryStore::with_provider(&root, CompilerConfig::default(), files.clone());
        (store, root)
    }

    /// How a test spells a path, normalized the way a response spells one.
    fn shown(path: &Path) -> String {
        super::normalize(path)
    }

    fn names(paths: &[PathBuf]) -> Vec<String> {
        paths.iter().map(|path| shown(path)).collect()
    }

    #[test]
    fn a_modified_header_is_read_again_and_nothing_else_is() {
        // The claim the whole module is built on: a summary does not depend on the contents of what it includes,
        // so an edited header is one file to read, not the fifty that include it. The intuitive answer — walk the
        // reverse edges and rebuild everything below — is what `SummaryStore::invalidate` does, and it is wrong
        // here (it costs fifty reads to learn that nothing changed).
        let files = MemoryFiles::new()
            .with_file("/p/widget.h", "struct Widget { int size; };\n")
            .with_file("/p/a.cpp", "#include \"widget.h\"\n")
            .with_file("/p/b.cpp", "#include \"widget.h\"\n")
            .with_file("/p/c.cpp", "#include \"widget.h\"\n");
        let (mut store, root) = memory_store("modified", &files);

        for path in ["/p/widget.h", "/p/a.cpp", "/p/b.cpp", "/p/c.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }

        let mut events = batch("/p");
        events.push(FileEvent::modified("/p/widget.h"));
        let response = store.respond(&events);

        assert_eq!(names(&response.reindex), ["/p/widget.h"]);
        assert!(response.forgotten.is_empty());
        assert!(!response.everything);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_modified_file_the_index_has_never_held_is_not_indexed() {
        // An event is not a project scan. Indexing whatever the editor touches would mean parsing every `.md` and
        // every `.json` in the tree, so a path the index does not hold and nothing includes is a no-op — and a
        // newly written `.cpp` is picked up by the file list, which is the caller's business.
        let files = MemoryFiles::new().with_file("/p/known.cpp", "int x;\n");
        let (mut store, root) = memory_store("unknown-path", &files);
        store
            .get(Path::new("/p/known.cpp"))
            .expect("the file reads");

        let mut events = batch("/p");
        events.push(FileEvent::modified("/p/README.md"));
        events.push(FileEvent::modified("/p/new.cpp"));
        let response = store.respond(&events);

        assert!(response.is_empty(), "{response:?}");

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_removed_file_is_forgotten_and_its_includers_are_read_again() {
        let files = MemoryFiles::new()
            .with_file("/p/widget.h", "struct Widget { int size; };\n")
            .with_file(
                "/p/main.cpp",
                "#include \"widget.h\"\nvoid f() { Widget w; }\n",
            );
        let (mut store, root) = memory_store("removed", &files);

        for path in ["/p/widget.h", "/p/main.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }
        assert!(matches!(
            store.index().definition("Widget", Path::new("/p/main.cpp")),
            crate::Known::Yes(_)
        ));

        let mut events = batch("/p");
        events.push(FileEvent::removed("/p/widget.h"));
        let response = store.respond(&events);

        assert_eq!(names(&response.forgotten), ["/p/widget.h"]);
        assert_eq!(
            names(&response.reindex),
            ["/p/main.cpp"],
            "the summary of the file that resolved to it records a search that would now fail"
        );
        assert!(
            matches!(
                store.index().definition("Widget", Path::new("/p/main.cpp")),
                crate::Known::Unknown(_)
            ),
            "and the declaration in the deleted file stops being found before anything is re-read"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_removed_file_nothing_included_is_only_forgotten() {
        let files = MemoryFiles::new().with_file("/p/alone.cpp", "int x;\n");
        let (mut store, root) = memory_store("removed-alone", &files);
        store
            .get(Path::new("/p/alone.cpp"))
            .expect("the file reads");

        let mut events = batch("/p");
        events.push(FileEvent::removed("/p/alone.cpp"));
        let response = store.respond(&events);

        assert_eq!(names(&response.forgotten), ["/p/alone.cpp"]);
        assert!(response.reindex.is_empty(), "nothing resolved to it");

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_created_file_that_nothing_searches_for_changes_nothing() {
        // The rule is exact rather than "any creation rebuilds the project": the created path has to be one a
        // file's include could have been reaching for, which is a question about spellings and search directories.
        let files = MemoryFiles::new()
            .with_file("/p/main.cpp", "#include \"generated.h\"\n")
            .with_file("/p/other.cpp", "#include \"other.h\"\n");
        let (mut store, root) = memory_store("created-irrelevant", &files);

        for path in ["/p/main.cpp", "/p/other.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }

        let mut events = batch("/p");
        events.push(FileEvent::created("/p/unrelated.h"));
        events.push(FileEvent::created("/p/deep/nested/generated.h"));
        let response = store.respond(&events);

        assert!(
            response.is_empty(),
            "neither creation is a candidate any include was searching: {response:?}"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_created_file_repairs_the_include_that_was_waiting_for_it() {
        let files = MemoryFiles::new()
            .with_file(
                "/p/main.cpp",
                "#include \"local/generated.h\"\nvoid f() { }\n",
            )
            .with_file("/p/local/other.h", "int other;\n");
        let (mut store, root) = memory_store("created-relevant", &files);

        store.get(Path::new("/p/main.cpp")).expect("the file reads");
        assert_eq!(
            store.stats().unstored,
            1,
            "the include did not resolve, so the summary was not stored"
        );

        let mut events = batch("/p");
        events.push(FileEvent::created("/p/local/generated.h"));
        let response = store.respond(&events);

        assert_eq!(
            names(&response.reindex),
            ["/p/main.cpp"],
            "the file that wrote that include is the file whose summary is wrong"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_created_file_that_outranks_the_current_resolution_wins() {
        // The hole a key computed from the text cannot see: `#include "x.h"` resolved to a configured directory
        // until somebody created the one beside the file, and the resolver's own ranking is what says the local
        // one is now earlier.
        let files = MemoryFiles::new()
            .with_file("/inc/x.h", "struct FromInclude { int x; };\n")
            .with_file("/p/a.cpp", "#include \"x.h\"\nvoid f() { }\n");
        let config = CompilerConfig::default().with_include_path("/inc");
        let root = std::env::temp_dir()
            .join("cppls-watch-tests")
            .join("outranks");
        let _ = std::fs::remove_dir_all(&root);

        let mut store = SummaryStore::with_provider(&root, config, files.clone());
        let held = store
            .get(Path::new("/p/a.cpp"))
            .expect("the file reads")
            .clone();
        assert_eq!(
            held.includes[0].resolved.as_deref(),
            Some(Path::new("/inc/x.h"))
        );

        let mut events = batch("/p");
        events.push(FileEvent::created("/p/x.h"));
        let response = store.respond(&events);
        assert_eq!(names(&response.reindex), ["/p/a.cpp"]);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_created_file_that_loses_to_the_current_resolution_changes_nothing() {
        // The other half of the ranking, and the one that keeps the rule from rebuilding a project on every file
        // creation: a file created in a directory that is searched *later* than the one that resolved cannot take
        // over.
        let files = MemoryFiles::new()
            .with_file("/first/x.h", "struct First { int x; };\n")
            .with_file("/p/a.cpp", "#include \"x.h\"\nvoid f() { }\n");
        let config = CompilerConfig::default()
            .with_include_path("/first")
            .with_include_path("/second");
        let root = std::env::temp_dir().join("cppls-watch-tests").join("loses");
        let _ = std::fs::remove_dir_all(&root);

        let mut store = SummaryStore::with_provider(&root, config, files.clone());
        store.get(Path::new("/p/a.cpp")).expect("the file reads");

        let mut events = batch("/p");
        events.push(FileEvent::created("/second/x.h"));
        let response = store.respond(&events);

        assert!(
            response.is_empty(),
            "the search already stopped at /first: {response:?}"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_file_named_by_two_rules_at_once_is_read_once() {
        // `main.cpp` writes one include that resolved and one that did not. Removing the first names `main.cpp` as
        // an includer; creating the second names it as a file whose search would now be answered. Two rules, one
        // file, one entry.
        let files = MemoryFiles::new()
            .with_file("/p/a.h", "int a;\n")
            .with_file("/p/main.cpp", "#include \"a.h\"\n#include \"b.h\"\n");
        let (mut store, root) = memory_store("dedupe", &files);

        store.get(Path::new("/p/a.h")).expect("the file reads");
        store.get(Path::new("/p/main.cpp")).expect("the file reads");

        let mut events = batch("/p");
        events.push(FileEvent::removed("/p/a.h"));
        events.push(FileEvent::created("/p/b.h"));
        let response = store.respond(&events);

        assert_eq!(names(&response.reindex), ["/p/main.cpp"]);
        assert_eq!(names(&response.forgotten), ["/p/a.h"]);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_file_that_is_gone_is_not_read_again() {
        // A header and one of the files that included it, both deleted. The removal of the header names the
        // includer, and the includer is not there: a step that could only come back `Missing` is not work.
        let files = MemoryFiles::new()
            .with_file("/p/widget.h", "struct Widget { int size; };\n")
            .with_file("/p/main.cpp", "#include \"widget.h\"\n");
        let (mut store, root) = memory_store("gone-includer", &files);

        for path in ["/p/widget.h", "/p/main.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }

        let mut events = batch("/p");
        events.push(FileEvent::removed("/p/widget.h"));
        events.push(FileEvent::removed("/p/main.cpp"));
        let response = store.respond(&events);

        assert_eq!(names(&response.forgotten), ["/p/widget.h", "/p/main.cpp"]);
        assert!(response.reindex.is_empty(), "{response:?}");

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn an_angle_include_does_not_search_the_files_own_directory() {
        // The repair rule follows the resolver, including the rule that makes the two spellings different: an angle
        // include does not look beside the file, so a header created there does *not* answer it. Getting this wrong
        // would rebuild a file on every creation in its directory for nothing.
        let files = MemoryFiles::new()
            .with_file("/p/main.cpp", "#include <generated.h>\n")
            .with_file("/p/other.cpp", "#include \"generated.h\"\n");
        let (mut store, root) = memory_store("angle-form", &files);

        for path in ["/p/main.cpp", "/p/other.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }

        let mut events = batch("/p");
        events.push(FileEvent::created("/p/generated.h"));
        let response = store.respond(&events);

        assert_eq!(
            names(&response.reindex),
            ["/p/other.cpp"],
            "only the quoted include was looking beside itself"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_batch_of_creations_is_answered_in_one_pass() {
        // The scan runs once over the index for the whole set of created paths, so a file waiting on the *first* of
        // them is found as reliably as one waiting on the last — and a branch switch, which arrives as a batch of
        // creations, does not cost one scan per created path.
        let files = MemoryFiles::new()
            .with_file("/p/first.cpp", "#include \"one.h\"\n")
            .with_file("/p/last.cpp", "#include \"two.h\"\n")
            .with_file("/p/quiet.cpp", "#include \"three.h\"\n");
        let (mut store, root) = memory_store("batch-of-creations", &files);

        for path in ["/p/first.cpp", "/p/last.cpp", "/p/quiet.cpp"] {
            store.get(Path::new(path)).expect("the file reads");
        }

        let mut events = batch("/p");
        events.push(FileEvent::created("/p/one.h"));
        for index in 0..200 {
            events.push(FileEvent::created(format!("/p/unrelated{index}.h")));
        }
        events.push(FileEvent::created("/p/two.h"));
        let response = store.respond(&events);

        assert_eq!(
            names(&response.reindex),
            ["/p/first.cpp", "/p/last.cpp"],
            "both ends of the batch are considered, and three.h is still missing"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_configuration_change_is_everything() {
        let files = MemoryFiles::new().with_file("/p/a.cpp", "int x;\n");
        let (mut store, root) = memory_store("config-change", &files);
        store.get(Path::new("/p/a.cpp")).expect("the file reads");

        let mut events = batch("/p");
        events.push(FileEvent::modified("/p/compile_commands.json"));
        events.push(FileEvent::modified("/p/a.cpp"));
        let response = store.respond(&events);

        assert!(response.everything);
        assert!(
            response.reindex.is_empty(),
            "the caller rebuilds from its file list: the paths in this batch are not the interesting set"
        );

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_batch_too_large_to_ask_about_one_path_at_a_time_is_everything() {
        // A branch switch arrives as thousands of events. Asking the project for its file list is then cheaper
        // than thousands of individual lookups — and no less correct, since the files that did not change are
        // cache hits.
        let files = MemoryFiles::new();
        let (mut store, root) = memory_store("overwhelming", &files);

        let mut events = batch("/p");
        for index in 0..super::OVERWHELMING_BATCH + 1 {
            events.push(FileEvent::modified(format!("/p/file{index}.cpp")));
        }
        let response = store.respond(&events);

        assert!(response.everything);

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_checkout_inside_git_does_nothing_at_all() {
        let files = MemoryFiles::new();
        let (mut store, root) = memory_store("git-churn", &files);

        let mut events = batch("/p");
        for path in [
            "/p/.git/index.lock",
            "/p/.git/HEAD",
            "/p/.git/objects/ab/cdef0123",
            "/p/.cppls/summaries/ab/abcdef.bin",
        ] {
            events.push(FileEvent::modified(path));
        }
        let response = store.respond(&events);

        assert!(events.is_empty(), "the batch kept nothing");
        assert!(response.is_empty(), "{response:?}");

        let _ = std::fs::remove_dir_all(&root);
    }

    // -------------------------------------------------------------------------------------------
    // Responding, with files on disk
    //
    // A watcher's central case is a file *appearing*, and `MemoryFiles` borrows immutably — it cannot express
    // that. So these run against the real provider on a real (temporary) project, which is also the provider the
    // watcher will actually drive.
    // -------------------------------------------------------------------------------------------

    /// A project on disk, removed when the test ends — including when it fails.
    struct Project {
        root: PathBuf,
    }

    impl Project {
        fn new(name: &str) -> Self {
            let root = std::env::temp_dir().join("cppls-watch-disk").join(name);
            let _ = std::fs::remove_dir_all(&root);
            std::fs::create_dir_all(&root).expect("the fixture directory");

            Project { root }
        }

        fn path(&self, name: &str) -> PathBuf {
            self.root.join(name)
        }

        fn write(&self, name: &str, text: &str) -> PathBuf {
            let path = self.path(name);
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent).expect("the fixture directory");
            }
            std::fs::write(&path, text).expect("the fixture writes");
            path
        }

        fn remove(&self, name: &str) {
            std::fs::remove_file(self.path(name)).expect("the fixture is removed");
        }

        fn store(&self, config: CompilerConfig) -> SummaryStore<DiskFiles> {
            SummaryStore::open(&self.root, config)
        }

        /// The batch a caller would build from this directory's events.
        fn batch(&self) -> ChangeBatch {
            ChangeBatch::new(WatchFilter::new(&self.root))
        }
    }

    impl Drop for Project {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.root);
        }
    }

    /// Do the work a response asks for, the way a caller would: the named files are the seeds of a work list, and
    /// the work list follows their includes from there.
    fn work(store: &mut SummaryStore<DiskFiles>, seeds: &[PathBuf]) {
        let mut list = store.worklist(seeds.to_vec(), Vec::new());
        while list.step().is_some() {}
    }

    #[test]
    fn a_header_that_appears_is_found_by_the_file_that_was_waiting_for_it() {
        // The end-to-end shape of the whole module: a file that says `#include "generated.h"` while there is no
        // such file, then the file is written, and after the response the declaration in it is reachable.
        let project = Project::new("appears");
        let main = project.write("main.cpp", "#include \"generated.h\"\nvoid f() { }\n");

        let mut store = project.store(CompilerConfig::default());
        let held = store.get(&main).expect("the file reads").clone();
        assert_eq!(held.includes[0].resolved, None);
        assert_eq!(
            store.stats().unstored,
            1,
            "and the summary was deliberately not stored, which is what makes this recoverable"
        );

        let generated = project.write("generated.h", "struct Generated { int x; };\n");

        let mut events = project.batch();
        events.push(FileEvent::created(&generated));
        let response = store.respond(&events);
        assert_eq!(names(&response.reindex), [shown(&main)]);

        // The work the response implies, done the way the three layers compose: the response names the files whose
        // summaries are wrong, and a work list started from them follows the includes — which is how the header
        // that just appeared gets a summary of its own.
        work(&mut store, &response.reindex);

        let rebuilt = store.index().summary(&main).expect("held").clone();
        assert_eq!(
            rebuilt.includes[0].resolved.as_deref(),
            Some(Path::new(&shown(&generated))),
            "the include resolves now, and the summary says where to"
        );
        assert!(
            store.index().summary(&generated).is_some(),
            "and the file it resolves to was indexed by following the include"
        );
        assert!(
            matches!(
                store.index().definition("Generated", &main),
                crate::Known::Yes(_)
            ),
            "so the declaration in it is reachable"
        );
    }

    #[test]
    fn a_header_that_is_deleted_stops_being_found() {
        let project = Project::new("deleted");
        project.write("widget.h", "struct Widget { int size; };\n");
        let main = project.write(
            "main.cpp",
            "#include \"widget.h\"\nvoid f() { Widget w; }\n",
        );

        let mut store = project.store(CompilerConfig::default());
        store
            .get(&project.path("widget.h"))
            .expect("the file reads");
        store.get(&main).expect("the file reads");
        assert!(matches!(
            store.index().definition("Widget", &main),
            crate::Known::Yes(_)
        ));

        project.remove("widget.h");

        let mut events = project.batch();
        events.push(FileEvent::removed(project.path("widget.h")));
        let response = store.respond(&events);

        assert_eq!(
            names(&response.forgotten),
            [shown(&project.path("widget.h"))]
        );
        assert_eq!(names(&response.reindex), [shown(&main)]);

        work(&mut store, &response.reindex);

        let rebuilt = store.index().summary(&main).expect("held").clone();
        assert_eq!(
            rebuilt.includes[0].resolved, None,
            "the search fails now, and the summary records the attempt rather than the old answer"
        );
        assert!(
            matches!(
                store.index().definition("Widget", &main),
                crate::Known::Unknown(_)
            ),
            "so the declaration is no longer found"
        );
    }

    #[test]
    fn a_file_renamed_without_being_changed_is_a_cache_hit() {
        // What a content-addressed key buys a rename: nothing has to be re-parsed, because the text is the text.
        //
        // The test also pins a **boundary**: the response does not name the new path, and that is deliberate. The
        // watcher never adds a file to the index except when an include proves it belongs there — a renamed
        // translation unit is the file list's business, and a renamed header is its includers' business (they
        // re-resolve and pull it in by name). Whoever asks, the cache is what makes asking free.
        let project = Project::new("rename");
        let before = project.write("old_name.h", "struct Renamed { int x; };\n");

        let mut store = project.store(CompilerConfig::default());
        store.get(&before).expect("the file reads");
        assert_eq!(store.stats().rebuilt, 1);

        let after = project.write("new_name.h", "struct Renamed { int x; };\n");
        project.remove("old_name.h");

        let mut events = project.batch();
        events.push(FileEvent::removed(&before));
        events.push(FileEvent::created(&after));
        let response = store.respond(&events);

        assert_eq!(names(&response.forgotten), [shown(&before)]);
        assert!(
            response.reindex.is_empty(),
            "nothing in the index names the new path, so nothing is read: {response:?}"
        );

        // The file list catches up, asks about the new path, and pays nothing for it — same text, same
        // directory, therefore the same key as the file that was just forgotten.
        store.get(&after).expect("the file reads");

        assert_eq!(
            store.stats().rebuilt,
            1,
            "the same text under a new name was found, not parsed again"
        );
        assert_eq!(store.stats().reused, 1);
        assert!(
            matches!(
                store.index().definition("Renamed", &after),
                crate::Known::Yes(_)
            ),
            "and the renamed file answers for itself"
        );
    }
}