glua_ls 1.0.27

Language server for Garry's Mod Lua (GLua).
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
use std::{
    collections::HashMap,
    sync::{
        Arc, Mutex as StdMutex,
        atomic::{AtomicBool, Ordering},
    },
    time::{Duration, Instant},
};

use glua_code_analysis::{EmmyLuaAnalysis, FileId, SharedDiagnosticData};
use log::{debug, info, warn};
use lsp_types::{Diagnostic, PublishDiagnosticsParams, Uri};
use tokio::sync::{Mutex, RwLock, Semaphore};
use tokio_util::sync::CancellationToken;

use crate::util::{LongRunningWatchdogStatus, spawn_long_running_watchdog};

use super::{ClientProxy, ProgressTask, StatusBar};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct SuppressedDiagnosticLines {
    start_line: u32,
    end_line: u32,
    expires_at: Instant,
}

#[derive(Clone, Default)]
pub(crate) struct SharedDiagnosticDataCache {
    cached: Arc<StdMutex<Option<Arc<SharedDiagnosticData>>>>,
}

impl SharedDiagnosticDataCache {
    fn get_or_precompute(&self, analysis: &EmmyLuaAnalysis) -> Arc<SharedDiagnosticData> {
        if let Ok(mut cache) = self.cached.lock() {
            if let Some(cached) = cache.as_ref() {
                return cached.clone();
            }

            let shared_data = analysis.precompute_diagnostic_shared_data();
            *cache = Some(shared_data.clone());
            return shared_data;
        }

        analysis.precompute_diagnostic_shared_data()
    }

    fn force_precompute(&self, analysis: &EmmyLuaAnalysis) -> Arc<SharedDiagnosticData> {
        let shared_data = analysis.precompute_diagnostic_shared_data();
        if let Ok(mut cache) = self.cached.lock() {
            *cache = Some(shared_data.clone());
        }
        shared_data
    }

    pub(crate) fn invalidate(&self) {
        if let Ok(mut cache) = self.cached.lock() {
            *cache = None;
        }
    }

    #[cfg(test)]
    fn cached_ptr(&self) -> Option<*const SharedDiagnosticData> {
        self.cached
            .lock()
            .expect("shared diagnostic cache mutex should not be poisoned")
            .as_ref()
            .map(Arc::as_ptr)
    }

    #[cfg(test)]
    fn is_empty(&self) -> bool {
        self.cached
            .lock()
            .expect("shared diagnostic cache mutex should not be poisoned")
            .is_none()
    }
}

#[derive(Clone)]
pub struct FileDiagnostic {
    analysis: Arc<RwLock<EmmyLuaAnalysis>>,
    client: Arc<ClientProxy>,
    status_bar: Arc<StatusBar>,
    workspace_loaded_notified: Arc<AtomicBool>,
    startup_complete_notified: Arc<AtomicBool>,
    diagnostic_tokens: Arc<Mutex<HashMap<FileId, CancellationToken>>>,
    workspace_diagnostic_token: Arc<Mutex<Option<CancellationToken>>>,
    cached_file_diagnostics: Arc<Mutex<HashMap<Uri, Vec<Diagnostic>>>>,
    recently_edited_lines: Arc<Mutex<HashMap<Uri, SuppressedDiagnosticLines>>>,
    shared_diagnostic_data_cache: SharedDiagnosticDataCache,
}

impl FileDiagnostic {
    pub fn new(
        analysis: Arc<RwLock<EmmyLuaAnalysis>>,
        status_bar: Arc<StatusBar>,
        client: Arc<ClientProxy>,
    ) -> Self {
        Self {
            analysis,
            client,
            workspace_loaded_notified: Arc::new(AtomicBool::new(false)),
            startup_complete_notified: Arc::new(AtomicBool::new(false)),
            diagnostic_tokens: Arc::new(Mutex::new(HashMap::new())),
            workspace_diagnostic_token: Arc::new(Mutex::new(None)),
            cached_file_diagnostics: Arc::new(Mutex::new(HashMap::new())),
            recently_edited_lines: Arc::new(Mutex::new(HashMap::new())),
            shared_diagnostic_data_cache: SharedDiagnosticDataCache::default(),
            status_bar,
        }
    }

    pub(crate) fn shared_diagnostic_data_cache(&self) -> SharedDiagnosticDataCache {
        self.shared_diagnostic_data_cache.clone()
    }

    fn get_or_precompute_shared_diagnostic_data(
        &self,
        analysis: &EmmyLuaAnalysis,
    ) -> Arc<SharedDiagnosticData> {
        self.shared_diagnostic_data_cache
            .get_or_precompute(analysis)
    }

    fn force_precompute_shared_diagnostic_data(
        &self,
        analysis: &EmmyLuaAnalysis,
    ) -> Arc<SharedDiagnosticData> {
        self.shared_diagnostic_data_cache.force_precompute(analysis)
    }

