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
//! Methods related to type analysis. The two most important methods are
//! [`Index::model_of_range`] and [`Index::type_of`].

use core::borrow::Borrow;
use std::{
	fmt::{Debug, Write},
	ops::ControlFlow,
	sync::{Arc, OnceLock, atomic::Ordering},
};

use dashmap::DashMap;
use fomat_macros::fomat;
use lasso::Spur;
use ropey::Rope;
use tracing::instrument;
use tree_sitter::{Node, Parser, QueryCursor, StreamingIterator};

use crate::{
	ImStr, dig, format_loc,
	index::{_G, _I, _R, Index, Symbol},
	model::{Method, ModelName, PropertyInfo},
	test_utils,
	utils::{ByteOffset, ByteRange, Defer, PreTravel, RangeExt, TryResultExt, python_next_named_sibling, rope_conv},
};
use ts_macros::query;

mod scope;
pub use scope::Scope;

pub fn type_cache() -> &'static TypeCache {
	static CACHE: OnceLock<TypeCache> = OnceLock::new();
	CACHE.get_or_init(TypeCache::default)
}

macro_rules! _T {
	(@ $builtin:expr) => {
		$crate::analyze::type_cache().get_or_intern(Type::PyBuiltin($builtin.into()))
	};
	($model:literal) => {
		$crate::analyze::type_cache().get_or_intern(Type::Model($model.into()))
	};
	($expr:expr) => {
		$crate::analyze::type_cache().get_or_intern($expr)
	};
}

macro_rules! _TR {
	($expr:expr) => {
		$crate::analyze::type_cache().resolve($expr)
	};
}

pub static MODEL_METHODS: phf::Set<&str> = phf::phf_set!(
	"create",
	"copy",
	"name_create",
	"browse",
	"filtered",
	"filtered_domain",
	"sorted",
	"search",
	"search_fetch",
	"name_search",
	"ensure_one",
	"with_context",
	"with_user",
	"with_company",
	"with_env",
	"sudo",
	"exists",
	"concat",
	// TODO: Limit to Forms only
	"new",
	"edit",
	"save",
);

/// The subset of types that may resolve to a model.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Type {
	Env,
	/// \*.env.ref()
	RefFn,
	/// Functions that return another model, regardless of input.
	ModelFn(ImStr),
	Model(ImStr),
	/// Unresolved model.
	Record(ImStr),
	Super,
	Method(ModelName, ImStr),
	/// To hardcode some methods, such as dict.items()
	PythonMethod(TypeId, ImStr),
	/// `odoo.http.request`
	HttpRequest,
	Dict(TypeId, TypeId),
	/// A bag of enumerated properties and their types
	DictBag(Vec<(DictKey, TypeId)>),
	/// Equivalent to Value, but may have a better semantic name
	PyBuiltin(ImStr),
	List(ListElement),
	Tuple(Vec<TypeId>),
	Iterable(Option<TypeId>),
	/// Can never be resolved, useful for non-model bindings.
	Value,
}

impl Type {
	#[inline]
	fn is_dictlike(&self) -> bool {
		matches!(self, Type::Dict(..) | Type::DictBag(..))
	}
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub enum ListElement {
	Vacant,
	Occupied(TypeId),
}

impl Debug for ListElement {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::Vacant => f.write_str("..."),
			Self::Occupied(inner) => inner.fmt(f),
		}
	}
}

impl From<ListElement> for Option<TypeId> {
	#[inline]
	fn from(value: ListElement) -> Self {
		match value {
			ListElement::Vacant => None,
			ListElement::Occupied(inner) => Some(inner),
		}
	}
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub enum DictKey {
	String(ImStr),
	Type(TypeId),
}

impl Debug for DictKey {
	#[inline]
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::String(key) => key.fmt(f),
			Self::Type(key) => key.fmt(f),
		}
	}
}

#[derive(Clone, Debug)]
pub enum FunctionParam {
	Param(ImStr),
	/// `(positional_separator)`
	PosEnd,
	/// `(keyword_separator)` or `(list_splat_pattern)`
	EitherEnd(Option<ImStr>),
	/// `(default_parameter)`
	Named(ImStr),
	Kwargs(ImStr),
}

impl core::fmt::Display for FunctionParam {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			FunctionParam::Param(param) => f.write_str(param),
			FunctionParam::PosEnd => f.write_char('/'),
			FunctionParam::EitherEnd(None) => f.write_char('*'),
			FunctionParam::EitherEnd(Some(param)) => write!(f, "*{param}"),
			FunctionParam::Named(param) => write!(f, "{param}=..."),
			FunctionParam::Kwargs(param) => write!(f, "**{param}"),
		}
	}
}

#[derive(Default)]
pub struct TypeCache {
	types: boxcar::Vec<Type>,
	ids: DashMap<Type, TypeId>,
}

impl TypeCache {
	#[inline]
	pub fn get_or_intern(&self, type_: Type) -> TypeId {
		if let Some(id) = self.ids.get(&type_) {
			return *id;
		}
		self.intern(type_)
	}
	fn intern(&self, type_: Type) -> TypeId {
		let id = TypeId(self.types.push(type_.clone()).try_into().unwrap());
		self.ids.insert(type_, id);
		id
	}
	#[inline]
	pub fn resolve<T: Borrow<TypeId>>(&self, id: T) -> &Type {
		unsafe { self.types.get_unchecked(id.borrow().0 as usize) }
	}
}

#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeId(u32);

impl TypeId {
	#[inline]
	pub fn is_dictlike(&self) -> bool {
		type_cache().resolve(self).is_dictlike()
	}
	#[inline]
	pub fn is_dict(&self) -> bool {
		matches!(type_cache().resolve(self), Type::Dict(..))
	}
	#[inline]
	pub fn is_dictbag(&self) -> bool {
		matches!(type_cache().resolve(self), Type::DictBag(..))
	}
}

impl Debug for TypeId {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		_TR!(*self).fmt(f)
	}
}

pub fn normalize<'r, 'n>(node: &'r mut Node<'n>) -> &'r mut Node<'n> {
	let mut cursor = node.walk();
	while matches!(
		node.kind(),
		"expression_statement" | "parenthesized_expression" | "module"
	) {
		let Some(child) = node.named_children(&mut cursor).find(|child| child.kind() != "comment") else {
			break;
		};
		*node = child;
	}
	node
}

