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
//! Handlers for hover requests.
//!
//! This module implements the LSP `textDocument/hover` functionality for WDL
//! files.
//!
//! See: [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover)

use anyhow::Result;
use anyhow::bail;
use lsp_types::Hover;
use lsp_types::HoverContents;
use lsp_types::MarkupContent;
use lsp_types::MarkupKind;
use tracing::debug;
use url::Url;
use wdl_ast::AstNode;
use wdl_ast::AstToken;
use wdl_ast::Documented;
use wdl_ast::SyntaxKind;
use wdl_ast::SyntaxNode;
use wdl_ast::SyntaxToken;
use wdl_ast::TreeNode;
use wdl_ast::TreeToken;
use wdl_ast::v1::AccessExpr;
use wdl_ast::v1::CallExpr;
use wdl_ast::v1::CallTarget;
use wdl_ast::v1::Decl;
use wdl_ast::v1::EnumVariant;
use wdl_ast::v1::LiteralStruct;
use wdl_ast::v1::LiteralStructItem;
use wdl_ast::v1::ParameterMetadataSection;
use wdl_ast::v1::StructDefinition;

use crate::Document;
use crate::SourcePosition;
use crate::SourcePositionEncoding;
use crate::graph::DocumentGraph;
use crate::graph::ParseState;
use crate::handlers::TypeEvalContext;
use crate::handlers::common::find_identifier_token_at_offset;
use crate::handlers::common::location_from_span;
use crate::handlers::common::position_to_offset;
use crate::handlers::common::provide_enum_documentation;
use crate::handlers::common::provide_struct_documentation;
use crate::handlers::common::provide_task_documentation;
use crate::handlers::common::provide_workflow_documentation;
use crate::stdlib::Function;
use crate::stdlib::STDLIB;
use crate::stdlib::TypeParameters;
use crate::types::CompoundType;
use crate::types::CustomType;
use crate::types::Type;
use crate::types::v1::ExprTypeEvaluator;

/// Handles a hover request.
///
/// Analyzes the context at the specified position and generates appropriate
/// hover information.
///
/// Provides hover information by:
/// 1. Attempting to resolve the symbol based on its CST context.
/// 2. Looking up the symbol in the current scope.
/// 3. Checking for global definitions (tasks, workflows and structs) across the
///    document and its imports.
pub fn hover(
    graph: &DocumentGraph,
    document_uri: &Url,
    position: SourcePosition,
    encoding: SourcePositionEncoding,
) -> Result<Option<Hover>> {
    let Some(index) = graph.get_index(document_uri) else {
        bail!("document `{document_uri}` not found in graph")
    };
    let node = graph.get(index);
    let (root, lines) = match node.parse_state() {
        ParseState::Parsed { lines, root, .. } => {
            (SyntaxNode::new_root(root.clone()), lines.clone())
        }
        _ => bail!("document `{uri}` has not been parsed", uri = document_uri),
    };

    let Some(document) = node.document() else {
        bail!("document analysis data not available for {}", document_uri);
    };

    let offset = position_to_offset(&lines, position, encoding)?;
    let Some(token) = find_identifier_token_at_offset(&root, offset) else {
        bail!("no identifier found at position");
    };

    let parent_node = token.parent().expect("token has no parent");

    if let Ok(Some(value)) = resolve_hover_content(&parent_node, &token, document, graph) {
        let range = location_from_span(document_uri, token.span(), &lines)?.range;
        Ok(Some(Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value,
            }),
            range: Some(range),
        }))
    } else {
        Ok(None)
    }
}

/// This function handles the search for hover information by trying
/// various resolution methods.
fn resolve_hover_content(
    parent_node: &SyntaxNode,
    token: &SyntaxToken,
    document: &Document,
    graph: &DocumentGraph,
) -> Result<Option<String>> {
    // Finds hover information based on the CST.
    if let Some(content) = resolve_hover_by_context(parent_node, token, document, graph)? {
        return Ok(Some(content));
    }

    // Finds hover information based on the scope.
    if let Some(scope) = document.find_scope_by_position(token.span().start())
        && let Some(name) = scope.lookup(token.text())
    {
        let (kind, documentation) = match name.ty() {
            Type::Call(_) => ("call", None),
            _ => {
                let doc = find_parameter_meta_documentation(token);
                ("variable", doc)
            }
        };
        let mut content = format!("```wdl\n({kind}) {}: {}\n```", token.text(), name.ty());
        if let Some(doc) = documentation {
            content.push_str("\n---\n");
            content.push_str(&doc);
        }

        return Ok(Some(content));
    }

    // Finds hover information across global definitions.
    if let Some(content) = find_global_hover_in_doc(document, token)? {
        return Ok(Some(content));
    }

    for (_, ns) in document.namespaces() {
        // SAFETY: we know `get_index` will return `Some` as `ns.source` comes from
        // `document.namespaces` which only contains namespaces for documents that
        // are guaranteed to be present in the graph.
        let node = graph.get(graph.get_index(ns.source()).unwrap());
        let Some(imported_doc) = node.document() else {
            continue;
        };
        if let Some(content) = find_global_hover_in_doc(imported_doc, token)? {
            return Ok(Some(content));
        }
    }

    Ok(None)
}