    fn diagnose_file_with_shared_data(
        &self,
        analysis: &EmmyLuaAnalysis,
        file_id: FileId,
        cancel_token: CancellationToken,
    ) -> Option<Vec<Diagnostic>> {
        if cancel_token.is_cancelled() {
            return None;
        }

        let shared_data = self.get_or_precompute_shared_diagnostic_data(analysis);
        if cancel_token.is_cancelled() {
            return None;
        }

        analysis.diagnose_file_with_shared(file_id, cancel_token, shared_data)
    }

    pub fn invalidate_shared_diagnostic_data(&self) {
        self.shared_diagnostic_data_cache.invalidate();
    }

    pub async fn note_recent_edit(
        &self,
        uri: &Uri,
        previous_text: Option<&str>,
        new_text: &str,
        suppress_for: Duration,
    ) {
        let Some(previous_text) = previous_text else {
            return;
        };

        let Some((start_line, end_line)) = changed_line_span(previous_text, new_text) else {
            return;
        };

        self.recently_edited_lines.lock().await.insert(
            uri.clone(),
            SuppressedDiagnosticLines {
                start_line,
                end_line,
                expires_at: Instant::now() + suppress_for,
            },
        );
    }

    pub async fn clear_recent_edit(&self, uri: &Uri) {
        self.recently_edited_lines.lock().await.remove(uri);
    }

    pub fn notify_workspace_loaded(&self) {
        if self.workspace_loaded_notified.swap(true, Ordering::AcqRel) {
            return;
        }

        info!("workspace loaded; language server is ready while diagnostics may continue");
        self.send_server_status("workspaceLoaded");
    }

    fn notify_startup_complete(&self) {
        if self.startup_complete_notified.swap(true, Ordering::AcqRel) {
            return;
        }

        info!("workspace diagnostics complete; language server startup fully complete");
        self.send_server_status("startupComplete");
    }

    fn send_server_status(&self, state: &'static str) {
        self.client.send_notification(
            "gluals/serverStatus",
            serde_json::json!({
                "state": state,
            }),
        );
    }

    pub async fn cache_fresh_file_diagnostics(&self, uri: &Uri, diagnostics: &[Diagnostic]) {
        self.cached_file_diagnostics
            .lock()
            .await
            .insert(uri.clone(), diagnostics.to_vec());
    }

    pub async fn cached_display_diagnostics(&self, uri: &Uri) -> Option<Vec<Diagnostic>> {
        let diagnostics = {
            let cache = self.cached_file_diagnostics.lock().await;
            cache.get(uri).cloned()
        }?;

        Some(self.filter_display_diagnostics(uri, diagnostics).await)
    }

    pub async fn filter_display_diagnostics(
        &self,
        uri: &Uri,
        diagnostics: Vec<Diagnostic>,
    ) -> Vec<Diagnostic> {
        let suppressed_lines = {
            let mut recent_edits = self.recently_edited_lines.lock().await;
            let Some(suppressed_lines) = recent_edits.get(uri).copied() else {
                return diagnostics;
            };

            if Instant::now() >= suppressed_lines.expires_at {
                recent_edits.remove(uri);
                return diagnostics;
            }

            suppressed_lines
        };

        filter_diagnostics_by_line_span(
            diagnostics,
            suppressed_lines.start_line,
            suppressed_lines.end_line,
        )
    }

    pub async fn publish_fresh_file_diagnostics(
        &self,
        uri: Uri,
        diagnostics: Vec<Diagnostic>,
        version: Option<i32>,
    ) {
        self.cache_fresh_file_diagnostics(&uri, &diagnostics).await;
        self.client.publish_diagnostics(PublishDiagnosticsParams {
            uri,
            diagnostics,
            version,
        });
    }

