vize_canon 0.23.0

Canon - The standard of correctness for Vize type checking
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
//! Core tsgo bridge implementation.
//!
//! Contains the `TsgoBridge` struct and its methods for spawning,
//! communicating with, and shutting down the tsgo process. Also includes
//! the `BatchTypeChecker` for efficient multi-document checking.

#[allow(clippy::disallowed_types)]
use std::sync::Arc;
use std::{
    path::PathBuf,
    process::Stdio,
    sync::atomic::{AtomicBool, AtomicU64, Ordering},
};

use dashmap::DashMap;
use serde_json::{json, Value};
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter},
    process::{Child as TokioChild, Command as TokioCommand},
    sync::{oneshot, Mutex},
};

use vize_carton::profiler::{CacheStats, Profiler};

use super::{
    protocol::{JsonRpcNotification, JsonRpcRequest},
    reader::{self, DiagnosticsCache, OpenDocuments, PendingMap, SharedStdin},
    types::*,
};
use vize_carton::cstr;
use vize_carton::String;
use vize_carton::ToCompactString;

/// Bridge to tsgo for type checking via LSP.
pub struct TsgoBridge {
    /// Configuration
    config: TsgoBridgeConfig,
    /// tsgo process handle
    process: Mutex<Option<TokioChild>>,
    /// Stdin writer (shared with reader task for responding to server requests)
    stdin: SharedStdin,
    /// Request ID counter
    request_id: AtomicU64,
    /// Pending requests (wrapped in Arc for sharing with reader task)
    pending: PendingMap,
    /// Whether the bridge is initialized
    initialized: AtomicBool,
    /// Profiler for performance tracking
    profiler: Profiler,
    /// Cache statistics
    cache_stats: CacheStats,
    /// Cached diagnostics by URI (wrapped in Arc for sharing with reader task)
    diagnostics_cache: DiagnosticsCache,
    /// Tracks open document URIs and their versions
    open_documents: OpenDocuments,
}

impl TsgoBridge {
    /// Create a new tsgo bridge with default configuration.
    pub fn new() -> Self {
        Self::with_config(TsgoBridgeConfig::default())
    }

    /// Create a new tsgo bridge with custom configuration.
    #[allow(clippy::disallowed_types)]
    pub fn with_config(config: TsgoBridgeConfig) -> Self {
        let profiler = if config.enable_profiling {
            Profiler::enabled()
        } else {
            Profiler::new()
        };

        Self {
            config,
            process: Mutex::new(None),
            stdin: Arc::new(Mutex::new(None)),
            request_id: AtomicU64::new(1),
            pending: Arc::new(DashMap::new()),
            initialized: AtomicBool::new(false),
            profiler,
            cache_stats: CacheStats::new(),
            diagnostics_cache: Arc::new(DashMap::new()),
            open_documents: Arc::new(DashMap::new()),
        }
    }

    /// Spawn and initialize the tsgo process.
    pub async fn spawn(&self) -> Result<(), TsgoBridgeError> {
        let _timer = self.profiler.timer("tsgo_spawn");

        // Find tsgo executable
        tracing::info!("tsgo_bridge: finding tsgo path...");
        let tsgo_path = self.find_tsgo_path()?;
        tracing::info!("tsgo_bridge: found tsgo at {:?}", tsgo_path);

        // Spawn tsgo with LSP mode
        let mut cmd = TokioCommand::new(&tsgo_path);
        cmd.arg("--lsp")
            .arg("--stdio")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped()); // Capture stderr for debugging

        if let Some(ref working_dir) = self.config.working_dir {
            tracing::info!("tsgo_bridge: working_dir = {:?}", working_dir);
            cmd.current_dir(working_dir);
        }

