call-graph-cli 0.3.0

Interactive call and type hierarchy TUI
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
use std::{
    fs,
    path::{Path, PathBuf},
    sync::{
        Arc, Mutex,
        atomic::{AtomicU64, Ordering},
    },
};

use anyhow::{Context, Result, anyhow};
use tokio::{sync::watch, task};
use tree_sitter::{Parser, Query, Tree};

use crate::fetch::{HierarchyQuery, HierarchyResponse, WorkspaceSymbolMatch};

mod index;

use index::ProjectIndex;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TreeSitterLanguage {
    Rust,
    C,
    Cpp,
    Python,
}

impl TreeSitterLanguage {
    pub fn detect(workspace_root: &Path) -> Option<Self> {
        if workspace_root.join("Cargo.toml").is_file()
            || contains_source_with_extension(workspace_root, &["rs"])
        {
            return Some(Self::Rust);
        }
        if contains_source_with_extension(workspace_root, &["cc", "cpp", "cxx", "hpp"]) {
            return Some(Self::Cpp);
        }
        if workspace_root.join("compile_commands.json").is_file()
            || workspace_root.join("CMakeLists.txt").is_file()
            || contains_source_with_extension(workspace_root, &["c", "h"])
        {
            return Some(Self::C);
        }
        if workspace_root.join("pyproject.toml").is_file()
            || workspace_root.join("setup.py").is_file()
            || workspace_root.join("requirements.txt").is_file()
            || contains_source_with_extension(workspace_root, &["py"])
        {
            return Some(Self::Python);
        }

        None
    }

    pub fn name(self) -> &'static str {
        match self {
            Self::Rust => "Rust",
            Self::C => "C",
            Self::Cpp => "C++",
            Self::Python => "Python",
        }
    }

    pub(super) fn grammar(self) -> tree_sitter::Language {
        match self {
            Self::Rust => tree_sitter_rust::LANGUAGE.into(),
            Self::C => tree_sitter_c::LANGUAGE.into(),
            Self::Cpp => tree_sitter_cpp::LANGUAGE.into(),
            Self::Python => tree_sitter_python::LANGUAGE.into(),
        }
    }

    pub(super) fn tags_query(self) -> &'static str {
        match self {
            Self::Rust => tree_sitter_rust::TAGS_QUERY,
            Self::C => tree_sitter_c::TAGS_QUERY,
            Self::Cpp => tree_sitter_cpp::TAGS_QUERY,
            Self::Python => tree_sitter_python::TAGS_QUERY,
        }
    }

    pub(super) fn call_query(self) -> &'static str {
        match self {
            Self::Rust | Self::Python => self.tags_query(),
            Self::C | Self::Cpp => {
                r#"
                (call_expression
                    function: (identifier) @name) @reference.call
                (call_expression
                    function: (field_expression
                        field: (field_identifier) @name)) @reference.call
                "#
            }
        }
    }

    pub(super) fn accepts_path(self, path: &Path) -> bool {
        let extension = path.extension().and_then(|extension| extension.to_str());
        match self {
            Self::Rust => extension == Some("rs"),
            Self::C => matches!(extension, Some("c" | "h")),
            Self::Cpp => matches!(extension, Some("cc" | "cpp" | "cxx" | "h" | "hpp")),
            Self::Python => extension == Some("py"),
        }
    }
}

/// Initialized syntax parser used when no LSP session is available.
///
/// Grammar/query readiness is deliberately narrower than workspace-search
/// readiness: tags queries identify syntax captures in one parsed file, while
/// directory traversal, candidate normalization and hierarchy confidence still
/// need explicit provider semantics.
pub struct TreeSitterProvider {
    workspace_root: PathBuf,
    language: TreeSitterLanguage,
    parser: Parser,
    symbol_query: Query,
    shared: Arc<SharedIndex>,
}

struct SharedIndex {
    workspace_root: PathBuf,
    language: TreeSitterLanguage,
    state: Mutex<IndexState>,
    next_build_id: AtomicU64,
    #[cfg(test)]
    build_count: std::sync::atomic::AtomicUsize,
    #[cfg(test)]
    pause_build: std::sync::atomic::AtomicBool,
}

type IndexBuildResult = std::result::Result<Arc<ProjectIndex>, Arc<str>>;

// The build state lives outside individual query futures. Search debounce may
// abort a waiter, but must not abort or duplicate the project-wide scan.
enum IndexState {
    Empty,
    Building {
        build_id: u64,
        receiver: watch::Receiver<Option<IndexBuildResult>>,
    },
    Ready(Arc<ProjectIndex>),
}