/// Resolves hover information based on the CST of the document.
///
/// This inspects the parent [`SyntaxNode`] of the hovered token to
/// determine the most specific context.
fn resolve_hover_by_context(
    parent_node: &SyntaxNode,
    token: &SyntaxToken,
    document: &Document,
    graph: &DocumentGraph,
) -> Result<Option<String>> {
    match parent_node.kind() {
        SyntaxKind::TypeRefNode | SyntaxKind::LiteralStructNode => {
            if let Some(s) = document.struct_by_name(token.text()) {
                let root = if let Some(ns_name) = s.namespace() {
                    // SAFETY: we just found a struct with this namespace name and the document
                    // guarantees that `document.namespaces` contains a corresponding entry for
                    // `ns_name`.
                    let ns = document.namespace(ns_name).unwrap();
                    let node = graph.get(graph.get_index(ns.source()).unwrap());
                    node.document().unwrap().root()
                } else {
                    document.root()
                };
                return Ok(provide_struct_documentation(s, &root));
            }
            if let Some(e) = document.enum_by_name(token.text()) {
                let root = if let Some(ns_name) = e.namespace() {
                    // SAFETY: we just found an enum with this namespace name and the document
                    // guarantees that `document.namespaces` contains a corresponding entry for
                    // `ns_name`.
                    let ns = document.namespace(ns_name).unwrap();
                    let node = graph.get(graph.get_index(ns.source()).unwrap());
                    node.document().unwrap().root()
                } else {
                    document.root()
                };
                return Ok(provide_enum_documentation(e, &root));
            }
        }
        SyntaxKind::EnumVariantNode => {
            let variant = EnumVariant::cast(parent_node.clone()).unwrap();
            let variant_name = variant.name().text().to_string();

            // Show the variant value (explicit or inferred)
            if let Some(value_expr) = variant.value() {
                // Has explicit value
                let content = format!(
                    "```wdl\n{} = {}\n```",
                    variant_name,
                    value_expr.inner().text()
                );
                return Ok(Some(content));
            } else {
                // Inferred value (defaults to string of variant name)
                let content = format!("```wdl\n{} = \"{}\"\n```", variant_name, variant_name);
                return Ok(Some(content));
            }
        }
        SyntaxKind::CallTargetNode => {
            let target = CallTarget::cast(parent_node.clone()).unwrap();
            let mut target_names = target.names();

            let (ns_name, callee_name) = match (target_names.next(), target_names.next()) {
                // Namespaced call
                (Some(ns), Some(name)) => {
                    if token.span() == name.span() {
                        (Some(ns), name)
                    } else if token.span() == ns.span() {
                        // namespace identifier hovered
                        if let Some(ns) = document.namespace(token.text()) {
                            return Ok(Some(format!(
                                "```wdl\n(import) {}\n```\nImports from `{}`",
                                token.text(),
                                ns.source()
                            )));
                        }
                        return Ok(None);
                    } else {
                        return Ok(None);
                    }
                }
                // Local call
                (Some(name), None) if token.span() == name.span() => (None, name),
                _ => return Ok(None),
            };

            let target_doc = if let Some(ns_name) = ns_name {
                // SAFETY: we just found a call with this namespace name and the document
                // guarantees that `document.namespaces` contains a corresponding entry for
                // `ns_name`.
                let ns = document.namespace(ns_name.text()).unwrap();

                // SAFETY: `ns.source` comes from a valid namespace entry which guarantees the
                // document exists in the graph.
                let node = graph.get(graph.get_index(ns.source()).unwrap());
                node.document().unwrap()
            } else {
                document
            };

            if let Some(task) = target_doc.task_by_name(callee_name.text()) {
                return Ok(provide_task_documentation(task, &target_doc.root()));
            }

            if let Some(workflow) = target_doc
                .workflow()
                .filter(|w| w.name() == callee_name.text())
            {
                return Ok(provide_workflow_documentation(workflow, &target_doc.root()));
            }
        }
        SyntaxKind::AccessExprNode => {
            let access_expr = AccessExpr::cast(parent_node.clone()).unwrap();
            let (expr, member) = access_expr.operands();
            if member.span() != token.span() {
                return Ok(None);
            }

            let Some(scope) = document.find_scope_by_position(parent_node.span().start()) else {
                return Ok(None);
            };

            let mut ctx = TypeEvalContext { scope, document };
            let mut evaluator = ExprTypeEvaluator::new(&mut ctx);
            let target_type = evaluator
                .evaluate_expr(&expr)
                .unwrap_or(crate::types::Type::Union);

            let (member_ty, documentation) = match target_type {
                Type::TypeNameRef(CustomType::Enum(e)) => {
                    if e.variants().iter().any(|text| text == member.text()) {
                        // Try to find the enum definition to get the actual value
                        if let Some(enum_entry) = document.enum_by_name(e.name()) {
                            let definition = enum_entry.definition();

                            // Find the specific variant
                            if let Some(variant) = definition
                                .variants()
                                .find(|v| v.name().text() == member.text())
                            {
                                let value_str = if let Some(value_expr) = variant.value() {
                                    value_expr.inner().text().to_string()
                                } else {
                                    format!("\"{}\"", member.text())
                                };

                                let content = format!(
                                    "```wdl\n{}.{}[{}] = {}\n```",
                                    e.name(),
                                    member.text(),
                                    e.inner_value_type(),
                                    value_str
                                );
                                return Ok(Some(content));
                            }
                        }

                        // Fallback to showing just the type
                        let content = format!(
                            "```wdl\n{}.{}[{}]\n```",
                            e.name(),
                            member.text(),
                            e.inner_value_type()
                        );
                        return Ok(Some(content));
                    }
                    (None, None)
                }
                Type::TypeNameRef(CustomType::Struct(_)) => {
                    // `Struct.member` is not valid in WDL.
                    return Ok(None);
                }
                Type::Compound(CompoundType::Custom(CustomType::Struct(s)), _) => {
                    let target_doc = if let Some(s) = document.struct_by_name(s.name()) {
                        if let Some(ns_name) = s.namespace() {
                            // SAFETY: we just found a struct with this namespace name and the
                            // document guarantees that `document.namespaces` contains a
                            // corresponding entry for `ns_name`.
                            let ns = document.namespace(ns_name).unwrap();

                            // SAFETY: `ns.source` comes from a valid namespace entry which
                            // guarantees the document exists in the graph.
                            let node = graph.get(graph.get_index(ns.source()).unwrap());
                            node.document().unwrap()
                        } else {
                            document
                        }
                    } else {
                        bail!("struct not found in document");
                    };
                    let doc = target_doc.struct_by_name(s.name()).and_then(|s| {
                        let def = StructDefinition::cast(SyntaxNode::new_root(s.node().clone()))?;
                        def.members()
                            .find(|m| m.name().text() == member.text())
                            .and_then(|decl| find_parameter_meta_documentation(decl.name().inner()))
                    });

                    (s.members().get(member.text()).cloned(), doc)
                }
                Type::Call(c) => (c.outputs().get(member.text()).map(|o| o.ty().clone()), None),
                Type::Compound(CompoundType::Pair(p), _) => match member.text() {
                    "left" => (Some(p.left_type().clone()), None),
                    "right" => (Some(p.right_type().clone()), None),
                    _ => (None, None),
                },
                Type::Compound(CompoundType::Custom(CustomType::Enum(e)), _) => {
                    if e.variants().iter().any(|text| text == member.text()) {
                        // Try to find the enum definition to get the actual value
                        if let Some(enum_entry) = document.enum_by_name(e.name()) {
                            let definition = enum_entry.definition();

                            // Find the specific variant
                            if let Some(variant) = definition
                                .variants()
                                .find(|v| v.name().text() == member.text())
                            {
                                let value_str = if let Some(value_expr) = variant.value() {
                                    value_expr.inner().text().to_string()
                                } else {
                                    format!("\"{}\"", member.text())
                                };

                                let content = format!(
                                    "```wdl\n{}.{}[{}] = {}\n```",
                                    e.name(),
                                    member.text(),
                                    e.inner_value_type(),
                                    value_str
                                );
                                return Ok(Some(content));
                            }
                        }

                        // Fallback to showing just the type
                        let content = format!(
                            "```wdl\n{}.{}[{}]\n```",
                            e.name(),
                            member.text(),
                            e.inner_value_type()
                        );
                        return Ok(Some(content));
                    }
                    (None, None)
                }
                _ => (None, None),
            };
            if let Some(ty) = member_ty {
                let mut content = format!("```wdl\n(property) {}: {}\n```", member.text(), ty);
                if let Some(doc) = documentation {
                    content.push_str("\n---\n");
                    content.push_str(&doc);
                }
                return Ok(Some(content));
            }
        }
        SyntaxKind::CallExprNode => {
            let Some(call_expr) = CallExpr::cast(parent_node.clone()) else {
                return Ok(None);
            };

            if call_expr.target().span() != token.span() {
                return Ok(None);
            }

            if let Some(func) = STDLIB.function(call_expr.target().text()) {
                let content = get_function_hover_content(call_expr.target().text(), func);
                return Ok(Some(content));
            }
        }

        SyntaxKind::LiteralStructItemNode => {
            let Some(item) = LiteralStructItem::cast(parent_node.clone()) else {
                return Ok(None);
            };

            let (name, _) = item.name_value();
            if name.span() != token.span() {
                return Ok(None);
            }

            let Some(struct_literal) = parent_node.parent().and_then(LiteralStruct::cast) else {
                return Ok(None);
            };

            let struct_name = struct_literal.name();
            if let Some(s) = document.struct_by_name(struct_name.text()) {
                let def = StructDefinition::cast(SyntaxNode::new_root(s.node().clone()))
                    .expect("should cast to StructDefinition");
                if let Some(member) = def.members().find(|m| m.name().text() == name.text()) {
                    let doc = find_parameter_meta_documentation(member.name().inner());
                    let mut content =
                        format!("```wdl\n(property) {}: {}\n```", name.text(), member.ty());
                    if let Some(doc) = doc {
                        content.push_str("\n---\n");
                        content.push_str(&doc);
                    }
                    return Ok(Some(content));
                }
            }
        }
        _ => debug!("hover is not implemented for {:?}", parent_node.kind()),
    }

    Ok(None)
}