#[rustfmt::skip]
query! {
	#[derive(Debug)]
	FieldCompletion(Name, SelfParam, Scope);
((class_definition
  (block
    (expression_statement [
      (assignment (identifier) @_name (string) @NAME)
	  (assignment (identifier) @_inherit (list . (string) @NAME)) ])?
    [
      (decorated_definition
        (function_definition
          (parameters . (identifier) @SELF_PARAM)) @SCOPE)
      (function_definition (parameters . (identifier) @SELF_PARAM)) @SCOPE])) @class
  (#eq? @_inherit "_inherit")
  (#match? @_name "^_(name|inherit)$"))
}

#[rustfmt::skip]
query! {
	MappedCall(Callee, Iter);
((call
  (attribute (_) @CALLEE (identifier) @_mapped)
  (argument_list [
    (lambda (lambda_parameters . (identifier) @ITER))
    (keyword_argument
      (identifier) @_func
      (lambda (lambda_parameters . (identifier) @ITER)))]))
  (#match? @_func "^(func|key)$")
  (#match? @_mapped "^(mapp|filter|sort|group)ed$"))
}

#[rustfmt::skip]
query! {
	PythonBuiltinCall(Append, AppendList, AppendMap, AppendMapKey, AppendValue, UpdateMap, UpdateArgs);
// value.append(...) OR value['foobar'].append(...)
(call
  (attribute
    (subscript (identifier) @APPEND_MAP (string (string_content) @APPEND_MAP_KEY))
    (identifier) @_append)
  (argument_list . (_) @APPEND_VALUE)
  (#eq? @_append "append"))

(call
  (attribute
    (identifier) @APPEND_LIST
    (identifier) @APPEND)
  (argument_list . (_) @APPEND_VALUE)
  (#eq? @_append "append"))

// value.update(...)
(call
  (attribute
  	(identifier) @UPDATE_MAP
  	(identifier) @_update)
  (argument_list) @UPDATE_ARGS
  (#eq? @_update "update"))
}

pub type ScopeControlFlow = ControlFlow<Option<Scope>, bool>;
impl Index {
	#[inline]
	pub fn model_of_range(&self, node: Node<'_>, range: ByteRange, contents: &str) -> Option<ModelName> {
		let (type_at_cursor, scope) = self.type_of_range(node, range, contents)?;
		self.try_resolve_model(_TR!(type_at_cursor), &scope)
	}
	pub fn type_of_range(&self, root: Node<'_>, range: ByteRange, contents: &str) -> Option<(TypeId, Scope)> {
		// Phase 1: Determine the scope.
		let (self_type, fn_scope, self_param) = determine_scope(root, contents, range.start.0)?;

		// Phase 2: Build the scope up to offset
		// What contributes to a method scope's variables?
		// 1. Top-level statements; completely opaque to us.
		// 2. Class definitions; technically useless to us.
		//    Self-type analysis only uses a small part of the class definition.
		// 3. Parameters, e.g. self which always has a fixed type
		// 4. Assignments (including walrus-assignment)
		let mut scope = Scope::default();
		let self_type = match self_type {
			Some(type_) => &contents[type_.byte_range().shrink(1)],
			None => "",
		};
		scope.insert(self_param.to_string(), Type::Model(self_type.into()));
		scope.super_ = Some(self_param.into());

		// special case: is this a property?
		// TODO: fields
		let node_at_cursor = fn_scope.descendant_for_byte_range(range.start.0, range.end.0)?;
		if node_at_cursor.kind() == "identifier" && fn_scope.child_by_field_name("name") == Some(node_at_cursor) {
			return Some((
				_T!(Type::Method(
					_I(self_type).into(),
					contents[node_at_cursor.byte_range()].into()
				)),
				scope,
			));
		}

		// Phase 3: With the proper context available, determine the type of the node.
		self.type_of_node(Some(scope), fn_scope, range, contents)
	}
	pub fn type_of_node(
		&self,
		scope: Option<Scope>,
		node: Node<'_>,
		range: ByteRange,
		contents: &str,
	) -> Option<(TypeId, Scope)> {
		let scope = scope.unwrap_or_default();
		let (orig_scope, scope) = Self::walk_scope(node, Some(scope), |scope, node| {
			self.build_scope(scope, node, range.end.0, contents)
		});
		let scope = scope.unwrap_or(orig_scope);
		let node_at_cursor = node.descendant_for_byte_range(range.start.0, range.end.0)?;
		let type_at_cursor = self.type_of(node_at_cursor, &scope, contents)?;
		Some((type_at_cursor, scope))
	}
	/// Builds the scope up to `offset`, in bytes.
	///
	/// #### About [ScopeControlFlow]
	/// This is one of the rare occasions where [ControlFlow] is used. It is similar to
	/// [Result] in that the try-operator (?) can be used to end iteration on a
	/// [ControlFlow::Break]. Otherwise, [ControlFlow::Continue] has a continuation value
	/// that must be passed up the chain, since it indicates whether [Scope::enter] was called.
	pub fn build_scope(&self, scope: &mut Scope, node: Node, offset: usize, contents: &str) -> ScopeControlFlow {
		if node.start_byte() > offset {
			return ControlFlow::Break(Some(core::mem::take(scope)));
		}
		match node.kind() {
			"assignment" | "named_expression" => {
				// (_ left right)
				let lhs = node.named_child(0).unwrap();
				if lhs.kind() == "identifier"
					&& let rhs = python_next_named_sibling(lhs).expect(format_loc!("rhs"))
					&& let Some(id) = self.type_of(rhs, scope, contents)
				{
					let lhs = &contents[lhs.byte_range()];
					scope.insert(lhs.to_string(), _TR!(id).clone());
				} else if lhs.kind() == "subscript"
					&& let Some(map) = dig!(lhs, identifier)
					&& let Some(key) = dig!(lhs, string(1).string_content(1))
					&& let Some(rhs) = python_next_named_sibling(lhs)
					&& let type_ = self.type_of(rhs, scope, contents)
					&& let Some(Type::DictBag(properties)) = scope.get_mut(&contents[map.byte_range()])
				{
					let type_ = type_.unwrap_or_else(|| _T!(Type::Value));
					let key = &contents[key.byte_range()];
					if let Some(idx) = properties.iter().position(|(prop, _)| match prop {
						DictKey::String(prop) => prop.as_str() == key,
						DictKey::Type(_) => false,
					}) {
						properties[idx].1 = type_;
					} else {
						properties.push((DictKey::String(ImStr::from(key)), type_));
					}
				} else if lhs.kind() == "pattern_list"
					&& let Some(rhs) = python_next_named_sibling(lhs)
					&& let Some(type_) = self.type_of(rhs, scope, contents)
				{
					self.destructure_into_patternlist_like(lhs, type_, scope, contents);
				}
			}
			"for_statement" => {
				// (for_statement left right body)
				scope.enter(true);
				let lhs = node.named_child(0).unwrap();

				if let Some(rhs) = python_next_named_sibling(lhs)
					&& let Some(type_) = self.type_of(rhs, scope, contents)
					&& let Some(inner) = self.type_of_iterable(type_)
				{
					self.destructure_into_patternlist_like(lhs, inner, scope, contents);
				}
				return ControlFlow::Continue(true);
			}
			"function_definition" => {
				let mut inherit_super = false;
				let mut node = node;
				while let Some(parent) = node.parent() {
					match parent.kind() {
						"decorated_definition" => {
							node = parent;
							continue;
						}
						"block" => inherit_super = parent.parent().is_some_and(|gp| gp.kind() == "class_definition"),
						_ => {}
					}
					break;
				}
				scope.enter(inherit_super);
				return ControlFlow::Continue(true);
			}
			"list_comprehension" | "set_comprehension" | "dictionary_comprehension" | "generator_expression"
				if node.byte_range().contains(&offset) =>
			{
				// (_ body: _ (for_in_clause left: _ right: _))
				let for_in = node.named_child(1).unwrap();
				if let Some(lhs) = for_in.child_by_field_name("left")
					&& let Some(rhs) = for_in.child_by_field_name("right")
					&& let Some(tid) = self.type_of(rhs, scope, contents)
					&& let Some(inner) = self.type_of_iterable(tid)
				{
					self.destructure_into_patternlist_like(lhs, inner, scope, contents);
				}
			}
			"call" if node.byte_range().contains_end(offset) => {
				// model.{mapped,filtered,sorted,*}(lambda rec: ..)
				let query = MappedCall::query();
				let mut cursor = QueryCursor::new();
				let mut matches = cursor.matches(query, node, contents.as_bytes());
				if let Some(mapped_call) = matches.next()
					&& let callee = mapped_call
						.nodes_for_capture_index(MappedCall::Callee as _)
						.next()
						.unwrap() && let Some(tid) = self.type_of(callee, scope, contents)
				{
					let iter = mapped_call
						.nodes_for_capture_index(MappedCall::Iter as _)
						.next()
						.unwrap();
					let iter = &contents[iter.byte_range()];
					scope.insert(iter.to_string(), _TR!(tid).clone());
				}
			}
			"call" => {
				let query = PythonBuiltinCall::query();
				let mut cursor = QueryCursor::new();
				let mut matches = cursor.matches(query, node, contents.as_bytes());
				let Some(call) = matches.next() else {
					return ControlFlow::Continue(false);
				};
				if let Some(value) = call.nodes_for_capture_index(PythonBuiltinCall::AppendValue as _).next()
					&& let Some(tid) = self.type_of(value, scope, contents)
				{
					if let Some(list) = call.nodes_for_capture_index(PythonBuiltinCall::AppendList as _).next() {
						if let Some(Type::List(slot @ ListElement::Vacant)) =
							scope.get_mut(&contents[list.byte_range()])
						{
							*slot = ListElement::Occupied(tid);
						}
					} else if let Some(map) = call.nodes_for_capture_index(PythonBuiltinCall::AppendMap as _).next() {
						let Some(key) = call
							.nodes_for_capture_index(PythonBuiltinCall::AppendMapKey as _)
							.next()
						else {
							return ControlFlow::Continue(false);
						};
						let key = &contents[key.byte_range()];

						if let Some(Type::DictBag(properties)) = scope.get_mut(&contents[map.byte_range()])
							&& let Some((_, slot)) = properties.iter_mut().find(|(prop, id)| match prop {
								DictKey::String(prop) => {
									prop.as_str() == key && _T!(Type::List(ListElement::Vacant)) == *id
								}
								DictKey::Type(_) => false,
							}) {
							*slot = _T!(Type::List(ListElement::Occupied(tid)));
						}
					}
				} else if let Some(map) = call.nodes_for_capture_index(PythonBuiltinCall::UpdateMap as _).next() {
					let Some(Type::DictBag(properties)) = scope.get_mut(&contents[map.byte_range()]) else {
						return ControlFlow::Continue(false);
					};
					let Some(args) = call.nodes_for_capture_index(PythonBuiltinCall::UpdateArgs as _).next() else {
						return ControlFlow::Continue(false);
					};

					let mut properties = core::mem::take(properties);
					let mut cursor = args.walk();
					let mut children = args.named_children(&mut cursor);
					if let Some(first) = children.by_ref().next()
						&& let Some(tid) = self.type_of(first, scope, contents)
						&& let Type::DictBag(update_props) = _TR!(tid)
					{
						properties.extend(update_props.clone());
					}

					for named_arg in children {
						if named_arg.kind() == "keyword_argument"
							&& let Some(name) = named_arg.child_by_field_name("name")
							&& let Some(value) = named_arg.child_by_field_name("value")
						{
							let key = &contents[name.byte_range()];
							let type_ = self.type_of(value, scope, contents).unwrap_or_else(|| _T!(Type::Value));
							if let Some(idx) = properties.iter().position(|(prop, _)| match prop {
								DictKey::String(prop) => prop.as_str() == key,
								DictKey::Type(_) => false,
							}) {
								properties[idx].1 = type_;
							} else {
								properties.push((DictKey::String(ImStr::from(key)), type_));
							}
						} else if named_arg.kind() == "dictionary_splat"
							&& let Some(value) = named_arg.named_child(0)
							&& let Some(tid) = self.type_of(value, scope, contents)
							&& let Type::DictBag(update_props) = _TR!(tid)
						{
							properties.extend(update_props.clone());
						}
					}

					scope.insert(contents[map.byte_range()].to_string(), Type::DictBag(properties));
				}
			}
			"with_statement" => {
				// with Form(self.env['..']) as alias:
				// TODO: Support more structures as needed
				// (with_statement
				// 	 (with_clause
				//     (with_item
				//       (as_pattern
				//         (call (identifier) ..)
				//         (as_pattern_target (identifier))))))
				if let Some(value) = dig!(node, with_clause.with_item.as_pattern.call)
					&& let Some(target) = python_next_named_sibling(value)
					&& target.kind() == "as_pattern_target"
					&& let Some(alias) = dig!(target, identifier)
					&& let Some(callee) = value.named_child(0)
				{
					// TODO: Remove this hardcoded case
					if callee.kind() == "identifier"
						&& "Form" == &contents[callee.byte_range()]
						&& let Some(first_arg) = value.named_child(1).expect("call args").named_child(0)
						&& let Some(type_) = self.type_of(first_arg, scope, contents)
					{
						let alias = &contents[alias.byte_range()];
						scope.insert(alias.to_string(), _TR!(type_).clone());
					} else if let Some(type_) = self.type_of(value, scope, contents) {
						let alias = &contents[alias.byte_range()];
						scope.insert(alias.to_string(), _TR!(type_).clone());
					}
				}
			}
			_ => {}
		}

		ControlFlow::Continue(false)
	}
	/// [Type::Value] is not returned by this method.
	pub fn type_of(&self, mut node: Node, scope: &Scope, contents: &str) -> Option<TypeId> {
		// What contributes to value types?
		// 1. *.env['foo'] => Model('foo')
		// 2. *.env.ref(<record-id>) => Model(<model of record-id>)

		// What preserves value types?
		// 1. for foo in bar;
		//    bar: 't => foo: 't
		// 2. foo = bar;
		//    bar: 't => foo: 't (and various other operators)
		// 3. foo.sudo();
		//    foo: 't => foo.sudo(): 't
		//    sudo, with_user, with_env, with_context, ..
		// 4. [foo for foo in bar];
		//    bar: 't => foo: 't
		// 5: foo[..]
		//    foo: 't => foo[..]: 't

		// What transforms value types?
		// 1. foo.bar;
		//    foo: Model('t) => bar: Model('t).field('bar')
		// 2. foo.mapped('..')
		//    foo: Model('t) => _: Model('t).mapped('..')
		// 3. foo.mapped(lambda rec: 't): 't
		#[cfg(debug_assertions)]
		if node.byte_range().len() <= 64 {
			tracing::trace!("type_of {} '{}'", node.kind(), &contents[node.byte_range()]);
		} else {
			tracing::trace!("type_of {} range={:?}", node.kind(), node.byte_range());
		}
		match normalize(&mut node).kind() {
			"subscript" => {
				let lhs = node.child_by_field_name("value")?;
				let rhs = node.child_by_field_name("subscript")?;
				let obj_ty = self.type_of(lhs, scope, contents)?;
				match _TR!(obj_ty) {
					Type::Env if rhs.kind() == "string" => {
						Some(_T!(Type::Model(contents[rhs.byte_range().shrink(1)].into())))
					}
					Type::Env => Some(_T!["unknown"]),
					Type::Model(_) | Type::Record(_) => Some(obj_ty),
					Type::Dict(key, value) => {
						let rhs = self.type_of(rhs, scope, contents);
						// FIXME: We trust that the user makes the correct judgment here and returns the type requested.
						rhs.is_none_or(|rhs| rhs == *key).then_some(*value)
					}
					Type::DictBag(properties) => {
						// compare by key
						if let Some(rhs) = dig!(rhs, string_content(1)) {
							let rhs = &contents[rhs.byte_range()];
							for (key, value) in properties {
								match key {
									DictKey::String(key) if key.as_str() == rhs => {
										return Some(*value);
									}
									DictKey::String(_) | DictKey::Type(_) => {}
								}
							}
							return None;
						}

						// compare by type
						let rhs = self.type_of(rhs, scope, contents)?;
						for (key, value) in properties {
							match key {
								DictKey::Type(key) if *key == rhs => return Some(*value),
								DictKey::Type(_) | DictKey::String(_) => {}
							}
						}

						None
					}
					// FIXME: Again, just trust that the user is doing the right thing.
					Type::List(ListElement::Occupied(slot)) => Some(*slot),
					_ => None,
				}
			}
			"attribute" => self.type_of_attribute_node(node, scope, contents),
			"identifier" => {
				if let Some(parent) = node.parent()
					&& parent.kind() == "attribute"
					&& parent.named_child(0).unwrap() != node
				{
					return self.type_of_attribute_node(parent, scope, contents);
				}

				let key = &contents[node.byte_range()];
				if key == "super" {
					return Some(_T!(Type::Super));
				}
				if let Some(type_) = scope.get(key) {
					return Some(_T!(type_.clone()));
				}
				if key == "request" {
					return Some(_T!(Type::HttpRequest));
				}
				None
			}
			"assignment" => {
				let rhs = node.named_child(1)?;
				self.type_of(rhs, scope, contents)
			}
			"call" => self.type_of_call_node(node, scope, contents),
			"binary_operator" | "boolean_operator" => {
				// (_ left right)
				if let Some(left) = node.child_by_field_name("left")
					&& let Some(typ) = self.type_of(left, scope, contents)
				{
					Some(typ)
				} else {
					self.type_of(node.child_by_field_name("right")?, scope, contents)
				}
			}
			"conditional_expression" => {
				// a if b else c
				let ty = node
					.named_child(0)
					.and_then(|child| self.type_of(child, scope, contents));
				ty.or_else(|| {
					node.named_child(2)
						.and_then(|child| self.type_of(child, scope, contents))
				})
			}
			"dictionary_comprehension" => {
				let pair = dig!(node, pair)?;
				let mut comprehension_scope;
				let mut pair_scope = scope;
				if let Some(for_in_clause) = dig!(node, for_in_clause(1))
					&& let Some(scrutinee) = for_in_clause.child_by_field_name("left")
					&& let Some(iteratee) = for_in_clause.child_by_field_name("right")
					&& let Some(iter_ty) = self.type_of(iteratee, scope, contents)
					&& let Some(iter_ty) = self.type_of_iterable(iter_ty)
				{
					// FIXME: How to prevent this clone?
					comprehension_scope = Scope::new(Some(scope.clone()));
					self.destructure_into_patternlist_like(scrutinee, iter_ty, &mut comprehension_scope, contents);
					pair_scope = &comprehension_scope;
				}
				let lhs = pair
					.named_child(0)
					.and_then(|lhs| self.type_of(lhs, pair_scope, contents));
				let rhs = pair
					.named_child(1)
					.and_then(|lhs| self.type_of(lhs, pair_scope, contents));
				if lhs.is_some() || rhs.is_some() {
					let value_id = _T!(Type::Value);
					Some(_T!(Type::Dict(lhs.unwrap_or(value_id), rhs.unwrap_or(value_id))))
				} else {
					None
				}
			}
			"dictionary" => {
				let mut properties = vec![];
				for child in node.named_children(&mut node.walk()) {
					if child.kind() == "pair"
						&& let Some(lhs) = child.child_by_field_name("key")
						&& let Some(rhs) = child.child_by_field_name("value")
					{
						let key;
						if let Some(lhs) = dig!(lhs, string_content(1)) {
							key = DictKey::String(ImStr::from(&contents[lhs.byte_range()]));
						} else if matches!(lhs.kind(), "true" | "false" | "string" | "none" | "float" | "integer") {
							key = DictKey::Type(_T!( @contents[lhs.byte_range()]));
						} else if let Some(lhs) = self.type_of(lhs, scope, contents) {
							key = DictKey::Type(lhs);
						} else {
							continue;
						}

						let value = self.type_of(rhs, scope, contents).unwrap_or_else(|| _T!(Type::Value));
						properties.push((key, value));
					}
				}
				Some(_T!(Type::DictBag(properties)))
			}
			"list" => {
				let mut slot = ListElement::Vacant;
				for child in node.named_children(&mut node.walk()) {
					if let Some(child) = self.type_of(child, scope, contents) {
						slot = ListElement::Occupied(child);
						break;
					}
				}
				Some(_T!(Type::List(slot)))
			}
			"expression_list" | "tuple" => {
				let mut cursor = node.walk();
				let value_id = _T!(Type::Value);
				let tuple = node.named_children(&mut cursor).filter_map(|child| {
					if child.kind() == "comment" {
						return None;
					}
					Some(self.type_of(child, scope, contents).unwrap_or(value_id))
				});
				Some(_T!(Type::Tuple(tuple.collect())))
			}
			"string" => Some(_T!( @ "str")),
			"integer" => Some(_T!( @ "int")),
			"float" => Some(_T!( @ "float")),
			"true" | "false" | "comparison_operator" => Some(_T!( @ "bool")),
			_ => None,
		}
	}
	pub(crate) fn type_of_iterable(&self, tid: TypeId) -> Option<TypeId> {
		match _TR!(tid) {
			Type::Model(_) => Some(tid),
			Type::List(inner) => inner.clone().into(),
			Type::Iterable(inner) => *inner,
			// TODO: tuple -> union
			_ => None,
		}
	}
	fn wrap_in_container<F: FnOnce(Type) -> Type>(type_: Type, producer: F) -> Type {
		match type_ {
			Type::Model(..) => type_,
			_ => producer(type_),
		}
	}
	fn type_of_call_node(&self, call: Node<'_>, scope: &Scope, contents: &str) -> Option<TypeId> {
		let func = call.named_child(0)?;
		if func.kind() == "identifier" {
			match &contents[func.byte_range()] {
				"zip" => {
					let args = call.named_child(1)?;
					let mut cursor = args.walk();
					let value_id = _T!(Type::Value);
					let children = args.named_children(&mut cursor).map(|child| {
						let tid = self.type_of(child, scope, contents).unwrap_or(value_id);
						self.type_of_iterable(tid).unwrap_or(value_id)
					});
					let tuple = _T!(Type::Tuple(children.collect()));
					return Some(_T!(Type::Iterable(Some(tuple))));
				}
				"enumerate" => {
					let arg = call.named_child(1)?.named_child(0);
					let arg = arg
						.and_then(|arg| self.type_of(arg, scope, contents))
						.unwrap_or_else(|| _T!(Type::Value));
					let intid = _T!(Type::PyBuiltin("int".into()));
					let tuple = _T!(Type::Tuple(vec![intid, arg]));
					return Some(_T!(Type::Iterable(Some(tuple))));
				}
				"tuple" => {
					let args = call.named_child(1)?;
					if args.kind() == "argument_list" {
						let mut cursor = args.walk();
						let value_id = _T!(Type::Value);
						let children = args
							.named_children(&mut cursor)
							.map(|child| self.type_of(child, scope, contents).unwrap_or(value_id));
						return Some(_T!(Type::Tuple(children.collect())));
					}
				}
				"dict" => {
					let arg = call.named_child(1)?.named_child(0)?;
					let arg = self.type_of(arg, scope, contents)?;
					let arg = self.type_of_iterable(arg)?;
					if let Type::Tuple(tuple) = _TR!(arg)
						&& let [lhs, rhs] = &tuple[..]
					{
						return Some(_T!(Type::Dict(*lhs, *rhs)));
					}
					return Some(_T!(Type::Dict(_T!(Type::Value), _T!(Type::Value))));
				}
				"defaultdict" => {
					let arg = call.named_child(1)?.named_child(0)?;
					if matches!(&contents[arg.byte_range()], "list" | "dict" | "float" | "int") {
						// TODO: consider more general functions and type ctors
						return Some(_T!(Type::Dict(
							_T!(Type::Value),
							_T!(Type::PyBuiltin(contents[arg.byte_range()].into()))
						)));
					}
					if arg.kind() != "lambda" {
						return Some(_T!(Type::Dict(_T!(Type::Value), _T!(Type::Value))));
					}
					// (lambda body: (_))
					let body = arg.child_by_field_name("body")?;
					let body_ty = self.type_of(body, scope, contents).unwrap_or_else(|| _T!(Type::Value));
					return Some(_T!(Type::Dict(_T!(Type::Value), body_ty)));
				}
				"super" => {}
				_ => return None,
			};
		}

		let func = self.type_of(func, scope, contents)?;
		match _TR!(func) {
			Type::RefFn => {
				// (call (_) @func (argument_list . (string) @xml_id))
				let xml_id = call.named_child(1)?.named_child(0)?;
				if xml_id.kind() == "string" {
					Some(_T!(Type::Record(contents[xml_id.byte_range().shrink(1)].into())))
				} else {
					None
				}
			}
			Type::ModelFn(model) => Some(_T!(Type::Model(model.clone()))),
			Type::Super => Some(_T!(scope.get(scope.super_.as_deref()?).cloned()?)),
			Type::Method(model, mapped) if mapped.as_str() == "mapped" => {
				// (call (_) @func (argument_list . [(string) (lambda)] @mapped))
				let mapped = call.named_child(1)?.named_child(0)?;
				match mapped.kind() {
					"string" => {
						let mut model: Spur = (*model).into();
						let mut mapped = &contents[mapped.byte_range().shrink(1)];
						self.models.resolve_mapped(&mut model, &mut mapped, None).ok()?;
						let type_ = self.type_of_attribute(&Type::Model(_R(model).into()), mapped, scope)?;
						let type_ = Index::wrap_in_container(type_, |it| Type::List(ListElement::Occupied(_T!(it))));
						Some(_T!(type_))
					}
					"lambda" => {
						// (lambda (lambda_parameters)? body: (_))
						let mut scope = Scope::new(Some(scope.clone()));
						if let Some(params) = mapped.child_by_field_name(b"parameters") {
							let first_arg = params.named_child(0)?;
							if first_arg.kind() == "identifier" {
								let first_arg = &contents[first_arg.byte_range()];
								scope.insert(first_arg.to_string(), Type::Model(_R(model).into()));
							}
						}
						let body = mapped.child_by_field_name(b"body")?;
						let type_ = self.type_of(body, &scope, contents).unwrap_or_else(|| _T!(Type::Value));
						let type_ = Index::wrap_in_container(_TR!(type_).clone(), |it| {
							Type::List(ListElement::Occupied(_T!(it)))
						});
						Some(_T!(type_))
					}
					_ => None,
				}
			}
			Type::Method(model, grouped) if grouped.as_str() == "grouped" => {
				// (call (_) @func (argument_list . [(string) (lambda)] @mapped))
				let grouped = call.named_child(1)?.named_child(0)?;
				match grouped.kind() {
					"string" => {
						let mut model: Spur = (*model).into();
						let mut grouped = &contents[grouped.byte_range().shrink(1)];
						self.models.resolve_mapped(&mut model, &mut grouped, None).ok()?;
						let model = Type::Model(_R(model).into());
						let groupby = self.type_of_attribute(&model, grouped, scope)?;
						Some(_T!(Type::Dict(_T!(groupby), _T!(model))))
					}
					"lambda" => {
						let mut scope = Scope::new(Some(scope.clone()));
						if let Some(params) = grouped.child_by_field_name(b"parameters") {
							let first_arg = params.named_child(0)?;
							if first_arg.kind() == "identifier" {
								let first_arg = &contents[first_arg.byte_range()];
								scope.insert(first_arg.to_string(), Type::Model(_R(model).into()));
							}
						}
						let body = grouped.child_by_field_name(b"body")?;
						let groupby = self.type_of(body, &scope, contents).unwrap_or_else(|| _T!(Type::Value));
						let model = Type::Model(_R(model).into());
						Some(_T!(Type::Dict(groupby, _T!(model))))
					}
					_ => None,
				}
			}
			Type::Method(model, read_group) if read_group.as_str() == "_read_group" => {
				let mut groupby = vec![];
				let mut aggs = vec![];
				let args = call.named_child(1)?;

				#[derive(PartialEq, Eq)]
				enum Aggregation<'a> {
					Recordset,
					Raw(&'a str),
				}

				fn gather_attributes<'out>(
					contents: &'out str,
					arg: Node,
					out: &mut Vec<(&'out str, Option<Aggregation<'out>>)>,
				) {
					let mut cursor = arg.walk();
					for field in arg.named_children(&mut cursor) {
						if let Some(field) = dig!(field, string_content(1)) {
							let mut field = &contents[field.byte_range()];
							let mut agg = None;
							if let Some((inner, raw_agg)) = field.split_once(':') {
								field = inner;
								match raw_agg {
									"recordset" => agg = Some(Aggregation::Recordset),
									_ => agg = Some(Aggregation::Raw(raw_agg)),
								}
							}
							out.push((field, agg));
						}
					}
				}

				for (idx, arg) in args.named_children(&mut args.walk()).enumerate().take(3) {
					if arg.kind() == "keyword_argument" {
						let out = match &contents[arg.child_by_field_name("key")?.byte_range()] {
							"groupby" => &mut groupby,
							"aggregates" => &mut aggs,
							_ => continue,
						};
						let Some(arg) = arg.child_by_field_name("value") else {
							continue;
						};
						if arg.kind() != "list" {
							continue;
						}
						gather_attributes(contents, arg, out);
						continue;
					}

					if arg.kind() != "list" || idx > 2 || idx == 0 {
						continue;
					}

					let out = if idx == 1 { &mut groupby } else { &mut aggs };
					gather_attributes(contents, arg, out);
				}

				groupby.extend(aggs);
				groupby.dedup();
				let model = Type::Model(_R(*model).into());
				let model_tid = _T!(model.clone());
				let value_id = _T!(Type::Value);
				// FIXME: This is not quite correct as only recordset and numeric aggregations make sense.
				let aggs = groupby.into_iter().map(|(attr, agg)| {
					if attr == "id"
						&& let Some(Aggregation::Recordset) = agg
					{
						return model_tid;
					}
					match self.type_of_attribute(&model, attr, scope) {
						Some(type_) => _T!(type_),
						None => value_id,
					}
				});
				let tuple = _T!(Type::Tuple(aggs.collect()));
				Some(_T!(Type::List(ListElement::Occupied(tuple))))
			}
			Type::Method(model, read) if read.as_str() == "read" => {
				let model = Type::Model(_R(model).into());
				let args = call.named_child(1)?;
				let fields = dig!(args, list)?;
				let mut dict: Vec<(DictKey, TypeId)> = vec![];
				for field in fields.named_children(&mut fields.walk()) {
					let Some(field) = dig!(field, string_content(1)) else {
						continue;
					};
					let key = &contents[field.byte_range()];
					if let Some(field_ty) = self.type_of_attribute(&model, key, scope) {
						dict.push((DictKey::String(key.into()), _T!(field_ty)));
					}
				}
				Some(_T!(Type::List(ListElement::Occupied(_T!(Type::DictBag(dict))))))
			}
			Type::Method(model, method) => {
				let method = _G(method)?;
				let args = self.prepare_call_scope(*model, method.into(), call, scope, contents);
				Some(self.eval_method_rtype(method.into(), **model, args)?)
			}
			Type::PythonMethod(dict, method) if dict.is_dict() => {
				let Type::Dict(lhs, rhs) = _TR!(dict) else {
					unreachable!()
				};
				match method.as_str() {
					"items" => {
						let tuple = _T!(Type::Tuple(vec![*lhs, *rhs]));
						Some(_T!(Type::Iterable(Some(tuple))))
					}
					"get" => Some(*rhs),
					_ => None,
				}
			}
			Type::PythonMethod(dictbag, method) if dictbag.is_dictbag() => {
				let Type::DictBag(items) = _TR!(dictbag) else {
					unreachable!()
				};
				match method.as_str() {
					"get" => {
						let args = call.named_child(1)?;
						let arg_as_string = dig!(args, string.string_content(1));
						let argtype = args
							.named_child(0)
							.and_then(|node| self.type_of(node, scope, contents))
							.unwrap_or_else(|| _T!(Type::Value));
						items.iter().find_map(|(key, val)| match key {
							DictKey::String(key) => match arg_as_string {
								None => None,
								Some(arg) => (key.as_str() == &contents[arg.byte_range()]).then_some(*val),
							},
							DictKey::Type(key) => (*key == argtype).then_some(*val),
						})
					}
					_ => None,
				}
			}
			Type::Env
			| Type::Record(..)
			| Type::Model(..)
			| Type::HttpRequest
			| Type::Value
			| Type::PyBuiltin(..)
			| Type::Dict(..)
			| Type::DictBag(..)
			| Type::List(..)
			| Type::Iterable(..)
			| Type::Tuple(..)
			| Type::PythonMethod(..) => None,
		}
	}

	#[instrument(skip_all, fields(model, method))]
	pub fn prepare_call_scope(
		&self,
		model: ModelName,
		method: Symbol<Method>,
		call: Node,
		scope: &Scope,
		contents: &str,
	) -> Option<(Vec<ImStr>, Scope)> {
		// (call
		//   (arguments_list
		//     (_)
		//     (keyword_argument (identifier) (_))))
		let arguments_list = dig!(call, argument_list(1))?;

		let model = self.models.populate_properties(model, &[])?;
		let method = model.methods.as_ref()?.get(&method)?;
		let arguments = method.arguments.clone().unwrap_or_default();
		if arguments.is_empty() {
			return None;
		}

		drop(model);
		let mut argtypes = Scope::new(None);
		let mut args = vec![];
		for (idx, arg) in arguments_list.named_children(&mut arguments_list.walk()).enumerate() {
			if arg.kind() == "keyword_argument"
				&& let Some(key) = arg.child_by_field_name("key")
				&& let Some(value) = arg.child_by_field_name("value")
			{
				let key = &contents[key.byte_range()];
				if !arguments.iter().any(|arg| match arg {
					FunctionParam::Named(arg) => arg.as_str() == key,
					_ => false,
				}) {
					continue;
				}
				let Some(tid) = self.type_of(value, scope, contents) else {
					continue;
				};
				args.push(key.into());
				argtypes.insert(key.to_string(), _TR!(tid).clone());
			} else if let Some(FunctionParam::Param(argname)) = arguments.get(idx)
				&& let Some(tid) = self.type_of(arg, scope, contents)
			{
				args.push(argname.clone());
				argtypes.insert(argname.to_string(), _TR!(tid).clone());
			} else {
				continue;
			}
		}

		Some((args, argtypes))
	}
	#[instrument(skip_all, ret)]
	fn type_of_attribute_node(&self, attribute: Node<'_>, scope: &Scope, contents: &str) -> Option<TypeId> {
		let lhs = attribute.named_child(0)?;
		let lhsid = self.type_of(lhs, scope, contents)?;
		let lhs = _TR!(lhsid);
		let rhs = attribute.named_child(1)?;
		let attrname = &contents[rhs.byte_range()];
		match &contents[rhs.byte_range()] {
			"env" if matches!(lhs, Type::Model(..) | Type::Record(..) | Type::HttpRequest) => Some(Type::Env),
			"website" if matches!(lhs, Type::HttpRequest) => Some(Type::Model("website".into())),
			"ref" if matches!(lhs, Type::Env) => Some(Type::RefFn),
			"user" if matches!(lhs, Type::Env) => Some(Type::Model("res.users".into())),
			"company" | "companies" if matches!(lhs, Type::Env) => Some(Type::Model("res.company".into())),
			"mapped" | "grouped" | "_read_group" | "read" => {
				let model = self.try_resolve_model(lhs, scope)?;
				Some(Type::Method(model, attrname.into()))
			}
			dict_method @ ("items" | "get") if lhs.is_dictlike() => Some(Type::PythonMethod(lhsid, dict_method.into())),
			func if MODEL_METHODS.contains(func) => match lhs {
				Type::Model(model) => Some(Type::ModelFn(model.clone())),
				Type::Record(xml_id) => {
					let xml_id = _G(xml_id)?;
					let record = self.records.get(&xml_id)?;
					Some(Type::ModelFn(_R(*record.model.as_deref()?).into()))
				}
				_ => None,
			},
			ident if rhs.kind() == "identifier" => self.type_of_attribute(lhs, ident, scope),
			_ => None,
		}
		.map(|it| _T!(it))
	}
	#[instrument(skip_all, fields(attr=attr), ret)]
	pub fn type_of_attribute(&self, type_: &Type, attr: &str, scope: &Scope) -> Option<Type> {
		let model = self.try_resolve_model(type_, scope)?;
		let model_entry = self.models.populate_properties(model, &[])?;
		if let Some(attr_key) = _G(attr)
			&& let Some(attr_kind) = model_entry.prop_kind(attr_key)
		{
			match attr_kind {
				PropertyInfo::Field(type_) => {
					drop(model_entry);
					if let Some(relation) = self.models.resolve_related_field(attr_key.into(), model.into()) {
						return Some(Type::Model(_R(relation).into()));
					}

					match _R(type_) {
						"Selection" | "Char" | "Text" | "Html" => Some(Type::PyBuiltin("str".into())),
						"Integer" => Some(Type::PyBuiltin("int".into())),
						"Float" | "Monetary" => Some(Type::PyBuiltin("float".into())),
						"Date" => Some(Type::PyBuiltin("date".into())),
						"Datetime" => Some(Type::PyBuiltin("datetime".into())),
						_ => None,
					}
				}
				PropertyInfo::Method => Some(Type::Method(model, attr.into())),
			}
		} else {
			match attr {
				"id" if matches!(type_, Type::Model(..) | Type::Record(..)) => Some(Type::PyBuiltin("Id".into())),
				"ids" if matches!(type_, Type::Model(..) | Type::Record(..)) => {
					Some(Type::List(ListElement::Occupied(_T!(Type::PyBuiltin("Id".into())))))
				}
				"display_name" if matches!(type_, Type::Model(..) | Type::Record(..)) => {
					Some(Type::PyBuiltin("str".into()))
				}
				"create_date" | "write_date" if matches!(type_, Type::Model(..) | Type::Record(..)) => {
					Some(Type::PyBuiltin("datetime".into()))
				}
				"create_uid" | "write_uid" if matches!(type_, Type::Model(..) | Type::Record(..)) => {
					Some(Type::Model("res.users".into()))
				}
				"_fields" if matches!(type_, Type::Model(..) | Type::Record(..)) => {
					Some(Type::Dict(_T!(Type::PyBuiltin("str".into())), _T!["ir.model.fields"]))
				}
				"env" if matches!(type_, Type::Model(..) | Type::Record(..) | Type::HttpRequest) => Some(Type::Env),
				_ => None,
			}
		}
	}
	pub fn has_attribute(&self, type_: &Type, attr: &str, scope: &Scope) -> bool {
		(|| -> Option<()> {
			let model = self.try_resolve_model(type_, scope)?;
			let entry = self.models.populate_properties(model, &[])?;
			let attr = _G(attr)?;
			entry.prop_kind(attr).map(|_| ())
		})()
		.is_some()
	}
	/// Call this method if it's unclear whether `type_` is a [`Type::Model`] and you just want the model's name.
	pub fn try_resolve_model(&self, type_: &Type, scope: &Scope) -> Option<ModelName> {
		match type_ {
			Type::Model(model) => Some(_G(model)?.into()),
			Type::Record(xml_id) => {
				// TODO: Refactor into method
				let xml_id = _G(xml_id)?;
				let record = self.records.get(&xml_id)?;
				record.model
			}
			Type::Super => self.try_resolve_model(scope.get(scope.super_.as_deref()?)?, scope),
			_ => None,
		}
	}
	#[inline]
	pub fn type_display(&self, type_: TypeId) -> Option<String> {
		self.type_display_indent(type_, 0)
	}
	fn type_display_indent(&self, type_: TypeId, indent: usize) -> Option<String> {
		match _TR!(type_) {
			Type::Dict(lhs, rhs) => {
				let lhs = self.type_display_indent(*lhs, indent);
				let lhs = lhs.as_deref().unwrap_or("...");
				let rhs = self.type_display_indent(*rhs, indent);
				let rhs = rhs.as_deref().unwrap_or("...");
				Some(fomat! { "dict[" (lhs) ", " (rhs) "]" })
			}
			Type::DictBag(properties) => {
				let preindent = " ".repeat(indent + 2);
				let empty_properties = properties.is_empty();
				let properties_fragment = fomat! {
					for (key, value) in properties {
						(preindent)
						match key {
							DictKey::String(key) => { "\"" (key) "\"" }
							DictKey::Type(key) if key.is_dictlike() => { "{...}" }
							DictKey::Type(key) => { (self.type_display_indent(*key, indent + 2).as_deref().unwrap_or("...")) }
						} ": " (self.type_display_indent(*value, indent + 2).as_deref().unwrap_or("..."))
					} sep { ",\n" }
				};
				let unindent = " ".repeat(indent);
				Some(fomat! {
					if !empty_properties {
						"{\n" (properties_fragment) "\n" (unindent) "}"
					} else {
						"{}"
					}
				})
			}
			Type::PyBuiltin(builtin) => Some(builtin.as_str().into()),
			Type::List(slot) => {
				let slot = match slot {
					ListElement::Vacant => None,
					ListElement::Occupied(slot) => self.type_display_indent(*slot, indent),
				};
				Some(match slot {
					Some(slot) => format!("list[{slot}]"),
					None => "list".into(),
				})
			}
			Type::Env => Some("Environment".into()),
			Type::Model(model) => Some(format!(r#"Model["{model}"]"#)),
			Type::Record(xml_id) => {
				let xml_id = _G(xml_id)?;
				let record = self.records.get(&xml_id)?;
				Some(_R(record.model?).into())
			}
			Type::Tuple(items) => Some(fomat! {
				"tuple["
				for item in items {
					(self.type_display_indent(*item, indent).as_deref().unwrap_or("..."))
				} sep { ", " }
				"]"
			}),
			Type::Iterable(output) => {
				let output = output.and_then(|inner| self.type_display_indent(inner, indent));
				let output = output.as_deref().unwrap_or("...");
				Some(format!("Iterable[{output}]"))
			}
			Type::Method(..) => unreachable!("Bug: this function should not handle methods"),
			Type::RefFn | Type::ModelFn(_) | Type::Super | Type::HttpRequest | Type::Value | Type::PythonMethod(..) => {
				if cfg!(debug_assertions) {
					Some(format!("{type_:?}"))
				} else {
					None
				}
			}
		}
	}
	/// Iterates depth-first over `node` using [`PreTravel`]. Automatically calls [`Scope::exit`] at suitable points.
	///
	/// [`ControlFlow::Continue`] accepts a boolean to indicate whether [`Scope::enter`] was called.
	///
	/// To accumulate bindings into a scope, use [`Index::build_scope`].
	pub fn walk_scope<T>(
		node: Node,
		scope: Option<Scope>,
		mut step: impl FnMut(&mut Scope, Node) -> ControlFlow<Option<T>, bool>,
	) -> (Scope, Option<T>) {
		let mut scope = scope.unwrap_or_default();
		let mut scope_ends = vec![];
		for node in PreTravel::new(node) {
			if !node.is_named() {
				continue;
			}
			if let Some(&end) = scope_ends.last()
				&& node.start_byte() > end
			{
				scope.exit();
				scope_ends.pop();
			}
			match step(&mut scope, node) {
				ControlFlow::Break(value) => return (scope, value),
				ControlFlow::Continue(entered) => {
					if entered {
						scope_ends.push(node.end_byte());
					}
				}
			}
		}
		(scope, None)
	}
	/// Resolves the return type of a method as well as populating its arguments and docstring.
	///
	/// `parameters` can be provided using [`Index::prepare_call_scope`].  
	#[instrument(level = "trace", ret, skip(self, model), fields(model = _R(model)))]
	pub fn eval_method_rtype(
		&self,
		method: Symbol<Method>,
		model: Spur,
		parameters: Option<(Vec<ImStr>, Scope)>,
	) -> Option<TypeId> {
		_ = self.models.populate_properties(model.into(), &[]);
		let mut model_entry = self.models.try_get_mut(&model).expect(format_loc!("deadlock"))?;
		let method_obj = model_entry.methods.as_mut()?.get_mut(&method)?;

		if method_obj
			.pending_eval
			.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
			.is_err()
		{
			return None;
		}

		let _guard = Defer(Some(|| {
			if let Some(model_entry) = self.models.get_mut(&model)
				&& let Some(methods) = model_entry.methods.as_ref()
				&& let Some(method) = methods.get(&method)
			{
				method.pending_eval.store(false, Ordering::Relaxed);
			}
		}));

		let (argnames, mut scope) = parameters.unwrap_or_default();
		let cache_key = argnames
			.into_iter()
			.map(|arg| _T!(scope.get(&*arg).cloned().unwrap_or(Type::Value)))
			.collect::<Vec<_>>();
		if let Some(tid) = method_obj.eval_cache.get(&cache_key) {
			drop(model_entry);
			return Some(tid);
		}

		let location = method_obj.locations.first().cloned()?;
		drop(model_entry);

		let ast;
		let contents;
		let end_offset: ByteOffset;
		let path = location.path.to_path();
		if let Some(cached) = self.ast_cache.get(&path) {
			end_offset = rope_conv(location.range.end, cached.rope.slice(..));
			ast = cached.tree.clone();
			contents = String::from(cached.rope.clone());
		} else {
			contents = test_utils::fs::read_to_string(location.path.to_path()).unwrap();
			let rope = Rope::from_str(&contents);
			end_offset = rope_conv(location.range.end, rope.slice(..));
			let mut parser = Parser::new();
			parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
			ast = parser.parse(contents.as_bytes(), None)?;
			self.ast_cache.insert(
				path,
				Arc::new(crate::index::AstCacheItem {
					tree: ast.clone(),
					rope,
				}),
			);
		}

		// TODO: Improve this heuristic
		fn is_toplevel_return(mut node: Node) -> bool {
			while node.kind() != "function_definition" {
				tracing::trace!("{}", node.kind());
				match node.parent() {
					Some(parent) => node = parent,
					None => return false,
				}
			}

			fn is_block_of_class(node: Node) -> bool {
				node.kind() == "block" && node.parent().is_some_and(|parent| parent.kind() == "class_definition")
			}

			if let Some(decoration) = node.parent()
				&& decoration.kind() == "decorated_definition"
			{
				return decoration.parent().is_some_and(is_block_of_class);
			}

			node.parent().is_some_and(is_block_of_class)
		}

		let (self_type, fn_scope, self_param) = determine_scope(ast.root_node(), &contents, end_offset.0)?;
		let self_type = match self_type {
			Some(type_) => &contents[type_.byte_range().shrink(1)],
			None => "",
		};
		scope.super_ = Some(self_param.into());
		scope.insert(self_param.to_string(), Type::Model(self_type.into()));
		let offset = fn_scope.end_byte();
		let (_, type_) = Self::walk_scope(fn_scope, Some(scope), |scope, node| {
			let entered = self.build_scope(scope, node, offset, &contents).map_break(|_| None)?;
			// TODO: When implementing freestanding functions, make the toplevel check optional
			if node.kind() == "return_statement" && is_toplevel_return(node) {
				let Some(child) = node.named_child(0) else {
					return ControlFlow::Continue(entered);
				};
				let Some(type_) = self.type_of(child, scope, &contents) else {
					return ControlFlow::Continue(entered);
				};

				let type_ = _TR!(type_);
				return match self.try_resolve_model(type_, scope) {
					Some(resolved) => ControlFlow::Break(Some(Type::Model(ImStr::from(_R(resolved))))),
					None => ControlFlow::Break(Some(type_.clone())),
				};
			}

			ControlFlow::Continue(entered)
		});

		let mut model = self.models.try_get_mut(&model).expect(format_loc!("deadlock"))?;
		let method = Arc::make_mut(model.methods.as_mut()?.get_mut(&method)?);

		let docstring = Self::parse_method_docstring(fn_scope, &contents)
			.map(|doc| ImStr::from(Method::postprocess_docstring(doc)));
		method.docstring = docstring;

		if let Some(params) = fn_scope.child_by_field_name("parameters") {
			// python parameters can be delineated by three separators:
			// - parameters before `/` are positional only (`positional_separator`)
			// - parameters between `/` and `*` can be either positional or named
			// - a catch-all positional `*args` (`keyword_separator` or `list_splat_pattern`)
			// - parameters after `*` and before `**` are named (`default_parameter`)
			// - a catch-all named `**kwargs` (`dictionary_splat_pattern` only)
			let mut cursor = params.walk();
			let args = params.named_children(&mut cursor).skip(1).filter_map(|param| {
				Some(match param.kind() {
					"identifier" => FunctionParam::Param(ImStr::from(&contents[param.byte_range()])),
					"positional_separator" => FunctionParam::PosEnd,
					"keyword_separator" => FunctionParam::EitherEnd(None),
					"list_splat_pattern" => {
						FunctionParam::EitherEnd(Some(ImStr::from(&contents[param.named_child(0)?.byte_range()])))
					}
					"dictionary_splat_pattern" => FunctionParam::Kwargs("kwargs".into()),
					"default_parameter" => {
						let name = param.named_child(0)?;
						let name = &contents[name.byte_range()];
						FunctionParam::Named(ImStr::from(name))
					}
					_ => return None,
				})
			});
			method.arguments = Some(args.collect());
		}

		method.pending_eval.store(false, Ordering::Release);
		if let Some(type_) = type_ {
			let tid = _T!(type_);
			method.eval_cache.insert(cache_key, tid);
			Some(tid)
		} else {
			None
		}
	}
	/// `pattern` is `(identifier | pattern_list | tuple_pattern)`, the `a, b` in `for a, b in ...`.
	fn destructure_into_patternlist_like(&self, pattern: Node, tid: TypeId, scope: &mut Scope, contents: &str) {
		if pattern.kind() == "identifier" {
			let name = &contents[pattern.byte_range()];
			scope.insert(name.to_string(), _TR!(tid).clone());
		} else if matches!(pattern.kind(), "pattern_list" | "tuple_pattern") {
			if let Type::Tuple(inner) = _TR!(tid) {
				let mut inner = inner.iter();
				for child in pattern.named_children(&mut pattern.walk()) {
					if matches!(child.kind(), "identifier" | "tuple_pattern")
						&& let Some(type_) = inner.next()
					{
						self.destructure_into_patternlist_like(child, *type_, scope, contents);
					}
				}
			} else if let Some(inner) = self.type_of_iterable(tid) {
				// spread this type to all params
				for child in pattern.named_children(&mut pattern.walk()) {
					if matches!(child.kind(), "identifier" | "tuple_pattern") {
						self.destructure_into_patternlist_like(child, inner, scope, contents);
					}
				}
			}
		}
	}
	fn parse_method_docstring<'out>(fn_scope: Node, contents: &'out str) -> Option<&'out str> {
		let block = fn_scope.child_by_field_name("body")?;
		dig!(block, expression_statement.string.string_content(1)).map(|node| &contents[node.byte_range()])
	}
}

/// Returns `(self_type, fn_scope, self_param)`.
///
/// `fn_scope` is customarily a `function_definition` node.
#[instrument(level = "trace", skip_all, ret)]
pub fn determine_scope<'out, 'node>(
	node: Node<'node>,
	contents: &'out str,
	offset: usize,
) -> Option<(Option<Node<'node>>, Node<'node>, &'out str)> {
	let query = FieldCompletion::query();
	let mut self_type = None;
	let mut self_param = None;
	let mut fn_scope = None;
	let mut cursor = QueryCursor::new();
	let mut matches = cursor.matches(query, node, contents.as_bytes());
	'scoping: while let Some(match_) = matches.next() {
		// @class
		let class = match_.captures.first()?;
		if !class.node.byte_range().contains_end(offset) {
			continue;
		}
		for capture in match_.captures {
			match FieldCompletion::from(capture.index) {
				Some(FieldCompletion::Name) => {
					if self_type.is_none() {
						self_type = Some(capture.node);
					}
				}
				Some(FieldCompletion::SelfParam) => {
					self_param = Some(capture.node);
				}
				Some(FieldCompletion::Scope) => {
					if !capture.node.byte_range().contains_end(offset) {
						continue 'scoping;
					}
					fn_scope = Some(capture.node);
				}
				None => {}
			}
		}
		if fn_scope.is_some() {
			break;
		}
	}
	let fn_scope = fn_scope?;
	let self_param = &contents[self_param?.byte_range()];
	Some((self_type, fn_scope, self_param))
}

#[cfg(test)]
mod tests {
	use pretty_assertions::assert_eq;
	use ropey::Rope;
	use tower_lsp_server::ls_types::Position;
	use tree_sitter::{Parser, QueryCursor, StreamingIterator, StreamingIteratorMut};

	use crate::analyze::{FieldCompletion, Type, type_cache};
	use crate::index::_I;
	use crate::utils::{ByteOffset, acc_vec, rope_conv};
	use crate::{index::Index, test_utils::cases::foo::prepare_foo_index};

	#[test]
	fn test_field_completion() {
		let mut parser = Parser::new();
		parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
		let contents = br#"
class Foo(models.AbstractModel):
	_name = 'foo'
	_description = 'What?'
	_inherit = 'inherit_foo'
	foo = fields.Char(related='related')
	@api.depends('mapped')
	def foo(self):
		pass
"#;
		let ast = parser.parse(&contents[..], None).unwrap();
		let query = FieldCompletion::query();
		let mut cursor = QueryCursor::new();
		let actual = cursor
			.matches(query, ast.root_node(), &contents[..])
			.map(|match_| {
				match_
					.captures
					.iter()
					.map(|capture| FieldCompletion::from(capture.index))
					.collect::<Vec<_>>()
			})
			.fold_mut(vec![], acc_vec);
		// Allow nested patterns
		let actual = actual.iter().map(Vec::as_slice).collect::<Vec<_>>();
		use FieldCompletion as T;
		assert!(
			matches!(
				&actual[..],
				[
					[None, None, Some(T::Name), Some(T::Scope), Some(T::SelfParam)],
					[None, None, Some(T::Name), Some(T::Scope), Some(T::SelfParam)]
				]
			),
			"{actual:?}"
		)
	}

	#[test]
	fn test_determine_scope() {
		let mut parser = Parser::new();
		parser.set_language(&tree_sitter_python::LANGUAGE.into()).unwrap();
		let contents = r#"
class Foo(models.Model):
	_name = 'foo'
	def scope(self):
		pass
"#;
		let ast = parser.parse(contents, None).unwrap();
		let rope = Rope::from(contents);
		let fn_start: ByteOffset = rope_conv(Position { line: 3, character: 1 }, rope.slice(..));
		let fn_scope = ast
			.root_node()
			.named_descendant_for_byte_range(fn_start.0, fn_start.0)
			.unwrap();
		super::determine_scope(ast.root_node(), contents, fn_start.0)
			.unwrap_or_else(|| panic!("{}", fn_scope.to_sexp()));
	}

	#[test]
	fn test_resolve_method_returntype() {
		let index = Index {
			models: prepare_foo_index(),
			..Default::default()
		};

		assert_eq!(
			index.eval_method_rtype(_I("test").into(), _I("bar"), None),
			Some(type_cache().get_or_intern(Type::Model("foo".into())))
		)
	}

	#[test]
	fn test_super_analysis() {
		let index = Index {
			models: prepare_foo_index(),
			..Default::default()
		};

		assert_eq!(
			index.eval_method_rtype(_I("test").into(), _I("quux"), None),
			Some(type_cache().get_or_intern(Type::Model("foo".into())))
		)
	}
}