    pub async fn add_diagnostic_task(
        &self,
        file_id: FileId,
        interval: u64,
        debounced_analysis: Option<Arc<crate::context::DebouncedAnalysis>>,
    ) {
        let mut tokens = self.diagnostic_tokens.lock().await;

        if let Some(token) = tokens.remove(&file_id) {
            token.cancel();
            debug!("cancel diagnostic: {:?}", file_id);
        }

        // create new token
        let cancel_token = CancellationToken::new();
        tokens.insert(file_id, cancel_token.clone());
        drop(tokens); // free the lock

        let analysis = self.analysis.clone();
        let file_diagnostic = self.clone();
        let diagnostic_tokens = self.diagnostic_tokens.clone();
        let file_id_clone = file_id;

        // Spawn a new task to perform diagnostic
        tokio::spawn(async move {
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_millis(interval)) => {
                    if let Some(da) = debounced_analysis {
                        da.wait_for_reindex(file_id_clone, cancel_token.clone()).await;
                    }
                    if cancel_token.is_cancelled() {
                        return;
                    }
                    let blocking_analysis = analysis.clone();
                    let blocking_token = cancel_token.clone();
                    let diagnostic_runner = file_diagnostic.clone();
                    match tokio::task::spawn_blocking(move || {
                        if blocking_token.is_cancelled() {
                            return None;
                        }

                        // Diagnose under a blocking read lock so CPU work does not run on Tokio workers.
                        let guard = blocking_analysis.blocking_read();
                        let uri = guard.get_uri(file_id_clone)?;
                        let diagnostics = diagnostic_runner.diagnose_file_with_shared_data(
                            &guard,
                            file_id_clone,
                            blocking_token.clone(),
                        )?;
                        Some((uri, diagnostics))
                    })
                    .await
                    {
                        Ok(Some((uri, diagnostics))) => {
                            file_diagnostic
                                .publish_fresh_file_diagnostics(uri, diagnostics, None)
                                .await;
                        }
                        Ok(None) => {
                            if !cancel_token.is_cancelled() {
                                info!("file not found: {:?}", file_id_clone);
                            }
                        }
                        Err(err) => {
                            warn!(
                                "single-file diagnostic worker failed for file {:?}: {}",
                                file_id_clone, err
                            );
                        }
                    }
                    // Remove our token only if this task was not cancelled.
                    // Keeping the check inside the lock avoids a check/remove race with replacements.
                    let mut tokens = diagnostic_tokens.lock().await;
                    if !cancel_token.is_cancelled() {
                        tokens.remove(&file_id_clone);
                    }
                }
                _ = cancel_token.cancelled() => {
                    debug!("cancel diagnostic: {:?}", file_id_clone);
                }
            }
        });
    }

    // todo add message show
    pub async fn add_files_diagnostic_task(
        &self,
        file_ids: Vec<FileId>,
        interval: u64,
        debounced_analysis: Option<Arc<crate::context::DebouncedAnalysis>>,
    ) {
        for file_id in file_ids {
            self.add_diagnostic_task(file_id, interval, debounced_analysis.clone())
                .await;
        }
    }

    /// 清除指定文件的诊断信息
    pub async fn clear_push_file_diagnostics(&self, uri: lsp_types::Uri) {
        self.cached_file_diagnostics.lock().await.remove(&uri);
        self.recently_edited_lines.lock().await.remove(&uri);

        let diagnostic_param = PublishDiagnosticsParams {
            uri,
            diagnostics: vec![],
            version: None,
        };
        self.client.publish_diagnostics(diagnostic_param);
    }

    pub async fn add_workspace_diagnostic_task(&self, interval: u64, silent: bool) {
        let mut token = self.workspace_diagnostic_token.lock().await;
        if let Some(token) = token.as_ref() {
            token.cancel();
            debug!("cancel workspace diagnostic");
        }

        let cancel_token = CancellationToken::new();
        token.replace(cancel_token.clone());
        drop(token);

        let analysis = self.analysis.clone();
        let client_proxy = self.client.clone();
        let status_bar = self.status_bar.clone();
        let file_diagnostic = self.clone();
        tokio::spawn(async move {
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_millis(interval)) => {
                    push_workspace_diagnostic(
                        file_diagnostic,
                        analysis,
                        client_proxy,
                        status_bar,
                        silent,
                        cancel_token,
                    ).await
                }
                _ = cancel_token.cancelled() => {
                    log::info!("cancel workspace diagnostic");
                }
            }
        });
    }

    #[allow(unused)]
    pub async fn cancel_all(&self) {
        let mut tokens = self.diagnostic_tokens.lock().await;
        for (_, token) in tokens.iter() {
            token.cancel();
        }
        tokens.clear();
    }

    pub async fn cancel_file_diagnostic(&self, file_id: FileId) {
        let mut tokens = self.diagnostic_tokens.lock().await;
        if let Some(token) = tokens.remove(&file_id) {
            token.cancel();
            debug!("cancel diagnostic: {:?}", file_id);
        }
    }

    pub async fn cancel_workspace_diagnostic(&self) {
        let mut token = self.workspace_diagnostic_token.lock().await;
        if let Some(token) = token.as_ref() {
            token.cancel();
            debug!("cancel workspace diagnostic");
        }
        token.take();
    }

    pub async fn pull_file_diagnostics(
        &self,
        uri: Uri,
        cancel_token: CancellationToken,
    ) -> Option<Vec<Diagnostic>> {
        if cancel_token.is_cancelled() {
            return None;
        }

        let analysis = self.analysis.clone();
        let file_diagnostic = self.clone();
        match tokio::task::spawn_blocking(move || {
            if cancel_token.is_cancelled() {
                return None;
            }

            // Pull diagnostics under a blocking read lock to avoid blocking async workers.
            let guard = analysis.blocking_read();
            let file_id = guard.get_file_id(&uri)?;

            if cancel_token.is_cancelled() {
                return None;
            }

            file_diagnostic.diagnose_file_with_shared_data(&guard, file_id, cancel_token)
        })
        .await
        {
            Ok(diagnostics) => diagnostics,
            Err(err) => {
                warn!("pull-file diagnostic worker failed: {}", err);
                None
            }
        }
    }

    pub async fn pull_workspace_diagnostics_slow(
        &self,
        cancel_token: CancellationToken,
    ) -> Vec<(Uri, Vec<Diagnostic>)> {
        info!("workspace diagnostic pull slow started");
        let watchdog_status =
            LongRunningWatchdogStatus::new("Preparing workspace diagnostics (slow pull)");
        let _watchdog =
            spawn_long_running_watchdog("workspace diagnostics", watchdog_status.clone());
        let mut token = self.workspace_diagnostic_token.lock().await;
        if let Some(token) = token.as_ref() {
            token.cancel();
            debug!("cancel workspace diagnostic");
        }
        token.replace(cancel_token.clone());
        drop(token);

        let mut result = Vec::new();
        let status_bar = self.status_bar.clone();
        status_bar
            .create_progress_task(ProgressTask::DiagnoseWorkspace)
            .await;
        status_bar.update_progress_task(
            ProgressTask::DiagnoseWorkspace,
            None,
            Some(String::from("Preparing diagnostics")),
        );
        watchdog_status.set_phase("Preparing workspace diagnostics (slow pull)");

        let (main_workspace_file_ids, shared_data) = {
            let analysis = self.analysis.read().await;
            let file_ids = analysis
                .compilation
                .get_db()
                .get_module_index()
                .get_main_workspace_file_ids();
            info!(
                "precomputing shared diagnostic data for {} workspace files",
                file_ids.len()
            );
            let shared_data = self.force_precompute_shared_diagnostic_data(&analysis);
            (file_ids, shared_data)
        };
        let (tx, mut rx) = tokio::sync::mpsc::channel::<Option<(Vec<Diagnostic>, Uri)>>(100);
        let valid_file_count = main_workspace_file_ids.len();
        if valid_file_count != 0 {
            status_bar.update_progress_task(
                ProgressTask::DiagnoseWorkspace,
                Some(0),
                Some(format!("Diagnosing 0/{}", valid_file_count)),
            );
            watchdog_status.set_progress(
                "Diagnosing workspace files (slow pull)",
                0,
                valid_file_count,
            );
        }
        let semaphore = Arc::new(Semaphore::new(workspace_diagnostic_parallelism()));

        for file_id in main_workspace_file_ids {
            let analysis = self.analysis.clone();
            let token = cancel_token.clone();
            let tx = tx.clone();
            let semaphore = semaphore.clone();
            let shared_data = shared_data.clone();
            tokio::spawn(async move {
                let result = diagnose_workspace_file_off_thread(
                    analysis,
                    semaphore,
                    file_id,
                    shared_data,
                    token,
                )
                .await;
                let _ = tx.send(result).await;
            });
        }
        drop(tx);

        let mut count = 0;
        let mut last_percentage = 0;
        while count < valid_file_count {
            tokio::select! {
                _ = cancel_token.cancelled() => {
                    break;
                }
                file_diagnostic_result = rx.recv() => {
                    let Some(file_diagnostic_result) = file_diagnostic_result else {
                        break;
                    };

                    if let Some((diagnostics, uri)) = file_diagnostic_result {
                        result.push((uri, diagnostics));
                    }
                    count += 1;
                    let percentage_done = ((count as f32 / valid_file_count as f32) * 100.0) as u32;
                    if last_percentage != percentage_done {
                        last_percentage = percentage_done;
                        let message = format!(
                            "Diagnosing {}/{} ({}%)",
                            count, valid_file_count, percentage_done
                        );
                        status_bar.update_progress_task(
                            ProgressTask::DiagnoseWorkspace,
                            Some(percentage_done),
                            Some(message),
                        );
                        watchdog_status.set_progress(
                            "Diagnosing workspace files (slow pull)",
                            count,
                            valid_file_count,
                        );
                    }
                }
            }
        }
        if count < valid_file_count && !cancel_token.is_cancelled() {
            warn!(
                "workspace diagnostic pull slow ended early: completed={} expected={}",
                count, valid_file_count
            );
        }

        status_bar.finish_progress_task(
            ProgressTask::DiagnoseWorkspace,
            Some("Diagnostics complete".to_string()),
        );
        if count == valid_file_count && !cancel_token.is_cancelled() {
            self.notify_startup_complete();
        } else {
            info!(
                "workspace diagnostic pull slow finished without startup completion: completed={} expected={} cancelled={}",
                count,
                valid_file_count,
                cancel_token.is_cancelled()
            );
        }

        result
    }

    pub async fn pull_workspace_diagnostics_fast(
        &self,
        cancel_token: CancellationToken,
    ) -> Vec<(Uri, Vec<Diagnostic>)> {
        info!("workspace diagnostic pull fast started");
        let watchdog_status =
            LongRunningWatchdogStatus::new("Preparing workspace diagnostics (fast pull)");
        let _watchdog =
            spawn_long_running_watchdog("workspace diagnostics", watchdog_status.clone());
        let mut token = self.workspace_diagnostic_token.lock().await;
        if let Some(token) = token.as_ref() {
            token.cancel();
            debug!("cancel workspace diagnostic");
        }
        token.replace(cancel_token.clone());
        drop(token);

        let mut result = Vec::new();
        let status_bar = self.status_bar.clone();
        status_bar
            .create_progress_task(ProgressTask::DiagnoseWorkspace)
            .await;
        status_bar.update_progress_task(
            ProgressTask::DiagnoseWorkspace,
            None,
            Some(String::from("Preparing diagnostics")),
        );
        watchdog_status.set_phase("Preparing workspace diagnostics (fast pull)");

        let (main_workspace_file_ids, shared_data) = {
            let analysis = self.analysis.read().await;
            let file_ids = analysis
                .compilation
                .get_db()
                .get_module_index()
                .get_main_workspace_file_ids();
            info!(
                "precomputing shared diagnostic data for {} workspace files",
                file_ids.len()
            );
            let shared_data = self.force_precompute_shared_diagnostic_data(&analysis);
            (file_ids, shared_data)
        };

        let (tx, mut rx) = tokio::sync::mpsc::channel::<Option<(Vec<Diagnostic>, Uri)>>(100);
        let valid_file_count = main_workspace_file_ids.len();
        if valid_file_count != 0 {
            status_bar.update_progress_task(
                ProgressTask::DiagnoseWorkspace,
                Some(0),
                Some(format!("Diagnosing 0/{}", valid_file_count)),
            );
            watchdog_status.set_progress(
                "Diagnosing workspace files (fast pull)",
                0,
                valid_file_count,
            );
        }

        let analysis = self.analysis.clone();
        let semaphore = Arc::new(Semaphore::new(workspace_diagnostic_parallelism()));
        for file_id in main_workspace_file_ids {
            let analysis = analysis.clone();
            let token = cancel_token.clone();
            let tx = tx.clone();
            let semaphore = semaphore.clone();
            let shared_data = shared_data.clone();
            tokio::spawn(async move {
                let result = diagnose_workspace_file_off_thread(
                    analysis,
                    semaphore,
                    file_id,
                    shared_data,
                    token,
                )
                .await;
                let _ = tx.send(result).await;
            });
        }
        drop(tx);

        let mut count = 0;
        if valid_file_count != 0 {
            let mut last_percentage = 0;

            while count < valid_file_count {
                tokio::select! {
                    _ = cancel_token.cancelled() => {
                        break;
                    }
                    file_diagnostic_result = rx.recv() => {
                        let Some(file_diagnostic_result) = file_diagnostic_result else {
                            break;
                        };

                        if let Some((diagnostics, uri)) = file_diagnostic_result {
                            result.push((uri, diagnostics));
                        }

                        count += 1;
                        let percentage_done = ((count as f32 / valid_file_count as f32) * 100.0) as u32;
                        if last_percentage != percentage_done {
                            last_percentage = percentage_done;
                            let message = format!(
                                "Diagnosing {}/{} ({}%)",
                                count, valid_file_count, percentage_done
                            );
                            status_bar.update_progress_task(
                                ProgressTask::DiagnoseWorkspace,
                                Some(percentage_done),
                                Some(message),
                            );
                            watchdog_status.set_progress(
                                "Diagnosing workspace files (fast pull)",
                                count,
                                valid_file_count,
                            );
                        }
                    }
                }
            }
        }
        if count < valid_file_count && !cancel_token.is_cancelled() {
            warn!(
                "workspace diagnostic pull fast ended early: completed={} expected={}",
                count, valid_file_count
            );
        }

        status_bar.finish_progress_task(
            ProgressTask::DiagnoseWorkspace,
            Some("Diagnostics complete".to_string()),
        );
        if count == valid_file_count && !cancel_token.is_cancelled() {
            self.notify_startup_complete();
        } else {
            info!(
                "workspace diagnostic pull fast finished without startup completion: completed={} expected={} cancelled={}",
                count,
                valid_file_count,
                cancel_token.is_cancelled()
            );
        }

        result
    }
}