#[derive(Clone)]
pub struct WorkspaceSymbolClient {
    shared: Arc<SharedIndex>,
}

#[derive(Clone)]
pub struct HierarchyClient {
    shared: Arc<SharedIndex>,
}

impl std::fmt::Debug for TreeSitterProvider {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TreeSitterProvider")
            .field("workspace_root", &self.workspace_root)
            .field("language", &self.language)
            .finish_non_exhaustive()
    }
}

impl TreeSitterProvider {
    pub fn start(workspace_root: impl Into<PathBuf>, language: TreeSitterLanguage) -> Result<Self> {
        let workspace_root = workspace_root.into();
        let grammar = language.grammar();
        let mut parser = Parser::new();
        parser
            .set_language(&grammar)
            .with_context(|| format!("failed to initialize {} grammar", language.name()))?;
        let symbol_query = Query::new(&grammar, language.tags_query())
            .with_context(|| format!("failed to initialize {} symbol query", language.name()))?;

        Ok(Self {
            shared: Arc::new(SharedIndex {
                workspace_root: workspace_root.clone(),
                language,
                state: Mutex::new(IndexState::Empty),
                next_build_id: AtomicU64::new(1),
                #[cfg(test)]
                build_count: std::sync::atomic::AtomicUsize::new(0),
                #[cfg(test)]
                pause_build: std::sync::atomic::AtomicBool::new(false),
            }),
            workspace_root,
            language,
            parser,
            symbol_query,
        })
    }

    pub fn workspace_root(&self) -> &Path {
        &self.workspace_root
    }

    pub fn language(&self) -> TreeSitterLanguage {
        self.language
    }

    pub fn parse(&mut self, source: &str) -> Result<Tree> {
        self.parser
            .parse(source, None)
            .with_context(|| format!("{} parser returned no syntax tree", self.language.name()))
    }

    pub fn symbol_capture_names(&self) -> &[&str] {
        self.symbol_query.capture_names()
    }

    pub fn workspace_symbol_client(&self) -> WorkspaceSymbolClient {
        WorkspaceSymbolClient {
            shared: Arc::clone(&self.shared),
        }
    }

    pub fn hierarchy_client(&self) -> HierarchyClient {
        HierarchyClient {
            shared: Arc::clone(&self.shared),
        }
    }
}

impl std::fmt::Debug for WorkspaceSymbolClient {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TreeSitterWorkspaceSymbolClient")
            .finish_non_exhaustive()
    }
}

impl WorkspaceSymbolClient {
    pub async fn query(&self, _query: &str) -> Result<Vec<WorkspaceSymbolMatch>> {
        Ok(load_index(&self.shared).await?.workspace_symbols())
    }
}

impl std::fmt::Debug for HierarchyClient {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TreeSitterHierarchyClient")
            .finish_non_exhaustive()
    }
}

impl HierarchyClient {
    pub async fn query(&self, query: HierarchyQuery) -> Result<HierarchyResponse> {
        load_index(&self.shared).await?.hierarchy(query)
    }
}

async fn load_index(shared: &Arc<SharedIndex>) -> Result<Arc<ProjectIndex>> {
    let (receiver, build) = {
        let mut state = shared
            .state
            .lock()
            .expect("Tree-sitter index state mutex poisoned");
        match &*state {
            IndexState::Ready(index) => return Ok(Arc::clone(index)),
            IndexState::Building { receiver, .. } => (receiver.clone(), None),
            IndexState::Empty => {
                let build_id = shared.next_build_id.fetch_add(1, Ordering::Relaxed);
                let (sender, receiver) = watch::channel(None);
                *state = IndexState::Building {
                    build_id,
                    receiver: receiver.clone(),
                };
                (receiver, Some((build_id, sender)))
            }
        }
    };

    if let Some((build_id, sender)) = build {
        spawn_index_build(Arc::clone(shared), build_id, sender);
    }

    wait_for_index(receiver).await
}