        tracing::info!("tsgo_bridge: spawning process...");
        let mut child = cmd.spawn().map_err(|e| {
            TsgoBridgeError::SpawnFailed(cstr!("Failed to spawn tsgo at {tsgo_path:?}: {e}"))
        })?;
        tracing::info!("tsgo_bridge: process spawned");

        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| TsgoBridgeError::SpawnFailed("Failed to get stdin".into()))?;

        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| TsgoBridgeError::SpawnFailed("Failed to get stdout".into()))?;

        let stderr = child.stderr.take();

        *self.process.lock().await = Some(child);
        *self.stdin.lock().await = Some(BufWriter::new(stdin));

        // Start stderr reader task (for debugging)
        if let Some(stderr) = stderr {
            tokio::spawn(async move {
                let mut reader = BufReader::new(stderr);
                #[allow(clippy::disallowed_types)]
                let mut line = std::string::String::new();
                loop {
                    line.clear();
                    match reader.read_line(&mut line).await {
                        Ok(0) => break,
                        Ok(_) => tracing::warn!("tsgo stderr: {}", line.trim()),
                        Err(_) => break,
                    }
                }
            });
        }

        // Start response reader task
        tracing::info!("tsgo_bridge: starting reader task...");
        #[allow(clippy::disallowed_types)]
        {
            reader::start_reader_task(
                stdout,
                Arc::clone(&self.pending),
                Arc::clone(&self.diagnostics_cache),
                Arc::clone(&self.stdin),
            );
        }

        // Initialize LSP
        tracing::info!("tsgo_bridge: calling initialize()...");
        self.initialize().await?;
        tracing::info!("tsgo_bridge: initialized");

        self.initialized.store(true, Ordering::SeqCst);

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        Ok(())
    }

    /// Find tsgo executable path.
    ///
    /// Search order:
    /// 1. Explicit config.tsgo_path
    /// 2. Native binary in node_modules (platform-specific) - walks up parent dirs
    /// 3. Local node_modules/.bin/tsgo (requires node)
    /// 4. Global PATH
    fn find_tsgo_path(&self) -> Result<PathBuf, TsgoBridgeError> {
        // 1. Use explicit path if provided
        if let Some(ref path) = self.config.tsgo_path {
            if path.exists() {
                return Ok(path.clone());
            }
        }

        let base_dir = self
            .config
            .working_dir
            .clone()
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_default());

        // Platform-specific paths for @typescript/native-preview
        let platform_suffix = if cfg!(target_os = "macos") {
            if cfg!(target_arch = "aarch64") {
                "darwin-arm64"
            } else {
                "darwin-x64"
            }
        } else if cfg!(target_os = "linux") {
            if cfg!(target_arch = "aarch64") {
                "linux-arm64"
            } else {
                "linux-x64"
            }
        } else if cfg!(target_os = "windows") {
            "win32-x64"
        } else {
            ""
        };

        // Helper to search for tsgo in a directory
        let search_in_dir = |dir: &std::path::Path| -> Option<PathBuf> {
            // Try pnpm structure first
            let pnpm_pattern = dir.join("node_modules/.pnpm");
            if pnpm_pattern.exists() {
                if let Ok(entries) = std::fs::read_dir(&pnpm_pattern) {
                    for entry in entries.flatten() {
                        let name = entry.file_name();
                        let name_str = name.to_string_lossy();
                        if name_str.starts_with("@typescript+native-preview-")
                            && name_str.contains(platform_suffix)
                        {
                            let native_path = entry.path().join(&*cstr!(
                                "node_modules/@typescript/native-preview-{}/lib/tsgo",
                                platform_suffix
                            ));
                            if native_path.exists() {
                                return Some(native_path);
                            }
                        }
                    }
                }
            }

            // Try npm/yarn structure
            let native_candidates = [
                dir.join(&*cstr!(
                    "node_modules/@typescript/native-preview-{}/lib/tsgo",
                    platform_suffix
                )),
                dir.join("node_modules/@typescript/native-preview/lib/tsgo"),
            ];

            for candidate in &native_candidates {
                if candidate.exists() {
                    return Some(candidate.clone());
                }
            }

            // Try .bin/tsgo (requires node in PATH)
            let bin_tsgo = dir.join("node_modules/.bin/tsgo");
            if bin_tsgo.exists() {
                return Some(bin_tsgo);
            }

            None
        };

        // 2. Search in base_dir first, then walk up parent directories
        if let Some(path) = search_in_dir(&base_dir) {
            tracing::info!("tsgo_bridge: found tsgo at {:?}", path);
            return Ok(path);
        }

        let mut current = base_dir.as_path();
        while let Some(parent) = current.parent() {
            if let Some(path) = search_in_dir(parent) {
                tracing::info!("tsgo_bridge: found tsgo at {:?}", path);
                return Ok(path);
            }
            current = parent;
        }

        // 3. Try global PATH
        if let Ok(path) = which::which("tsgo") {
            tracing::info!("tsgo_bridge: found tsgo in PATH at {:?}", path);
            return Ok(path);
        }

        Err(TsgoBridgeError::SpawnFailed(
            "tsgo not found. Install with: npm install -D @typescript/native-preview".into(),
        ))
    }

    /// Send LSP initialize request.
    async fn initialize(&self) -> Result<(), TsgoBridgeError> {
        let _timer = self.profiler.timer("lsp_initialize");

        let root_uri = self
            .config
            .working_dir
            .as_ref()
            .map(|p| cstr!("file://{}", p.display()))
            .unwrap_or_else(|| "file:///".into());

        tracing::info!("tsgo_bridge: LSP rootUri = {}", root_uri);

        let params = json!({
            "processId": std::process::id(),
            "capabilities": {
                "textDocument": {
                    "synchronization": {
                        "didSave": true
                    },
                    "publishDiagnostics": {
                        "relatedInformation": true
                    }
                }
            },
            "rootUri": root_uri,
            "initializationOptions": {}
        });

        tracing::info!("tsgo_bridge: sending initialize request...");
        self.send_request("initialize", Some(params)).await?;
        tracing::info!("tsgo_bridge: initialize response received");

        // Send initialized notification
        tracing::info!("tsgo_bridge: sending initialized notification...");
        self.send_notification("initialized", Some(json!({})))
            .await?;
        tracing::info!("tsgo_bridge: initialized notification sent");

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        Ok(())
    }

    /// Send a JSON-RPC request and wait for response.
    pub(crate) async fn send_request(
        &self,
        method: &str,
        params: Option<Value>,
    ) -> Result<Value, TsgoBridgeError> {
        let id = self.request_id.fetch_add(1, Ordering::SeqCst);

        let request = JsonRpcRequest {
            jsonrpc: "2.0",
            id,
            method: method.into(),
            params,
        };

        let content = serde_json::to_string(&request)
            .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;

        let message = cstr!("Content-Length: {}\r\n\r\n{}", content.len(), content);

        // Create response channel
        let (tx, rx) = oneshot::channel();
        self.pending.insert(id, tx);

        // Send request
        {
            let mut stdin_guard = self.stdin.lock().await;
            if let Some(ref mut stdin) = *stdin_guard {
                stdin
                    .write_all(message.as_bytes())
                    .await
                    .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;
                stdin
                    .flush()
                    .await
                    .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;
            } else {
                return Err(TsgoBridgeError::NotInitialized);
            }
        }

        // Wait for response with timeout
        match tokio::time::timeout(std::time::Duration::from_millis(self.config.timeout_ms), rx)
            .await
        {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(TsgoBridgeError::CommunicationError(
                "Response channel closed".into(),
            )),
            Err(_) => {
                self.pending.remove(&id);
                Err(TsgoBridgeError::Timeout)
            }
        }
    }

    /// Send a JSON-RPC notification (no response expected).
    pub(crate) async fn send_notification(
        &self,
        method: &str,
        params: Option<Value>,
    ) -> Result<(), TsgoBridgeError> {
        let notification = JsonRpcNotification {
            jsonrpc: "2.0",
            method: method.into(),
            params,
        };

        let content = serde_json::to_string(&notification)
            .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;

        let message = cstr!("Content-Length: {}\r\n\r\n{}", content.len(), content);

        let mut stdin_guard = self.stdin.lock().await;
        if let Some(ref mut stdin) = *stdin_guard {
            stdin
                .write_all(message.as_bytes())
                .await
                .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;
            stdin
                .flush()
                .await
                .map_err(|e| TsgoBridgeError::CommunicationError(e.to_compact_string()))?;
            Ok(())
        } else {
            Err(TsgoBridgeError::NotInitialized)
        }
    }

    /// Open a virtual document for type checking.
    pub async fn open_virtual_document(
        &self,
        name: &str,
        content: &str,
    ) -> Result<String, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        let _timer = self.profiler.timer("open_virtual_document");

        // Use file:// URI scheme for compatibility with tsgo
        // tsgo only publishes diagnostics for file:// URIs
        let uri = if name.starts_with("file://") || name.starts_with('/') {
            if name.starts_with("file://") {
                String::from(name)
            } else {
                cstr!("file://{name}")
            }
        } else {
            cstr!("{VIRTUAL_URI_SCHEME}://{name}")
        };

        // Clear cached diagnostics for this URI
        self.diagnostics_cache.remove(&uri);

        let params = json!({
            "textDocument": {
                "uri": uri,
                "languageId": "typescript",
                "version": 1,
                "text": content
            }
        });

        self.send_notification("textDocument/didOpen", Some(params))
            .await?;

        // Track this document as open with version 1
        self.open_documents.insert(uri.clone(), 1);

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        Ok(uri)
    }

    /// Open or update a virtual document. Uses didChange if already open, didOpen otherwise.
    pub async fn open_or_update_virtual_document(
        &self,
        name: &str,
        content: &str,
    ) -> Result<String, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        // Build URI first to check if document is already open
        let uri = if name.starts_with("file://") || name.starts_with('/') {
            if name.starts_with("file://") {
                String::from(name)
            } else {
                cstr!("file://{name}")
            }
        } else {
            cstr!("{VIRTUAL_URI_SCHEME}://{name}")
        };

        // Check if document is already open
        if let Some(mut version_ref) = self.open_documents.get_mut(&uri) {
            // Document is already open, send didChange with incremented version
            let new_version = *version_ref + 1;
            *version_ref = new_version;
            drop(version_ref); // Release the lock before async call

            self.update_virtual_document(&uri, content, new_version)
                .await?;
            Ok(uri)
        } else {
            // Document not open, send didOpen
            self.open_virtual_document(name, content).await
        }
    }

    /// Update a virtual document.
    pub async fn update_virtual_document(
        &self,
        uri: &str,
        content: &str,
        version: i32,
    ) -> Result<(), TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        let _timer = self.profiler.timer("update_virtual_document");

        // Clear cached diagnostics for this URI
        self.diagnostics_cache.remove(uri);

        let params = json!({
            "textDocument": {
                "uri": uri,
                "version": version
            },
            "contentChanges": [{
                "text": content
            }]
        });

        self.send_notification("textDocument/didChange", Some(params))
            .await?;

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        Ok(())
    }

    /// Close a virtual document.
    pub async fn close_virtual_document(&self, uri: &str) -> Result<(), TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        // Remove from cache and open documents tracking
        self.diagnostics_cache.remove(uri);
        self.open_documents.remove(uri);

        let params = json!({
            "textDocument": {
                "uri": uri
            }
        });

        self.send_notification("textDocument/didClose", Some(params))
            .await
    }

    /// Get diagnostics for a document.
    /// First tries textDocument/diagnostic request, then falls back to cached publishDiagnostics.
    pub async fn get_diagnostics(&self, uri: &str) -> Result<Vec<LspDiagnostic>, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        // Check cache first (diagnostics arrive via publishDiagnostics notification)
        if let Some(cached) = self.diagnostics_cache.get(uri) {
            self.cache_stats.hit();
            tracing::info!(
                "tsgo_bridge: cache hit for {}, {} diagnostics",
                uri,
                cached.len()
            );
            return Ok(cached.clone());
        }

        self.cache_stats.miss();

        // Try textDocument/diagnostic request first (LSP 3.17+ pull diagnostics)
        // This is how CLI gets diagnostics
        tracing::info!(
            "tsgo_bridge: requesting diagnostics via textDocument/diagnostic for {}",
            uri
        );

        let params = json!({
            "textDocument": {
                "uri": uri
            }
        });

        match self
            .send_request("textDocument/diagnostic", Some(params))
            .await
        {
            Ok(result) => {
                // Parse diagnostic response
                if let Some(items) = result.get("items").and_then(|i| i.as_array()) {
                    let diags: Vec<LspDiagnostic> = items
                        .iter()
                        .filter_map(|d| serde_json::from_value(d.clone()).ok())
                        .collect();
                    tracing::info!(
                        "tsgo_bridge: received {} diagnostics via request for {}",
                        diags.len(),
                        uri
                    );
                    // Cache for later
                    self.diagnostics_cache
                        .insert(String::from(uri), diags.clone());
                    return Ok(diags);
                }
                tracing::info!(
                    "tsgo_bridge: diagnostic request returned no items for {}",
                    uri
                );
            }
            Err(e) => {
                tracing::warn!("tsgo_bridge: textDocument/diagnostic request failed: {}", e);
            }
        }

        // Fallback: wait briefly for publishDiagnostics notification
        tracing::info!("tsgo_bridge: waiting for publishDiagnostics for {}", uri);
        let max_wait = std::time::Duration::from_millis(500);
        let poll_interval = std::time::Duration::from_millis(50);
        let start = std::time::Instant::now();

        while start.elapsed() < max_wait {
            if let Some(cached) = self.diagnostics_cache.get(uri) {
                tracing::info!(
                    "tsgo_bridge: diagnostics arrived via notification for {}, {} items",
                    uri,
                    cached.len()
                );
                return Ok(cached.clone());
            }
            tokio::time::sleep(poll_interval).await;
        }

        tracing::info!(
            "tsgo_bridge: no diagnostics for {} (file may have no errors)",
            uri
        );

        // Return empty if no diagnostics (file might have no errors)
        Ok(vec![])
    }

    /// Type check a virtual TypeScript document.
    pub async fn type_check(
        &self,
        name: &str,
        content: &str,
    ) -> Result<TypeCheckResult, TsgoBridgeError> {
        let _timer = self.profiler.timer("type_check");

        let uri = self.open_virtual_document(name, content).await?;

        // Wait for diagnostics
        let diagnostics = self.get_diagnostics(&uri).await?;

        // Keep document open for incremental updates
        // self.close_virtual_document(&uri).await?;

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        Ok(TypeCheckResult {
            diagnostics,
            source_map: None,
        })
    }

    /// Shutdown the bridge.
    pub async fn shutdown(&self) -> Result<(), TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Ok(());
        }

        // Send shutdown request
        let _ = self.send_request("shutdown", None).await;

        // Send exit notification
        let _ = self.send_notification("exit", None).await;

        // Kill process if still running
        let mut process_guard = self.process.lock().await;
        if let Some(mut process) = process_guard.take() {
            let _ = process.kill().await;
        }

        self.initialized.store(false, Ordering::SeqCst);
        Ok(())
    }

    /// Check if bridge is initialized.
    pub fn is_initialized(&self) -> bool {
        self.initialized.load(Ordering::SeqCst)
    }

    /// Get profiler reference.
    pub fn profiler(&self) -> &Profiler {
        &self.profiler
    }

    /// Get cache statistics.
    pub fn cache_stats(&self) -> &CacheStats {
        &self.cache_stats
    }

    /// Clear diagnostics cache.
    pub fn clear_cache(&self) {
        self.diagnostics_cache.clear();
        self.cache_stats.reset();
    }

    /// Get hover information at a position.
    ///
    /// Sends a textDocument/hover request to tsgo.
    pub async fn hover(
        &self,
        uri: &str,
        line: u32,
        character: u32,
    ) -> Result<Option<LspHover>, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        let _timer = self.profiler.timer("tsgo_hover");

        let params = json!({
            "textDocument": {
                "uri": uri
            },
            "position": {
                "line": line,
                "character": character
            }
        });

        let result = self
            .send_request("textDocument/hover", Some(params))
            .await?;

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        // null response means no hover info
        if result.is_null() {
            return Ok(None);
        }

        let hover: LspHover = serde_json::from_value(result).map_err(|e| {
            TsgoBridgeError::CommunicationError(cstr!("Failed to parse hover: {e}"))
        })?;

        Ok(Some(hover))
    }

    /// Get definition location for a symbol at a position.
    ///
    /// Sends a textDocument/definition request to tsgo.
    pub async fn definition(
        &self,
        uri: &str,
        line: u32,
        character: u32,
    ) -> Result<Vec<LspLocation>, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        let _timer = self.profiler.timer("tsgo_definition");

        let params = json!({
            "textDocument": {
                "uri": uri
            },
            "position": {
                "line": line,
                "character": character
            }
        });

        let result = self
            .send_request("textDocument/definition", Some(params))
            .await?;

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        // null response means no definition
        if result.is_null() {
            return Ok(Vec::new());
        }

        // Try parsing as definition response (can be location, array, or links)
        let response: LspDefinitionResponse = serde_json::from_value(result).map_err(|e| {
            TsgoBridgeError::CommunicationError(cstr!("Failed to parse definition: {e}"))
        })?;

        Ok(response.into_locations())
    }

    /// Get completion items at a position.
    ///
    /// Sends a textDocument/completion request to tsgo.
    pub async fn completion(
        &self,
        uri: &str,
        line: u32,
        character: u32,
    ) -> Result<Vec<LspCompletionItem>, TsgoBridgeError> {
        if !self.initialized.load(Ordering::SeqCst) {
            return Err(TsgoBridgeError::NotInitialized);
        }

        let _timer = self.profiler.timer("tsgo_completion");

        let params = json!({
            "textDocument": {
                "uri": uri
            },
            "position": {
                "line": line,
                "character": character
            },
            "context": {
                "triggerKind": 1  // Invoked
            }
        });

        let result = self
            .send_request("textDocument/completion", Some(params))
            .await?;

        if let Some(timer) = _timer {
            timer.record(&self.profiler);
        }

        // null response means no completions
        if result.is_null() {
            return Ok(Vec::new());
        }

        // Try parsing as completion response (can be array or list)
        let response: LspCompletionResponse = serde_json::from_value(result).map_err(|e| {
            TsgoBridgeError::CommunicationError(cstr!("Failed to parse completion: {e}"))
        })?;

        Ok(response.items())
    }
}

