wdl-analysis 0.19.1

Analysis of Workflow Description Language (WDL) documents.
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
//! Implementation of the analyzer.

use std::ffi::OsStr;
use std::fmt;
use std::future::Future;
use std::mem::ManuallyDrop;
use std::ops::Range;
use std::path::Path;
use std::path::absolute;
use std::sync::Arc;
use std::thread::JoinHandle;

use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use ignore::WalkBuilder;
use indexmap::IndexSet;
use line_index::LineCol;
use line_index::LineIndex;
use line_index::WideEncoding;
use line_index::WideLineCol;
use lsp_types::CompletionResponse;
use lsp_types::DocumentSymbolResponse;
use lsp_types::GotoDefinitionResponse;
use lsp_types::Hover;
use lsp_types::InlayHint;
use lsp_types::Location;
use lsp_types::SemanticTokensResult;
use lsp_types::SignatureHelp;
use lsp_types::SymbolInformation;
use lsp_types::WorkspaceEdit;
use path_clean::PathClean;
use tokio::runtime::Handle;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
use url::Url;

use crate::config::Config;
use crate::document::Document;
use crate::graph::DocumentGraphNode;
use crate::graph::ParseState;
use crate::queue::AddRequest;
use crate::queue::AnalysisQueue;
use crate::queue::AnalyzeRequest;
use crate::queue::CompletionRequest;
use crate::queue::DocumentSymbolRequest;
use crate::queue::FindAllReferencesRequest;
use crate::queue::FormatRequest;
use crate::queue::GotoDefinitionRequest;
use crate::queue::HoverRequest;
use crate::queue::InlayHintsRequest;
use crate::queue::NotifyChangeRequest;
use crate::queue::NotifyIncrementalChangeRequest;
use crate::queue::RemoveRequest;
use crate::queue::RenameRequest;
use crate::queue::Request;
use crate::queue::SemanticTokenRequest;
use crate::queue::SignatureHelpRequest;
use crate::queue::WorkspaceSymbolRequest;
use crate::rayon::RayonHandle;

/// Represents the kind of analysis progress being reported.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProgressKind {
    /// The progress is for parsing documents.
    Parsing,
    /// The progress is for analyzing documents.
    Analyzing,
}

impl fmt::Display for ProgressKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parsing => write!(f, "parsing"),
            Self::Analyzing => write!(f, "analyzing"),
        }
    }
}

/// Converts a local file path to a file schemed URI.
pub fn path_to_uri(path: impl AsRef<Path>) -> Option<Url> {
    Url::from_file_path(absolute(path).ok()?.clean()).ok()
}

/// Represents the result of an analysis.
///
/// Analysis results are cheap to clone.
#[derive(Debug, Clone)]
pub struct AnalysisResult {
    /// The error that occurred when attempting to parse the file (e.g. the file
    /// could not be opened).
    error: Option<Arc<Error>>,
    /// The monotonic version of the document that was parsed.
    ///
    /// This value comes from incremental changes to the file.
    ///
    /// If `None`, the parsed version had no incremental changes.
    version: Option<i32>,
    /// The lines indexed for the parsed file.
    lines: Option<Arc<LineIndex>>,
    /// The analyzed document.
    document: Document,
}

impl AnalysisResult {
    /// Constructs a new analysis result for the given graph node.
    pub(crate) fn new(node: &DocumentGraphNode) -> Self {
        if let Some(error) = node.analysis_error() {
            return Self {
                error: Some(error.clone()),
                version: node.parse_state().version(),
                lines: node.parse_state().lines().cloned(),
                document: Document::default_from_uri(node.uri().clone()),
            };
        }

        let (error, version, lines) = match node.parse_state() {
            ParseState::NotParsed => unreachable!("document should have been parsed"),
            ParseState::Error(e) => (Some(e), None, None),
            ParseState::Parsed { version, lines, .. } => (None, *version, Some(lines)),
        };

        Self {
            error: error.cloned(),
            version,
            lines: lines.cloned(),
            document: node
                .document()
                .expect("analysis should have completed")
                .clone(),
        }
    }