fn spawn_index_build(
    shared: Arc<SharedIndex>,
    build_id: u64,
    sender: watch::Sender<Option<IndexBuildResult>>,
) {
    let _build_task = tokio::spawn(async move {
        let workspace_root = shared.workspace_root.clone();
        let language = shared.language;
        #[cfg(test)]
        let build_shared = Arc::clone(&shared);
        let result = task::spawn_blocking(move || {
            #[cfg(test)]
            {
                use std::{thread, time::Duration};

                build_shared.build_count.fetch_add(1, Ordering::SeqCst);
                while build_shared.pause_build.load(Ordering::SeqCst) {
                    thread::sleep(Duration::from_millis(1));
                }
            }
            ProjectIndex::build(&workspace_root, language)
        })
        .await
        .context("Tree-sitter indexing task failed")
        .and_then(|result| result)
        .map(Arc::new)
        .map_err(|error| Arc::<str>::from(format!("{error:#}")));

        {
            let mut state = shared
                .state
                .lock()
                .expect("Tree-sitter index state mutex poisoned");
            if matches!(
                &*state,
                IndexState::Building {
                    build_id: active_build_id,
                    ..
                } if *active_build_id == build_id
            ) {
                *state = match &result {
                    Ok(index) => IndexState::Ready(Arc::clone(index)),
                    Err(_) => IndexState::Empty,
                };
            }
        }

        sender.send_replace(Some(result));
    });
}

async fn wait_for_index(
    mut receiver: watch::Receiver<Option<IndexBuildResult>>,
) -> Result<Arc<ProjectIndex>> {
    loop {
        if let Some(result) = receiver.borrow().clone() {
            return result.map_err(|error| anyhow!(error.to_string()));
        }
        receiver
            .changed()
            .await
            .context("Tree-sitter indexing task ended without a result")?;
    }
}

fn contains_source_with_extension(workspace_root: &Path, extensions: &[&str]) -> bool {
    fs::read_dir(workspace_root).is_ok_and(|entries| {
        entries.filter_map(Result::ok).any(|entry| {
            entry
                .path()
                .extension()
                .and_then(|extension| extension.to_str())
                .is_some_and(|extension| extensions.contains(&extension))
        })
    })
}

#[cfg(test)]
mod tests {
    use std::{
        fs,
        sync::atomic::Ordering,
        time::{SystemTime, UNIX_EPOCH},
    };

    use super::{TreeSitterLanguage, TreeSitterProvider};
    use crate::{
        fetch::{FetchSource, HierarchyQuery},
        state::{HierarchyDirection, HierarchyKind, SourceLocation, SymbolIdentity},
    };

    #[test]
    fn detects_supported_workspace_languages() {
        let workspace = temporary_workspace("detect");
        fs::write(workspace.join("main.cpp"), "int main() {}\n").unwrap();

        assert_eq!(
            TreeSitterLanguage::detect(&workspace),
            Some(TreeSitterLanguage::Cpp)
        );

        fs::remove_dir_all(workspace).unwrap();
    }

    #[test]
    fn initializes_and_parses_each_supported_grammar() {
        let cases = [
            (TreeSitterLanguage::Rust, "fn main() {}"),
            (TreeSitterLanguage::C, "int main(void) { return 0; }"),
            (TreeSitterLanguage::Cpp, "int main() { return 0; }"),
            (TreeSitterLanguage::Python, "def main():\n    pass\n"),
        ];

        for (language, source) in cases {
            let mut provider = TreeSitterProvider::start(".", language).unwrap();
            let tree = provider.parse(source).unwrap();
            assert!(
                !tree.root_node().has_error(),
                "{} parse failed",
                language.name()
            );
            assert!(!provider.symbol_capture_names().is_empty());
        }
    }

    #[test]
    fn normalizes_tree_sitter_byte_columns_to_utf16() {
        assert_eq!(super::index::utf16_column("é😀name", 6, 6).unwrap(), 3);
    }