fn workspace_diagnostic_parallelism() -> usize {
    if let Ok(raw) = std::env::var("GLUALS_WORKSPACE_DIAGNOSTIC_PARALLELISM")
        && let Ok(parsed) = raw.parse::<usize>()
        && parsed > 0
    {
        return parsed;
    }

    std::thread::available_parallelism()
        .map(std::num::NonZeroUsize::get)
        .unwrap_or(1)
        .clamp(1, 16)
}

async fn diagnose_workspace_file_off_thread(
    analysis: Arc<RwLock<EmmyLuaAnalysis>>,
    semaphore: Arc<Semaphore>,
    file_id: FileId,
    shared_data: Arc<glua_code_analysis::SharedDiagnosticData>,
    cancel_token: CancellationToken,
) -> Option<(Vec<Diagnostic>, Uri)> {
    if cancel_token.is_cancelled() {
        return None;
    }

    let permit = tokio::select! {
        _ = cancel_token.cancelled() => {
            return None;
        }
        permit = semaphore.acquire_owned() => {
            match permit {
                Ok(permit) => permit,
                Err(_) => return None,
            }
        }
    };

    let blocking_analysis = analysis;
    let blocking_shared_data = shared_data;
    let blocking_token = cancel_token.clone();
    match tokio::task::spawn_blocking(move || {
        let _permit = permit;
        if blocking_token.is_cancelled() {
            return None;
        }

        // Diagnose under a blocking read lock to avoid starving Tokio worker threads.
        let guard = blocking_analysis.blocking_read();
        let diagnostics = guard.diagnose_file_with_shared(
            file_id,
            blocking_token.clone(),
            blocking_shared_data,
        )?;
        let uri = guard.get_uri(file_id)?;
        Some((diagnostics, uri))
    })
    .await
    {
        Ok(result) => result,
        Err(err) => {
            warn!(
                "workspace diagnostic worker failed for file {:?}: {}",
                file_id, err
            );
            None
        }
    }
}

