odoo-lsp 0.6.2

Language server for Odoo Python/JS/XML
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
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
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
//! The main indexer for all language items, including [`Record`]s, QWeb [`Template`]s, and Owl [`Component`]s

use mini_moka::sync::Cache;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::sync::atomic::Ordering::Relaxed;

use dashmap::DashMap;
use derive_more::Deref;
use globwalk::FileType;
use ignore::Match;
use ignore::gitignore::Gitignore;
use lasso::ThreadedRodeo;
use smart_default::SmartDefault;
use tower_lsp_server::{Client, ls_types::*};
use xmlparser::{Token, Tokenizer};

use crate::prelude::*;

pub use crate::component::{Component, ComponentName};
use crate::model::{Model, ModelIndex, ModelLocation, ModelType};
use crate::record::{Record, RecordMetadata};
use crate::template::{NewTemplate, gather_templates};
pub use crate::template::{Template, TemplateName};

mod js;
mod module;
mod record;
pub(crate) mod symbol;
mod template;

pub use js::JsQuery;
pub use module::ModuleEntry;
pub use record::{RecordId, SymbolMap, SymbolSet};
pub use symbol::{_G, _I, _P, _R, PathSymbol, Symbol};
pub use template::TemplateIndex;

pub type Interner = Wrapper<ThreadedRodeo>;

#[derive(Default, Deref)]
#[repr(transparent)]
pub struct Wrapper<T>(#[deref] T);

impl Interner {
	fn get_counter(&self) -> &'static DashMap<Spur, u32> {
		static COUNTER: std::sync::OnceLock<DashMap<Spur, u32>> = std::sync::OnceLock::new();
		COUNTER.get_or_init(|| DashMap::with_shard_amount(4))
	}
	pub fn get_or_intern<T: AsRef<str>>(&self, value: T) -> Spur {
		let out = self.0.get_or_intern(value);
		*self.get_counter().entry(out).or_default().value_mut() += 1;
		out
	}
	pub fn get_or_intern_path(&self, path: &Path) -> Spur {
		self.get_or_intern(path.to_string_lossy())
	}
}

#[derive(SmartDefault)]
pub struct Index {
	/// root -> module key -> module's relpath to root
	#[default(_code = "DashMap::with_shard_amount(4)")]
	pub roots: DashMap<PathBuf, HashMap<Symbol<ModuleEntry>, ModuleEntry>>,
	pub records: record::RecordIndex,
	pub templates: template::TemplateIndex,
	pub models: ModelIndex,
	pub components: js::ComponentIndex,
	#[default(_code = "DashMap::with_shard_amount(4)")]
	pub widgets: DashMap<ImStr, MinLoc>,
	#[default(_code = "DashMap::with_shard_amount(4)")]
	pub actions: DashMap<ImStr, MinLoc>,
	/// Tracks auto_install modules that couldn't be loaded due to missing dependencies
	/// Maps module_name -> list of missing dependencies with their dependency chains
	#[default(_code = "DashMap::with_shard_amount(4)")]
	pub unloaded_auto_install: DashMap<ModuleName, Vec<(ModuleName, Vec<ModuleName>)>>,
	/// Cache for transitive dependencies to avoid recalculation
	#[default(_code = "DashMap::with_shard_amount(4)")]
	pub(crate) transitive_deps_cache: DashMap<ModuleName, HashSet<ModuleName>>,
	#[default(_code = "Cache::new(16)")]
	pub(crate) ast_cache: Cache<PathBuf, Arc<AstCacheItem>>,
}

pub struct AstCacheItem {
	pub tree: tree_sitter::Tree,
	pub rope: Rope,
}

pub type ModuleName = Symbol<ModuleEntry>;

enum Output {
	// Nothing,
	Xml {
		records: Vec<Record>,
		metadata: Vec<Option<RecordMetadata>>,
		templates: Vec<NewTemplate>,
	},
	Models {
		path: PathSymbol,
		models: Vec<Model>,
	},
	JsItems {
		components: HashMap<ComponentName, Component>,
		widgets: Vec<(ImStr, MinLoc)>,
		actions: Vec<(ImStr, MinLoc)>,
	},
}

impl Index {
	pub fn delete_marked_entries(&self) {
		self.records.retain(|_, record| !record.deleted);
		self.models.retain(|_, model| !model.deleted);

		for mut model in self.models.iter_mut() {
			let before = model.ancestors.len();
			model.ancestors.retain(|key| self.models.contains_key(key));
			if model.ancestors.len() != before {
				model.fields = None;
				model.methods = None;
			}
		}
	}
	#[tracing::instrument(skip_all, fields(root=format!("{}", root.display())), ret)]
	pub async fn add_root(&self, root: &Path, client: Option<Client>) -> anyhow::Result<()> {
		if self.roots.contains_key(root) {
			return Ok(());
		}

		let manifests = ok!(
			globwalk::glob_builder(root.join("**/__manifest__.py").to_string_lossy())
				.file_type(FileType::FILE | FileType::SYMLINK)
				.follow_links(true)
				.build(),
			"Could not glob into {:?}",
			root
		);
		let mut gitignore = ignore::gitignore::GitignoreBuilder::new(root);
		gitignore
			.add(".gitignore")
			.inspect(|err| warn!("error adding {root:?}/.gitignore: {err:?}"));
		let gitignore = gitignore
			.build()
			.inspect_err(|err| warn!("gitignore error for {root:?}: {err:?}"))
			.ok();

		for manifest in manifests {
			let Ok(manifest) = manifest else { continue };
			let Some(module_dir) = manifest.path().parent() else {
				continue;
			};
			fn matched_top_to_bottom(gitignore: &Gitignore, path: &Path) -> bool {
				let ancestors = path.ancestors().collect::<Vec<_>>();
				for ancestor in ancestors.into_iter().rev() {
					if let Match::Ignore(_) = gitignore.matched(ancestor, true) {
						return true;
					}
				}
				false
			}
			if let Some(true) = gitignore
				.as_ref()
				.map(|g| matched_top_to_bottom(g, module_dir.strip_prefix(root).unwrap()))
			{
				continue;
			}
			let module_name = module_dir.file_name().unwrap().to_string_lossy().to_string();
			let module_key = _I(&module_name);
			let module_path = ok!(
				module_dir.strip_prefix(root),
				"module_dir={:?} is not a subpath of root={:?}",
				module_dir,
				root
			);
			match self.roots.entry(root.into()).or_default().entry(module_key.into()) {
				std::collections::hash_map::Entry::Vacant(entry) => {
					let manifest_info = parse_manifest_info(manifest.path())
						.inspect_err(|err| {
							warn!(
								"could not parse manifest for {}: {}",
								module_path.file_name().unwrap().display(),
								err
							)
						})
						.unwrap_or(ManifestInfo {
							dependencies: Box::new([]),
							auto_install: false,
						});
					let module = ModuleEntry {
						path: module_path.to_str().expect("non-utf8 path").into(),
						dependencies: manifest_info.dependencies,
						auto_install: manifest_info.auto_install,
						loaded: Default::default(),
						loaded_dependents: Default::default(),
					};
					entry.insert(module);
				}
				std::collections::hash_map::Entry::Occupied(preexisting) => {
					if let (Some(client), "base") = (client.clone(), module_name.as_str()) {
						let duplicate_path = module_path.to_str().expect("non-utf8 path");
						tokio::spawn(Self::notify_duplicate_base(
							client,
							preexisting.get().path.clone(),
							duplicate_path.into(),
						));
					}
				}
			}
		}

		// Add implicit base dependencies to all modules
		self.add_implicit_base_dependencies();

		// Clear transitive dependencies cache since modules have been added/modified
		self.transitive_deps_cache.clear();

		// After adding all modules in the root, check for auto_install modules with unsatisfied dependencies
		// We pass an empty set because no modules are loaded yet, this will identify all auto_install
		// modules and track those with missing dependencies
		debug!("Checking for auto_install modules after adding root");
		let auto_install_check = self.find_auto_install_modules(&HashSet::new());
		debug!("Found {} auto_install modules to check", auto_install_check.len());

		Ok(())
	}
	#[instrument(skip(self), ret)]
	pub async fn add_root_for_file(&self, path: &Path) {
		if self.find_module_of(path).is_some() {
			return;
		}

		debug!("oob: {}", path.display());
		let mut path = Some(path);
		while let Some(path_) = path {
			if tokio::fs::try_exists(path_.with_file_name("__manifest__.py"))
				.await
				.unwrap_or(false)
				&& let Some(file_path) = path_.parent().and_then(|p| p.parent())
			{
				_ = self
					.add_root(file_path, None)
					.await
					.inspect_err(|err| warn!("failed to add root {}:\n{err}", file_path.display()));
				return;
			}
			path = path_.parent();
		}
	}
	#[instrument(skip_all, ret, fields(path = path.display().to_string()))]
	pub(crate) async fn load_modules_for_document(&self, path: &Path) -> Option<()> {
		let module_name = self.find_module_of(path)?;

		self.load_module(module_name).await
	}
	/// Resolve all transitive dependencies for a module
	pub fn resolve_transitive_dependencies(&self, module: ModuleName) -> HashSet<ModuleName> {
		// Check cache first
		if let Some(cached) = self.transitive_deps_cache.get(&module) {
			return cached.clone();
		}

		let mut dependencies = HashSet::new();
		let mut stack = vec![module];

		while let Some(current) = stack.pop() {
			// If we've already processed this module, skip it (handles circular deps)
			if !dependencies.insert(current) {
				continue;
			}

			// Find the module in any root
			for root in self.roots.iter() {
				if let Some(module_entry) = root.get(&current) {
					// Add all direct dependencies to the stack
					for &dep in module_entry.dependencies.iter() {
						if !dependencies.contains(&dep) {
							stack.push(dep);
						}
					}
					break;
				}
			}
		}

		// Cache the result
		self.transitive_deps_cache.insert(module, dependencies.clone());

		dependencies
	}