    /// Gets the error that occurred when attempting to parse the document.
    ///
    /// An example error would be if the file could not be opened.
    ///
    /// Returns `None` if the document was parsed successfully.
    pub fn error(&self) -> Option<&Arc<Error>> {
        self.error.as_ref()
    }

    /// Gets the incremental version of the parsed document.
    ///
    /// Returns `None` if there was an error parsing the document or if the
    /// parsed document had no incremental changes.
    pub fn version(&self) -> Option<i32> {
        self.version
    }

    /// Gets the line index of the parsed document.
    ///
    /// Returns `None` if there was an error parsing the document.
    pub fn lines(&self) -> Option<&Arc<LineIndex>> {
        self.lines.as_ref()
    }

    /// Gets the analyzed document.
    pub fn document(&self) -> &Document {
        &self.document
    }
}

/// Represents a position in a document's source.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)]
pub struct SourcePosition {
    /// Line position in a document (zero-based).
    // NOTE: this field must come before `character` to maintain a correct sort order.
    pub line: u32,
    /// Character offset on a line in a document (zero-based). The meaning of
    /// this offset is determined by the position encoding.
    pub character: u32,
}

impl SourcePosition {
    /// Constructs a new source position from a line and character offset.
    pub fn new(line: u32, character: u32) -> Self {
        Self { line, character }
    }
}

/// Represents the encoding of a source position.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum SourcePositionEncoding {
    /// The position is UTF8 encoded.
    ///
    /// A position's character is the UTF-8 offset from the start of the line.
    UTF8,
    /// The position is UTF16 encoded.
    ///
    /// A position's character is the UTF-16 offset from the start of the line.
    UTF16,
}

/// Represents an edit to a document's source.
#[derive(Debug, Clone)]
pub struct SourceEdit {
    /// The range of the edit.
    ///
    /// Note that invalid ranges will cause the edit to be ignored.
    range: Range<SourcePosition>,
    /// The encoding of the edit positions.
    encoding: SourcePositionEncoding,
    /// The replacement text.
    text: String,
}

impl SourceEdit {
    /// Creates a new source edit for the given range and replacement text.
    pub fn new(
        range: Range<SourcePosition>,
        encoding: SourcePositionEncoding,
        text: impl Into<String>,
    ) -> Self {
        Self {
            range,
            encoding,
            text: text.into(),
        }
    }

    /// Gets the range of the edit.
    pub(crate) fn range(&self) -> Range<SourcePosition> {
        self.range.start..self.range.end
    }

    /// Applies the edit to the given string if it's in range.
    pub(crate) fn apply(&self, source: &mut String, lines: &LineIndex) -> Result<()> {
        let (start, end) = match self.encoding {
            SourcePositionEncoding::UTF8 => (
                LineCol {
                    line: self.range.start.line,
                    col: self.range.start.character,
                },
                LineCol {
                    line: self.range.end.line,
                    col: self.range.end.character,
                },
            ),
            SourcePositionEncoding::UTF16 => (
                lines
                    .to_utf8(
                        WideEncoding::Utf16,
                        WideLineCol {
                            line: self.range.start.line,
                            col: self.range.start.character,
                        },
                    )
                    .context("invalid edit start position")?,
                lines
                    .to_utf8(
                        WideEncoding::Utf16,
                        WideLineCol {
                            line: self.range.end.line,
                            col: self.range.end.character,
                        },
                    )
                    .context("invalid edit end position")?,
            ),
        };

        let range: Range<usize> = lines
            .offset(start)
            .context("invalid edit start position")?
            .into()
            ..lines
                .offset(end)
                .context("invalid edit end position")?
                .into();

        if !source.is_char_boundary(range.start) {
            bail!("edit start position is not at a character boundary");
        }

        if !source.is_char_boundary(range.end) {
            bail!("edit end position is not at a character boundary");
        }

        source.replace_range(range, &self.text);
        Ok(())
    }
}