    #[tokio::test]
    async fn indexes_rust_symbols_and_bidirectional_static_calls_once() {
        let workspace = temporary_workspace("rust-index");
        fs::write(
            workspace.join("lib.rs"),
            r#"
            struct Worker;
            trait Job {
                fn execute(&self) {}
            }
            impl Worker {
                fn run(&self) {
                    helper();
                    self.finish();
                }
                fn finish(&self) {}
            }
            fn helper() {}
            "#,
        )
        .unwrap();
        fs::create_dir(workspace.join("target")).unwrap();
        fs::write(workspace.join("target/ignored.rs"), "fn ignored() {}\n").unwrap();
        let provider = TreeSitterProvider::start(&workspace, TreeSitterLanguage::Rust).unwrap();
        let symbol_client = provider.workspace_symbol_client();
        let hierarchy_client = provider.hierarchy_client();

        let symbols = symbol_client.query("").await.unwrap();
        let names = symbols
            .iter()
            .map(|symbol| symbol.name.as_str())
            .collect::<Vec<_>>();
        assert!(names.contains(&"Worker::run"));
        assert!(names.contains(&"Worker::finish"));
        assert!(names.contains(&"Job::execute"));
        assert!(names.contains(&"helper"));
        assert!(!names.contains(&"ignored"));

        let run = identity(&symbols, "Worker::run", HierarchyKind::Call);
        let outgoing = hierarchy_client
            .query(HierarchyQuery {
                symbol: run.clone(),
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert_eq!(outgoing.source, FetchSource::TreeSitter);
        assert_eq!(
            outgoing
                .children
                .iter()
                .map(|child| child.symbol.as_str())
                .collect::<Vec<_>>(),
            ["helper", "Worker::finish"]
        );

        let helper = identity(&symbols, "helper", HierarchyKind::Call);
        let incoming = hierarchy_client
            .query(HierarchyQuery {
                symbol: helper,
                direction: HierarchyDirection::Incoming,
            })
            .await
            .unwrap();
        assert_eq!(incoming.children, [run]);
        fs::remove_dir_all(workspace).unwrap();
    }

    #[tokio::test]
    async fn indexes_python_methods_calls_and_type_inheritance() {
        let workspace = temporary_workspace("python-index");
        fs::write(
            workspace.join("main.py"),
            "class Base:\n    pass\n\nclass Child(Base):\n    def run(self):\n        helper()\n\ndef helper():\n    pass\n",
        )
        .unwrap();
        let provider = TreeSitterProvider::start(&workspace, TreeSitterLanguage::Python).unwrap();
        let symbols = provider.workspace_symbol_client().query("").await.unwrap();
        let hierarchy = provider.hierarchy_client();

        let run = identity(&symbols, "Child.run", HierarchyKind::Call);
        let outgoing = hierarchy
            .query(HierarchyQuery {
                symbol: run,
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert_eq!(
            outgoing
                .children
                .iter()
                .map(|child| child.symbol.as_str())
                .collect::<Vec<_>>(),
            ["helper"]
        );

        let base = identity(&symbols, "Base", HierarchyKind::Type);
        let child = identity(&symbols, "Child", HierarchyKind::Type);
        let subtypes = hierarchy
            .query(HierarchyQuery {
                symbol: base.clone(),
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert_eq!(subtypes.children.as_slice(), std::slice::from_ref(&child));
        let supertypes = hierarchy
            .query(HierarchyQuery {
                symbol: child,
                direction: HierarchyDirection::Incoming,
            })
            .await
            .unwrap();
        assert_eq!(supertypes.children, [base]);
        fs::remove_dir_all(workspace).unwrap();
    }

    #[tokio::test]
    async fn python_self_calls_prefer_the_method_on_the_current_class() {
        let workspace = temporary_workspace("python-self-call");
        fs::write(
            workspace.join("main.py"),
            "class First:\n    def finish(self):\n        pass\n\nclass Second:\n    def run(self):\n        self.finish()\n\n    def finish(self):\n        pass\n",
        )
        .unwrap();
        let provider = TreeSitterProvider::start(&workspace, TreeSitterLanguage::Python).unwrap();
        let symbols = provider.workspace_symbol_client().query("").await.unwrap();
        let run = identity(&symbols, "Second.run", HierarchyKind::Call);

        let outgoing = provider
            .hierarchy_client()
            .query(HierarchyQuery {
                symbol: run,
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();

        assert_eq!(
            outgoing
                .children
                .iter()
                .map(|child| child.symbol.as_str())
                .collect::<Vec<_>>(),
            ["Second.finish"]
        );
        fs::remove_dir_all(workspace).unwrap();
    }

    #[tokio::test]
    async fn indexes_c_and_cpp_calls_plus_cpp_inheritance() {
        let c_workspace = temporary_workspace("c-index");
        fs::write(
            c_workspace.join("main.c"),
            "void helper(void);\nvoid helper(void) {}\nvoid run(void) { helper(); }\n",
        )
        .unwrap();
        let c_provider = TreeSitterProvider::start(&c_workspace, TreeSitterLanguage::C).unwrap();
        assert_call_edge(&c_provider, "run", "helper").await;

        let cpp_workspace = temporary_workspace("cpp-index");
        fs::write(
            cpp_workspace.join("main.cpp"),
            "class Base {};\nclass Child : public Base { public: void run(); };\nvoid helper() {}\nvoid Child::run() { helper(); }\n",
        )
        .unwrap();
        let cpp_provider =
            TreeSitterProvider::start(&cpp_workspace, TreeSitterLanguage::Cpp).unwrap();
        assert_call_edge(&cpp_provider, "Child::run", "helper").await;
        let symbols = cpp_provider
            .workspace_symbol_client()
            .query("")
            .await
            .unwrap();
        let base = identity(&symbols, "Base", HierarchyKind::Type);
        let child = identity(&symbols, "Child", HierarchyKind::Type);
        let response = cpp_provider
            .hierarchy_client()
            .query(HierarchyQuery {
                symbol: base,
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert_eq!(response.children, [child]);

        fs::remove_dir_all(c_workspace).unwrap();
        fs::remove_dir_all(cpp_workspace).unwrap();
    }

    #[tokio::test]
    async fn leaves_ambiguous_targets_unbound_and_requires_an_exact_root() {
        let workspace = temporary_workspace("ambiguous-index");
        fs::write(workspace.join("first.rs"), "pub fn helper() {}\n").unwrap();
        fs::write(workspace.join("second.rs"), "pub fn helper() {}\n").unwrap();
        fs::write(workspace.join("main.rs"), "fn run() { helper(); }\n").unwrap();
        let provider = TreeSitterProvider::start(&workspace, TreeSitterLanguage::Rust).unwrap();
        let symbols = provider.workspace_symbol_client().query("").await.unwrap();
        let run = identity(&symbols, "run", HierarchyKind::Call);

        let response = provider
            .hierarchy_client()
            .query(HierarchyQuery {
                symbol: run,
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert!(response.children.is_empty());

        let error = provider
            .hierarchy_client()
            .query(HierarchyQuery {
                symbol: SymbolIdentity {
                    symbol: "helper".to_owned(),
                    kind: HierarchyKind::Call,
                    location: None,
                },
                direction: HierarchyDirection::Incoming,
            })
            .await
            .unwrap_err();
        assert!(error.to_string().contains("ambiguous"));
        fs::remove_dir_all(workspace).unwrap();
    }

    #[tokio::test]
    async fn cancelling_the_first_query_does_not_restart_project_indexing() {
        let workspace = temporary_workspace("cancelled-index-query");
        fs::write(workspace.join("lib.rs"), "fn main() {}\n").unwrap();
        let provider = TreeSitterProvider::start(&workspace, TreeSitterLanguage::Rust).unwrap();
        provider.shared.pause_build.store(true, Ordering::SeqCst);
        let first_client = provider.workspace_symbol_client();
        let first_query = tokio::spawn(async move { first_client.query("").await });

        let mut build_started = false;
        for _ in 0..10_000 {
            if provider.shared.build_count.load(Ordering::SeqCst) == 1 {
                build_started = true;
                break;
            }
            tokio::task::yield_now().await;
        }
        if !build_started {
            provider.shared.pause_build.store(false, Ordering::SeqCst);
            panic!("background index build did not start");
        }

        first_query.abort();
        assert!(first_query.await.unwrap_err().is_cancelled());
        provider.shared.pause_build.store(false, Ordering::SeqCst);
        let symbols = provider.workspace_symbol_client().query("").await.unwrap();

        assert!(symbols.iter().any(|symbol| symbol.name == "main"));
        assert_eq!(provider.shared.build_count.load(Ordering::SeqCst), 1);
        fs::remove_dir_all(workspace).unwrap();
    }

    async fn assert_call_edge(provider: &TreeSitterProvider, caller: &str, callee: &str) {
        let symbols = provider.workspace_symbol_client().query("").await.unwrap();
        let caller = identity(&symbols, caller, HierarchyKind::Call);
        let response = provider
            .hierarchy_client()
            .query(HierarchyQuery {
                symbol: caller,
                direction: HierarchyDirection::Outgoing,
            })
            .await
            .unwrap();
        assert_eq!(
            response
                .children
                .iter()
                .map(|child| child.symbol.as_str())
                .collect::<Vec<_>>(),
            [callee]
        );
    }

    fn identity(
        symbols: &[crate::fetch::WorkspaceSymbolMatch],
        name: &str,
        kind: HierarchyKind,
    ) -> SymbolIdentity {
        let symbol = symbols
            .iter()
            .find(|symbol| symbol.name == name)
            .unwrap_or_else(|| panic!("missing indexed symbol {name:?}"));
        let position = symbol.range.unwrap().start;
        SymbolIdentity {
            symbol: symbol.name.clone(),
            kind,
            location: Some(SourceLocation {
                uri: symbol.uri.to_string(),
                line: Some(position.line),
                character: Some(position.character),
            }),
        }
    }

    fn temporary_workspace(name: &str) -> std::path::PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let path = std::env::temp_dir().join(format!("cgraph-{name}-{unique}"));
        fs::create_dir(&path).unwrap();
        path
    }
}