shader_language_server 1.4.2

Language server for HLSL / GLSL / WGSL shaders using LSP protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
//! This is where we cache and update file symbol and validation data depending on its include context and variant.
//!
//! # File roles
//!
//! Every watched file lives in [`ServerLanguageFileCache::files`], keyed by its
//! [`Url`]. A single file can play one or more of three roles at once, tracked
//! by the flags on [`ServerFileCache`]:
//!
//! - **Main file** (`is_main_file`): a file the editor has explicitly opened
//!   (`textDocument/didOpen`). Its content lives in memory rather than on disk,
//!   since the user may have unsaved edits. Main files get their symbols and
//!   diagnostics computed and published to the client.
//! - **Variant file** (`is_variant_file`): a file selected through the variant
//!   window. A variant pins an entry point / stage / defines and acts as the
//!   compilation root used to validate the whole include tree below it (see
//!   [`ShaderVariant`]). There is at most one active variant
//!   ([`ServerLanguageFileCache::variant`]).
//! - **Dependency**: a file pulled in only because another file `#include`s it.
//!   It is watched (so edits to it can invalidate its includers) but is not
//!   itself a publish target. A file is a pure dependency when neither role
//!   flag is set.
//!
//! These roles overlap: an opened header is both a main file and a dependency
//! of the translation units that include it. Only main and variant files are
//! "cachable" ([`ServerFileCache::is_cachable_file`]) and carry the heavyweight
//! [`ServerFileCacheData`] (symbols, intrinsics, diagnostics); plain
//! dependencies keep only their parsed [`ShaderModuleHandle`] and borrow their
//! cached symbols from whichever main/variant file pulled them in.
//!
//! # Included vs includer files
//!
//! The cache navigates the include tree in **both directions**, and the two
//! directions are named after the `#include` relation so they can't be confused:
//!
//! - **Included files** are the files a given file pulls in — everything reachable
//!   *downward* by following its `#include`s. Computed by walking the file's own
//!   symbol cache: [`get_all_included_files`] returns every watched include,
//!   [`get_included_main_files`] only the cachable ones.
//! - **Includer files** go *upward*: the main/variant files that `#include` the
//!   given file and therefore must be re-cached when it changes. Computed by
//!   scanning every cachable file for one whose symbol cache `has_dependency` on
//!   the given path ([`get_includer_main_files`]).
//!
//! So for an include tree `main.hlsl -> common.hlsl -> math.hlsl`:
//! `common.hlsl`'s included files are `{math.hlsl}`, while its includer files
//! are `{main.hlsl}`. This is why an edit to `common.hlsl` re-caches its
//! *includer* `main.hlsl` (step 3 below), and why evicting `main.hlsl` prunes
//! the files it *included* that nothing else needs.
//!
//! # Caching flow
//!
//! Caching is driven through [`ServerLanguageFileCache::cache_batched_file_data`],
//! which receives a batch of [`AsyncCacheRequest`]s (typically the files touched
//! by an edit) and:
//!
//! 1. If the active variant — or any file it relies on — is in the batch, the
//!    variant is recomputed first, because validating the variant produces the
//!    authoritative diagnostics for its entire include tree.
//! 2. Each remaining requested file is cached via [`cache_file_data`]
//!    ([`Self::cache_file_data`]), which parses symbols, runs the validator, and
//!    stores the result in [`ServerFileCacheData`].
//! 3. Dirty files trigger re-caching of their *includer* main files (the files
//!    that `#include` them), found via [`get_includer_main_files`]
//!    ([`Self::get_includer_main_files`]).
//!
//! [`cache_file_data`] also performs **dirty tracking** (skipping work when a
//! file's preprocessor context is unchanged), copies variant diagnostics down
//! onto the included main files in its include tree, and prunes **dangling
//! dependencies** — files that are no longer reachable from any main/variant
//! file after the include tree changed.
//!
//! # Lifecycle
//!
//! - `watch_*` ([`watch_main_file`], [`watch_variant_file`], [`watch_dependency`])
//!   add or upgrade a file's roles when it is opened / selected / included.
//! - [`update_file`] keeps the parsed tree-sitter module in sync with editor
//!   edits without recomputing symbols.
//! - `remove_*` ([`remove_main_file`], [`remove_variant_file`]) downgrade a file
//!   (clearing the closed role) or, if it has no remaining role and nothing
//!   depends on it, evict it and any dependencies that become dangling.
//!
//! [`cache_file_data`]: Self::cache_file_data
//! [`get_includer_main_files`]: Self::get_includer_main_files
//! [`get_all_included_files`]: Self::get_all_included_files
//! [`get_included_main_files`]: Self::get_included_main_files
//! [`watch_main_file`]: Self::watch_main_file
//! [`watch_variant_file`]: Self::watch_variant_file
//! [`watch_dependency`]: Self::watch_dependency
//! [`update_file`]: Self::update_file
//! [`remove_main_file`]: Self::remove_main_file
//! [`remove_variant_file`]: Self::remove_variant_file

use std::{
    cell::RefCell,
    collections::{HashMap, HashSet},
    path::{Path, PathBuf},
    rc::Rc,
};

use crate::{
    profile_scope,
    server::{
        async_message::AsyncCacheRequest,
        clean_url,
        common::{lsp_range_to_shader_range, read_string_lossy},
        server_language_data::ServerLanguageData,
    },
};
use log::{debug, info, warn};
use lsp_types::Url;
use shader_sense::{
    position::ShaderFileRange,
    shader::ShadingLanguage,
    shader_error::{ShaderDiagnostic, ShaderDiagnosticList, ShaderDiagnosticSeverity, ShaderError},
    symbols::{
        intrinsics::ShaderIntrinsics,
        prepocessor::ShaderPreprocessorContext,
        shader_module::{ShaderModuleHandle, ShaderSymbols},
        shader_module_parser::ShaderModuleParser,
        symbol_list::ShaderSymbolListRef,
        symbol_provider::SymbolProvider,
    },
    validator::validator::ValidatorImpl,
};

use super::{server_config::ServerConfig, shader_variant::ShaderVariant};