	/// Find the dependency chain from source module to target module
	/// Returns None if no path exists, or Some(path) where path includes both source and target
	fn find_dependency_chain(&self, source: ModuleName, target: ModuleName) -> Option<Vec<ModuleName>> {
		let mut visited = HashSet::new();
		let mut stack = vec![(source, vec![source])];

		while let Some((current, path)) = stack.pop() {
			if current == target {
				return Some(path);
			}

			if !visited.insert(current) {
				continue;
			}

			// Find the module in any root
			for root in self.roots.iter() {
				if let Some(module_entry) = root.get(&current) {
					for &dep in module_entry.dependencies.iter() {
						if !visited.contains(&dep) {
							let mut new_path = path.clone();
							new_path.push(dep);
							stack.push((dep, new_path));
						}
					}
					break;
				}
			}
		}

		None
	}
	/// Get all modules that are physically present in the filesystem
	pub fn get_all_available_modules(&self) -> HashSet<ModuleName> {
		let mut modules = HashSet::new();

		for root in self.roots.iter() {
			for (&module_key, _) in root.iter() {
				modules.insert(module_key);
			}
		}

		modules
	}

	/// Add implicit base dependency to all modules except base itself
	fn add_implicit_base_dependencies(&self) {
		let base_key: ModuleName = _I("base").into();

		for mut root in self.roots.iter_mut() {
			let modules = root.value_mut();

			// Only add implicit base dependencies if base module exists
			if !modules.contains_key(&base_key) {
				continue;
			}

			// Collect modules that need base dependency
			let mut updates = Vec::new();
			for (&module_key, module_entry) in modules.iter() {
				if module_key != base_key && !module_entry.dependencies.contains(&base_key) {
					let mut deps = module_entry.dependencies.to_vec();
					deps.push(base_key);
					updates.push((module_key, deps));
				}
			}

			// Apply updates
			for (module_key, new_deps) in updates {
				if let Some(entry) = modules.get_mut(&module_key) {
					entry.dependencies = new_deps.into_boxed_slice();
				}
			}
		}
	}

	/// Find all auto_install modules whose dependencies are satisfied by the given set of modules
	fn find_auto_install_modules(&self, loaded_modules: &HashSet<ModuleName>) -> Vec<ModuleName> {
		let mut auto_install_modules = Vec::new();

		// Get all available modules (not just loaded ones)
		let all_available_modules = self.get_all_available_modules();

		for root in self.roots.iter() {
			debug!("Checking root: {:?}", root.key());
			for (&module_key, module_entry) in root.iter() {
				debug!(
					"Checking module: {} auto_install={}",
					_R(module_key),
					module_entry.auto_install
				);
				if module_entry.auto_install && !loaded_modules.contains(&module_key) {
					// Get all transitive dependencies for this module
					let transitive_deps = self.resolve_transitive_dependencies(module_key);

					// Check if all transitive dependencies are available (not necessarily loaded)
					let missing_deps: Vec<ModuleName> = transitive_deps
						.iter()
						.filter(|&dep| *dep != module_key && !all_available_modules.contains(dep))
						.cloned()
						.collect();

					if missing_deps.is_empty() {
						// Now check if all transitive dependencies are loaded
						let unloaded_deps: Vec<ModuleName> = transitive_deps
							.iter()
							.filter(|&dep| *dep != module_key && !loaded_modules.contains(dep))
							.cloned()
							.collect();

						if unloaded_deps.is_empty() {
							debug!(
								"Module {} is auto_install and all transitive dependencies are loaded",
								_R(module_key)
							);
							auto_install_modules.push(module_key);
						} else {
							debug!(
								"Module {} is auto_install but not all transitive dependencies are loaded. Unloaded: {:?}",
								_R(module_key),
								unloaded_deps
							);
						}
					} else {
						debug!(
							"Module {} is auto_install but some transitive dependencies are not available. Missing: {:?}",
							_R(module_key),
							missing_deps
						);
						// Track this module as unloaded with its missing dependencies and their chains
						let missing_with_chains: Vec<(ModuleName, Vec<ModuleName>)> = missing_deps
							.into_iter()
							.map(|missing_dep| {
								let chain = self
									.find_dependency_chain(module_key, missing_dep)
									.unwrap_or_else(|| vec![module_key, missing_dep]);
								(missing_dep, chain)
							})
							.collect();
						self.unloaded_auto_install.insert(module_key, missing_with_chains);
					}
				}
			}
		}

		auto_install_modules
	}