async fn push_workspace_diagnostic(
    file_diagnostic: FileDiagnostic,
    analysis: Arc<RwLock<EmmyLuaAnalysis>>,
    client_proxy: Arc<ClientProxy>,
    status_bar: Arc<StatusBar>,
    silent: bool,
    cancel_token: CancellationToken,
) {
    info!("workspace diagnostic push started; silent={}", silent);
    let watchdog_status = LongRunningWatchdogStatus::new("Preparing workspace diagnostics (push)");
    let _watchdog = spawn_long_running_watchdog("workspace diagnostics", watchdog_status.clone());
    if !silent {
        status_bar
            .create_progress_task(ProgressTask::DiagnoseWorkspace)
            .await;
        status_bar.update_progress_task(
            ProgressTask::DiagnoseWorkspace,
            None,
            Some(String::from("Preparing diagnostics")),
        );
    }
    watchdog_status.set_phase("Preparing workspace diagnostics (push)");

    let (main_workspace_file_ids, shared_data) = {
        let read_analysis = analysis.read().await;
        let file_ids = read_analysis
            .compilation
            .get_db()
            .get_module_index()
            .get_main_workspace_file_ids();
        info!(
            "precomputing shared diagnostic data for {} workspace files",
            file_ids.len()
        );
        let shared_data = file_diagnostic.force_precompute_shared_diagnostic_data(&read_analysis);
        (file_ids, shared_data)
    };
    // diagnostic files
    let (tx, mut rx) = tokio::sync::mpsc::channel::<FileId>(100);
    let valid_file_count = main_workspace_file_ids.len();
    if !silent && valid_file_count != 0 {
        status_bar.update_progress_task(
            ProgressTask::DiagnoseWorkspace,
            Some(0),
            Some(format!("Diagnosing 0/{}", valid_file_count)),
        );
    }
    if valid_file_count != 0 {
        watchdog_status.set_progress("Diagnosing workspace files (push)", 0, valid_file_count);
    }

    let semaphore = Arc::new(Semaphore::new(workspace_diagnostic_parallelism()));
    for file_id in main_workspace_file_ids {
        let analysis = analysis.clone();
        let token = cancel_token.clone();
        let client = client_proxy.clone();
        let semaphore = semaphore.clone();
        let tx = tx.clone();
        let shared_data = shared_data.clone();
        tokio::spawn(async move {
            let result = diagnose_workspace_file_off_thread(
                analysis,
                semaphore,
                file_id,
                shared_data,
                token,
            )
            .await;
            if let Some((diagnostics, uri)) = result {
                let diagnostic_param = lsp_types::PublishDiagnosticsParams {
                    uri,
                    diagnostics,
                    version: None,
                };
                client.publish_diagnostics(diagnostic_param);
            }
            let _ = tx.send(file_id).await;
        });
    }
    drop(tx);

    let mut count = 0;
    if valid_file_count != 0 {
        let mut last_percentage = 0;
        while count < valid_file_count {
            tokio::select! {
                _ = cancel_token.cancelled() => {
                    break;
                }
                maybe_file_id = rx.recv() => {
                    if maybe_file_id.is_none() {
                        break;
                    }
                    count += 1;
                    let percentage_done = ((count as f32 / valid_file_count as f32) * 100.0) as u32;
                    if last_percentage != percentage_done {
                        last_percentage = percentage_done;
                        if !silent {
                            let message = format!(
                                "Diagnosing {}/{} ({}%)",
                                count, valid_file_count, percentage_done
                            );
                            status_bar.update_progress_task(
                                ProgressTask::DiagnoseWorkspace,
                                Some(percentage_done),
                                Some(message),
                            );
                        }
                        watchdog_status.set_progress(
                            "Diagnosing workspace files (push)",
                            count,
                            valid_file_count,
                        );
                    }
                }
            }
        }
        if count < valid_file_count && !cancel_token.is_cancelled() {
            warn!(
                "workspace diagnostic push ended early: completed={} expected={} silent={}",
                count, valid_file_count, silent
            );
        }
    }

    if !silent {
        status_bar.finish_progress_task(
            ProgressTask::DiagnoseWorkspace,
            Some("Diagnostics complete".to_string()),
        );
        if count == valid_file_count && !cancel_token.is_cancelled() {
            file_diagnostic.notify_startup_complete();
        } else {
            info!(
                "workspace diagnostic push finished without startup completion: completed={} expected={} cancelled={} silent={}",
                count,
                valid_file_count,
                cancel_token.is_cancelled(),
                silent
            );
        }
    }
}

