corsa_client 0.37.0

Typed stdio API client bindings for Corsa
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
//! Relationship-oriented `ApiClient` methods.
//!
//! This group answers questions about how types, signatures, and symbols relate
//! to each other after the checker has already identified them.

use super::{
    ApiClient, IndexInfo, ProjectHandle, SignatureHandle, SnapshotHandle, SymbolHandle, TypeHandle,
    TypePredicateResponse, TypeResponse,
    requests_symbols::{SignatureOnlyRequest, TypeOnlyRequest, TypeProjectRequest},
};
use crate::Result;
use corsa_core::utils::split_top_level_type_text;

impl ApiClient {
    /// Returns the symbol attached to a type, if one exists.
    pub async fn get_symbol_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<super::SymbolResponse>> {
        self.call_optional("getSymbolOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the return type of a signature.
    pub async fn get_return_type_of_signature(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        signature: SignatureHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional(
            "getReturnTypeOfSignature",
            SignatureOnlyRequest {
                snapshot,
                project,
                signature,
            },
        )
        .await
    }

    /// Returns the rest type of a signature, if any.
    pub async fn get_rest_type_of_signature(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        signature: SignatureHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional(
            "getRestTypeOfSignature",
            SignatureOnlyRequest {
                snapshot,
                project,
                signature,
            },
        )
        .await
    }

    /// Returns the type predicate declared on a signature, if any.
    pub async fn get_type_predicate_of_signature(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        signature: SignatureHandle,
    ) -> Result<Option<TypePredicateResponse>> {
        self.call_optional(
            "getTypePredicateOfSignature",
            SignatureOnlyRequest {
                snapshot,
                project,
                signature,
            },
        )
        .await
    }

    /// Returns the immediate base types of a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_base_types(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        self.get_base_types_with_texts(snapshot, project, r#type, &[])
            .await
    }

    /// Returns the immediate base types of a type, using already-rendered type
    /// text as a fallback when the upstream handle can no longer be rendered.
    pub async fn get_base_types_with_texts(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
        type_texts: &[String],
    ) -> Result<Vec<TypeResponse>> {
        let bases = self
            .get_base_types_direct(snapshot.clone(), project.clone(), r#type.clone())
            .await?;
        if !bases.is_empty() {
            return Ok(bases);
        }

        if let Some(target) = self
            .get_target_of_type_or_none(snapshot.clone(), r#type.clone())
            .await?
            .filter(|target| target.id != r#type)
        {
            let target_bases = self
                .get_base_types_direct(snapshot.clone(), project.clone(), target.id)
                .await?;
            if !target_bases.is_empty() {
                return Ok(target_bases);
            }
        }

        let mut construct_return_bases = Vec::new();
        let construct_signatures = self
            .get_signatures_of_type_or_empty(snapshot.clone(), project.clone(), r#type.clone(), 1)
            .await?;
        for signature in construct_signatures {
            let Some(return_type) = self
                .get_return_type_of_signature_or_none(
                    snapshot.clone(),
                    project.clone(),
                    signature.id,
                )
                .await?
                .filter(|return_type| return_type.id != r#type)
            else {
                continue;
            };
            append_unique_types(&mut construct_return_bases, vec![return_type.clone()]);
            let mut return_bases = self
                .get_base_types_direct(snapshot.clone(), project.clone(), return_type.id.clone())
                .await?;
            if return_bases.is_empty() {
                if let Some(target) = self
                    .get_target_of_type_or_none(snapshot.clone(), return_type.id.clone())
                    .await?
                    .filter(|target| target.id != return_type.id)
                {
                    return_bases = self
                        .get_base_types_direct(snapshot.clone(), project.clone(), target.id)
                        .await?;
                }
            }
            if return_bases.is_empty() {
                return_bases = self
                    .get_types_of_type(snapshot.clone(), return_type.id)
                    .await?;
            }
            append_unique_types(&mut construct_return_bases, return_bases);
        }
        if !construct_return_bases.is_empty() {
            return Ok(construct_return_bases);
        }

        let intersection_parts = self
            .get_types_of_type(snapshot.clone(), r#type.clone())
            .await?;
        if !intersection_parts.is_empty() {
            return Ok(intersection_parts);
        }

        let text_parts = self
            .fallback_intersection_parts_from_text(snapshot, project, r#type.clone())
            .await?;
        if !text_parts.is_empty() {
            return Ok(text_parts);
        }

        let hinted_text_parts =
            fallback_intersection_parts_from_type_texts(r#type.as_str(), type_texts);
        if !hinted_text_parts.is_empty() {
            return Ok(hinted_text_parts);
        }

        Ok(Vec::new())
    }

    async fn get_base_types_direct(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        match self
            .call::<Option<Vec<TypeResponse>>, _>(
                "getBaseTypes",
                TypeProjectRequest {
                    snapshot,
                    project,
                    r#type,
                },
            )
            .await
        {
            Ok(items) => Ok(items.unwrap_or_default()),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(Vec::new())
            }
            Err(error) => Err(error),
        }
    }

    async fn get_target_of_type_or_none(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        match self.get_target_of_type(snapshot, r#type).await {
            Ok(target) => Ok(target),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    async fn get_signatures_of_type_or_empty(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
        kind: i32,
    ) -> Result<Vec<super::SignatureResponse>> {
        match self
            .get_signatures_of_type(snapshot, project, r#type, kind)
            .await
        {
            Ok(signatures) => Ok(signatures),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(Vec::new())
            }
            Err(error) => Err(error),
        }
    }

    async fn get_return_type_of_signature_or_none(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        signature: SignatureHandle,
    ) -> Result<Option<TypeResponse>> {
        match self
            .get_return_type_of_signature(snapshot, project, signature)
            .await
        {
            Ok(return_type) => Ok(return_type),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    /// Returns the properties exposed by a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_properties_of_type(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<super::SymbolResponse>> {
        self.call::<Option<Vec<super::SymbolResponse>>, _>(
            "getPropertiesOfType",
            TypeProjectRequest {
                snapshot,
                project,
                r#type,
            },
        )
        .await
        .map(|items| items.unwrap_or_default())
    }

    /// Returns index signature information for a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_index_infos_of_type(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<IndexInfo>> {
        self.call::<Option<Vec<IndexInfo>>, _>(
            "getIndexInfosOfType",
            TypeProjectRequest {
                snapshot,
                project,
                r#type,
            },
        )
        .await
        .map(|items| items.unwrap_or_default())
    }

    /// Returns the constraint of a type parameter, if one exists.
    pub async fn get_constraint_of_type_parameter(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional(
            "getConstraintOfTypeParameter",
            TypeProjectRequest {
                snapshot,
                project,
                r#type,
            },
        )
        .await
    }

    /// Returns the type arguments of an instantiated type.
    ///
    /// Missing server data is normalized to an empty vector. A stale type
    /// handle that the server has dropped from its snapshot registry is also
    /// treated as "no type arguments" so callers can keep analyzing the type.
    pub async fn get_type_arguments(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        match self
            .call::<Option<Vec<TypeResponse>>, _>(
                "getTypeArguments",
                TypeProjectRequest {
                    snapshot: snapshot.clone(),
                    project: project.clone(),
                    r#type: r#type.clone(),
                },
            )
            .await
        {
            Ok(Some(items)) if !items.is_empty() => {
                self.structural_type_arguments_from_symbols(snapshot, project, items)
                    .await
            }
            Ok(_) => {
                self.fallback_type_arguments_from_text(snapshot, project, r#type)
                    .await
            }
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                self.fallback_type_arguments_from_text(snapshot, project, r#type)
                    .await
            }
            Err(error) => Err(error),
        }
    }

    async fn structural_type_arguments_from_symbols(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        items: Vec<TypeResponse>,
    ) -> Result<Vec<TypeResponse>> {
        let mut structural = Vec::with_capacity(items.len());
        for item in items {
            let Some(symbol) = item.symbol.clone() else {
                structural.push(item);
                continue;
            };
            let Some(candidate) = self
                .symbol_type_or_none(snapshot.clone(), project.clone(), symbol)
                .await?
            else {
                structural.push(item);
                continue;
            };
            if self
                .same_type_text(snapshot.clone(), project.clone(), &item, &candidate)
                .await?
            {
                structural.push(candidate);
            } else {
                structural.push(item);
            }
        }
        Ok(structural)
    }

    async fn symbol_type_or_none(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        symbol: SymbolHandle,
    ) -> Result<Option<TypeResponse>> {
        match self
            .get_declared_type_of_symbol(snapshot.clone(), project.clone(), symbol.clone())
            .await
        {
            Ok(Some(r#type)) => Ok(Some(r#type)),
            Ok(None) => self.type_of_symbol_or_none(snapshot, project, symbol).await,
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    async fn type_of_symbol_or_none(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        symbol: SymbolHandle,
    ) -> Result<Option<TypeResponse>> {
        match self.get_type_of_symbol(snapshot, project, symbol).await {
            Ok(r#type) => Ok(r#type),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    async fn same_type_text(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        left: &TypeResponse,
        right: &TypeResponse,
    ) -> Result<bool> {
        if left.id == right.id || type_response_texts_overlap(left, right) {
            return Ok(true);
        }
        let Some(left_text) = self
            .type_response_text_or_none(snapshot.clone(), project.clone(), left)
            .await?
        else {
            return Ok(false);
        };
        let Some(right_text) = self
            .type_response_text_or_none(snapshot, project, right)
            .await?
        else {
            return Ok(false);
        };
        Ok(left_text == right_text)
    }

    async fn type_response_text_or_none(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: &TypeResponse,
    ) -> Result<Option<String>> {
        if let Some(text) = r#type.texts.iter().find(|text| !text.is_empty()) {
            return Ok(Some(text.clone()));
        }
        match self
            .type_to_string(snapshot, project, r#type.id.clone(), None, None)
            .await
        {
            Ok(text) => Ok(Some(text)),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    /// Returns the target type underlying an instantiated or mapped type.
    pub async fn get_target_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getTargetOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns nested or constituent types associated with a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_types_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        match self
            .call::<Option<Vec<TypeResponse>>, _>(
                "getTypesOfType",
                TypeOnlyRequest { snapshot, r#type },
            )
            .await
        {
            Ok(items) => Ok(items.unwrap_or_default()),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(Vec::new())
            }
            Err(error) => Err(error),
        }
    }

    /// Returns the direct type parameters declared on a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_type_parameters_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        self.call::<Option<Vec<TypeResponse>>, _>(
            "getTypeParametersOfType",
            TypeOnlyRequest { snapshot, r#type },
        )
        .await
        .map(|items| items.unwrap_or_default())
    }

    /// Returns outer type parameters captured by a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_outer_type_parameters_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        self.call::<Option<Vec<TypeResponse>>, _>(
            "getOuterTypeParametersOfType",
            TypeOnlyRequest { snapshot, r#type },
        )
        .await
        .map(|items| items.unwrap_or_default())
    }

    /// Returns local type parameters introduced while resolving a type.
    ///
    /// Missing server data is normalized to an empty vector.
    pub async fn get_local_type_parameters_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        self.call::<Option<Vec<TypeResponse>>, _>(
            "getLocalTypeParametersOfType",
            TypeOnlyRequest { snapshot, r#type },
        )
        .await
        .map(|items| items.unwrap_or_default())
    }

    /// Returns the object side of a wrapper type, if one exists.
    pub async fn get_object_type_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getObjectTypeOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the index side of a wrapper type, if one exists.
    pub async fn get_index_type_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getIndexTypeOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the check side of a wrapper or conditional type, if one exists.
    pub async fn get_check_type_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getCheckTypeOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the `extends` side of a conditional type, if one exists.
    pub async fn get_extends_type_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getExtendsTypeOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the base type recorded directly on a type, if one exists.
    pub async fn get_base_type_of_type(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        self.call_optional("getBaseTypeOfType", TypeOnlyRequest { snapshot, r#type })
            .await
    }

    /// Returns the constraint recorded directly on a type, if one exists.
    pub async fn get_constraint_of_type(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        if let Some(constraint) = self
            .get_constraint_of_type_parameter(snapshot.clone(), project, r#type.clone())
            .await?
        {
            return Ok(Some(constraint));
        }
        self.get_constraint_of_type_direct(snapshot, r#type).await
    }

    async fn get_constraint_of_type_direct(
        &self,
        snapshot: SnapshotHandle,
        r#type: TypeHandle,
    ) -> Result<Option<TypeResponse>> {
        match self
            .call_optional("getConstraintOfType", TypeOnlyRequest { snapshot, r#type })
            .await
        {
            Ok(constraint) => Ok(constraint),
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                Ok(None)
            }
            Err(error) => Err(error),
        }
    }

    async fn fallback_type_arguments_from_text(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        let text = match self
            .type_to_string(snapshot, project, r#type.clone(), None, None)
            .await
        {
            Ok(text) => text,
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                return Ok(Vec::new());
            }
            Err(error) => return Err(error),
        };
        let Some(arguments) = generic_argument_text(&text) else {
            return Ok(Vec::new());
        };
        Ok(split_top_level_type_text(arguments, ',')
            .into_iter()
            .enumerate()
            .map(|(index, text)| synthetic_type_response(r#type.as_str(), index, text))
            .collect())
    }

    async fn fallback_intersection_parts_from_text(
        &self,
        snapshot: SnapshotHandle,
        project: ProjectHandle,
        r#type: TypeHandle,
    ) -> Result<Vec<TypeResponse>> {
        let text = match self
            .type_to_string(snapshot, project, r#type.clone(), None, None)
            .await
        {
            Ok(text) => text,
            Err(error)
                if Self::is_stale_handle_error(&error) || Self::is_protocol_panic_error(&error) =>
            {
                return Ok(Vec::new());
            }
            Err(error) => return Err(error),
        };
        let parts = split_top_level_type_text(text.as_str(), '&');
        if parts.len() <= 1 {
            return Ok(Vec::new());
        }
        Ok(parts
            .into_iter()
            .enumerate()
            .map(|(index, text)| synthetic_type_response(r#type.as_str(), index, text))
            .collect())
    }
}

fn append_unique_types(target: &mut Vec<TypeResponse>, source: Vec<TypeResponse>) {
    for item in source {
        if target.iter().any(|existing| existing.id == item.id) {
            continue;
        }
        target.push(item);
    }
}

fn type_response_texts_overlap(left: &TypeResponse, right: &TypeResponse) -> bool {
    left.texts
        .iter()
        .filter(|text| !text.is_empty())
        .any(|left_text| right.texts.iter().any(|right_text| left_text == right_text))
}

fn fallback_intersection_parts_from_type_texts(
    source_type: &str,
    type_texts: &[String],
) -> Vec<TypeResponse> {
    for text in type_texts {
        let parts = split_top_level_type_text(text.as_str(), '&');
        if parts.len() <= 1 {
            continue;
        }
        return parts
            .into_iter()
            .enumerate()
            .map(|(index, text)| synthetic_type_response(source_type, index, text))
            .collect();
    }
    Vec::new()
}

fn generic_argument_text(text: &str) -> Option<&str> {
    let text = text.trim();
    let mut angle_depth = 0usize;
    let mut square_depth = 0usize;
    let mut paren_depth = 0usize;
    let mut brace_depth = 0usize;
    let mut quote = None;
    let mut open = None;
    for (index, ch) in text.char_indices() {
        if let Some(active_quote) = quote {
            if ch == active_quote {
                quote = None;
            }
            continue;
        }
        match ch {
            '\'' | '"' | '`' => quote = Some(ch),
            '<' if angle_depth == 0
                && square_depth == 0
                && paren_depth == 0
                && brace_depth == 0 =>
            {
                open.get_or_insert(index);
                angle_depth += 1;
            }
            '<' => angle_depth += 1,
            '>' if angle_depth > 0 => angle_depth -= 1,
            '[' => square_depth += 1,
            ']' if square_depth > 0 => square_depth -= 1,
            '(' => paren_depth += 1,
            ')' if paren_depth > 0 => paren_depth -= 1,
            '{' => brace_depth += 1,
            '}' if brace_depth > 0 => brace_depth -= 1,
            _ => {}
        }
    }
    let open = open?;
    if angle_depth != 0 || !text.ends_with('>') {
        return None;
    }
    Some(&text[open + 1..text.len() - 1])
}

fn synthetic_type_response(source_type: &str, index: usize, text: String) -> TypeResponse {
    TypeResponse {
        id: TypeHandle::from(format!(
            "synthetic-type-argument:{source_type}:{index}:{text}"
        )),
        flags: 0,
        object_flags: None,
        value: None,
        target: None,
        type_parameters: Vec::new(),
        outer_type_parameters: Vec::new(),
        local_type_parameters: Vec::new(),
        element_flags: Vec::new(),
        fixed_length: None,
        readonly: None,
        object_type: None,
        index_type: None,
        check_type: None,
        extends_type: None,
        base_type: None,
        subst_constraint: None,
        texts: vec![text],
        symbol: None,
    }
}