/// Represents an incremental change to a document.
#[derive(Clone, Debug)]
pub struct IncrementalChange {
    /// The monotonic version of the document.
    ///
    /// This is expected to increase for each incremental change.
    pub version: i32,
    /// The source to start from for applying edits.
    ///
    /// If this is `Some`, a full reparse will occur after applying edits to
    /// this string.
    ///
    /// If this is `None`, edits will be applied to the existing CST and an
    /// attempt will be made to incrementally parse the file.
    pub start: Option<String>,
    /// The source edits to apply.
    pub edits: Vec<SourceEdit>,
}

/// Represents a Workflow Description Language (WDL) document analyzer.
///
/// By default, analysis parses documents, performs validation checks, resolves
/// imports, and performs type checking.
///
/// Each analysis operation is processed in order of request; however, the
/// individual parsing, resolution, and analysis of documents is performed
/// across a thread pool.
///
/// Note that dropping the analyzer is a blocking operation as it will wait for
/// the queue thread to join.
///
/// The type parameter is the context type passed to the progress callback.
#[derive(Debug)]
pub struct Analyzer<Context> {
    /// The sender for sending analysis requests to the queue.
    sender: ManuallyDrop<mpsc::UnboundedSender<Request<Context>>>,
    /// The join handle for the queue task.
    handle: Option<JoinHandle<()>>,
    /// The config to use during analysis.
    config: Config,
}

