malt-index 0.4.0

Native index of the MATLAB path (functions, classes, packages, class folders): the Rust core of malt
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
//! Parsing one MATLAB file into entities, with tree-sitter.
//!
//! The file kind follows the first declaration: a file whose first statement is a
//! `function` is a function file, one starting with `classdef` is a class file,
//! and anything else is a script. Syntax errors never stop the parse: tree-sitter
//! recovers, and nodes that are not understood are skipped.

use std::cell::RefCell;
use std::path::Path;

use tree_sitter::{Node, Parser, Tree};

use crate::comments::{comment_docstring, node_text};
use crate::config::ParserConfig;
use crate::error::IndexError;
use crate::expr::Expr;
use crate::live::is_mlx;
use crate::model::{
    Argument, ArgumentKind, Detail, Docstring, Entity, FunctionDetail, Kind, PropertyDetail,
};

thread_local! {
    static PARSER: RefCell<Option<Parser>> = const { RefCell::new(None) };
}

/// Parse a file from disk. The text is decoded lossily; a UTF-8 BOM is ignored.
///
/// A `.mlx` file is a live script and is read by [`crate::live`].
pub fn parse_file(path: &Path, config: &ParserConfig) -> Result<Entity, IndexError> {
    if is_mlx(path) {
        return crate::live::parse_live_file(path);
    }
    let bytes = std::fs::read(path).map_err(|e| IndexError::io(path, &e))?;
    log::debug!("parsing {}", path.display());
    parse_source(path, &bytes, config)
}

/// Parse MATLAB source bytes that belong to `path`.
pub fn parse_source(
    path: &Path,
    bytes: &[u8],
    config: &ParserConfig,
) -> Result<Entity, IndexError> {
    let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes);
    // Text that is not UTF-8 is read as Latin-1 (maxx detects the encoding; the
    // Latin-1 reading matches it for the Western files MATLAB users write).
    let transcoded: Vec<u8>;
    let src: &[u8] = if std::str::from_utf8(bytes).is_ok() {
        bytes
    } else {
        transcoded = bytes
            .iter()
            .flat_map(|b| char::from(*b).to_string().into_bytes())
            .collect();
        &transcoded
    };
    let tree = parse_tree(src)?;
    if tree.root_node().has_error() {
        log::warn!("{}: syntax errors were recovered", path.display());
    }
    let ctx = Ctx {
        src,
        path,
        stem: path
            .file_stem()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_default(),
        config,
    };
    Ok(ctx.file(&tree))
}

fn parse_tree(src: &[u8]) -> Result<Tree, IndexError> {
    PARSER.with(|cell| {
        let mut slot = cell.borrow_mut();
        if slot.is_none() {
            let mut parser = Parser::new();
            parser
                .set_language(&tree_sitter_matlab::LANGUAGE.into())
                .map_err(|e| IndexError::Grammar(e.to_string()))?;
            *slot = Some(parser);
        }
        let parser = slot
            .as_mut()
            .ok_or_else(|| IndexError::Grammar("parser unavailable".into()))?;
        parser
            .parse(src, None)
            .ok_or_else(|| IndexError::Grammar("parse was cancelled".into()))
    })
}

/// The named children of a node, in order.
pub(crate) fn named(node: Node) -> Vec<Node> {
    let mut cursor = node.walk();
    node.named_children(&mut cursor).collect()
}

/// All children of a node (named and anonymous tokens), in order.
fn all(node: Node) -> Vec<Node> {
    (0..node.child_count())
        .filter_map(|i| node.child(i))
        .collect()
}

/// The first named child of a given kind.
fn named_of_kind<'t>(node: Node<'t>, kind: &str) -> Option<Node<'t>> {
    named(node).into_iter().find(|n| n.kind() == kind)
}

/// Start and end line of a node, 1-based and inclusive.
fn span(node: Node) -> (usize, usize) {
    (node.start_position().row + 1, node.end_position().row + 1)
}