#[derive(Debug, Clone, Default)]
pub struct ServerFileCacheData {
    pub symbol_cache: ShaderSymbols, // Store symbols to avoid computing them at every change.
    pub intrinsics: ShaderSymbolListRef<'static>, // Cached intrinsics to not recompute them everytime
    pub diagnostic_cache: ShaderDiagnosticList,   // Cached diagnostic
}

#[derive(Debug, Clone)]
pub struct ServerFileCache {
    pub shading_language: ShadingLanguage,
    pub shader_module: ShaderModuleHandle, // Store content on change as its not on disk.
    pub data: Option<ServerFileCacheData>, // Data for file opened and edited.
    // A file can be dependency, main, dependent variant or main variant.
    is_main_file: bool,    // main file are opened file in editor.
    is_variant_file: bool, // variant are set through variant window.
}

impl ServerFileCache {
    pub fn is_main_file(&self) -> bool {
        self.is_main_file
    }
    pub fn is_cachable_file(&self) -> bool {
        self.is_main_file || self.is_variant_file
    }
    pub fn has_data(&self) -> bool {
        self.data.is_some()
    }
    pub fn get_data(&self) -> &ServerFileCacheData {
        assert!(
            self.data.is_some(),
            "Trying to get data from file {} which does not have cache.",
            RefCell::borrow(&self.shader_module).file_path.display()
        );
        self.data.as_ref().unwrap()
    }
}

pub struct ServerLanguageFileCache {
    pub files: HashMap<Url, ServerFileCache>,
    pub variant: Option<ShaderVariant>,
    pub workspace_folder: Vec<Url>,
}