	/// Check if there are any unloaded auto_install modules that might extend a model
	/// Returns the first unloaded auto_install module with its missing dependencies and chains
	pub fn get_unloaded_auto_install_for_model(
		&self,
		model_name: &str,
	) -> Option<(ModuleName, Vec<(ModuleName, Vec<ModuleName>)>)> {
		// Check each unloaded auto_install module to see if it extends the given model
		for entry in self.unloaded_auto_install.iter() {
			let module_name = *entry.key();

			// Find the module's root and path
			let Some(root_path) = self.find_root_from_module(module_name) else {
				continue;
			};

			let Some(root) = self.roots.get(&root_path) else {
				continue;
			};

			let Some(module_entry) = root.get(&module_name) else {
				continue;
			};

			// Check if this module extends the given model by scanning its Python files
			let module_path = root_path.join(module_entry.path.as_str());
			if self.check_module_extends_model(&module_path, model_name) {
				return Some((module_name, entry.value().clone()));
			}
		}
		None
	}

	/// Check if a module extends a specific model by scanning its Python files
	fn check_module_extends_model(&self, module_path: &Path, target_model: &str) -> bool {
		// Look for Python files in the module
		let models_path = module_path.join("models");
		let py_files = if models_path.exists() {
			// Check models directory
			std::fs::read_dir(&models_path).ok()
		} else {
			// Check module root
			std::fs::read_dir(module_path).ok()
		};

		let Some(entries) = py_files else {
			return false;
		};

		for entry in entries.flatten() {
			let path = entry.path();
			if path.extension().and_then(|s| s.to_str()) != Some("py") {
				continue;
			}

			// Read and parse the Python file
			let Ok(contents) = std::fs::read(&path) else {
				continue;
			};

			// Parse models from the file
			let Ok(models) = index_models(&contents) else {
				continue;
			};

			// Check if any model inherits from the target model
			for model in models {
				match &model.type_ {
					ModelType::Base { ancestors, .. } => {
						if ancestors.iter().any(|ancestor| ancestor.as_str() == target_model) {
							return true;
						}
					}
					ModelType::Inherit(inherits) => {
						if inherits.iter().any(|inherit| inherit.as_str() == target_model) {
							return true;
						}
					}
				}
			}
		}

		false
	}