fn changed_line_span(previous_text: &str, new_text: &str) -> Option<(u32, u32)> {
    let previous_bytes = previous_text.as_bytes();
    let new_bytes = new_text.as_bytes();

    if previous_bytes == new_bytes {
        return None;
    }

    let mut prefix = 0usize;
    let prefix_limit = previous_bytes.len().min(new_bytes.len());
    while prefix < prefix_limit && previous_bytes[prefix] == new_bytes[prefix] {
        prefix += 1;
    }

    let mut previous_suffix = previous_bytes.len();
    let mut new_suffix = new_bytes.len();
    while previous_suffix > prefix
        && new_suffix > prefix
        && previous_bytes[previous_suffix - 1] == new_bytes[new_suffix - 1]
    {
        previous_suffix -= 1;
        new_suffix -= 1;
    }

    let start_line = count_newlines(&new_bytes[..prefix]);
    if count_newlines(previous_bytes) != count_newlines(new_bytes) {
        return Some((start_line as u32, u32::MAX));
    }

    let end_line = count_newlines(&new_bytes[..new_suffix]).max(start_line);
    Some((start_line as u32, end_line as u32))
}

fn count_newlines(bytes: &[u8]) -> usize {
    bytes.iter().filter(|&&byte| byte == b'\n').count()
}