/// The value of an attribute: `Name` alone is `true`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum AttrValue {
    Flag(bool),
    Text(String),
}

impl AttrValue {
    fn truthy(&self) -> bool {
        match self {
            AttrValue::Flag(b) => *b,
            AttrValue::Text(s) => !s.is_empty(),
        }
    }
}

/// The parse context: the source of one file.
pub(crate) struct Ctx<'a> {
    src: &'a [u8],
    path: &'a Path,
    stem: String,
    config: &'a ParserConfig,
}

impl Ctx<'_> {
    fn text(&self, node: Node) -> String {
        node_text(self.src, node)
    }

    /// The file: its kind is decided by the first statement.
    fn file(&self, tree: &Tree) -> Entity {
        let root = tree.root_node();
        let children = named(root);
        let first = children.iter().position(|n| n.kind() != "comment");
        let header: Vec<Node> = children[..first.unwrap_or(children.len())].to_vec();
        let header_doc = comment_docstring(self.src, &header);
        let first_node = first.map(|i| children[i]);

        let mut entity = match first_node.map(|n| n.kind()) {
            Some("function_definition") => {
                let node = first_node.unwrap_or(root);
                let mut entity = self
                    .function(node, Role::File, &Default::default())
                    .unwrap_or_else(|| Entity::new(Kind::Function, &self.stem));
                entity.docstring = entity.docstring.or(header_doc);
                entity
            }
            Some("class_definition") => {
                let node = first_node.unwrap_or(root);
                let mut entity = self.class(node);
                entity.docstring = entity.docstring.or(header_doc);
                entity
            }
            _ => {
                let mut entity = Entity::new(Kind::Script, &self.stem);
                entity.path = Some(self.path.to_path_buf());
                entity.docstring = header_doc;
                entity
            }
        };
        // Local functions: every other top-level function of the file. A function
        // file's local functions are its members; a script's are too.
        if entity.kind != Kind::Class {
            for child in &children {
                if child.kind() == "function_definition" && Some(*child) != first_node {
                    if let Some(local) = self.function(*child, Role::Nested, &Default::default()) {
                        entity.members.insert(local.name.clone(), local);
                    }
                }
            }
        }
        entity
    }

    /// A classdef: bases, class attributes, enumerations, properties and methods.
    fn class(&self, node: Node) -> Entity {
        let children = named(node);
        let name = self.stem.clone();
        let bases: Vec<Expr> = children
            .iter()
            .filter(|c| c.kind() == "superclasses")
            .flat_map(|s| named(*s))
            .filter(|c| c.kind() == "property_name")
            .filter_map(|c| Expr::from_nodes(self.src, &[c]))
            .collect();
        let mut attributes = std::collections::BTreeSet::new();
        for attrs in children.iter().filter(|c| c.kind() == "attributes") {
            for attribute in named(*attrs)
                .into_iter()
                .filter(|c| c.kind() == "attribute")
            {
                if let Some((key, value)) = self.attribute(attribute) {
                    if matches!(key.as_str(), "Sealed" | "Abstract" | "Hidden") && value.truthy() {
                        attributes.insert(key);
                    }
                }
            }
        }
        let block_start = children
            .iter()
            .position(|c| {
                matches!(
                    c.kind(),
                    "properties" | "methods" | "enumeration" | "events"
                )
            })
            .unwrap_or(children.len());
        let leading: Vec<Node> = children[..block_start]
            .iter()
            .filter(|c| c.kind() == "comment")
            .copied()
            .collect();

        let events: Vec<String> = children
            .iter()
            .filter(|c| c.kind() == "events")
            .flat_map(|block| named(*block))
            .filter(|c| c.kind() == "identifier")
            .map(|c| self.text(c))
            .collect();

        let mut entity = Entity::new(Kind::Class, &name);
        let (lineno, endlineno) = span(node);
        entity.lineno = Some(lineno);
        entity.endlineno = Some(endlineno);
        entity.path = Some(self.path.to_path_buf());
        entity.docstring = comment_docstring(self.src, &leading);
        entity.attributes = attributes;
        entity.detail = Detail::Class {
            bases,
            events,
            declares_constructor: false,
        };

        for block in children.iter().filter(|c| c.kind() == "enumeration") {
            for member in self.enumeration(*block) {
                entity.members.insert(member.name.clone(), member);
            }
        }
        for block in children.iter().filter(|c| c.kind() == "properties") {
            for property in self.properties(*block) {
                entity.members.insert(property.name.clone(), property);
            }
        }
        let mut declares_constructor = false;
        for block in children.iter().filter(|c| c.kind() == "methods") {
            let attrs = self.method_attributes(*block);
            // An abstract method is a signature with no body (`function_signature`).
            for method in named(*block)
                .into_iter()
                .filter(|c| matches!(c.kind(), "function_definition" | "function_signature"))
            {
                if let Some(method) = self.function(method, Role::Method, &attrs) {
                    declares_constructor |= method.name == name;
                    self.attach_method(&mut entity, method);
                }
            }
        }
        if let Detail::Class {
            declares_constructor: flag,
            ..
        } = &mut entity.detail
        {
            *flag = declares_constructor;
        }
        entity
    }

    /// A getter or setter attaches to the property of the same name, when the
    /// class declares it; otherwise it is an ordinary member. Like maxx, a
    /// getter or setter whose name is taken by a non-property member is dropped.
    fn attach_method(&self, class: &mut Entity, method: Entity) {
        let is_accessor = method.function().map(|f| (f.is_getter, f.is_setter));
        let (getter, setter) = is_accessor.unwrap_or((false, false));
        if getter || setter {
            if let Some(existing) = class.members.get_mut(&method.name) {
                if let Detail::Property(detail) = &mut existing.detail {
                    if getter {
                        detail.getter = Some(Box::new(method));
                    } else {
                        detail.setter = Some(Box::new(method));
                    }
                }
                return;
            }
        }
        class.members.insert(method.name.clone(), method);
    }

    /// The `attribute` node: its name and its value (`Name`, `Name = value`).
    pub(crate) fn attribute(&self, node: Node) -> Option<(String, AttrValue)> {
        let key_node = named_of_kind(node, "identifier")?;
        let key = self.text(key_node);
        let values: Vec<Node> = all(node)
            .into_iter()
            .filter(|c| c.id() != key_node.id() && c.kind() != "=" && c.is_named())
            .collect();
        let value = match values.as_slice() {
            [] => AttrValue::Flag(true),
            [single] if single.kind() == "boolean" => {
                let t = self.text(*single).to_lowercase();
                AttrValue::Flag(t == "true" || t == "1")
            }
            _ => AttrValue::Text(
                values
                    .iter()
                    .map(|v| self.text(*v))
                    .collect::<Vec<_>>()
                    .join(""),
            ),
        };
        Some((key, value))
    }

    /// Attributes of a `methods` block, as the set of attribute strings.
    fn method_attributes(&self, block: Node) -> std::collections::BTreeSet<String> {
        let mut set = std::collections::BTreeSet::new();
        for attrs in named(block)
            .into_iter()
            .filter(|c| c.kind() == "attributes")
        {
            for attribute in named(attrs).into_iter().filter(|c| c.kind() == "attribute") {
                let Some((key, value)) = self.attribute(attribute) else {
                    continue;
                };
                match key.as_str() {
                    "Abstract" | "Hidden" | "Sealed" | "Static" if value.truthy() => {
                        set.insert(key);
                    }
                    "Access" => {
                        let access = access_value(&value);
                        if access != "public" {
                            set.insert(format!("Access={access}"));
                        }
                    }
                    _ => {}
                }
            }
        }
        set
    }

    /// The members of a `properties` block, in order.
    fn properties(&self, block: Node) -> Vec<Entity> {
        let mut set = std::collections::BTreeSet::new();
        for attrs in named(block)
            .into_iter()
            .filter(|c| c.kind() == "attributes")
        {
            for attribute in named(attrs).into_iter().filter(|c| c.kind() == "attribute") {
                let Some((key, value)) = self.attribute(attribute) else {
                    continue;
                };
                match key.as_str() {
                    "AbortSet" | "Abstract" | "Constant" | "Dependent" | "GetObservable"
                    | "Hidden" | "NonCopyable" | "SetObservable" | "Transient" | "WeakHandle"
                        if value.truthy() =>
                    {
                        set.insert(key);
                    }
                    "Access" | "GetAccess" | "SetAccess" => {
                        let access = access_value(&value);
                        if access != "public" {
                            set.insert(format!("{key}={access}"));
                        }
                    }
                    _ => {}
                }
            }
        }

        let items: Vec<Node> = named(block)
            .into_iter()
            .filter(|c| matches!(c.kind(), "property" | "comment"))
            .collect();
        let mut properties: Vec<Entity> = Vec::new();
        let mut pending: Option<Docstring> = None;
        for item in items {
            if item.kind() == "comment" {
                let doc = comment_docstring(self.src, &[item]);
                if self.config.docstring_before_properties {
                    pending = doc;
                } else if let (Some(doc), Some(last)) = (doc, properties.last_mut()) {
                    last.docstring = Some(doc);
                }
                continue;
            }
            let (name, detail) = self.property_parts(item);
            let mut entity = Entity::new(Kind::Property, &name);
            let (lineno, endlineno) = span(item);
            entity.lineno = Some(lineno);
            entity.endlineno = Some(endlineno);
            entity.path = Some(self.path.to_path_buf());
            entity.attributes = set.clone();
            entity.detail = Detail::Property(detail);
            if self.config.docstring_before_properties {
                entity.docstring = pending.take();
            }
            properties.push(entity);
        }
        properties
    }

    /// The name, type, dimensions, validators and default of a property node.
    fn property_parts(&self, node: Node) -> (String, PropertyDetail) {
        let name_node = node.child_by_field_name("name");
        let name = match name_node {
            Some(n) if n.kind() == "property_name" => {
                named(n).last().map(|i| self.text(*i)).unwrap_or_default()
            }
            Some(n) => self.text(n),
            None => String::new(),
        };
        // The type is every identifier after the name (maxx joins them: `uint8 scalar`
        // is read as `uint8scalar`), up to the validators and the default.
        let mut detail = PropertyDetail::default();
        let mut type_parts: Vec<Node> = Vec::new();
        for child in named(node) {
            if name_node.is_some_and(|n| n.id() == child.id()) {
                continue;
            }
            match child.kind() {
                "dimensions" => detail.dimensions = Some(self.dimensions(child)),
                "validation_functions" => detail.validators = Expr::from_nodes(self.src, &[child]),
                "default_value" => detail.default = self.default_expr(child),
                "identifier" | "property_name"
                    if detail.default.is_none() && detail.validators.is_none() =>
                {
                    type_parts.push(child);
                }
                _ => {}
            }
        }
        detail.type_ = Expr::from_nodes(self.src, &type_parts);
        (name, detail)
    }

    /// The dimensions as written: the numbers and `:` (spread operators) of a `dimensions` node.
    fn dimensions(&self, node: Node) -> Vec<String> {
        named(node)
            .into_iter()
            .filter(|c| matches!(c.kind(), "number" | "spread_operator"))
            .map(|c| self.text(c))
            .collect()
    }

    /// The default value as written: the nodes after `=`, joined.
    fn default_expr(&self, node: Node) -> Option<Expr> {
        let value: Vec<Node> = all(node).into_iter().filter(|c| c.kind() != "=").collect();
        Expr::from_nodes(self.src, &value)
    }

    /// The enumeration members of an `enumeration` block.
    fn enumeration(&self, block: Node) -> Vec<Entity> {
        // The block's content in source order: each member name (the first
        // identifier of an `enum` node), its value nodes, and the comments.
        enum Item<'t> {
            Name(Node<'t>, String),
            Value(Node<'t>),
            Comment(Node<'t>),
        }
        let mut content: Vec<(usize, Item)> = Vec::new();
        for child in named(block) {
            match child.kind() {
                "enum" => {
                    for (i, part) in named(child).into_iter().enumerate() {
                        if i == 0 && part.kind() == "identifier" {
                            content.push((part.start_byte(), Item::Name(child, self.text(part))));
                        } else {
                            content.push((part.start_byte(), Item::Value(part)));
                        }
                    }
                }
                "comment" => content.push((child.start_byte(), Item::Comment(child))),
                _ => {}
            }
        }
        content.sort_by_key(|(start, _)| *start);

        let before_mode = self.config.docstring_before_enumerations;
        let mut members: Vec<Entity> = Vec::new();
        let mut current: Option<(Node, String)> = None;
        let mut values: Vec<Node> = Vec::new();
        let mut after: Vec<Node> = Vec::new();
        let mut before: Vec<Node> = Vec::new();
        let mut flush = |current: &Option<(Node, String)>, comments: &[Node], values: &[Node]| {
            if let Some((enum_node, name)) = current {
                let mut entity = Entity::new(Kind::Enumeration, name);
                let (lineno, endlineno) = span(*enum_node);
                entity.lineno = Some(lineno);
                entity.endlineno = Some(endlineno);
                entity.path = Some(self.path.to_path_buf());
                entity.docstring = comment_docstring(self.src, comments);
                entity.detail = Detail::Enumeration {
                    value: Expr::from_nodes(self.src, values),
                };
                members.push(entity);
            }
        };
        for (_, item) in content {
            match item {
                Item::Name(enum_node, name) => {
                    let comments = if before_mode { &before } else { &after };
                    flush(&current, comments, &values);
                    before = std::mem::take(&mut after);
                    values.clear();
                    current = Some((enum_node, name));
                }
                Item::Comment(node) => after.push(node),
                Item::Value(node) => values.push(node),
            }
        }
        let comments = if before_mode { &before } else { &after };
        flush(&current, comments, &values);
        members
    }

    /// Parse a function node. Returns `None` when the name is missing (a
    /// syntax error in a method).
    pub(crate) fn function(
        &self,
        node: Node,
        role: Role,
        attrs: &std::collections::BTreeSet<String>,
    ) -> Option<Entity> {
        let is_method = role == Role::Method;
        let name = match role {
            Role::File => self.stem.clone(),
            Role::Method | Role::Nested => self.text(node.child_by_field_name("name")?),
        };
        if name.is_empty() {
            return None;
        }
        let children = named(node);
        let is_getter = all(node)
            .iter()
            .any(|c| !c.is_named() && self.text(*c).trim() == "get.");
        let is_setter = all(node)
            .iter()
            .any(|c| !c.is_named() && self.text(*c).trim() == "set.");

        let block_start = children
            .iter()
            .position(|c| matches!(c.kind(), "arguments_statement" | "block"))
            .unwrap_or(children.len());
        let leading: Vec<Node> = children[..block_start]
            .iter()
            .filter(|c| c.kind() == "comment")
            .copied()
            .collect();

        let mut inputs: Vec<Argument> = Vec::new();
        if let Some(args) = children.iter().find(|c| c.kind() == "function_arguments") {
            for child in named(*args) {
                if let Some(arg) = self.signature_name(child) {
                    inputs.push(Argument::new(&arg, ArgumentKind::PositionalOnly));
                }
            }
        }
        let mut returns: Vec<Argument> = Vec::new();
        if let Some(output) = children.iter().find(|c| c.kind() == "function_output") {
            for child in named(*output) {
                if child.kind() == "multioutput_variable" {
                    for part in named(child) {
                        if let Some(arg) = self.signature_name(part) {
                            returns.push(Argument::new(&arg, ArgumentKind::PositionalOnly));
                        }
                    }
                } else if let Some(arg) = self.signature_name(child) {
                    returns.push(Argument::new(&arg, ArgumentKind::PositionalOnly));
                }
            }
        }

        for block in children
            .iter()
            .filter(|c| c.kind() == "arguments_statement")
        {
            self.arguments_block(*block, &mut inputs, &mut returns);
        }
        for input in inputs.iter_mut() {
            if input.name == "varargin" {
                input.kind = ArgumentKind::Varargin;
            }
        }

        let is_static = attrs.contains("Static");
        if is_method && name != self.stem && !is_static && !inputs.is_empty() {
            // The first input of a method is the object itself.
            inputs.remove(0);
        }

        let (lineno, endlineno) = span(node);
        let mut entity = Entity::new(Kind::Function, &name);
        entity.lineno = Some(lineno);
        entity.endlineno = Some(endlineno);
        entity.path = Some(self.path.to_path_buf());
        entity.docstring = comment_docstring(self.src, &leading);
        entity.attributes = attrs.clone();
        entity.detail = Detail::Function(FunctionDetail {
            arguments: inputs,
            returns,
            is_getter,
            is_setter,
        });

        // Local and nested functions: the functions in the body of this one.
        if let Some(body) = children.iter().find(|c| c.kind() == "block") {
            for child in named(*body) {
                if child.kind() == "function_definition" {
                    if let Some(nested) = self.function(child, Role::Nested, &Default::default()) {
                        entity.members.insert(nested.name.clone(), nested);
                    }
                }
            }
        }
        Some(entity)
    }

    /// The name of a signature item: an identifier, or `~` for an ignored one.
    fn signature_name(&self, node: Node) -> Option<String> {
        match node.kind() {
            "identifier" => Some(self.text(node)),
            "ignored_argument" => Some("~".to_string()),
            _ => None,
        }
    }

    /// Apply one `arguments` block to the inputs and outputs of a function, as maxx
    /// does: only names in the signature are updated; a name-value argument
    /// (`opts.Name`) is added as a keyword-only argument.
    fn arguments_block(
        &self,
        block: Node,
        inputs: &mut Vec<Argument>,
        returns: &mut Vec<Argument>,
    ) {
        let attrs: Vec<String> = named_of_kind(block, "attributes")
            .map(|a| {
                named(a)
                    .into_iter()
                    .filter(|c| c.kind() == "identifier")
                    .map(|c| self.text(c))
                    .collect()
            })
            .unwrap_or_default();
        let is_input = attrs.iter().any(|a| a == "Input") || !attrs.iter().any(|a| a == "Output");
        let before_mode = self.config.docstring_before_arguments;

        // Where the next comment attaches: the input or output named here, or nothing.
        let mut current: Option<(Slot, String)> = None;
        let mut pending: Option<Docstring> = None;
        for item in named(block) {
            match item.kind() {
                "comment" => {
                    let doc = comment_docstring(self.src, &[item]);
                    if before_mode {
                        pending = doc;
                    } else if let (Some(doc), Some((slot, name))) = (doc, current.clone()) {
                        if let Some(arg) = find_mut(inputs, returns, slot, &name) {
                            arg.docstring = Some(doc);
                        }
                    }
                }
                "class_property" => {
                    // `opts.?Class.path`: a keyword argument whose class is a class path.
                    let text = self.text(item);
                    let (opts, rest) = match text.split_once(".?") {
                        Some(parts) => parts,
                        None => continue,
                    };
                    let class_path = rest.trim().trim_end_matches(';').trim();
                    if class_path.is_empty() {
                        continue;
                    }
                    let mut arg = Argument::new(opts.trim(), ArgumentKind::KeywordOnly);
                    arg.type_ = Some(Expr::from_node_text(self.src, item, class_path));
                    if before_mode {
                        arg.docstring = pending.take();
                    }
                    upsert(inputs, arg);
                    current = Some((Slot::Input, opts.trim().to_string()));
                }
                "property" => {
                    let (name, detail) = self.property_parts(item);
                    let name_node = item.child_by_field_name("name");
                    let is_keyword = name_node.is_some_and(|n| n.kind() == "property_name");
                    let has_default = detail.default.is_some();
                    let doc = if before_mode { pending.take() } else { None };
                    if is_keyword {
                        let options = name_node
                            .and_then(|n| named(n).first().map(|i| self.text(*i)))
                            .unwrap_or_default();
                        inputs.retain(|a| a.name != options);
                        let mut arg = Argument::new(&name, ArgumentKind::KeywordOnly);
                        fill_argument(&mut arg, &detail);
                        arg.docstring = doc;
                        upsert(inputs, arg);
                        current = Some((Slot::Input, name));
                    } else {
                        let kind = if has_default {
                            ArgumentKind::Optional
                        } else {
                            ArgumentKind::PositionalOnly
                        };
                        let (slot, list) = if is_input {
                            (Slot::Input, &mut *inputs)
                        } else {
                            (Slot::Output, &mut *returns)
                        };
                        if let Some(arg) = list.iter_mut().find(|a| a.name == name) {
                            arg.kind = kind;
                            fill_argument(arg, &detail);
                            arg.docstring = doc;
                            current = Some((slot, name));
                        } else {
                            // A name that is not in the signature is dropped, as maxx does.
                            current = Some((Slot::Detached, name));
                        }
                    }
                }
                _ => {}
            }
        }
    }
}

/// What a function node is: the file's own function (named after the file), a method
/// of a classdef, or a local or nested function (named by its identifier).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Role {
    File,
    Method,
    Nested,
}