/// Finds hover information for a globally defined symbol within a [`Document`].
fn find_global_hover_in_doc(document: &Document, token: &SyntaxToken) -> Result<Option<String>> {
    if let Some(s) = document.struct_by_name(token.text()) {
        return Ok(provide_struct_documentation(s, &document.root()));
    }
    if let Some(e) = document.enum_by_name(token.text()) {
        return Ok(provide_enum_documentation(e, &document.root()));
    }
    if let Some(t) = document.task_by_name(token.text()) {
        return Ok(provide_task_documentation(t, &document.root()));
    }
    if let Some(w) = document.workflow().filter(|w| w.name() == token.text()) {
        return Ok(provide_workflow_documentation(w, &document.root()));
    }
    Ok(None)
}

/// Generates markdown content for a standard library function's hover info.
///
/// This includes all overloaded signatures and the documentation from the WDL
/// specification.
fn get_function_hover_content(name: &str, func: &Function) -> String {
    let (detail, docs) = match func {
        Function::Monomorphic(m) => {
            let sig = m.signature();
            let params = TypeParameters::new(sig.type_parameters());
            let detail = format!("```wdl\n{}{}\n```", name, sig.display(&params));
            let docs = sig.definition().unwrap_or("");
            (detail, docs)
        }
        Function::Polymorphic(p) => {
            let detail = p
                .signatures()
                .iter()
                .map(|s| {
                    let params = TypeParameters::new(s.type_parameters());
                    format!("```wdl\n{}{}\n```", name, s.display(&params))
                })
                .collect::<Vec<_>>()
                .join("\n---\n");

            let docs = p
                .signatures()
                .first()
                .and_then(|s| s.definition())
                .unwrap_or("");
            (detail, docs)
        }
    };
    format!("{detail}\n\n{docs}")
}