impl Default for TsgoBridge {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for TsgoBridge {
    fn drop(&mut self) {
        // Note: Can't do async cleanup in Drop, caller should call shutdown()
    }
}

/// Batch type checker for checking multiple documents efficiently.
#[allow(clippy::disallowed_types)]
pub struct BatchTypeChecker {
    /// Bridge instance
    bridge: Arc<TsgoBridge>,
    /// Batch size
    batch_size: usize,
}

#[allow(clippy::disallowed_types)]
impl BatchTypeChecker {
    /// Create a new batch type checker.
    pub fn new(bridge: Arc<TsgoBridge>) -> Self {
        Self {
            bridge,
            batch_size: 10,
        }
    }

    /// Set batch size.
    pub fn with_batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Check multiple documents in batch.
    pub async fn check_batch(
        &self,
        documents: &[(String, String)],
    ) -> Vec<Result<TypeCheckResult, TsgoBridgeError>> {
        let _timer = self.bridge.profiler().timer("batch_type_check");

        let mut results = Vec::with_capacity(documents.len());

        for chunk in documents.chunks(self.batch_size) {
            // Open all documents in the chunk
            let mut uris = Vec::with_capacity(chunk.len());
            for (name, content) in chunk {
                match self.bridge.open_virtual_document(name, content).await {
                    Ok(uri) => uris.push(Some(uri)),
                    Err(e) => {
                        results.push(Err(e));
                        uris.push(None);
                    }
                }
            }

            // Wait for diagnostics to be computed (reduced for faster batch processing)
            tokio::time::sleep(std::time::Duration::from_millis(50)).await;

            // Collect diagnostics
            for uri in uris.into_iter().flatten() {
                match self.bridge.get_diagnostics(&uri).await {
                    Ok(diagnostics) => {
                        results.push(Ok(TypeCheckResult {
                            diagnostics,
                            source_map: None,
                        }));
                    }
                    Err(e) => results.push(Err(e)),
                }
            }
        }

        if let Some(timer) = _timer {
            timer.record(self.bridge.profiler());
        }

        results
    }
}