impl ServerLanguageFileCache {
    pub fn new() -> Self {
        Self {
            files: HashMap::new(),
            variant: None,
            workspace_folder: Vec::new(),
        }
    }
    fn get_workspace_folder(&self, uri: &Url) -> Option<&Url> {
        let file_path = uri.to_file_path().unwrap();
        self.workspace_folder
            .iter()
            .find(|w| file_path.starts_with(&w.to_file_path().unwrap()))
    }
    // Get all main files that #include the given file (its includers).
    pub fn get_includer_main_files(&self, included_url: &Url) -> HashSet<Url> {
        let included_file_path = included_url.to_file_path().unwrap();
        self.files
            .iter()
            .filter(|(file_url, file)| {
                *file_url != included_url
                    && file.is_cachable_file()
                    && file.has_data()
                    && file
                        .get_data()
                        .symbol_cache
                        .has_dependency(&included_file_path)
            })
            .map(|(url, _file)| url.clone())
            .collect()
    }
    // Find a variant from the list of all main file and return the first found.
    pub fn find_variant_from_includer_main_files(
        &self,
        included_file_uri: &Url,
    ) -> Option<ShaderVariant> {
        let first_main_file = self.get_includer_main_files(included_file_uri);
        if let Some(first_main_file) = first_main_file.into_iter().next() {
            let shading_language = self.files.get(&first_main_file).unwrap().shading_language;
            info!(
                "No variant set, using {} for {} as a variant automatically.",
                first_main_file, included_file_uri
            );
            // Auto generate a variant from found main file.
            Some(ShaderVariant {
                url: first_main_file,
                shading_language,
                entry_point: "".into(),
                stage: None,
                defines: HashMap::new(),
                includes: Vec::new(),
            })
        } else {
            None
        }
    }
    // Get all main files that the given file #includes (its included files).
    #[allow(dead_code)]
    pub fn get_included_main_files(&self, url: &Url) -> HashSet<Url> {
        match self.files.get(url) {
            Some(file) => {
                let mut included_files = HashSet::new();
                file.get_data().symbol_cache.visit_includes(&mut |include| {
                    let include_uri = Url::from_file_path(&include.get_absolute_path()).unwrap();
                    let is_cachable = match self.files.get(&include_uri) {
                        Some(deps) => deps.is_cachable_file(),
                        None => false,
                    };
                    if is_cachable {
                        included_files.insert(include_uri);
                    }
                });
                included_files
            }
            None => {
                debug_assert!(
                    false,
                    "Trying to get included main files for file {} that is not watched",
                    url
                );
                HashSet::new()
            }
        }
    }
    // Get all files that the given file #includes (its included files).
    pub fn get_all_included_files(&self, url: &Url) -> HashSet<Url> {
        match self.files.get(url) {
            Some(file) => {
                let mut included_files = HashSet::new();
                file.get_data().symbol_cache.visit_includes(&mut |include| {
                    let include_uri = Url::from_file_path(&include.get_absolute_path()).unwrap();
                    match self.files.get(&include_uri) {
                        Some(_) => {
                            included_files.insert(include_uri);
                        }
                        None => {}
                    };
                });
                included_files
            }
            None => {
                debug_assert!(
                    false,
                    "Trying to get all included files for file {} that is not watched",
                    url
                );
                HashSet::new()
            }
        }
    }
    #[allow(unused)]
    pub fn is_variant_including_file(&self, url: &Url) -> bool {
        let file_path = url.to_file_path().unwrap();
        match &self.variant {
            Some(variant) => match self.files.get(&variant.url) {
                Some(variant_cached_file) => variant_cached_file
                    .get_data()
                    .symbol_cache
                    .has_dependency(&file_path),
                None => false,
            },
            None => false,
        }
    }
    fn __cache_file_data(
        &mut self,
        uri: &Url,
        validator: &dyn ValidatorImpl,
        shader_module_parser: &mut ShaderModuleParser,
        symbol_provider: &SymbolProvider,
        config: &ServerConfig,
        variant: &Option<ShaderVariant>,
        dirty_deps: HashSet<PathBuf>,
    ) -> Result<(), ShaderError> {
        let file_path = uri.to_file_path().unwrap();

        // Compute params
        let shader_params =
            config.into_shader_params(self.get_workspace_folder(uri), variant.clone());
        let mut context =
            ShaderPreprocessorContext::main(&file_path, shader_params.context.clone());

        // Do not recache & revalidate if not dirty.
        match &self.files.get(&uri).unwrap().data {
            Some(data) => {
                let is_dirty = data
                    .symbol_cache
                    .get_preprocessor()
                    .context
                    .is_dirty(&file_path, &context);
                let has_cache = self.files.get_mut(&uri).unwrap().data.is_some();
                let has_dirty = !dirty_deps.is_empty();
                if !is_dirty && !has_dirty && has_cache {
                    return Ok(());
                }
            }
            None => {}
        };
        for dirty_dep in dirty_deps {
            context.mark_dirty(dirty_dep);
        }

        // Get old data and replace it by dummy to avoid empty data on early exit.
        let old_data = self.files.get_mut(&uri).unwrap().data.take();
        self.files.get_mut(&uri).unwrap().data = Some(ServerFileCacheData::default());

        // Get symbols for main file.
        let (mut symbols, symbol_diagnostics) = if config.get_symbols() {
            profile_scope!("Querying symbols for file {}", uri);
            let shading_language = self.files.get(uri).unwrap().shading_language;
            let shader_module = Rc::clone(&self.files.get(uri).unwrap().shader_module);
            let shader_module = RefCell::borrow(&shader_module);
            match symbol_provider.query_symbols_with_context(
                &shader_module,
                &mut context,
                &shader_params.compilation,
                &mut |include| {
                    let include_uri = Url::from_file_path(&include.get_absolute_path()).unwrap();
                    let included_file = self.watch_dependency(
                        &include_uri,
                        shading_language,
                        shader_module_parser,
                    )?;
                    Ok(Some(Rc::clone(&included_file.shader_module)))
                },
                old_data.map(|e| e.symbol_cache),
            ) {
                Ok(symbols) => (symbols, ShaderDiagnosticList::default()),
                Err(error) => {
                    // Return this error & store it to display it as a diagnostic & dont prevent linting.
                    match error.into_diagnostic(ShaderDiagnosticSeverity::Warning) {
                        Some(diagnostic) => (
                            ShaderSymbols::new(&file_path, shader_params.context.clone()),
                            ShaderDiagnosticList {
                                diagnostics: vec![diagnostic],
                            },
                        ),
                        None => return Err(error),
                    }
                }
            }
        } else {
            (ShaderSymbols::default(), ShaderDiagnosticList::default())
        };
        // Get diagnostics
        let diagnostics = if config.get_validate() {
            profile_scope!("Validating file {}", uri);
            let shading_language = self.files.get(uri).unwrap().shading_language;
            let shader_module = Rc::clone(&self.files.get(uri).unwrap().shader_module);

            let mut diagnostic_list = {
                // TODO: should print warning if validation is too long.
                profile_scope!("Raw validation");
                let variant_shader_module = match &variant {
                    Some(variant) => {
                        Rc::clone(&self.files.get(&variant.url).unwrap().shader_module)
                    }
                    None => shader_module,
                };
                let diagnostics = match validator.validate_shader(
                    &RefCell::borrow(&variant_shader_module).content,
                    RefCell::borrow(&variant_shader_module).file_path.as_path(),
                    &shader_params,
                    &mut |deps_path: &Path| -> Option<String> {
                        let deps_uri = Url::from_file_path(deps_path).unwrap();
                        let deps_file = match self.get_file(&deps_uri) {
                            Some(deps_file) => deps_file,
                            None => {
                                if config.get_symbols() {
                                    warn!(
                                        "Should only get there if symbols did not add deps: {} from includer {}.",
                                        deps_uri,
                                        uri, // This is includer as we dont recurse here.
                                    );
                                }
                                // If include does not exist, add it to watched files.
                                match self.watch_dependency(
                                    &deps_uri,
                                    shading_language,
                                    shader_module_parser,
                                ) {
                                    Ok(deps_file) => deps_file,
                                    Err(err) => {
                                        warn!("Failed to watch deps {}", err);
                                        return None;
                                    }
                                }
                            }
                        };
                        let content = RefCell::borrow(&deps_file.shader_module).content.clone();
                        Some(content)
                    },
                ) {
                    Ok(diagnostics) => diagnostics,
                    Err(err) => ShaderDiagnosticList { diagnostics: vec![
                        ShaderDiagnostic {
                            severity: ShaderDiagnosticSeverity::Error,
                            error: format!("Failed to validate shader: {}", err),
                            range: ShaderFileRange::zero(file_path.clone())
                        }
                    ]},
                };
                diagnostics
            };
            {
                // Filter by severity.
                let required_severity = config.get_severity();
                diagnostic_list
                    .diagnostics
                    .retain(|e| e.severity.is_required(required_severity.clone()));
            }
            {
                // If includes have issues, diagnose them.
                let mut ascended_diagnostics: Vec<ShaderDiagnostic> =
                    symbols
                        .get_preprocessor()
                        .includes
                        .iter()
                        .filter_map(|include| {
                            for diagnostic in &diagnostic_list.diagnostics {
                                if diagnostic.severity != ShaderDiagnosticSeverity::Error {
                                    continue;
                                }
                                let diagnostic_path = &diagnostic.range.file_path;
                                if *diagnostic_path == file_path {
                                    continue; // Main file diagnostics
                                }
                                if *diagnostic_path == include.get_absolute_path() {
                                    return Some(ShaderDiagnostic {
                                        severity: ShaderDiagnosticSeverity::Error,
                                        error: format!(
                                            "File {} has issues:\n{}", // TODO: add command to file
                                            include.get_relative_path(),
                                            diagnostic.error
                                        ),
                                        range: include.get_file_range(),
                                    });
                                }
                                match include.cache.as_ref().unwrap().find_include(&mut |i| {
                                    i.get_absolute_path() == *diagnostic_path
                                }) {
                                    Some(includer) => {
                                        return Some(ShaderDiagnostic {
                                            severity: ShaderDiagnosticSeverity::Error,
                                            error: format!(
                                                "File {} has issues:\n{}",
                                                includer.get_relative_path(),
                                                diagnostic.error
                                            ),
                                            range: include.get_file_range(),
                                        })
                                    }
                                    None => {}
                                }
                            }
                            None
                        })
                        .collect();
                diagnostic_list
                    .diagnostics
                    .append(&mut ascended_diagnostics);
            }
            diagnostic_list
        } else {
            ShaderDiagnosticList::default()
        };

        symbols
            .get_preprocessor_mut()
            .diagnostics
            .extend(symbol_diagnostics.diagnostics);
        let shading_language = self.files.get(uri).unwrap().shading_language;
        let intrinsics = ShaderIntrinsics::get(shading_language)
            .get_intrinsics_symbol(&shader_params.compilation);
        self.files.get_mut(uri).unwrap().data = Some(ServerFileCacheData {
            symbol_cache: symbols,
            intrinsics,
            diagnostic_cache: diagnostics,
        });
        Ok(())
    }