impl<Context> Analyzer<Context>
where
    Context: Send + Clone + 'static,
{
    /// Constructs a new analyzer with the given config.
    ///
    /// The provided progress callback will be invoked during analysis.
    ///
    /// The analyzer will use a default validator for validation.
    ///
    /// The analyzer must be constructed from the context of a Tokio runtime.
    pub fn new<Progress, Return>(config: Config, progress: Progress) -> Self
    where
        Progress: Fn(Context, ProgressKind, usize, usize) -> Return + Send + 'static,
        Return: Future<Output = ()>,
    {
        Self::new_with_validator(config, progress, crate::Validator::default)
    }

    /// Constructs a new analyzer with the given config and validator function.
    ///
    /// The provided progress callback will be invoked during analysis.
    ///
    /// This validator function will be called once per worker thread to
    /// initialize a thread-local validator.
    ///
    /// The analyzer must be constructed from the context of a Tokio runtime.
    pub fn new_with_validator<Progress, Return, Validator>(
        config: Config,
        progress: Progress,
        validator: Validator,
    ) -> Self
    where
        Progress: Fn(Context, ProgressKind, usize, usize) -> Return + Send + 'static,
        Return: Future<Output = ()>,
        Validator: Fn() -> crate::Validator + Send + Sync + 'static,
    {
        let (tx, rx) = mpsc::unbounded_channel();
        let tokio = Handle::current();
        let inner_config = config.clone();
        let handle = std::thread::spawn(move || {
            let queue = AnalysisQueue::new(inner_config, tokio, progress, validator);
            queue.run(rx);
        });

        Self {
            sender: ManuallyDrop::new(tx),
            handle: Some(handle),
            config,
        }
    }

    /// Adds a document to the analyzer. Document can be a local file or a URL.
    ///
    /// Returns an error if the document could not be added.
    pub async fn add_document(&self, uri: Url) -> Result<()> {
        let mut documents = IndexSet::new();
        documents.insert(uri);

        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Add(AddRequest {
                documents,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to receive response from analysis queue because the channel has closed")
        })?;

        Ok(())
    }

    /// Adds a directory to the analyzer. It will recursively search for WDL
    /// documents in the supplied directory.
    ///
    /// Returns an error if there was a problem discovering documents for the
    /// specified path.
    pub async fn add_directory(&self, path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref().to_path_buf();
        let config = self.config.clone();
        // Start by searching for documents
        let documents = RayonHandle::spawn(move || -> Result<IndexSet<Url>> {
            let mut documents = IndexSet::new();

            let metadata = path.metadata().with_context(|| {
                format!(
                    "failed to read metadata for `{path}`",
                    path = path.display()
                )
            })?;

            if metadata.is_file() {
                bail!("`{path}` is a file, not a directory", path = path.display());
            }

            let mut walker = WalkBuilder::new(&path);
            if let Some(ignore_filename) = config.ignore_filename() {
                walker.add_custom_ignore_filename(ignore_filename);
            }
            let walker = walker
                .standard_filters(false)
                .parents(true)
                .follow_links(true)
                .build();

            for result in walker {
                let entry = result.with_context(|| {
                    format!("failed to read directory `{path}`", path = path.display())
                })?;

                // Skip entries without a file type
                let Some(file_type) = entry.file_type() else {
                    continue;
                };
                // Skip non-files
                if !file_type.is_file() {
                    continue;
                }
                // Skip files without a `.wdl` extension
                if entry.path().extension() != Some(OsStr::new("wdl")) {
                    continue;
                }

                documents.insert(path_to_uri(entry.path()).with_context(|| {
                    format!(
                        "failed to convert path `{path}` to a URI",
                        path = entry.path().display()
                    )
                })?);
            }

            Ok(documents)
        })
        .await?;

        if documents.is_empty() {
            return Ok(());
        }

        // Send the add request to the queue
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Add(AddRequest {
                documents,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to receive response from analysis queue because the channel has closed")
        })?;

        Ok(())
    }

    /// Removes the specified documents from the analyzer.
    ///
    /// If a specified URI is a prefix (i.e. directory) of documents known to
    /// the analyzer, those documents will be removed.
    ///
    /// Documents are only removed when not referenced from importing documents.
    pub async fn remove_documents(&self, documents: Vec<Url>) -> Result<()> {
        // Send the remove request to the queue
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Remove(RemoveRequest {
                documents,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to receive response from analysis queue because the channel has closed")
        })?;

        Ok(())
    }

    /// Notifies the analyzer that a document has an incremental change.
    ///
    /// Changes to documents that aren't known to the analyzer are ignored.
    pub fn notify_incremental_change(
        &self,
        document: Url,
        change: IncrementalChange,
    ) -> Result<()> {
        self.sender
            .send(Request::NotifyIncrementalChange(
                NotifyIncrementalChangeRequest { document, change },
            ))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })
    }

    /// Notifies the analyzer that a document has fully changed and should be
    /// fetched again.
    ///
    /// Changes to documents that aren't known to the analyzer are ignored.
    ///
    /// If `discard_pending` is true, then any pending incremental changes are
    /// discarded; otherwise, the full change is ignored if there are pending
    /// incremental changes.
    pub fn notify_change(&self, document: Url, discard_pending: bool) -> Result<()> {
        self.sender
            .send(Request::NotifyChange(NotifyChangeRequest {
                document,
                discard_pending,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })
    }

    /// Analyzes a specific document.
    ///
    /// The provided context is passed to the progress callback.
    ///
    /// If the document is up-to-date and was previously analyzed, the current
    /// analysis result is returned.
    ///
    /// Returns an analysis result for each document that was analyzed.
    pub async fn analyze_document(
        &self,
        context: Context,
        document: Url,
    ) -> Result<Vec<AnalysisResult>> {
        // Send the analyze request to the queue
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Analyze(AnalyzeRequest {
                document: Some(document),
                context,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to receive response from analysis queue because the channel has closed")
        })?
    }

    /// Performs analysis of all documents.
    ///
    /// The provided context is passed to the progress callback.
    ///
    /// If a document is up-to-date and was previously analyzed, the current
    /// analysis result is returned.
    ///
    /// Returns an analysis result for each document that was analyzed.
    pub async fn analyze(&self, context: Context) -> Result<Vec<AnalysisResult>> {
        // Send the analyze request to the queue
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Analyze(AnalyzeRequest {
                document: None, // analyze all documents
                context,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send request to analysis queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to receive response from analysis queue because the channel has closed")
        })?
    }

    /// Formats a document.
    pub async fn format_document(&self, document: Url) -> Result<Option<(u32, u32, String)>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Format(FormatRequest {
                document,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!("failed to send format request to the queue because the channel has closed")
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to send format request to the queue because the channel has closed")
        })
    }

    /// Performs a "goto definition" for a symbol at the current position.
    pub async fn goto_definition(
        &self,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
    ) -> Result<Option<GotoDefinitionResponse>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::GotoDefinition(GotoDefinitionRequest {
                document,
                position,
                encoding,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send goto definition request to analysis queue because the channel \
                     has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive goto definition response from analysis queue because the \
                 channel has closed"
            )
        })
    }

    /// Performs a `find references` for a symbol across all the documents.
    pub async fn find_all_references(
        &self,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
        include_declaration: bool,
    ) -> Result<Vec<Location>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::FindAllReferences(FindAllReferencesRequest {
                document,
                position,
                encoding,
                include_declaration,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send find all references request to analysis queue because the \
                     channel has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive find all references response from analysis queue because the \
                 client channel has closed"
            )
        })
    }

    /// Performs a `auto-completion` for a symbol.
    pub async fn completion(
        &self,
        context: Context,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
    ) -> Result<Option<CompletionResponse>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Completion(CompletionRequest {
                document,
                position,
                encoding,
                context,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send completion request to analysis queue because the channel has \
                     closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to send completion request to analysis queue because the channel has \
                 closed"
            )
        })
    }

    /// Performs a `hover` for a symbol at a given position in a document.
    pub async fn hover(
        &self,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
    ) -> Result<Option<Hover>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Hover(HoverRequest {
                document,
                position,
                encoding,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send hover request to analysis queue because the channel has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!("failed to send hover request to analysis queue because the channel has closed")
        })
    }

    /// Renames a symbol at a given position across the workspace.
    pub async fn rename(
        &self,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
        new_name: String,
    ) -> Result<Option<WorkspaceEdit>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::Rename(RenameRequest {
                document,
                position,
                encoding,
                new_name,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send rename request to analysis queue because the channel has \
                     closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive rename response from analysis queue because the channel has \
                 closed"
            )
        })
    }

    /// Gets semantic tokens for a document
    pub async fn semantic_tokens(&self, document: Url) -> Result<Option<SemanticTokensResult>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::SemanticTokens(SemanticTokenRequest {
                document,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send semantic tokens request to analysis queue because the channel \
                     has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive semantic tokens response from analysis queue because the \
                 channel has closed"
            )
        })
    }

    /// Gets document symbols for a document.
    pub async fn document_symbol(&self, document: Url) -> Result<Option<DocumentSymbolResponse>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::DocumentSymbol(DocumentSymbolRequest {
                document,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send document symbol request to analysis queue because the channel \
                     has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive document symbol request to analysis queue because the channel \
                 has closed"
            )
        })
    }

    /// Gets document symbols for the workspace.
    pub async fn workspace_symbol(&self, query: String) -> Result<Option<Vec<SymbolInformation>>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::WorkspaceSymbol(WorkspaceSymbolRequest {
                query,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send workspace symbol request to analysis queue because the \
                     channel has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive workspace symbol response from analysis queue because the \
                 channel has closed"
            )
        })
    }

    /// Gets signature help for a function call at a given position.
    pub async fn signature_help(
        &self,
        document: Url,
        position: SourcePosition,
        encoding: SourcePositionEncoding,
    ) -> Result<Option<SignatureHelp>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::SignatureHelp(SignatureHelpRequest {
                document,
                position,
                encoding,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send signature help request to analysis queue because the channel \
                     has closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive signature help response from analysis queue because the \
                 channel has closed"
            )
        })
    }

    /// Requests inlay hints for a document.
    pub async fn inlay_hints(
        &self,
        document: Url,
        range: lsp_types::Range,
    ) -> Result<Option<Vec<InlayHint>>> {
        let (tx, rx) = oneshot::channel();
        self.sender
            .send(Request::InlayHints(InlayHintsRequest {
                document,
                range,
                completed: tx,
            }))
            .map_err(|_| {
                anyhow!(
                    "failed to send inlay hints request to analysis queue because the channel has \
                     closed"
                )
            })?;

        rx.await.map_err(|_| {
            anyhow!(
                "failed to receive inlay hints response from analysis queue because the channel \
                 has closed"
            )
        })
    }
}