	async fn load_module(&self, module_name: ModuleName) -> Option<()> {
		{
			let root = self.find_root_from_module(module_name)?;
			let root = self.roots.get(&root)?;
			let path_module = root.get(&module_name)?;

			if path_module.loaded.load(Relaxed) {
				return None;
			}
		}

		let mut modules = Vec::new();
		let mut roots = Vec::new();
		let mut key_to_idx = HashMap::new();
		let mut visited = std::collections::HashSet::new();

		let mut stack = vec![(module_name, false)];
		while let Some((module_key, deps_done)) = stack.pop() {
			if !deps_done {
				if !visited.insert(module_key) {
					continue;
				}
				// Find the module and push dependencies first
				for root in self.roots.iter() {
					if let Some(module) = root.get(&module_key) {
						stack.push((module_key, true)); // revisit after deps
						for dep in module.dependencies.iter().rev() {
							if !visited.contains(dep) {
								stack.push((*dep, false));
							}
						}
						break;
					}
				}
			} else {
				// All dependencies processed, add to result
				for root in self.roots.iter() {
					if root.contains_key(&module_key) {
						key_to_idx.insert(module_key, modules.len());
						modules.push(module_key);
						roots.push(root);
						break;
					}
				}
			}
		}

		let mut outputs = tokio::task::JoinSet::new();
		for (module_key, root) in modules.into_iter().zip(roots.into_iter()) {
			trace!("{} depends on {}", _R(module_name), _R(module_key));
			let root_display = root.key().to_string_lossy();
			let root_key = _I(&root_display);
			let module = root
				.get(&module_key)
				.expect(format_loc!("module must already be present by now"));
			if module.loaded.compare_exchange(false, true, Relaxed, Relaxed) != Ok(false) {
				continue;
			}

			info!("{} depends on {}", _R(module_name), _R(module_key));
			let module_dir = Path::new(&*root_display).join(&module.path);
			if let Ok(xmls) = globwalk::glob_builder(format!("{}/**/*.xml", module_dir.display()))
				.file_type(FileType::FILE | FileType::SYMLINK)
				.follow_links(true)
				.build()
			{
				for xml in xmls {
					let Ok(xml) = xml else { continue };
					outputs.spawn(add_root_xml(root_key, xml.path().to_path_buf(), module_key));
				}
			}
			if let Ok(pys) = globwalk::glob_builder(format!("{}/**/*.py", module_dir.display()))
				.file_type(FileType::FILE | FileType::SYMLINK)
				.follow_links(true)
				.build()
			{
				for py in pys {
					let py = match py {
						Ok(entry) => entry,
						Err(err) => {
							debug!("{err}");
							continue;
						}
					};
					let path = py.path().to_path_buf();
					outputs.spawn(add_root_py(root_key, path));
				}
			}
			if let Ok(scripts) = globwalk::glob_builder(format!("{}/**/*.js", module_dir.display()))
				.file_type(FileType::FILE | FileType::SYMLINK)
				.follow_links(true)
				.build()
			{
				for js in scripts {
					let Ok(js) = js else { continue };
					let path = js.path().to_path_buf();
					outputs.spawn(js::add_root_js(root_key, path));
				}
			}
		}

		while let Some(res) = outputs.join_next().await {
			let outputs = match res {
				Ok(Ok(out)) => out,
				Ok(Err(err)) => {
					warn!("task failed: {err}");
					continue;
				}
				Err(err) => {
					debug!("join error: {err}");
					continue;
				}
			};
			match outputs {
				Output::Xml {
					records,
					metadata,
					templates,
				} => {
					self.records.append(records.into_iter().zip(metadata.into_iter()));
					self.templates.append(templates);
				}
				Output::Models { path, models } => {
					self.models.append(path, false, &models);
				}
				Output::JsItems {
					components,
					widgets,
					actions,
				} => {
					self.components.extend(components);
					for (widget, loc) in widgets {
						if !self.widgets.contains_key(&widget) {
							self.widgets.insert(widget, loc);
						}
					}
					for (action, loc) in actions {
						if !self.actions.contains_key(&action) {
							self.actions.insert(action, loc);
						}
					}
				}
			}
		}

		// After loading all requested modules, check for auto_install modules
		let mut all_loaded_modules = HashSet::new();
		for root in self.roots.iter() {
			for (&module_key, module_entry) in root.iter() {
				if module_entry.loaded.load(Relaxed) {
					all_loaded_modules.insert(module_key);
				}
			}
		}
		trace!(
			"Currently loaded modules: {:?}",
			all_loaded_modules.iter().map(|m| _R(*m)).collect::<Vec<_>>()
		);

		// Find and load auto_install modules
		let auto_install_candidates = self.find_auto_install_modules(&all_loaded_modules);
		if !auto_install_candidates.is_empty() {
			debug!(
				"Found {} auto_install candidates after loading modules",
				auto_install_candidates.len()
			);
			for auto_module in auto_install_candidates {
				trace!(
					"Auto-installing module {} because all dependencies are satisfied",
					_R(auto_module)
				);
				// Remove from unloaded_auto_install since it's now being loaded
				self.unloaded_auto_install.remove(&auto_module);
				// Recursively load auto_install modules
				Box::pin(self.load_module(auto_module)).await;
			}
		}

		Some(())
	}
	#[instrument(skip_all, fields(module))]
	pub(crate) fn discover_dependents_of(&self, module: ModuleName) -> Vec<ModuleName> {
		let mut dependents = HashMap::<ModuleName, HashSet<ModuleName>>::new();
		let mut all_modules = vec![];
		// First, collect all modules and their direct dependencies
		for root in self.roots.iter() {
			for (&module_key, module_entry) in root.iter() {
				all_modules.push(module_key);
				for dep in &module_entry.dependencies {
					dependents.entry(*dep).or_default().insert(module_key);
				}
			}
		}
		// Compute transitive dependents for each module
		let module_to_idx: HashMap<ModuleName, usize> = all_modules.iter().enumerate().map(|(i, m)| (*m, i)).collect();
		let mut graph = vec![vec![]; all_modules.len()];

		// Build the graph: for each module, add edges to its direct dependents
		for (i, module) in all_modules.iter().enumerate() {
			if let Some(deps) = dependents.get(module) {
				for dep in deps {
					if let Some(&dep_idx) = module_to_idx.get(dep) {
						graph[i].push(dep_idx);
					}
				}
			}
		}

		drop(module_to_idx);

		// DP/memoization for transitive dependents
		let mut memo: Vec<Option<HashSet<usize>>> = vec![None; all_modules.len()];

		fn dfs(idx: usize, graph: &Vec<Vec<usize>>, memo: &mut Vec<Option<HashSet<usize>>>) -> HashSet<usize> {
			debug_assert!(idx < memo.len());
			debug_assert!(idx < graph.len());
			if let Some(cached) = unsafe { memo.get_unchecked(idx) } {
				return cached.clone();
			}
			let mut result = HashSet::new();
			for &dep_idx in unsafe { graph.get_unchecked(idx) } {
				if dep_idx == idx {
					continue;
				}
				result.insert(dep_idx);
				result.extend(dfs(dep_idx, graph, memo));
			}
			memo[idx] = Some(result.clone());
			result
		}

		let idx = all_modules.iter().position(|&m| m == module).unwrap();
		dfs(idx, &graph, &mut memo)
			.into_iter()
			.map(|idx| all_modules[idx])
			.collect()
	}
	/// Inverse of [Index::load_module], useful for requests that work in the context of the larger codebase.
	pub(crate) async fn load_modules_dependent_on(&self, module_name: ModuleName) -> Option<()> {
		let root = self.find_root_from_module(module_name)?;
		{
			let root = self.roots.try_get_mut(&root).try_unwrap()?;
			let module = root.get(&module_name)?;
			if module.loaded_dependents.load(Relaxed) {
				return None;
			}
		}

		let mut tasks = self
			.discover_dependents_of(module_name)
			.into_iter()
			.map(|dep| self.load_module(dep))
			.collect::<FuturesUnordered<_>>();
		if !tasks.is_empty() {
			while tasks.next().await.is_some() {}
		}

		self.roots
			.get(&root)?
			.get(&module_name)?
			.loaded_dependents
			.store(true, Relaxed);
		Some(())
	}
	async fn notify_duplicate_base(client: Client, old_path: ImStr, new_path: PathBuf) {
		let resp = client
			.show_message_request(
				MessageType::WARNING,
				concat!(
					"Duplicate base module found. The language server's performance may be affected.\n",
					"Please configure your workspace (or .odoo_lsp) to only include one version of Odoo as described in the wiki.",
				)
				.to_string(),
				Some(vec![
					MessageActionItem {
						title: "Go to odoo-lsp wiki".to_string(),
						properties: Default::default(),
					},
					MessageActionItem {
						title: "Dismiss".to_string(),
						properties: Default::default(),
					},
					MessageActionItem {
						title: "Show conflicting modules".to_string(),
						properties: Default::default(),
					},
				]),
			)
			.await;
		match resp {
			Ok(Some(resp)) if resp.title == "Go to odoo-lsp wiki" => {
				_ = client
					.show_document(ShowDocumentParams {
						uri: Uri::from_str("https://github.com/Desdaemon/odoo-lsp/wiki#usage").unwrap(),
						external: Some(true),
						take_focus: None,
						selection: None,
					})
					.await;
			}
			Ok(Some(resp)) if resp.title == "Show conflicting modules" => {
				_ = client
					.show_document(ShowDocumentParams {
						uri: Uri::from_file_path(old_path.as_str()).unwrap(),
						external: Some(false),
						take_focus: Some(true),
						selection: None,
					})
					.await;
				_ = client
					.show_document(ShowDocumentParams {
						uri: Uri::from_file_path(&new_path).unwrap(),
						external: Some(false),
						take_focus: Some(false),
						selection: None,
					})
					.await;
			}
			_ => {}
		}
	}
	pub fn remove_root(&self, root: &Path) {
		self.roots.remove(root);
		for mut entry in self.records.iter_mut() {
			let module = _R(entry.module);
			if path_contains(root, module) {
				entry.deleted = true;
			}
		}

		for mut entry in self.models.iter_mut() {
			if let Some(ModelLocation(ref loc, _)) = entry.base
				&& path_contains(root, loc.path.to_path())
			{
				entry.deleted = true;
			}
		}
	}
	/// Has complexity of `O(len(self.roots))`
	pub fn find_module_of(&self, path: &Path) -> Option<ModuleName> {
		use std::path::Component;
		for entry in self.roots.iter() {
			if let Ok(path) = path.strip_prefix(entry.key()) {
				for component in path.components() {
					if let Component::Normal(norm) = component
						&& let Some(module) = _G(norm.to_string_lossy())
						&& entry.value().contains_key(&module)
					{
						return Some(module.into());
					}
				}
			}
		}

		None
	}

	#[tracing::instrument(skip_all, ret)]
	pub fn find_root_of(&self, path: &Path) -> Option<PathBuf> {
		for root_ in self.roots.iter() {
			if path.starts_with(root_.key()) {
				return Some(root_.key().to_owned());
			}
		}
		None
	}

	pub fn find_root_from_module(&self, key: ModuleName) -> Option<PathBuf> {
		for root in self.roots.iter() {
			if root.contains_key(&key) {
				return Some(root.key().to_owned());
			}
		}

		None
	}

	/// Resolves a Python module path like "odoo.addons.some_module.controllers.main" to its file path
	pub fn resolve_py_module(&self, module_path: &str) -> Option<PathBuf> {
		use tracing::debug;

		debug!("Resolving module path: {}", module_path);
		let parts: Vec<&str> = module_path.split('.').map(|s| s.trim()).collect();
		debug!("Module parts: {:?}", parts);

		// Handle odoo.addons.module_name.* pattern
		if parts.len() >= 3 && parts[0] == "odoo" && parts[1] == "addons" {
			let module_name = parts[2];
			let module_key = _G(module_name)?;
			debug!("Looking for module: {}", module_name);

			// Find the root and module entry
			for root_entry in self.roots.iter() {
				debug!("Checking root: {}", root_entry.key().display());
				if let Some(module_entry) = root_entry.get(&module_key) {
					let root_path = root_entry.key();
					let module_dir = root_path.join(&module_entry.path);
					debug!("Found module at: {}", module_dir.display());

					// Build the file path from remaining parts
					if parts.len() > 3 {
						let mut file_path = module_dir;
						for part in &parts[3..] {
							file_path = file_path.join(part);
						}
						file_path.set_extension("py");
						debug!("Checking file path: {}", file_path.display());

						if file_path.exists() {
							debug!("Found file: {}", file_path.display());
							return Some(file_path);
						} else {
							debug!("File does not exist: {}", file_path.display());
						}
					} else {
						// Just the module itself - look for __init__.py
						let init_path = module_dir.join("__init__.py");
						debug!("Checking init path: {}", init_path.display());
						if init_path.exists() {
							debug!("Found init file: {}", init_path.display());
							return Some(init_path);
						}
					}
				}
			}
		}

		debug!("Module resolution failed for: {}", module_path);
		None
	}
}

