forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.rs
893 lines (808 loc) · 25.9 KB
/
config.rs
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use super::logging::lsp_log;
use crate::util::path::specifier_to_file_path;
use deno_core::error::AnyError;
use deno_core::serde::Deserialize;
use deno_core::serde::Serialize;
use deno_core::serde_json;
use deno_core::serde_json::Value;
use deno_core::ModuleSpecifier;
use lsp::Url;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;
use tower_lsp::lsp_types as lsp;
pub const SETTINGS_SECTION: &str = "deno";
#[derive(Debug, Clone, Default)]
pub struct ClientCapabilities {
pub code_action_disabled_support: bool,
pub line_folding_only: bool,
pub snippet_support: bool,
pub status_notification: bool,
/// The client provides the `experimental.testingApi` capability, which is
/// built around VSCode's testing API. It indicates that the server should
/// send notifications about tests discovered in modules.
pub testing_api: bool,
pub workspace_configuration: bool,
pub workspace_did_change_watched_files: bool,
}
fn is_true() -> bool {
true
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CodeLensSettings {
/// Flag for providing implementation code lenses.
#[serde(default)]
pub implementations: bool,
/// Flag for providing reference code lenses.
#[serde(default)]
pub references: bool,
/// Flag for providing reference code lens on all functions. For this to have
/// an impact, the `references` flag needs to be `true`.
#[serde(default)]
pub references_all_functions: bool,
/// Flag for providing test code lens on `Deno.test` statements. There is
/// also the `test_args` setting, but this is not used by the server.
#[serde(default = "is_true")]
pub test: bool,
}
impl Default for CodeLensSettings {
fn default() -> Self {
Self {
implementations: false,
references: false,
references_all_functions: false,
test: true,
}
}
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CodeLensSpecifierSettings {
/// Flag for providing test code lens on `Deno.test` statements. There is
/// also the `test_args` setting, but this is not used by the server.
#[serde(default = "is_true")]
pub test: bool,
}
impl Default for CodeLensSpecifierSettings {
fn default() -> Self {
Self { test: true }
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CompletionSettings {
#[serde(default)]
pub complete_function_calls: bool,
#[serde(default = "is_true")]
pub names: bool,
#[serde(default = "is_true")]
pub paths: bool,
#[serde(default = "is_true")]
pub auto_imports: bool,
#[serde(default)]
pub imports: ImportCompletionSettings,
}
impl Default for CompletionSettings {
fn default() -> Self {
Self {
complete_function_calls: false,
names: true,
paths: true,
auto_imports: true,
imports: ImportCompletionSettings::default(),
}
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsSettings {
#[serde(default)]
pub parameter_names: InlayHintsParamNamesOptions,
#[serde(default)]
pub parameter_types: InlayHintsParamTypesOptions,
#[serde(default)]
pub variable_types: InlayHintsVarTypesOptions,
#[serde(default)]
pub property_declaration_types: InlayHintsPropDeclTypesOptions,
#[serde(default)]
pub function_like_return_types: InlayHintsFuncLikeReturnTypesOptions,
#[serde(default)]
pub enum_member_values: InlayHintsEnumMemberValuesOptions,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsParamNamesOptions {
#[serde(default)]
pub enabled: InlayHintsParamNamesEnabled,
#[serde(default = "is_true")]
pub suppress_when_argument_matches_name: bool,
}
impl Default for InlayHintsParamNamesOptions {
fn default() -> Self {
Self {
enabled: InlayHintsParamNamesEnabled::None,
suppress_when_argument_matches_name: true,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum InlayHintsParamNamesEnabled {
None,
Literals,
All,
}
impl Default for InlayHintsParamNamesEnabled {
fn default() -> Self {
Self::None
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsParamTypesOptions {
#[serde(default)]
pub enabled: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsVarTypesOptions {
#[serde(default)]
pub enabled: bool,
#[serde(default = "is_true")]
pub suppress_when_type_matches_name: bool,
}
impl Default for InlayHintsVarTypesOptions {
fn default() -> Self {
Self {
enabled: false,
suppress_when_type_matches_name: true,
}
}
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsPropDeclTypesOptions {
#[serde(default)]
pub enabled: bool,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsFuncLikeReturnTypesOptions {
#[serde(default)]
pub enabled: bool,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct InlayHintsEnumMemberValuesOptions {
#[serde(default)]
pub enabled: bool,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ImportCompletionSettings {
/// A flag that indicates if non-explicitly set origins should be checked for
/// supporting import suggestions.
#[serde(default = "is_true")]
pub auto_discover: bool,
/// A map of origins which have had explicitly set if import suggestions are
/// enabled.
#[serde(default)]
pub hosts: HashMap<String, bool>,
}
impl Default for ImportCompletionSettings {
fn default() -> Self {
Self {
auto_discover: true,
hosts: HashMap::default(),
}
}
}
/// Deno language server specific settings that can be applied uniquely to a
/// specifier.
#[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct SpecifierSettings {
/// A flag that indicates if Deno is enabled for this specifier or not.
pub enable: bool,
/// A list of paths, using the workspace folder as a base that should be Deno
/// enabled.
#[serde(default)]
pub enable_paths: Vec<String>,
/// Code lens specific settings for the resource.
#[serde(default)]
pub code_lens: CodeLensSpecifierSettings,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct TestingSettings {
/// A vector of arguments which should be used when running the tests for
/// a workspace.
#[serde(default)]
pub args: Vec<String>,
/// Enable or disable the testing API if the client is capable of supporting
/// the testing API.
#[serde(default = "is_true")]
pub enable: bool,
}
impl Default for TestingSettings {
fn default() -> Self {
Self {
args: vec!["--allow-all".to_string(), "--no-check".to_string()],
enable: true,
}
}
}
fn default_to_true() -> bool {
true
}
fn empty_string_none<'de, D: serde::Deserializer<'de>>(
d: D,
) -> Result<Option<String>, D::Error> {
let o: Option<String> = Option::deserialize(d)?;
Ok(o.filter(|s| !s.is_empty()))
}
/// Deno language server specific settings that are applied to a workspace.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceSettings {
/// A flag that indicates if Deno is enabled for the workspace.
#[serde(default)]
pub enable: bool,
/// A list of paths, using the root_uri as a base that should be Deno enabled.
#[serde(default)]
pub enable_paths: Vec<String>,
/// An option that points to a path string of the path to utilise as the
/// cache/DENO_DIR for the language server.
#[serde(default, deserialize_with = "empty_string_none")]
pub cache: Option<String>,
/// Override the default stores used to validate certificates. This overrides
/// the environment variable `DENO_TLS_CA_STORE` if present.
pub certificate_stores: Option<Vec<String>>,
/// An option that points to a path string of the config file to apply to
/// code within the workspace.
#[serde(default, deserialize_with = "empty_string_none")]
pub config: Option<String>,
/// An option that points to a path string of the import map to apply to the
/// code within the workspace.
#[serde(default, deserialize_with = "empty_string_none")]
pub import_map: Option<String>,
/// Code lens specific settings for the workspace.
#[serde(default)]
pub code_lens: CodeLensSettings,
#[serde(default)]
pub inlay_hints: InlayHintsSettings,
/// A flag that indicates if internal debug logging should be made available.
#[serde(default)]
pub internal_debug: bool,
/// A flag that indicates if linting is enabled for the workspace.
#[serde(default = "default_to_true")]
pub lint: bool,
/// A flag that indicates if Dene should validate code against the unstable
/// APIs for the workspace.
#[serde(default)]
pub suggest: CompletionSettings,
/// Testing settings for the workspace.
#[serde(default)]
pub testing: TestingSettings,
/// An option which sets the cert file to use when attempting to fetch remote
/// resources. This overrides `DENO_CERT` if present.
#[serde(default, deserialize_with = "empty_string_none")]
pub tls_certificate: Option<String>,
/// An option, if set, will unsafely ignore certificate errors when fetching
/// remote resources.
#[serde(default)]
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
#[serde(default)]
pub unstable: bool,
}
impl Default for WorkspaceSettings {
fn default() -> Self {
WorkspaceSettings {
enable: false,
enable_paths: vec![],
cache: None,
certificate_stores: None,
config: None,
import_map: None,
code_lens: Default::default(),
inlay_hints: Default::default(),
internal_debug: false,
lint: true,
suggest: Default::default(),
testing: Default::default(),
tls_certificate: None,
unsafely_ignore_certificate_errors: None,
unstable: false,
}
}
}
impl WorkspaceSettings {
/// Determine if any code lenses are enabled at all. This allows short
/// circuiting when there are no code lenses enabled.
pub fn enabled_code_lens(&self) -> bool {
self.code_lens.implementations || self.code_lens.references
}
/// Determine if any inlay hints are enabled. This allows short circuiting
/// when there are no inlay hints enabled.
pub fn enabled_inlay_hints(&self) -> bool {
!matches!(
self.inlay_hints.parameter_names.enabled,
InlayHintsParamNamesEnabled::None
) || self.inlay_hints.parameter_types.enabled
|| self.inlay_hints.variable_types.enabled
|| self.inlay_hints.property_declaration_types.enabled
|| self.inlay_hints.function_like_return_types.enabled
|| self.inlay_hints.enum_member_values.enabled
}
}
#[derive(Debug, Clone, Default)]
pub struct ConfigSnapshot {
pub client_capabilities: ClientCapabilities,
pub enabled_paths: HashMap<Url, Vec<Url>>,
pub settings: Settings,
}
impl ConfigSnapshot {
/// Determine if the provided specifier is enabled or not.
pub fn specifier_enabled(&self, specifier: &ModuleSpecifier) -> bool {
if !self.enabled_paths.is_empty() {
let specifier_str = specifier.as_str();
for (workspace, enabled_paths) in self.enabled_paths.iter() {
if specifier_str.starts_with(workspace.as_str()) {
return enabled_paths
.iter()
.any(|path| specifier_str.starts_with(path.as_str()));
}
}
}
if let Some(settings) = self.settings.specifiers.get(specifier) {
settings.enable
} else {
self.settings.workspace.enable
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Settings {
pub specifiers: BTreeMap<ModuleSpecifier, SpecifierSettings>,
pub workspace: WorkspaceSettings,
}
#[derive(Debug)]
pub struct Config {
pub client_capabilities: ClientCapabilities,
enabled_paths: HashMap<Url, Vec<Url>>,
pub root_uri: Option<ModuleSpecifier>,
settings: Settings,
pub workspace_folders: Option<Vec<(ModuleSpecifier, lsp::WorkspaceFolder)>>,
}
impl Config {
pub fn new() -> Self {
Self {
client_capabilities: ClientCapabilities::default(),
enabled_paths: Default::default(),
/// Root provided by the initialization parameters.
root_uri: None,
settings: Default::default(),
workspace_folders: None,
}
}
pub fn get_workspace_settings(&self) -> WorkspaceSettings {
self.settings.workspace.clone()
}
/// Set the workspace settings directly, which occurs during initialization
/// and when the client does not support workspace configuration requests
pub fn set_workspace_settings(
&mut self,
value: Value,
) -> Result<(), AnyError> {
let workspace_settings = serde_json::from_value(value)?;
self.settings.workspace = workspace_settings;
Ok(())
}
pub fn snapshot(&self) -> Arc<ConfigSnapshot> {
Arc::new(ConfigSnapshot {
client_capabilities: self.client_capabilities.clone(),
enabled_paths: self.enabled_paths.clone(),
settings: self.settings.clone(),
})
}
pub fn has_specifier_settings(&self, specifier: &ModuleSpecifier) -> bool {
self.settings.specifiers.contains_key(specifier)
}
pub fn specifier_enabled(&self, specifier: &ModuleSpecifier) -> bool {
if !self.enabled_paths.is_empty() {
let specifier_str = specifier.as_str();
for (workspace, enabled_paths) in self.enabled_paths.iter() {
if specifier_str.starts_with(workspace.as_str()) {
return enabled_paths
.iter()
.any(|path| specifier_str.starts_with(path.as_str()));
}
}
}
self
.settings
.specifiers
.get(specifier)
.map(|settings| settings.enable)
.unwrap_or_else(|| self.settings.workspace.enable)
}
/// Gets the root directories or file paths based on the workspace config.
pub fn enabled_root_urls(&self) -> Vec<Url> {
let mut urls: Vec<Url> = Vec::new();
if !self.settings.workspace.enable && self.enabled_paths.is_empty() {
// do not return any urls when disabled
return urls;
}
for (workspace, enabled_paths) in &self.enabled_paths {
if !enabled_paths.is_empty() {
urls.extend(enabled_paths.iter().cloned());
} else {
urls.push(workspace.clone());
}
}
if urls.is_empty() {
if let Some(root_dir) = &self.root_uri {
urls.push(root_dir.clone())
}
}
sort_and_remove_non_leaf_urls(&mut urls);
urls
}
pub fn specifier_code_lens_test(&self, specifier: &ModuleSpecifier) -> bool {
let value = self
.settings
.specifiers
.get(specifier)
.map(|settings| settings.code_lens.test)
.unwrap_or_else(|| self.settings.workspace.code_lens.test);
value
}
pub fn update_capabilities(
&mut self,
capabilities: &lsp::ClientCapabilities,
) {
if let Some(experimental) = &capabilities.experimental {
self.client_capabilities.status_notification = experimental
.get("statusNotification")
.and_then(|it| it.as_bool())
== Some(true);
self.client_capabilities.testing_api =
experimental.get("testingApi").and_then(|it| it.as_bool())
== Some(true);
}
if let Some(workspace) = &capabilities.workspace {
self.client_capabilities.workspace_configuration =
workspace.configuration.unwrap_or(false);
self.client_capabilities.workspace_did_change_watched_files = workspace
.did_change_watched_files
.and_then(|it| it.dynamic_registration)
.unwrap_or(false);
}
if let Some(text_document) = &capabilities.text_document {
self.client_capabilities.line_folding_only = text_document
.folding_range
.as_ref()
.and_then(|it| it.line_folding_only)
.unwrap_or(false);
self.client_capabilities.code_action_disabled_support = text_document
.code_action
.as_ref()
.and_then(|it| it.disabled_support)
.unwrap_or(false);
self.client_capabilities.snippet_support =
if let Some(completion) = &text_document.completion {
completion
.completion_item
.as_ref()
.and_then(|it| it.snippet_support)
.unwrap_or(false)
} else {
false
};
}
}
/// Given the configured workspaces or root URI and the their settings,
/// update and resolve any paths that should be enabled
pub fn update_enabled_paths(&mut self) -> bool {
if let Some(workspace_folders) = self.workspace_folders.clone() {
let mut touched = false;
for (workspace, _) in workspace_folders {
if let Some(settings) = self.settings.specifiers.get(&workspace) {
if self.update_enabled_paths_entry(
workspace,
settings.enable_paths.clone(),
) {
touched = true;
}
}
}
touched
} else if let Some(root_uri) = self.root_uri.clone() {
self.update_enabled_paths_entry(
root_uri,
self.settings.workspace.enable_paths.clone(),
)
} else {
false
}
}
/// Update a specific entry in the enabled paths for a given workspace.
fn update_enabled_paths_entry(
&mut self,
workspace: ModuleSpecifier,
enabled_paths: Vec<String>,
) -> bool {
let mut touched = false;
if !enabled_paths.is_empty() {
if let Ok(workspace_path) = specifier_to_file_path(&workspace) {
let mut paths = Vec::new();
for path in &enabled_paths {
let fs_path = workspace_path.join(path);
match ModuleSpecifier::from_file_path(fs_path) {
Ok(path_uri) => {
paths.push(path_uri);
}
Err(_) => {
lsp_log!("Unable to resolve a file path for `deno.enablePath` from \"{}\" for workspace \"{}\".", path, workspace);
}
}
}
if !paths.is_empty() {
touched = true;
self.enabled_paths.insert(workspace.clone(), paths);
}
}
} else {
touched = true;
self.enabled_paths.remove(&workspace);
}
touched
}
pub fn get_specifiers(&self) -> Vec<ModuleSpecifier> {
self.settings.specifiers.keys().cloned().collect()
}
pub fn set_specifier_settings(
&mut self,
specifier: ModuleSpecifier,
settings: SpecifierSettings,
) -> bool {
if let Some(existing) = self.settings.specifiers.get(&specifier) {
if *existing == settings {
return false;
}
}
self.settings.specifiers.insert(specifier, settings);
true
}
}
/// Removes any URLs that are a descendant of another URL in the collection.
fn sort_and_remove_non_leaf_urls(dirs: &mut Vec<Url>) {
if dirs.is_empty() {
return;
}
dirs.sort();
for i in (0..dirs.len() - 1).rev() {
let prev = &dirs[i + 1];
if prev.as_str().starts_with(dirs[i].as_str()) {
dirs.remove(i + 1);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use deno_core::resolve_url;
use deno_core::serde_json::json;
#[test]
fn test_config_specifier_enabled() {
let mut config = Config::new();
let specifier = resolve_url("file:https:///a.ts").unwrap();
assert!(!config.specifier_enabled(&specifier));
config
.set_workspace_settings(json!({
"enable": true
}))
.expect("could not update");
assert!(config.specifier_enabled(&specifier));
}
#[test]
fn test_config_snapshot_specifier_enabled() {
let mut config = Config::new();
let specifier = resolve_url("file:https:///a.ts").unwrap();
assert!(!config.specifier_enabled(&specifier));
config
.set_workspace_settings(json!({
"enable": true
}))
.expect("could not update");
let config_snapshot = config.snapshot();
assert!(config_snapshot.specifier_enabled(&specifier));
}
#[test]
fn test_config_specifier_enabled_path() {
let mut config = Config::new();
let specifier_a = resolve_url("file:https:///project/worker/a.ts").unwrap();
let specifier_b = resolve_url("file:https:///project/other/b.ts").unwrap();
assert!(!config.specifier_enabled(&specifier_a));
assert!(!config.specifier_enabled(&specifier_b));
let mut enabled_paths = HashMap::new();
enabled_paths.insert(
Url::parse("file:https:///project/").unwrap(),
vec![Url::parse("file:https:///project/worker/").unwrap()],
);
config.enabled_paths = enabled_paths;
assert!(config.specifier_enabled(&specifier_a));
assert!(!config.specifier_enabled(&specifier_b));
let config_snapshot = config.snapshot();
assert!(config_snapshot.specifier_enabled(&specifier_a));
assert!(!config_snapshot.specifier_enabled(&specifier_b));
}
#[test]
fn test_set_workspace_settings_defaults() {
let mut config = Config::new();
config
.set_workspace_settings(json!({}))
.expect("could not update");
assert_eq!(
config.get_workspace_settings(),
WorkspaceSettings {
enable: false,
enable_paths: Vec::new(),
cache: None,
certificate_stores: None,
config: None,
import_map: None,
code_lens: CodeLensSettings {
implementations: false,
references: false,
references_all_functions: false,
test: true,
},
inlay_hints: InlayHintsSettings {
parameter_names: InlayHintsParamNamesOptions {
enabled: InlayHintsParamNamesEnabled::None,
suppress_when_argument_matches_name: true
},
parameter_types: InlayHintsParamTypesOptions { enabled: false },
variable_types: InlayHintsVarTypesOptions {
enabled: false,
suppress_when_type_matches_name: true
},
property_declaration_types: InlayHintsPropDeclTypesOptions {
enabled: false
},
function_like_return_types: InlayHintsFuncLikeReturnTypesOptions {
enabled: false
},
enum_member_values: InlayHintsEnumMemberValuesOptions {
enabled: false
},
},
internal_debug: false,
lint: true,
suggest: CompletionSettings {
complete_function_calls: false,
names: true,
paths: true,
auto_imports: true,
imports: ImportCompletionSettings {
auto_discover: true,
hosts: HashMap::new(),
}
},
testing: TestingSettings {
args: vec!["--allow-all".to_string(), "--no-check".to_string()],
enable: true
},
tls_certificate: None,
unsafely_ignore_certificate_errors: None,
unstable: false,
}
);
}
#[test]
fn test_empty_cache() {
let mut config = Config::new();
config
.set_workspace_settings(json!({ "cache": "" }))
.expect("could not update");
assert_eq!(
config.get_workspace_settings(),
WorkspaceSettings::default()
);
}
#[test]
fn test_empty_import_map() {
let mut config = Config::new();
config
.set_workspace_settings(json!({ "import_map": "" }))
.expect("could not update");
assert_eq!(
config.get_workspace_settings(),
WorkspaceSettings::default()
);
}
#[test]
fn test_empty_tls_certificate() {
let mut config = Config::new();
config
.set_workspace_settings(json!({ "tls_certificate": "" }))
.expect("could not update");
assert_eq!(
config.get_workspace_settings(),
WorkspaceSettings::default()
);
}
#[test]
fn test_empty_config() {
let mut config = Config::new();
config
.set_workspace_settings(json!({ "config": "" }))
.expect("could not update");
assert_eq!(
config.get_workspace_settings(),
WorkspaceSettings::default()
);
}
#[test]
fn test_sort_and_remove_non_leaf_urls() {
fn run_test(dirs: Vec<&str>, expected_output: Vec<&str>) {
let mut dirs = dirs
.into_iter()
.map(|dir| Url::parse(dir).unwrap())
.collect();
sort_and_remove_non_leaf_urls(&mut dirs);
let dirs: Vec<_> = dirs.iter().map(|dir| dir.as_str()).collect();
assert_eq!(dirs, expected_output);
}
run_test(
vec![
"file:https:///test/asdf/test/asdf/",
"file:https:///test/asdf/",
"file:https:///test/asdf/",
"file:https:///testing/456/893/",
"file:https:///testing/456/893/test/",
],
vec!["file:https:///test/asdf/", "file:https:///testing/456/893/"],
);
run_test(vec![], vec![]);
}
#[test]
fn config_enabled_root_urls() {
let mut config = Config::new();
let root_dir = Url::parse("file:https:///example/").unwrap();
config.root_uri = Some(root_dir.clone());
config.settings.workspace.enable = false;
config.settings.workspace.enable_paths = Vec::new();
assert_eq!(config.enabled_root_urls(), vec![]);
config.settings.workspace.enable = true;
assert_eq!(config.enabled_root_urls(), vec![root_dir]);
config.settings.workspace.enable = false;
let root_dir1 = Url::parse("file:https:///root1/").unwrap();
let root_dir2 = Url::parse("file:https:///root2/").unwrap();
let root_dir3 = Url::parse("file:https:///root3/").unwrap();
config.enabled_paths = HashMap::from([
(
root_dir1.clone(),
vec![
root_dir1.join("sub_dir").unwrap(),
root_dir1.join("sub_dir/other").unwrap(),
root_dir1.join("test.ts").unwrap(),
],
),
(root_dir2.clone(), vec![root_dir2.join("other.ts").unwrap()]),
(root_dir3.clone(), vec![]),
]);
assert_eq!(
config.enabled_root_urls(),
vec![
root_dir1.join("sub_dir").unwrap(),
root_dir1.join("test.ts").unwrap(),
root_dir2.join("other.ts").unwrap(),
root_dir3
]
);
}
}