fn filter_diagnostics_by_line_span(
    diagnostics: Vec<Diagnostic>,
    start_line: u32,
    end_line: u32,
) -> Vec<Diagnostic> {
    diagnostics
        .into_iter()
        .filter(|diagnostic| {
            diagnostic.range.end.line < start_line || diagnostic.range.start.line > end_line
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use crate::context::{ClientProxy, StatusBar};
    use glua_code_analysis::{DiagnosticCode, EmmyLuaAnalysis, Emmyrc, file_path_to_uri};
    use googletest::prelude::*;
    use lsp_server::{Connection, Message};
    use lsp_types::NumberOrString;
    use lsp_types::{Position, Range};
    use std::sync::Arc;
    use tokio::sync::RwLock;
    use tokio_util::sync::CancellationToken;

    use super::{FileDiagnostic, changed_line_span, filter_diagnostics_by_line_span};

    fn diagnostic(start_line: u32, end_line: u32) -> lsp_types::Diagnostic {
        lsp_types::Diagnostic {
            range: Range {
                start: Position {
                    line: start_line,
                    character: 0,
                },
                end: Position {
                    line: end_line,
                    character: 0,
                },
            },
            ..Default::default()
        }
    }

    #[gtest]
    fn changed_line_span_tracks_single_line_edits() -> Result<()> {
        verify_that!(
            changed_line_span("print(1)\n", "print(12)\n"),
            some(eq((0, 0)))
        )?;
        Ok(())
    }

    #[gtest]
    fn changed_line_span_tracks_multiline_edits() -> Result<()> {
        verify_that!(
            changed_line_span("a\nb\nc\n", "a\nb\nx\ny\nc\n"),
            some(eq((2, u32::MAX)))
        )?;
        Ok(())
    }

    #[gtest]
    fn changed_line_span_suppresses_to_eof_when_line_numbers_shift() -> Result<()> {
        verify_that!(
            changed_line_span("bad\nfoo\nbar\n", "x\nbad\nfoo\nbar\n"),
            some(eq((0, u32::MAX)))
        )?;
        Ok(())
    }

    #[gtest]
    fn filter_diagnostics_by_line_span_removes_overlapping_ranges() -> Result<()> {
        let filtered = filter_diagnostics_by_line_span(
            vec![
                diagnostic(0, 0),
                diagnostic(1, 1),
                diagnostic(1, 2),
                diagnostic(3, 3),
            ],
            1,
            1,
        )
        .into_iter()
        .map(|diagnostic| (diagnostic.range.start.line, diagnostic.range.end.line))
        .collect::<Vec<_>>();

        verify_that!(filtered.as_slice(), eq(&[(0, 0), (3, 3)]))?;
        Ok(())
    }

    #[gtest]
    fn workspace_loaded_notification_does_not_suppress_startup_complete() -> Result<()> {
        let (connection, peer) = Connection::memory();
        let client = Arc::new(ClientProxy::new(connection));
        let status_bar = Arc::new(StatusBar::new(client.clone()));
        let analysis = Arc::new(RwLock::new(EmmyLuaAnalysis::new()));
        let file_diagnostic = FileDiagnostic::new(analysis, status_bar, client);

        file_diagnostic.notify_workspace_loaded();
        file_diagnostic.notify_workspace_loaded();
        file_diagnostic.notify_startup_complete();

        let statuses = peer
            .receiver
            .try_iter()
            .filter_map(|message| match message {
                Message::Notification(notification)
                    if notification.method == "gluals/serverStatus" =>
                {
                    notification
                        .params
                        .get("state")
                        .and_then(serde_json::Value::as_str)
                        .map(str::to_string)
                }
                _ => None,
            })
            .collect::<Vec<_>>();

        verify_that!(
            statuses.as_slice(),
            eq(&["workspaceLoaded".to_string(), "startupComplete".to_string()])
        )?;
        Ok(())
    }

    #[gtest]
    fn single_file_diagnostics_use_shared_workspace_data() -> Result<()> {
        let mut analysis = EmmyLuaAnalysis::new();
        let workspace = std::env::temp_dir().join("gmod_glua_ls_shared_single_file_diagnostics");
        analysis.add_main_workspace(workspace.clone());

        let mut emmyrc = Emmyrc::default();
        emmyrc.gmod.enabled = true;
        analysis.update_config(Arc::new(emmyrc));
        analysis
            .diagnostic
            .enable_only(DiagnosticCode::GmodRealmMismatchHeuristic);

        let server_uri = file_path_to_uri(&workspace.join("lua/autorun/server/sv_api.lua"))
            .expect("server URI should parse");
        analysis.update_file_by_uri(
            &server_uri,
            Some("function ServerOnlyApi() return true end".to_string()),
        );

        let client_uri = file_path_to_uri(&workspace.join("lua/autorun/client/cl_user.lua"))
            .expect("client URI should parse");
        let client_file = analysis
            .update_file_by_uri(&client_uri, Some("ServerOnlyApi()".to_string()))
            .expect("client file should be indexed");

        let (connection, _peer) = Connection::memory();
        let client = Arc::new(ClientProxy::new(connection));
        let status_bar = Arc::new(StatusBar::new(client.clone()));
        let analysis = Arc::new(RwLock::new(analysis));
        let file_diagnostic = FileDiagnostic::new(analysis.clone(), status_bar, client);

        let diagnostics = {
            let guard = analysis.blocking_read();
            file_diagnostic
                .diagnose_file_with_shared_data(&guard, client_file, CancellationToken::new())
                .unwrap_or_default()
        };

        let target_code = Some(NumberOrString::String(
            DiagnosticCode::GmodRealmMismatchHeuristic
                .get_name()
                .to_string(),
        ));
        verify_that!(
            diagnostics
                .iter()
                .any(|diagnostic| diagnostic.code == target_code),
            eq(true)
        )?;

        let first_cached = file_diagnostic.shared_diagnostic_data_cache.cached_ptr();
        {
            let guard = analysis.blocking_read();
            let _ = file_diagnostic.diagnose_file_with_shared_data(
                &guard,
                client_file,
                CancellationToken::new(),
            );
        }
        let second_cached = file_diagnostic.shared_diagnostic_data_cache.cached_ptr();
        verify_that!(second_cached, eq(first_cached))?;

        file_diagnostic.invalidate_shared_diagnostic_data();
        verify_that!(
            file_diagnostic.shared_diagnostic_data_cache.is_empty(),
            eq(true)
        )?;

        Ok(())
    }
}