/// Finds documentation for a variable declaration.
///
/// Doc comments (`##`) on the declaration are preferred. Falls back to the
/// matching entry in the enclosing `parameter_meta` section.
fn find_parameter_meta_documentation(token: &SyntaxToken) -> Option<String> {
    use crate::handlers::common::docs::comments_to_string;

    // Check for doc comments on the declaration node itself via the Documented
    // trait.
    if let Some(doc) = token
        .parent_ancestors()
        .find_map(Decl::cast)
        .and_then(|d| match &d {
            Decl::Bound(b) => b.doc_comments(),
            Decl::Unbound(u) => u.doc_comments(),
        })
        .and_then(comments_to_string)
    {
        return Some(doc);
    }

    // Fall back to parameter_meta.
    let parent = token.parent_ancestors().find(|p| {
        matches!(
            p.kind(),
            SyntaxKind::StructDefinitionNode
                | SyntaxKind::TaskDefinitionNode
                | SyntaxKind::WorkflowDefinitionNode
        )
    })?;
    let param_meta = parent.children().find_map(ParameterMetadataSection::cast)?;

    for item in param_meta.items() {
        if item.name().text() == token.text() {
            let doc_text = item.value().text().to_string();
            return Some(doc_text.trim_matches('"').to_string());
        }
    }
    None
}