    pub fn cache_file_data(
        &mut self,
        uri: &Url,
        validator: &dyn ValidatorImpl,
        shader_module_parser: &mut ShaderModuleParser,
        symbol_provider: &SymbolProvider,
        config: &ServerConfig,
        variant: &Option<ShaderVariant>,
        dirty_deps: HashSet<PathBuf>,
    ) -> Result<HashSet<Url>, ShaderError> {
        profile_scope!("Caching file data for file {}", uri);
        assert!(
            self.files.get(&uri).unwrap().is_cachable_file(),
            "Trying to cache data of dependency {}...",
            uri
        );
        // Check if we cache this file for the first time.
        // Fill it default to avoid early return and empty cache.
        let file_path = uri.to_file_path().unwrap();

        info!("Caching file {} as variant: {:#?}", uri, variant);
        let mut old_included_files = if self.files.get(&uri).unwrap().data.is_some() {
            self.get_all_included_files(uri)
        } else {
            HashSet::new() // No old included files as no cache
        };
        self.__cache_file_data(
            uri,
            validator,
            shader_module_parser,
            symbol_provider,
            config,
            variant,
            dirty_deps,
        )?;

        // Copy variant deps data to all its included data.
        if let Some(variant) = &variant {
            let variant_file = self.files.get(&variant.url).unwrap();
            let mut file_to_cache = HashMap::new();
            variant_file
                .get_data()
                .symbol_cache
                .visit_includes(&mut |include| {
                    // Here, we could visit the same include twice, which will overwrite final cache.
                    let include_url = Url::from_file_path(include.get_absolute_path()).unwrap();
                    match self.files.get(&include_url) {
                        Some(cached_file) => {
                            // Ensure we did not already got cache for this file,
                            // second include might have way less symbols (because of include guard mostly)
                            if !file_to_cache.contains_key(&include_url) {
                                if cached_file.is_main_file() {
                                    let symbol_cache = include.cache.clone().unwrap();
                                    let diagnostic_cache = ShaderDiagnosticList {
                                        diagnostics: variant_file
                                            .get_data()
                                            .diagnostic_cache
                                            .diagnostics
                                            .iter()
                                            .filter(|d| {
                                                let deps_file_path = &d.range.file_path;
                                                *deps_file_path == include.get_absolute_path()
                                                    || symbol_cache.has_dependency(deps_file_path)
                                            })
                                            .cloned()
                                            .collect(),
                                    };
                                    let shading_language =
                                        self.files.get(uri).unwrap().shading_language;
                                    let intrinsics = ShaderIntrinsics::get(shading_language)
                                        .get_intrinsics_symbol(
                                            &config
                                                .into_shader_params(
                                                    self.get_workspace_folder(uri),
                                                    Some(variant.clone()),
                                                )
                                                .compilation,
                                        );
                                    file_to_cache.insert(
                                        include_url,
                                        ServerFileCacheData {
                                            symbol_cache,
                                            intrinsics,
                                            diagnostic_cache,
                                        },
                                    );
                                }
                            }
                        }
                        None => {}
                    }
                });
            for (include_url, mut include_data) in file_to_cache {
                // When copying variant cache, some file in tree might be at their second include,
                // which remove most of their symbols due to include guard.
                // To workaround this, try to find their first occurence in variant and copy it.
                let mut first_include: HashSet<PathBuf> = HashSet::new();
                let mut reached_include = false;
                let variant_file = self.files.get(&variant.url).unwrap();
                include_data
                    .symbol_cache
                    .visit_includes_mut(&mut |include| {
                        // We only need previously declared element. Stop once we reach it.
                        if !reached_include {
                            reached_include = include.get_absolute_path() == file_path;
                            match variant_file.get_data().symbol_cache.find_include(
                                &mut |variant_include| {
                                    include.get_absolute_path()
                                        == variant_include.get_absolute_path()
                                },
                            ) {
                                Some(variant_include) => {
                                    if first_include.insert(include.get_absolute_path().into()) {
                                        include.cache = variant_include.cache.clone();
                                    }
                                }
                                None => {} // Not found
                            }
                        }
                    });
                self.files.get_mut(&include_url).unwrap().data = Some(include_data);
                // Mark them for publishing diagnostics.
            }
        }
        // Get dangling dependencies that need to be removed.
        let new_included_files = self.get_all_included_files(uri);
        old_included_files.retain(|f| {
            if f != uri {
                // Check if file was removed from include tree.
                if new_included_files.iter().find(|n| *n == f).is_none() {
                    self.is_dangling_file(f)
                } else {
                    false // Keep it by removing it from update.
                }
            } else {
                false // Avoid removing main file twice.
            }
        });
        // Remove these deps from cache.
        for old_included_file in &old_included_files {
            info!("Removing dangling deps {}", old_included_file);
            self.files.remove(old_included_file);
        }

        debug_assert!(
            self.get_file(uri).unwrap().data.is_some(),
            "Failed to cache data for file {}",
            uri
        );
        Ok(old_included_files)
    }
    pub fn cache_batched_file_data<F: Fn(&str, u32, u32)>(
        &mut self,
        mut async_cache_requests: Vec<AsyncCacheRequest>,
        language_data: &mut HashMap<ShadingLanguage, ServerLanguageData>,
        config: &ServerConfig,
        progress_callback: F,
    ) -> Result<(HashSet<Url>, HashSet<Url>), ShaderError> {
        fn get_file_name(uri: &Url) -> String {
            uri.to_file_path()
                .unwrap()
                .file_name()
                .unwrap()
                .to_string_lossy()
                .into_owned()
        }

        // Check if preamble file changed and update it.
        // We only track it if its open in editor.
        let need_to_recompute_all_glsl =
            if let Some(preamble_path) = config.get_glsl_preamble_path() {
                if let Ok(preamble_uri) = Url::from_file_path(preamble_path) {
                    let has_glsl_preamble_in_request = async_cache_requests
                        .iter()
                        .find(|r| r.url == preamble_uri)
                        .is_some();
                    has_glsl_preamble_in_request
                } else {
                    warn!("Failed to parse preamble path {:?}.", preamble_path);
                    false
                }
            } else {
                false
            };
        if need_to_recompute_all_glsl {
            for (url, file) in &self.files {
                if file.shading_language == ShadingLanguage::Glsl {
                    // Check if file already in request. If not, update it.
                    if async_cache_requests
                        .iter()
                        .find(|r| r.url == *url)
                        .is_none()
                    {
                        info!("Preamble edited, caching file {} for recomputing", url);
                        async_cache_requests.push(AsyncCacheRequest::new(
                            url.clone(),
                            ShadingLanguage::Glsl,
                            true,
                        ));
                    }
                }
            }
        }
        // Get unique files to update in batch aswell as dirty ones.
        let dirty_files: HashSet<Url> = async_cache_requests
            .iter()
            .filter(|r| r.dirty)
            .map(|r| r.url.clone())
            .collect();
        let dirty_dependencies: HashSet<PathBuf> = dirty_files
            .iter()
            .map(|url| url.to_file_path().unwrap())
            .collect();

        let main_variant_option = self.variant.clone();

        let need_to_recompute_main_variant = if let Some(main_variant) = &main_variant_option {
            let has_main_variant_in_request = async_cache_requests
                .iter()
                .find(|r| r.url == main_variant.url)
                .is_some();
            let has_main_variant_included_files_in_request =
                if let Some(variant_data) = &self.files.get(&main_variant.url).unwrap().data {
                    async_cache_requests
                        .iter()
                        .find(|r| {
                            let file_path = r.url.to_file_path().unwrap();
                            variant_data
                                .symbol_cache
                                .find_include(&mut |include| {
                                    include.get_absolute_path().as_os_str() == file_path.as_os_str()
                                })
                                .is_some()
                        })
                        .is_some()
                } else {
                    false
                };
            has_main_variant_in_request || has_main_variant_included_files_in_request
        } else {
            false // no variant.
        };
        let mut files_to_clear = HashSet::new();
        let mut includer_files_to_update = HashSet::new();
        let mut files_updating: HashSet<Url> =
            async_cache_requests.iter().map(|r| r.url.clone()).collect();
        let mut unique_remaining_files = files_updating.clone();
        let mut files_to_publish = HashSet::new();
        let mut file_progress_index = 0;
        if need_to_recompute_main_variant {
            // Recompute variant.
            let main_variant = main_variant_option.clone().unwrap();
            let main_variant_url = main_variant.url.clone();
            let main_variant_shading_language = main_variant.shading_language;
            let language_data = language_data
                .get_mut(&main_variant_shading_language)
                .unwrap();
            unique_remaining_files.remove(&main_variant_url);
            files_to_publish.insert(main_variant_url.clone());
            files_updating.insert(main_variant_url.clone());
            let file_name = get_file_name(&main_variant_url);
            file_progress_index += 1;
            progress_callback(
                &file_name,
                file_progress_index,
                unique_remaining_files.len() as u32 + 1,
            );
            let removed_files = self.cache_file_data(
                &main_variant_url,
                language_data.validator.as_mut(),
                &mut language_data.shader_module_parser,
                &mut language_data.symbol_provider,
                &config,
                &Some(main_variant.clone()),
                dirty_dependencies.clone(),
            )?;
            files_to_clear.extend(removed_files);
            // Remove request for included files as they are already updated by variant.
            let included_files = self.get_all_included_files(&main_variant_url);
            unique_remaining_files.retain(|f| {
                if included_files.contains(f) {
                    let includer_files = self.get_includer_main_files(f);
                    includer_files_to_update.extend(includer_files);
                    false
                } else {
                    true
                }
            });
            files_updating.extend(included_files);
            // If file is dirty, request update for its includer files.
            if dirty_files.contains(&main_variant_url) {
                let includer_files = self.get_includer_main_files(&main_variant_url);
                for includer_file in includer_files {
                    if !files_updating.contains(&includer_file) {
                        info!(
                            "File {} is being updated as it #includes {}",
                            includer_file, main_variant_url
                        );
                        files_updating.insert(includer_file.clone());
                        includer_files_to_update.insert(includer_file);
                    }
                }
            }
        }
        // Find automatic variant to be computed first.
        let mut auto_variant_computed = 0;
        if config.get_automatic_variant_discovery() {
            let automatic_remaining_files = unique_remaining_files.clone();
            for remaining_file in &automatic_remaining_files {
                // Some check we assume to avoid conflict with manual variant.
                debug_assert!(
                    main_variant_option.iter().find(|v| v.url == *remaining_file).is_none(),
                    "Should never be reached as it should be removed from unique_remaining_files array"
                );
                debug_assert!(
                    main_variant_option.iter().find(|v| self.get_all_included_files(&v.url).contains(remaining_file)).is_none(),
                    "Should never be reached as it should be removed from unique_remaining_files array as deps"
                );
                if let Some(auto_variant) =
                    self.find_variant_from_includer_main_files(remaining_file)
                {
                    info!(
                        "Found file {} as automatic variant for file {}",
                        auto_variant.url, remaining_file
                    );
                    let auto_variant_url = auto_variant.url.clone();
                    let auto_variant_shading_language = auto_variant.shading_language;
                    let language_data = language_data
                        .get_mut(&auto_variant_shading_language)
                        .unwrap();
                    unique_remaining_files.remove(&auto_variant_url);
                    files_to_publish.insert(auto_variant_url.clone());
                    files_updating.insert(auto_variant_url.clone());
                    let file_name = get_file_name(&auto_variant_url);
                    file_progress_index += 1;
                    auto_variant_computed += 1;
                    progress_callback(
                        &file_name,
                        file_progress_index,
                        unique_remaining_files.len() as u32 + 1,
                    );
                    let removed_files = self.cache_file_data(
                        &auto_variant_url,
                        language_data.validator.as_mut(),
                        &mut language_data.shader_module_parser,
                        &mut language_data.symbol_provider,
                        &config,
                        &Some(auto_variant.clone()),
                        dirty_dependencies.clone(),
                    )?;
                    files_to_clear.extend(removed_files);
                    // Remove request for included files as they are already updated by variant.
                    let included_files = self.get_all_included_files(&auto_variant_url);
                    unique_remaining_files.retain(|f| {
                        if included_files.contains(f) {
                            let includer_files = self.get_includer_main_files(f);
                            includer_files_to_update.extend(includer_files);
                            false
                        } else {
                            true
                        }
                    });
                    files_updating.extend(included_files);
                    // If file is dirty, request update for its includer files.
                    if dirty_files.contains(&auto_variant_url) {
                        let includer_files = self.get_includer_main_files(&auto_variant_url);
                        for includer_file in includer_files {
                            if !files_updating.contains(&includer_file) {
                                info!(
                                    "File {} is being updated as it #includes {}",
                                    includer_file, auto_variant_url
                                );
                                files_updating.insert(includer_file.clone());
                                includer_files_to_update.insert(includer_file);
                            }
                        }
                    }
                }
            }
        }
        // We compute all files that were not handled by variant update.
        for remaining_file in &unique_remaining_files {
            let file_name = get_file_name(&remaining_file);
            file_progress_index += 1;
            progress_callback(
                &file_name,
                file_progress_index,
                (unique_remaining_files.len() + includer_files_to_update.len()) as u32
                    + need_to_recompute_main_variant as u32,
            );
            // Check file is still watched and a main file
            let shading_language = match self.files.get(&remaining_file) {
                Some(file) => {
                    if !file.is_main_file() {
                        files_to_clear.insert(remaining_file.clone());
                        continue;
                    } else {
                        files_to_publish.insert(remaining_file.clone());
                        file.shading_language
                    }
                }
                None => {
                    files_to_clear.insert(remaining_file.clone());
                    continue;
                }
            };
            let language_data = language_data.get_mut(&shading_language).unwrap();
            let removed_files = self.cache_file_data(
                &remaining_file,
                language_data.validator.as_mut(),
                &mut language_data.shader_module_parser,
                &mut language_data.symbol_provider,
                &config,
                &self.variant.clone().filter(|v| v.url == *remaining_file),
                dirty_dependencies.clone(),
            )?;
            files_to_clear.extend(removed_files);
            // If file is dirty, request update for its includer files.
            if dirty_files.contains(&remaining_file) {
                let includer_files = self.get_includer_main_files(&remaining_file);
                for includer_file in includer_files {
                    if !files_updating.contains(&includer_file) {
                        info!(
                            "File {} is being updated as it #includes {}",
                            includer_file, remaining_file
                        );
                        files_updating.insert(includer_file.clone());
                        includer_files_to_update.insert(includer_file);
                    }
                }
            }
        }
        // Update includer files now that we did everything else.
        // These are the main files that #include the files we just updated.
        for includer_file in &includer_files_to_update {
            let shading_language = self.files.get(&includer_file).unwrap().shading_language;
            let language_data = language_data.get_mut(&shading_language).unwrap();
            let file_name = get_file_name(&includer_file);
            file_progress_index += 1;
            progress_callback(
                &file_name,
                file_progress_index,
                (unique_remaining_files.len() + includer_files_to_update.len()) as u32
                    + need_to_recompute_main_variant as u32,
            );
            let removed_files = self.cache_file_data(
                &includer_file,
                language_data.validator.as_mut(),
                &mut language_data.shader_module_parser,
                &mut language_data.symbol_provider,
                &config,
                &self.variant.clone().filter(|v| v.url == *includer_file),
                dirty_dependencies.clone(),
            )?;
            files_to_clear.extend(removed_files);
        }
        debug_assert!(
            (unique_remaining_files.len()
                + includer_files_to_update.len()
                + need_to_recompute_main_variant as usize
                + auto_variant_computed)
                == file_progress_index as usize,
            "Invalid count for progress report ({} unique files, {} includer, {} variant, {} auto variant, expecting a total of {})",
            unique_remaining_files.len(),
            includer_files_to_update.len(),
            need_to_recompute_main_variant as u32,
            auto_variant_computed,
            file_progress_index
        );
        // TODO: Diagnostics return here are unique but might be in incorrect order...
        files_to_publish.extend(includer_files_to_update);
        Ok((files_to_clear, files_to_publish))
    }
    pub fn watch_variant_file(
        &mut self,
        uri: &Url,
        lang: ShadingLanguage,
        shader_module_parser: &mut ShaderModuleParser,
    ) -> Result<(), ShaderError> {
        assert!(*uri == clean_url(&uri));
        let file_path = uri.to_file_path().unwrap();
        // Check if watched file already watched as deps or variant.
        match self.files.get_mut(&uri) {
            Some(cached_file) => {
                if !cached_file.is_variant_file {
                    cached_file.is_variant_file = true;
                    info!(
                        "Starting watching {:#?} file as variant file at {}. {} files in cache.",
                        lang,
                        file_path.display(),
                        self.files.len(),
                    );
                }
            }
            None => {
                let text = read_string_lossy(&file_path).unwrap();
                let shader_module = Rc::new(RefCell::new(
                    shader_module_parser.create_module(&file_path, &text)?,
                ));
                let cached_file = ServerFileCache {
                    shading_language: lang,
                    shader_module: shader_module,
                    data: None,
                    is_main_file: false,
                    is_variant_file: true,
                };
                let none = self.files.insert(uri.clone(), cached_file);
                assert!(none.is_none());
                info!(
                    "Starting watching {:#?} variant file at {}. {} files in cache.",
                    lang,
                    file_path.display(),
                    self.files.len(),
                );
            }
        };
        Ok(())
    }
    pub fn watch_main_file(
        &mut self,
        uri: &Url,
        lang: ShadingLanguage,
        text: &str,
        shader_module_parser: &mut ShaderModuleParser,
    ) -> Result<(), ShaderError> {
        assert!(*uri == clean_url(&uri));
        let file_path = uri.to_file_path().unwrap();

        // Check if watched file already watched as deps
        match self.files.get_mut(&uri) {
            Some(cached_file) => {
                debug_assert!(
                    !cached_file.is_main_file,
                    "File {} already watched as main.",
                    uri
                );
                cached_file.is_main_file = true;
                // When the cached module was loaded as an include dependency,
                // its content was read from disk via `read_string_lossy` /
                // `std::fs::read_to_string`, which preserves the on-disk bytes
                // verbatim (e.g. CRLF on Windows). The client-provided didOpen
                // text may have been normalized by the editor (e.g. CRLF -> LF),
                // so the two buffers can differ in length. We cannot simply
                // replace `.content` and keep the existing tree-sitter tree:
                // the stored tree's byte offsets would still refer to the
                // longer pre-normalization buffer, and subsequent symbol
                // queries (e.g. `symbol_parser::get_name`) would slice the
                // new, shorter content out of bounds and panic with
                // "byte index N is out of bounds of ...".
                //
                // When the content actually differs, re-parse from scratch so
                // the tree and content stay in sync. When they match byte-for-
                // byte, skip the re-parse as an optimization.
                {
                    let mut module = RefCell::borrow_mut(&cached_file.shader_module);
                    if module.content != *text {
                        *module = shader_module_parser.create_module(&file_path, text)?;
                    }
                }
                info!(
                    "Starting watching {:#?} dependency file as main file at {}. {} files in cache.",
                    lang,
                    file_path.display(),
                    self.files.len(),
                );
            }
            None => {
                let shader_module = Rc::new(RefCell::new(
                    shader_module_parser.create_module(&file_path, &text)?,
                ));
                debug_assert!(self.variant.as_ref().map(|v| v.url != *uri).unwrap_or(true));
                let cached_file = ServerFileCache {
                    shading_language: lang,
                    shader_module: shader_module,
                    data: None,
                    is_main_file: true,
                    is_variant_file: false, // Cannot be a variant if its not watched.
                };
                let none = self.files.insert(uri.clone(), cached_file);
                debug_assert!(none.is_none());
                info!(
                    "Starting watching {:#?} main file at {}. {} files in cache.",
                    lang,
                    file_path.display(),
                    self.files.len(),
                );
            }
        };
        Ok(())
    }
    pub fn watch_dependency(
        &mut self,
        uri: &Url,
        lang: ShadingLanguage,
        shader_module_parser: &mut ShaderModuleParser,
    ) -> Result<&ServerFileCache, ShaderError> {
        assert!(*uri == clean_url(&uri));
        let file_path = uri.to_file_path().unwrap();
        // If file is not watched, add it as deps.
        match self.files.get(&uri) {
            Some(file) => {
                if file.is_main_file() {
                    debug!(
                        "Starting watching {:#?} main file as deps at {}. {} files in cache.",
                        lang,
                        file_path.display(),
                        self.files.len(),
                    );
                } else if file.is_variant_file {
                    debug!(
                        "Already watched {:#?} deps file as variant at {}. {} files in cache.",
                        lang,
                        file_path.display(),
                        self.files.len(),
                    );
                } else {
                    debug!(
                        "Already watched {:#?} deps file at {}. {} files in cache.",
                        lang,
                        file_path.display(),
                        self.files.len(),
                    );
                }
            }
            None => {
                let text = read_string_lossy(&file_path).unwrap();
                let shader_module = Rc::new(RefCell::new(
                    shader_module_parser.create_module(&file_path, &text)?,
                ));
                let cached_file = ServerFileCache {
                    shading_language: lang,
                    shader_module: shader_module,
                    data: None,
                    is_main_file: false,
                    is_variant_file: self
                        .variant
                        .as_ref()
                        .map(|v| v.url == *uri)
                        .unwrap_or(false),
                };
                let none = self.files.insert(uri.clone(), cached_file);
                assert!(none.is_none());
                info!(
                    "Starting watching {:#?} dependency file at {}. {} files in cache.",
                    lang,
                    file_path.display(),
                    self.files.len(),
                );
            }
        }
        Ok(self.files.get(&uri).unwrap())
    }
    pub fn update_file(
        &mut self,
        uri: &Url,
        shader_module_parser: &mut ShaderModuleParser,
        range: Option<lsp_types::Range>,
        partial_content: Option<&String>,
    ) -> Result<(), ShaderError> {
        let cached_file = self.get_file(uri).unwrap();
        profile_scope!(
            "Updating file {} (Content {:?} at {:?})",
            uri,
            partial_content,
            range
        );
        // Update abstract syntax tree
        if let (Some(range), Some(partial_content)) = (range, partial_content) {
            let shader_range = lsp_range_to_shader_range(&range);
            shader_module_parser.update_module_partial(
                &mut RefCell::borrow_mut(&cached_file.shader_module),
                &shader_range,
                &partial_content,
            )?;
        } else if let Some(whole_content) = partial_content {
            shader_module_parser.update_module(
                &mut RefCell::borrow_mut(&cached_file.shader_module),
                &whole_content,
            )?;
        } else {
            // No update on content to perform.
            assert!(false, "Calling update_file unnecessarily");
        }
        Ok(())
    }
    pub fn get_file(&self, uri: &Url) -> Option<&ServerFileCache> {
        assert!(*uri == clean_url(&uri));
        match self.files.get(uri) {
            Some(cached_file) => Some(&cached_file),
            None => None,
        }
    }
    fn is_used_as_dependency(&self, uri: &Url) -> Option<(&Url, &ServerFileCache)> {
        let file_path = uri.to_file_path().unwrap();
        self.files.iter().find(|(file_url, file_cache)| {
            if *file_url != uri {
                file_cache.has_data()
                    && file_cache
                        .get_data()
                        .symbol_cache
                        .has_dependency(&file_path)
            } else {
                false
            }
        })
    }
    fn is_dangling_file(&self, uri: &Url) -> bool {
        match self.files.get(uri) {
            Some(cached_file) => {
                !cached_file.is_cachable_file() && self.is_used_as_dependency(uri).is_none()
            }
            None => {
                debug_assert!(
                    false,
                    "Checking if file {} is dangling but its not watched.",
                    uri
                );
                false
            }
        }
    }
    // Dependency removal are handled by remove_variant & remove_file.
    pub fn remove_variant_file(&mut self, uri: &Url) -> Result<Vec<Url>, ShaderError> {
        let used_as_deps = self.is_used_as_dependency(uri).is_some();
        let mut dangling_files = self.get_all_included_files(uri);
        match self.files.get_mut(&uri) {
            Some(cached_file) => {
                if used_as_deps || cached_file.is_main_file() {
                    let shading_language = cached_file.shading_language;
                    // Used as deps. Reset cache only if not main.
                    if !cached_file.is_main_file {
                        cached_file.data = None;
                    }
                    debug_assert!(cached_file.is_variant_file);
                    cached_file.is_variant_file = false;
                    info!(
                        "Converted {:#?} variant file to {} at {}. {} files in cache.",
                        shading_language,
                        if cached_file.is_main_file {
                            "main file"
                        } else {
                            "deps file"
                        },
                        uri,
                        self.files.len()
                    );
                    Ok(vec![])
                } else {
                    match self.files.remove(uri) {
                        Some(mut cached_file) => {
                            let shading_language = cached_file.shading_language;
                            assert!(
                                cached_file.data.is_some(),
                                "Removing variant file without data"
                            );
                            // Get dangling dependencies that need to be removed.
                            dangling_files.retain(|f| {
                                if uri != f {
                                    self.is_dangling_file(f)
                                } else {
                                    false // Avoid removing main file twice.
                                }
                            });
                            // Remove main file before deps & drop cache for ref.
                            let data = cached_file.data.unwrap();
                            drop(data);
                            cached_file.is_variant_file = false; // Just to be sure.
                            info!(
                                "Removed {:#?} main file at {}. {} files in cache.",
                                cached_file.shading_language,
                                uri,
                                self.files.len()
                            );
                            // Remove these deps from cache.
                            for dangling_file in &dangling_files {
                                self.files.remove(dangling_file);
                                info!(
                                    "Removed {:#?} dangling deps {}. {} files in cache.",
                                    shading_language,
                                    dangling_file,
                                    self.files.len()
                                );
                            }
                            Ok(
                                vec![vec![uri.clone()], dangling_files.into_iter().collect()]
                                    .concat(),
                            )
                        }
                        None => Err(ShaderError::InternalErr(format!(
                            "Trying to remove variant file {} that is not watched",
                            uri.path()
                        ))),
                    }
                }
            }
            None => Err(ShaderError::InternalErr(format!(
                "Trying to remove variant file {} that is not watched",
                uri.path()
            ))),
        }
    }
    pub fn remove_main_file(&mut self, uri: &Url) -> Result<Vec<Url>, ShaderError> {
        let used_as_deps = self.is_used_as_dependency(uri).is_some();
        let mut dangling_files = if self.files.get(&uri).unwrap().data.is_some() {
            self.get_all_included_files(uri)
        } else {
            HashSet::new()
        };
        match self.files.get_mut(&uri) {
            Some(cached_file) => {
                if used_as_deps || cached_file.is_variant_file {
                    let shading_language = cached_file.shading_language;
                    // Used as deps. Reset cache only if not main.
                    if !cached_file.is_variant_file {
                        cached_file.data = None;
                    }
                    debug_assert!(cached_file.is_main_file);
                    cached_file.is_main_file = false;
                    info!(
                        "Converted {:#?} main file to {} at {}. {} files in cache.",
                        shading_language,
                        if cached_file.is_main_file {
                            "variant file"
                        } else {
                            "deps file"
                        },
                        uri,
                        self.files.len()
                    );
                    Ok(vec![])
                } else {
                    match self.files.remove(uri) {
                        Some(mut cached_file) => {
                            let shading_language = cached_file.shading_language;
                            // Get dangling dependencies that need to be removed.
                            dangling_files.retain(|f| {
                                if uri != f {
                                    self.is_dangling_file(f)
                                } else {
                                    false // Avoid removing main file twice.
                                }
                            });
                            // Remove main file before deps & drop cache for ref.
                            if let Some(data) = cached_file.data {
                                drop(data);
                            }
                            cached_file.is_main_file = false; // Just to be sure.
                            info!(
                                "Removed {:#?} main file at {}. {} files in cache.",
                                cached_file.shading_language,
                                uri,
                                self.files.len()
                            );
                            // Remove these deps from cache.
                            for dangling_file in &dangling_files {
                                self.files.remove(dangling_file);
                                info!(
                                    "Removed {:#?} dangling deps {}. {} files in cache.",
                                    shading_language,
                                    dangling_file,
                                    self.files.len()
                                );
                            }
                            Ok(
                                vec![vec![uri.clone()], dangling_files.into_iter().collect()]
                                    .concat(),
                            )
                        }
                        None => Err(ShaderError::InternalErr(format!(
                            "Trying to remove main file {} that is not watched",
                            uri.path()
                        ))),
                    }
                }
            }
            None => Err(ShaderError::InternalErr(format!(
                "Trying to remove main file {} that is not watched",
                uri.path()
            ))),
        }
    }
    pub fn get_all_symbols<'a>(&'a self, uri: &Url) -> ShaderSymbolListRef<'a> {
        let cached_file = self.files.get(uri).unwrap();
        assert!(cached_file.data.is_some(), "File {} do not have cache", uri);
        let data = &cached_file.get_data();
        // Add main file symbols
        let mut symbol_cache = data.symbol_cache.get_all_symbols();
        // Add config symbols
        for symbol in data.symbol_cache.get_context().get_defines().iter() {
            symbol_cache.macros.push(&symbol);
        }
        // Add intrinsics symbols
        symbol_cache.append(data.intrinsics.clone());
        symbol_cache
    }
}