/// The access level of an attribute value: one of the known levels; a list of classes
/// (`{?Cls1, ?Cls2}`) or one class (`?Cls`) is `restricted`; anything else is `private`.
fn access_value(value: &AttrValue) -> String {
    match value {
        AttrValue::Text(s)
            if matches!(s.as_str(), "public" | "protected" | "private" | "immutable") =>
        {
            s.clone()
        }
        AttrValue::Text(s) if s.starts_with('{') || s.starts_with('?') => "restricted".to_string(),
        _ => "private".to_string(),
    }
}

/// Copy the dimensions, validators, type and default of a property into an argument.
fn fill_argument(arg: &mut Argument, detail: &PropertyDetail) {
    if detail.dimensions.is_some() {
        arg.dimensions = detail.dimensions.clone();
    }
    if detail.type_.is_some() {
        arg.type_ = detail.type_.clone();
    }
    if detail.validators.is_some() {
        arg.validators = detail.validators.clone();
    }
    if detail.default.is_some() {
        arg.default = detail.default.clone();
    }
}

/// Replace the argument of the same name in place, or append it.
fn upsert(list: &mut Vec<Argument>, arg: Argument) {
    match list.iter().position(|a| a.name == arg.name) {
        Some(i) => list[i] = arg,
        None => list.push(arg),
    }
}

/// The list an arguments-block comment attaches to.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Slot {
    Input,
    Output,
    /// A name that is not in the signature: its comments go nowhere.
    Detached,
}

fn find_mut<'a>(
    inputs: &'a mut [Argument],
    returns: &'a mut [Argument],
    slot: Slot,
    name: &str,
) -> Option<&'a mut Argument> {
    match slot {
        Slot::Input => inputs.iter_mut().find(|a| a.name == name),
        Slot::Output => returns.iter_mut().find(|a| a.name == name),
        Slot::Detached => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(src: &str) -> Entity {
        parse_source(
            Path::new("/x/Sample.m"),
            src.as_bytes(),
            &ParserConfig::default(),
        )
        .unwrap()
    }

    #[test]
    fn function_file_takes_its_name_from_the_file() {
        let e = parse("function r = Sample(a)\nr = a;\nend\n");
        assert_eq!(e.kind, Kind::Function);
        assert_eq!(e.name, "Sample");
        assert_eq!(e.lineno, Some(1));
        assert_eq!(e.endlineno, Some(3));
    }
}