Skip to main content

mago_syntax/comments/
docblock.rs

1use mago_span::HasSpan;
2
3use crate::ast::Program;
4use crate::ast::Trivia;
5use crate::ast::TriviaKind;
6
7/// An iterator that yields docblock trivia nodes preceding a position, walking
8/// backwards through stacked docblocks using the same gap-checking logic as
9/// `get_docblock_before_position`.
10///
11/// By default all docblocks are yielded. Call `important_only(patterns)` to
12/// restrict the iterator to docblocks whose text contains at least one of the
13/// given substrings, skipping non-matching entries rather than stopping.
14pub struct PrecedingDocblocks<'arena, 'pat> {
15    trivia: &'arena [Trivia<'arena>],
16    start: u32,
17    important_patterns: &'pat [&'pat str],
18}
19
20impl<'arena> PrecedingDocblocks<'arena, 'static> {
21    #[must_use]
22    pub fn new(trivia: &'arena [Trivia<'arena>], start_offset: u32) -> Self {
23        Self { trivia, start: start_offset, important_patterns: &[] }
24    }
25}
26
27impl<'arena> PrecedingDocblocks<'arena, '_> {
28    /// Restrict this iterator to docblocks whose text contains at least one of
29    /// `patterns`. Non-matching docblocks are skipped; the search continues
30    /// backward past them.
31    #[must_use]
32    pub fn important_only<'new_pat>(self, patterns: &'new_pat [&'new_pat str]) -> PrecedingDocblocks<'arena, 'new_pat> {
33        PrecedingDocblocks { trivia: self.trivia, start: self.start, important_patterns: patterns }
34    }
35}
36
37impl<'arena> Iterator for PrecedingDocblocks<'arena, '_> {
38    type Item = &'arena Trivia<'arena>;
39
40    fn next(&mut self) -> Option<Self::Item> {
41        loop {
42            let trivia = get_docblock_before_position(self.trivia, self.start)?;
43            self.start = trivia.span.start_offset();
44            if self.important_patterns.is_empty() || self.important_patterns.iter().any(|p| trivia.value.contains(*p)) {
45                return Some(trivia);
46            }
47        }
48    }
49}
50
51/// Retrieves the docblock comment associated with a given node in the program.
52/// If the node is preceded by a docblock comment, it returns that comment.
53///
54/// This function searches for the last docblock comment that appears before the node's start position,
55/// ensuring that it is directly preceding the node without any non-whitespace characters in between.
56///
57/// # Arguments
58///
59/// * `program` - The program containing the trivia.
60/// * `node` - The node for which to find the preceding docblock comment.
61///
62/// # Returns
63///
64/// An `Option` containing a reference to the `Trivia` representing the docblock comment if found,
65/// or `None` if no suitable docblock comment exists before the node.
66#[inline]
67#[must_use]
68pub fn get_docblock_for_node<'arena>(
69    program: &'arena Program<'arena>,
70    node: impl HasSpan,
71) -> Option<&'arena Trivia<'arena>> {
72    get_docblock_before_position(program.trivia.as_slice(), node.span().start.offset)
73}
74
75/// Retrieves the docblock comment that appears before a specific position in the source code.
76///
77/// This function scans the trivia associated with the source code and returns the last docblock comment
78/// that appears before the specified position, ensuring that it is directly preceding the node
79/// without any non-whitespace characters in between.
80///
81/// # Arguments
82///
83/// * `trivias` - A slice of trivia associated with the source code.
84/// * `node_start_offset` - The start offset of the node for which to find the preceding docblock comment.
85///
86/// # Returns
87///
88/// An `Option` containing a reference to the `Trivia` representing the docblock comment if found,
89#[must_use]
90pub fn get_docblock_before_position<'arena>(
91    trivias: &'arena [Trivia<'arena>],
92    node_start_offset: u32,
93) -> Option<&'arena Trivia<'arena>> {
94    let candidate_partition_idx = trivias.partition_point(|trivia| trivia.span.start.offset < node_start_offset);
95    if candidate_partition_idx == 0 {
96        return None;
97    }
98
99    // Track the earliest position we've "covered" by trivia.
100    // Start from node_start_offset and work backwards.
101    // As we iterate, we verify that each trivia connects to the next (no code gaps).
102    // Since the parser captures all whitespace as WhiteSpace trivia, any gap not covered
103    // by a trivia node is actual code, so we just check for contiguity.
104    let mut covered_from = node_start_offset;
105
106    for i in (0..candidate_partition_idx).rev() {
107        let trivia = &trivias[i];
108        let trivia_end = trivia.span.end_offset();
109
110        if trivia_end != covered_from {
111            // Gap between this trivia and our covered region contains code.
112            return None;
113        }
114
115        match trivia.kind {
116            TriviaKind::DocBlockComment => {
117                // Found a docblock with no code between it and the node.
118                return Some(trivia);
119            }
120            TriviaKind::WhiteSpace
121            | TriviaKind::SingleLineComment
122            | TriviaKind::MultiLineComment
123            | TriviaKind::HashComment => {
124                covered_from = trivia.span.start_offset();
125            }
126        }
127    }
128
129    // Iterated through all preceding trivia without finding a suitable docblock.
130    None
131}
132
133#[cfg(test)]
134#[allow(clippy::unwrap_used)]
135mod tests {
136    use bumpalo::Bump;
137    use mago_database::file::FileId;
138    use mago_span::HasSpan;
139
140    use crate::parser::parse_file_content;
141
142    use super::get_docblock_before_position;
143
144    #[test]
145    fn whitespace_between_docblock_and_class_is_trivia() {
146        // The parser emits WhiteSpace trivia for all whitespace, so there is no
147        // code gap between the docblock's end offset and the class's start offset.
148        // This verifies the assumption that strict trivia contiguity == no code gap.
149        let arena = Bump::new();
150        let program = parse_file_content(&arena, FileId::zero(), "<?php\n\n/** @return int */\n\nclass Foo {}");
151        // statements[0] is the <?php opening tag; statements[1] is the class.
152        let class_start = program.statements.iter().nth(1).unwrap().span().start.offset;
153        let docblock = get_docblock_before_position(program.trivia.as_slice(), class_start);
154        assert!(docblock.is_some(), "expected docblock to be found across whitespace");
155        assert!(docblock.unwrap().value.contains("@return int"));
156    }
157
158    #[test]
159    fn code_between_docblock_and_function_blocks_attribution() {
160        let arena = Bump::new();
161        let program =
162            parse_file_content(&arena, FileId::zero(), "<?php\n/** @return int */\necho 1;\nfunction foo() {}");
163        // statements: [0]=OpeningTag, [1]=Echo, [2]=Function
164        let func_start = program.statements.iter().nth(2).unwrap().span().start.offset;
165        let docblock = get_docblock_before_position(program.trivia.as_slice(), func_start);
166        assert!(docblock.is_none(), "expected no docblock when code intervenes");
167    }
168}