impl Default for Analyzer<()> {
    fn default() -> Self {
        Self::new(Default::default(), |_, _, _, _| async {})
    }
}

impl<C> Drop for Analyzer<C> {
    fn drop(&mut self) {
        unsafe { ManuallyDrop::drop(&mut self.sender) };
        if let Some(handle) = self.handle.take() {
            handle.join().unwrap();
        }
    }
}

/// Constant that asserts `Analyzer` is `Send + Sync`; if not, it fails to
/// compile.
const _: () = {
    /// Helper that will fail to compile if T is not `Send + Sync`.
    const fn _assert<T: Send + Sync>() {}
    _assert::<Analyzer<()>>();
};

#[cfg(test)]
mod test {
    use std::fs;

    use tempfile::TempDir;
    use wdl_ast::Severity;

    use super::*;

    #[tokio::test]
    async fn it_returns_empty_results() {
        let analyzer = Analyzer::default();
        let results = analyzer.analyze(()).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn it_analyzes_a_document() {
        let dir = TempDir::new().expect("failed to create temporary directory");
        let path = dir.path().join("foo.wdl");
        fs::write(
            &path,
            r#"version 1.1

task test {
    command <<<>>>
}

workflow test {
}
"#,
        )
        .expect("failed to create test file");

        // Analyze the file and check the resulting diagnostic
        let analyzer = Analyzer::default();
        analyzer
            .add_document(path_to_uri(&path).expect("should convert to URI"))
            .await
            .expect("should add document");

        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.diagnostics().count(), 1);
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().rule(),
            None
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().severity(),
            Severity::Error
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().message(),
            "conflicting workflow name `test`"
        );