#[derive(Debug)]
struct ManifestInfo {
	dependencies: Box<[Symbol<ModuleEntry>]>,
	auto_install: bool,
}

fn parse_manifest_info(manifest: &Path) -> anyhow::Result<ManifestInfo> {
	query! {
		ManifestQuery(DependsList, AutoInstall);

	((dictionary
		(pair
			(string (string_content) @_depends)
			(list) @DEPENDS_LIST
		)
	) (#eq? @_depends "depends"))

	((dictionary
		(pair
			(string (string_content) @_auto_install)
			[(true) (false) (list)] @AUTO_INSTALL
		)
	) (#eq? @_auto_install "auto_install"))
	}

	let contents = test_utils::fs::read_to_string(manifest)?;
	let mut parser = Parser::new();
	parser.set_language(&tree_sitter_python::LANGUAGE.into())?;
	let ast = ok!(parser.parse(&contents, None));

	let mut cursor = QueryCursor::new();
	let root = ast.root_node();
	let mut deps = vec![];
	let mut auto_install = false;

	let mut captures = cursor.captures(ManifestQuery::query(), root, contents.as_bytes());
	while let Some((match_, idx)) = captures.next() {
		let capture = match_.captures[*idx];
		match ManifestQuery::from(capture.index) {
			Some(ManifestQuery::DependsList) => {
				let list_node = capture.node;
				let mut child_cursor = list_node.walk();
				for child in list_node.children(&mut child_cursor) {
					if child.kind() == "string" {
						let dep = &contents[child.byte_range().shrink(1)];
						deps.push(_I(dep).into());
					}
				}
			}
			Some(ManifestQuery::AutoInstall) => {
				let node = capture.node;
				auto_install = match node.kind() {
					"true" => true,
					"false" => false,
					_ => false,
				};
			}
			_ => {}
		}
	}

	Ok(ManifestInfo {
		dependencies: deps.into_boxed_slice(),
		auto_install,
	})
}

async fn add_root_xml(root: Spur, path: PathBuf, module_name: ModuleName) -> anyhow::Result<Output> {
	let path_uri = PathSymbol::strip_root(root, &path);
	let file = ok!(tokio::fs::read(&path).await, "Could not read {}", path.display());
	let file = String::from_utf8_lossy(&file);
	let mut reader = Tokenizer::from(file.as_ref());
	let mut records = vec![];
	let mut metadata = vec![];
	let mut templates = vec![];
	let rope = Rope::from_str(&file);
	let rope = rope.slice(..);
	loop {
		match reader.next() {
			Some(Ok(Token::ElementStart { local, span, .. })) => match local.as_str() {
				"record" => {
					if let Some((record, meta)) =
						Record::from_reader(ByteOffset(span.start()), module_name, path_uri, &mut reader, rope)?
					{
						records.push(record);
						metadata.push(meta);
					}
				}
				"template" => {
					if let Some((template, meta)) =
						Record::template(ByteOffset(span.start()), module_name, path_uri, &mut reader, rope)?
					{
						records.push(template);
						metadata.push(meta);
					} else {
						gather_templates(path_uri, &mut reader, rope, &mut templates, true)?;
					}
				}
				"menuitem" => {
					if let Some((menuitem, meta)) =
						Record::menuitem(ByteOffset(span.start()), module_name, path_uri, &mut reader, rope)?
					{
						records.push(menuitem);
						metadata.push(meta);
					}
				}
				"templates" => {
					gather_templates(path_uri, &mut reader, rope, &mut templates, false)?;
				}
				_ => {}
			},
			None => break,
			Some(Err(err)) => {
				debug!("error parsing {}:\n{err}", path.display());
				break;
			}
			_ => {}
		}
	}

	Ok(Output::Xml {
		records,
		metadata,
		templates,
	})
}

#[rustfmt::skip]
query! {
	#[derive(Debug, PartialEq, Eq)]
	ModelQuery(Model, Name);
((class_definition
  (argument_list [
    (identifier) @_Model
    (attribute
      (identifier) @_models (identifier) @_Model) ])
  (block
    (expression_statement
      (assignment (identifier) @NAME)) )) @MODEL
  (#eq? @_models "models")
  (#match? @_Model "^(Transient|Abstract)?Model$")
  (#match? @NAME "^_(name|inherits?)$"))
}

async fn add_root_py(root: Spur, path: PathBuf) -> anyhow::Result<Output> {
	let contents = ok!(tokio::fs::read(&path).await, "Could not read {}", path.display());

	let path = PathSymbol::strip_root(root, &path);
	let models = index_models(&contents)?;
	Ok(Output::Models { path, models })
}

pub fn index_models(contents: &[u8]) -> anyhow::Result<Vec<Model>> {
	let mut parser = tree_sitter::Parser::new();
	parser.set_language(&tree_sitter_python::LANGUAGE.into())?;

	let ast = parser
		.parse(contents, None)
		.ok_or_else(|| anyhow!("{} AST not parsed", loc!()))?;
	let query = ModelQuery::query();
	let mut cursor = QueryCursor::new();

	let mut models = HashMap::new();
	struct NewModel<'a> {
		range: tree_sitter::Range,
		byte_range: ByteRange,
		name: Option<&'a [u8]>,
		inherits: Vec<&'a [u8]>,
	}
	let mut matches = cursor.matches(query, ast.root_node(), contents);
	while let Some(match_) = matches.next() {
		let model_node = match_
			.nodes_for_capture_index(ModelQuery::Model as _)
			.next()
			.ok_or_else(|| errloc!("model_node"))?;
		let capture = match_
			.nodes_for_capture_index(ModelQuery::Name as _)
			.next()
			.ok_or_else(|| errloc!("capture"))?;
		let model = models.entry(model_node.id()).or_insert_with(|| NewModel {
			name: None,
			range: model_node.range(),
			byte_range: model_node.byte_range().map_unit(ByteOffset),
			inherits: vec![],
		});
		match &contents[capture.byte_range()] {
			b"_name" => {
				let Some(name_decl) = python_next_named_sibling(capture) else {
					continue;
				};
				if name_decl.kind() == "string" {
					let name = name_decl.byte_range().shrink(1);
					if name.is_empty() {
						continue;
					}
					model.name = Some(&contents[name]);
				}
			}
			b"_inherit" => {
				let Some(inherit_decl) = python_next_named_sibling(capture) else {
					continue;
				};
				match inherit_decl.kind() {
					"string" => {
						let inherit = inherit_decl.byte_range().shrink(1);
						if !inherit.is_empty() {
							model.inherits.push(&contents[inherit]);
						}
					}
					"list" => {
						for maybe_inherit_decl in inherit_decl.named_children(&mut inherit_decl.walk()) {
							if maybe_inherit_decl.kind() == "string" {
								let inherit = maybe_inherit_decl.byte_range().shrink(1);
								if !inherit.is_empty() {
									model.inherits.push(&contents[inherit]);
								}
							}
						}
					}
					_ => {
						continue;
					}
				}
			}
			b"_inherits" => {
				let Some(inherit_dict) = python_next_named_sibling(capture) else {
					continue;
				};
				if inherit_dict.kind() != "dictionary" {
					continue;
				}
				for pair in inherit_dict.named_children(&mut inherit_dict.walk()) {
					if pair.kind() != "pair" {
						continue;
					}
					let key = pair.named_child(0).expect("key");
					if key.kind() == "string" {
						model.inherits.push(&contents[key.byte_range().shrink(1)]);
					}
				}
			}
			_ => {}
		}
	}

	let models = models.into_values().flat_map(|mut model| {
		let mut has_primary = false;
		if !model.inherits.is_empty() {
			// Rearranges the primary inherit to the first index
			if let Some(base) = &model.name
				&& let Some(position) = model.inherits.iter().position(|inherit| inherit == base)
			{
				model.inherits.swap(position, 0);
				has_primary = true;
			}
		}
		let inherits = model
			.inherits
			.into_iter()
			.map(|inherit| ImStr::from(String::from_utf8_lossy(inherit).as_ref()))
			.collect::<Vec<_>>();
		let range = span_conv(model.range);
		let byte_range = model.byte_range;
		match (inherits.is_empty(), model.name) {
			(false, None) => Some(Model {
				range,
				byte_range,
				// if _name is not defined and _inherit = [A, B, C], A is considered the inherit base by default
				type_: ModelType::Inherit(inherits),
			}),
			(true, None) => None,
			(_, Some(base)) => {
				if has_primary {
					Some(Model {
						range,
						byte_range,
						type_: ModelType::Inherit(inherits),
					})
				} else {
					Some(Model {
						type_: ModelType::Base {
							name: ImStr::from(String::from_utf8_lossy(base).as_ref()),
							ancestors: inherits,
						},
						range,
						byte_range,
					})
				}
			}
		}
	});
	Ok(models.collect())
}

#[cfg(test)]
mod tests {
	use crate::index::{_I, Index, Interner, ModelQuery, ModuleEntry};
	use crate::model::ModelType;
	use crate::utils::acc_vec;
	use pretty_assertions::assert_eq;
	use std::collections::HashMap;
	use std::path::{Path, PathBuf};
	use tree_sitter::{Parser, QueryCursor, StreamingIterator, StreamingIteratorMut};

	use super::{index_models, parse_manifest_info};

	#[test]
	fn test_interner_functionality() {
		let interner = Interner::default();

		// Test get_or_intern with strings
		let spur1 = interner.get_or_intern("test_string");
		let spur2 = interner.get_or_intern("test_string");
		assert_eq!(spur1, spur2, "Same string should return same Spur");

		let spur3 = interner.get_or_intern("different_string");
		assert_ne!(spur1, spur3, "Different strings should return different Spurs");

		// Test get_or_intern_path
		let path = Path::new("/test/path");
		let path_spur1 = interner.get_or_intern_path(path);
		let path_spur2 = interner.get_or_intern_path(path);
		assert_eq!(path_spur1, path_spur2, "Same path should return same Spur");

		// Test that path and string with same content return same Spur
		let string_spur = interner.get_or_intern("/test/path");
		assert_eq!(
			path_spur1, string_spur,
			"Path and equivalent string should return same Spur"
		);
	}

	#[test]
	fn test_find_module_of() {
		let index = Index::default();

		// Setup test data
		let root = PathBuf::from("/test/root");
		let mut modules = HashMap::new();

		let module_entry = ModuleEntry {
			path: "test_module".into(),
			dependencies: Box::new([]),
			auto_install: false,
			loaded: Default::default(),
			loaded_dependents: Default::default(),
		};
		modules.insert(_I("test_module").into(), module_entry);
		index.roots.insert(root.clone(), modules);

		// Test finding module
		let test_path = Path::new("/test/root/test_module/models/test.py");
		let found_module = index.find_module_of(test_path);
		assert!(found_module.is_some(), "Should find module for path within module");

		// Test path outside any module
		let outside_path = Path::new("/other/path/file.py");
		let not_found = index.find_module_of(outside_path);
		assert!(not_found.is_none(), "Should not find module for path outside roots");
	}

	#[test]
	fn test_find_root_of() {
		let index = Index::default();

		// Setup test data
		let root1 = PathBuf::from("/test/root1");
		let root2 = PathBuf::from("/test/root2");
		index.roots.insert(root1.clone(), HashMap::new());
		index.roots.insert(root2.clone(), HashMap::new());

		// Test finding root
		let test_path = Path::new("/test/root1/some/nested/path");
		let found_root = index.find_root_of(test_path);
		assert_eq!(found_root, Some(root1), "Should find correct root");

		// Test path not under any root
		let outside_path = Path::new("/other/path");
		let not_found = index.find_root_of(outside_path);
		assert!(not_found.is_none(), "Should not find root for path outside all roots");
	}

	#[test]
	fn test_find_root_from_module() {
		let index = Index::default();

		// Setup test data
		let root = PathBuf::from("/test/root");
		let mut modules = HashMap::new();
		let module_key = _I("test_module").into();

		let module_entry = ModuleEntry {
			path: "test_module".into(),
			dependencies: Box::new([]),
			auto_install: false,
			loaded: Default::default(),
			loaded_dependents: Default::default(),
		};
		modules.insert(module_key, module_entry);
		index.roots.insert(root.clone(), modules);

		// Test finding root from module
		let found_root = index.find_root_from_module(module_key);
		assert_eq!(found_root, Some(root), "Should find root containing the module");

		// Test non-existent module
		let non_existent = _I("non_existent").into();
		let not_found = index.find_root_from_module(non_existent);
		assert!(not_found.is_none(), "Should not find root for non-existent module");
	}

	#[test]
	fn test_resolve_py_module() {
		let index = Index::default();

		// Setup test data
		let root = PathBuf::from("/test/root");
		let mut modules = HashMap::new();
		let module_key = _I("test_module").into();

		let module_entry = ModuleEntry {
			path: "test_module".into(),
			dependencies: Box::new([]),
			auto_install: false,
			loaded: Default::default(),
			loaded_dependents: Default::default(),
		};
		modules.insert(module_key, module_entry);
		index.roots.insert(root, modules);

		// Test resolving odoo.addons.module_name pattern
		let module_path = "odoo.addons.test_module";
		let resolved = index.resolve_py_module(module_path);
		// Note: This will return None in test because the file doesn't actually exist
		// In a real scenario with actual files, this would resolve to the __init__.py path
		assert!(resolved.is_none(), "Should return None when file doesn't exist");

		// Test resolving with submodule
		let submodule_path = "odoo.addons.test_module.models.test";
		let resolved_sub = index.resolve_py_module(submodule_path);
		assert!(resolved_sub.is_none(), "Should return None when file doesn't exist");

		// Test invalid module path
		let invalid_path = "invalid.module.path";
		let not_resolved = index.resolve_py_module(invalid_path);
		assert!(not_resolved.is_none(), "Should not resolve invalid module path");
	}

	#[test]
	fn test_discover_dependents_of() {
		let index = Index::default();

		// Setup test data with dependencies: base -> module_a -> module_b
		let root = PathBuf::from("/test/root");
		let mut modules = HashMap::new();

		let base_key = _I("base").into();
		let module_a_key = _I("module_a").into();
		let module_b_key = _I("module_b").into();

		modules.insert(
			base_key,
			ModuleEntry {
				path: "base".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			module_a_key,
			ModuleEntry {
				path: "module_a".into(),
				dependencies: Box::new([base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			module_b_key,
			ModuleEntry {
				path: "module_b".into(),
				dependencies: Box::new([module_a_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root, modules);

		// Test discovering dependents of base (should find module_a and module_b)
		let base_dependents = index.discover_dependents_of(base_key);
		assert!(
			base_dependents.contains(&module_a_key),
			"base should have module_a as dependent"
		);
		assert!(
			base_dependents.contains(&module_b_key),
			"base should have module_b as transitive dependent"
		);

		// Test discovering dependents of module_a (should find module_b)
		let module_a_dependents = index.discover_dependents_of(module_a_key);
		assert!(
			module_a_dependents.contains(&module_b_key),
			"module_a should have module_b as dependent"
		);
		assert!(
			!module_a_dependents.contains(&base_key),
			"module_a should not have base as dependent"
		);

		// Test discovering dependents of module_b (should find none)
		let module_b_dependents = index.discover_dependents_of(module_b_key);
		assert!(module_b_dependents.is_empty(), "module_b should have no dependents");
	}

	#[test]
	fn test_discover_dependents_of_with_auto_install() {
		let index = Index::default();

		// Setup test data: sale and crm modules, plus sale_crm with auto_install
		let root = PathBuf::from("/test/root");
		let mut modules = HashMap::new();

		let sale_key = _I("sale").into();
		let crm_key = _I("crm").into();
		let sale_crm_key = _I("sale_crm").into();

		modules.insert(
			sale_key,
			ModuleEntry {
				path: "sale".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			crm_key,
			ModuleEntry {
				path: "crm".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			sale_crm_key,
			ModuleEntry {
				path: "sale_crm".into(),
				dependencies: Box::new([sale_key, crm_key]),
				auto_install: true,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root, modules);

		// Test: sale should have sale_crm as dependent
		let sale_dependents = index.discover_dependents_of(sale_key);

		assert!(
			sale_dependents.contains(&sale_crm_key),
			"sale should have sale_crm as dependent due to auto_install, but got: {:?}",
			sale_dependents
		);

		// Test: crm should have sale_crm as dependent
		let crm_dependents = index.discover_dependents_of(crm_key);
		assert!(
			crm_dependents.contains(&sale_crm_key),
			"crm should have sale_crm as dependent due to auto_install, but got: {:?}",
			crm_dependents
		);
	}

	#[test]
	fn test_resolve_transitive_dependencies() {
		let index = Index::default();
		let root = PathBuf::from("/test/root");

		// Setup: A depends on B, B depends on C, C depends on base
		let a_key = _I("module_a").into();
		let b_key = _I("module_b").into();
		let c_key = _I("module_c").into();
		let base_key = _I("base").into();

		let mut modules = HashMap::new();

		modules.insert(
			base_key,
			ModuleEntry {
				path: "base".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			c_key,
			ModuleEntry {
				path: "module_c".into(),
				dependencies: Box::new([base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			b_key,
			ModuleEntry {
				path: "module_b".into(),
				dependencies: Box::new([c_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			a_key,
			ModuleEntry {
				path: "module_a".into(),
				dependencies: Box::new([b_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root.clone(), modules);

		// Test that resolving A returns all transitive dependencies
		let deps = index.resolve_transitive_dependencies(a_key);
		assert!(deps.contains(&a_key), "Should include the module itself");
		assert!(deps.contains(&b_key), "Should include direct dependency B");
		assert!(deps.contains(&c_key), "Should include transitive dependency C");
		assert!(deps.contains(&base_key), "Should include transitive dependency base");
		assert_eq!(deps.len(), 4, "Should have exactly 4 dependencies");
	}

	#[test]
	fn test_circular_dependency_handling() {
		let index = Index::default();
		let root = PathBuf::from("/test/root");

		// Setup: A depends on B, B depends on C, C depends on A (circular)
		let a_key = _I("module_a").into();
		let b_key = _I("module_b").into();
		let c_key = _I("module_c").into();
		let base_key = _I("base").into();

		let mut modules = HashMap::new();

		modules.insert(
			base_key,
			ModuleEntry {
				path: "base".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			a_key,
			ModuleEntry {
				path: "module_a".into(),
				dependencies: Box::new([b_key, base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			b_key,
			ModuleEntry {
				path: "module_b".into(),
				dependencies: Box::new([c_key, base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules.insert(
			c_key,
			ModuleEntry {
				path: "module_c".into(),
				dependencies: Box::new([a_key, base_key]), // Circular dependency back to A
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root.clone(), modules);

		// Test that circular dependencies don't cause infinite loop
		let deps = index.resolve_transitive_dependencies(a_key);
		assert!(deps.contains(&a_key), "Should include module A");
		assert!(deps.contains(&b_key), "Should include module B");
		assert!(deps.contains(&c_key), "Should include module C");
		assert!(deps.contains(&base_key), "Should include base");
		assert_eq!(deps.len(), 4, "Should handle circular deps correctly");
	}

	#[test]
	fn test_get_all_available_modules() {
		let index = Index::default();
		let root1 = PathBuf::from("/test/root1");
		let root2 = PathBuf::from("/test/root2");

		let mut modules1 = HashMap::new();
		let mut modules2 = HashMap::new();

		let base_key = _I("base").into();
		let sale_key = _I("sale").into();
		let stock_key = _I("stock").into();

		modules1.insert(
			base_key,
			ModuleEntry {
				path: "base".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules1.insert(
			sale_key,
			ModuleEntry {
				path: "sale".into(),
				dependencies: Box::new([base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		modules2.insert(
			stock_key,
			ModuleEntry {
				path: "stock".into(),
				dependencies: Box::new([base_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root1, modules1);
		index.roots.insert(root2, modules2);

		// Test that we get all modules from all roots
		let all_modules = index.get_all_available_modules();
		assert!(all_modules.contains(&base_key), "Should include base");
		assert!(all_modules.contains(&sale_key), "Should include sale");
		assert!(all_modules.contains(&stock_key), "Should include stock");
		assert_eq!(all_modules.len(), 3, "Should have exactly 3 modules");
	}

	#[test]
	fn test_implicit_base_dependency() {
		let index = Index::default();
		let root = PathBuf::from("/test/root");

		// Test that non-base modules get base as implicit dependency
		let sale_key = _I("sale").into();
		let product_key = _I("product").into();
		let base_key = _I("base").into();

		let mut modules = HashMap::new();

		// Add base module first
		modules.insert(
			base_key,
			ModuleEntry {
				path: "base".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		// Add sale module with only product as explicit dependency
		// This should get base added implicitly
		modules.insert(
			sale_key,
			ModuleEntry {
				path: "sale".into(),
				dependencies: Box::new([product_key]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		// Add product module with no dependencies
		// This should get base added implicitly
		modules.insert(
			product_key,
			ModuleEntry {
				path: "product".into(),
				dependencies: Box::new([]),
				auto_install: false,
				loaded: Default::default(),
				loaded_dependents: Default::default(),
			},
		);

		index.roots.insert(root.clone(), modules);

		// Process implicit base dependencies
		index.add_implicit_base_dependencies();

		let modules = index.roots.get(&root).unwrap();
		let sale_entry = modules.get(&sale_key).unwrap();
		assert!(
			sale_entry.dependencies.contains(&product_key),
			"sale should have product as dependency"
		);
		assert!(
			sale_entry.dependencies.contains(&base_key),
			"sale should have base as implicit dependency"
		);

		// Test that base module doesn't get base as dependency
		let base_entry = modules.get(&base_key).unwrap();
		assert!(
			!base_entry.dependencies.contains(&base_key),
			"base should not have itself as dependency"
		);

		// Test that product gets base as implicit dependency
		let product_entry = modules.get(&product_key).unwrap();
		assert!(
			product_entry.dependencies.contains(&base_key),
			"product should have base as implicit dependency"
		);
	}

	#[test]
	fn test_delete_marked_entries() {
		let index = Index::default();

		// Add some test models
		let root = _I("/test");
		let path = crate::index::PathSymbol::strip_root(root, Path::new("/test/path"));
		let models = vec![crate::model::Model {
			type_: ModelType::Base {
				name: "test_model".into(),
				ancestors: vec![],
			},
			range: Default::default(),
			byte_range: Default::default(),
		}];
		index.models.append(path, false, &models);

		// Mark an entry for deletion
		if let Some(mut entry) = index.models.iter_mut().next() {
			entry.deleted = true;
		}

		let count_before = index.models.len();
		index.delete_marked_entries();
		let count_after = index.models.len();

		assert!(count_after < count_before, "Should have fewer entries after deletion");
	}

	#[test]
	fn test_parse_manifest() {
		use crate::test_utils;

		// Setup test manifest content
		let manifest_content = br#"
{
    'name': 'Test Module',
    'depends': ['base', 'web', 'mail'],
    'version': '1.0',
}
"#;

		let manifest_path = PathBuf::from("/test/module/__manifest__.py");

		// Mock the file system
		if let Ok(mut fs) = test_utils::fs::TEST_FS.write() {
			fs.insert(manifest_path.clone(), manifest_content);
		}

		let manifest_info = parse_manifest_info(&manifest_path).unwrap();

		// Should include 'base' (auto-added) plus the dependencies from the manifest
		assert!(
			manifest_info.dependencies.len() >= 3,
			"Should have at least base, web, and mail dependencies"
		);

		// Check that base is included (it's auto-added for non-base modules)
		let base_key = _I("base").into();
		assert!(
			manifest_info.dependencies.contains(&base_key),
			"Should include base dependency"
		);

		// Check auto_install is false by default
		assert!(!manifest_info.auto_install, "auto_install should be false by default");
	}

	#[test]
	fn test_parse_manifest_with_auto_install() {
		use crate::test_utils;

		// Setup test manifest content with auto_install
		let manifest_content = br#"
{
    'name': 'Test Auto Install Module',
    'depends': ['base', 'web'],
    'auto_install': True,
}
"#;

		let manifest_path = PathBuf::from("/test/auto_module/__manifest__.py");

		// Mock the file system
		if let Ok(mut fs) = test_utils::fs::TEST_FS.write() {
			fs.insert(manifest_path.clone(), manifest_content);
		}

		let manifest_info = parse_manifest_info(&manifest_path).unwrap();

		assert!(
			manifest_info.dependencies.len() >= 2,
			"Should have at least base and web dependencies"
		);

		assert!(manifest_info.auto_install, "auto_install should be true");
	}

	#[test]
	fn test_model_query() {
		let mut parser = Parser::new();
		parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
		let contents = br#"
class Foo(models.AbstractModel):
	_name = 'foo'
	_inherit = ['foo', 'bar']
	@api.depends('foo')
	def foo(self):
		pass

class Bar(models.Model):
	def _default_stuff(): pass
	_name = 'bar'
	what = fields.Foo()
	_inherit = 'baz'

class Base(models.Model):
	_name = 'haha'
	_description = 'What?'
	_order = 'foo, bar'

"#;
		let ast = parser.parse(&contents[..], None).unwrap();
		let query = ModelQuery::query();
		let mut cursor = QueryCursor::new();
		let expected: &[&[&str]] = &[
			&["models", "AbstractModel", "_name"],
			&["models", "AbstractModel", "_inherit"],
			&["models", "Model", "_name"],
			&["models", "Model", "_inherit"],
			&["models", "Model", "_name"],
		];
		let actual = cursor
			.matches(query, ast.root_node(), &contents[..])
			.map(|match_| {
				match_
					.captures
					.iter()
					.skip(1)
					.map(|capture| String::from_utf8_lossy(&contents[capture.node.byte_range()]))
					.collect::<Vec<_>>()
			})
			.fold_mut(vec![], acc_vec);
		assert_eq!(expected, actual);
	}

	#[test]
	fn test_index_query() {
		let models = index_models(
			br#"
class TransifexCodeTranslation(models.Model):
    _name = "transifex.code.translation"
    _description = "Code Translation"
    _log_access = False
"#,
		)
		.unwrap();
		assert!(matches!(
			&models[..],
			[super::Model {
				type_: super::ModelType::Base { .. },
				..
			}]
		));
	}

	#[test]
	fn test_depends_manifest_parsing() {
		use crate::test_utils;

		if let Ok(mut fs) = test_utils::fs::TEST_FS.write() {
			fs.insert(
				"foobar/__manifest__.py".into(),
				r#"{
    "name": "foobar",
    "depends": [
        "foo",
        # test dependencies
        "bar",  # this was not recognized
        # "skip",
    ],
}
"#
				.as_bytes(),
			);
		}

		let manifest_path = std::path::Path::new("foobar/__manifest__.py");
		let result = parse_manifest_info(manifest_path);

		match result {
			Ok(manifest_info) => {
				let deps = &manifest_info.dependencies;
				println!("Dependencies found: {} total", deps.len());
				for (i, dep) in deps.iter().enumerate() {
					println!("  {}. {:?}", i + 1, dep);
				}

				assert_eq!(
					deps.len(),
					2,
					"Expected to find 2 dependencies (foo + bar), but found {}",
					deps.len()
				);

				let dep_names: Vec<String> = deps.iter().map(|d| format!("{d:?}")).collect();
				assert!(
					dep_names.contains(&"Symbol<ModuleEntry>(\"foo\")".to_string()),
					"Expected to find 'foo' dependency"
				);
				assert!(
					dep_names.contains(&"Symbol<ModuleEntry>(\"bar\")".to_string()),
					"Expected to find 'bar' dependency (should now be recognized despite comment)"
				);
			}
			Err(e) => {
				panic!("Parsing should succeed but with incomplete results, got error: {e}");
			}
		}
	}
}