        // Analyze again and ensure the analysis result id is unchanged
        let id = results[0].document.id().clone();
        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.id().as_ref(), id.as_ref());
        assert_eq!(results[0].document.diagnostics().count(), 1);
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().rule(),
            None
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().severity(),
            Severity::Error
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().message(),
            "conflicting workflow name `test`"
        );
    }

    #[tokio::test]
    async fn it_reanalyzes_a_document_on_change() {
        let dir = TempDir::new().expect("failed to create temporary directory");
        let path = dir.path().join("foo.wdl");
        fs::write(
            &path,
            r#"version 1.1

task test {
    command <<<>>>
}

workflow test {
}
"#,
        )
        .expect("failed to create test file");

        // Analyze the file and check the resulting diagnostic
        let analyzer = Analyzer::default();
        analyzer
            .add_document(path_to_uri(&path).expect("should convert to URI"))
            .await
            .expect("should add document");

        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.diagnostics().count(), 1);
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().rule(),
            None
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().severity(),
            Severity::Error
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().message(),
            "conflicting workflow name `test`"
        );

        // Rewrite the file to correct the issue
        fs::write(
            &path,
            r#"version 1.1

task test {
    command <<<>>>
}

workflow something_else {
}
"#,
        )
        .expect("failed to create test file");

        let uri = path_to_uri(&path).expect("should convert to URI");
        analyzer.notify_change(uri.clone(), false).unwrap();

        // Analyze again and ensure the analysis result id is changed and the issue
        // fixed
        let id = results[0].document.id().clone();
        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].document.id().as_ref() != id.as_ref());
        assert_eq!(results[0].document.diagnostics().count(), 0);

        // Analyze again and ensure the analysis result id is unchanged
        let id = results[0].document.id().clone();
        let results = analyzer.analyze_document((), uri).await.unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].document.id().as_ref() == id.as_ref());
        assert_eq!(results[0].document.diagnostics().count(), 0);
    }

    #[tokio::test]
    async fn it_reanalyzes_a_document_on_incremental_change() {
        let dir = TempDir::new().expect("failed to create temporary directory");
        let path = dir.path().join("foo.wdl");
        fs::write(
            &path,
            r#"version 1.1

task test {
    command <<<>>>
}

workflow test {
}
"#,
        )
        .expect("failed to create test file");

        // Analyze the file and check the resulting diagnostic
        let analyzer = Analyzer::default();
        analyzer
            .add_document(path_to_uri(&path).expect("should convert to URI"))
            .await
            .expect("should add document");

        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].document.diagnostics().count(), 1);
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().rule(),
            None
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().severity(),
            Severity::Error
        );
        assert_eq!(
            results[0].document.diagnostics().next().unwrap().message(),
            "conflicting workflow name `test`"
        );

        // Edit the file to correct the issue
        let uri = path_to_uri(&path).expect("should convert to URI");
        analyzer
            .notify_incremental_change(
                uri.clone(),
                IncrementalChange {
                    version: 2,
                    start: None,
                    edits: vec![SourceEdit {
                        range: SourcePosition::new(6, 9)..SourcePosition::new(6, 13),
                        encoding: SourcePositionEncoding::UTF8,
                        text: "something_else".to_string(),
                    }],
                },
            )
            .unwrap();

        // Analyze again and ensure the analysis result id is changed and the issue was
        // fixed
        let id = results[0].document.id().clone();
        let results = analyzer.analyze_document((), uri).await.unwrap();
        assert_eq!(results.len(), 1);
        assert!(results[0].document.id().as_ref() != id.as_ref());
        assert_eq!(results[0].document.diagnostics().count(), 0);
    }

    #[tokio::test]
    async fn it_removes_documents() {
        let dir = TempDir::new().expect("failed to create temporary directory");
        let foo = dir.path().join("foo.wdl");
        fs::write(
            &foo,
            r#"version 1.1
workflow test {
}
"#,
        )
        .expect("failed to create test file");

        let bar = dir.path().join("bar.wdl");
        fs::write(
            &bar,
            r#"version 1.1
workflow test {
}
"#,
        )
        .expect("failed to create test file");

        let baz = dir.path().join("baz.wdl");
        fs::write(
            &baz,
            r#"version 1.1
workflow test {
}
"#,
        )
        .expect("failed to create test file");

        // Add all three documents to the analyzer
        let analyzer = Analyzer::default();
        analyzer
            .add_directory(dir.path())
            .await
            .expect("should add documents");

        // Analyze the documents
        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 3);
        assert!(results[0].document.diagnostics().next().is_none());
        assert!(results[1].document.diagnostics().next().is_none());
        assert!(results[2].document.diagnostics().next().is_none());

        // Analyze the documents again
        let results = analyzer.analyze(()).await.unwrap();
        assert_eq!(results.len(), 3);

        // Remove the documents by directory
        analyzer
            .remove_documents(vec![
                path_to_uri(dir.path()).expect("should convert to URI"),
            ])
            .await
            .unwrap();
        let results = analyzer.analyze(()).await.unwrap();
        assert!(results.is_empty());
    }
}