| 163 | } |
| 164 | |
| 165 | fn preceding_docstring_with( |
| 166 | node: Node, |
| 167 | src: &str, |
| 168 | step_over: &[&str], |
| 169 | skip_trailing: bool, |
| 170 | skip_detached: bool, |
| 171 | skip_directives: bool, |
| 172 | ) -> Option<String> { |
| 173 | let mut anchor = node; |
| 174 | while let Some(parent) = anchor.parent() { |
| 175 | if is_wrapper(parent.kind()) { |
| 176 | anchor = parent; |
| 177 | } else { |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | let mut comments: Vec<Node> = Vec::new(); |
| 183 | let mut sibling = anchor.prev_named_sibling(); |
| 184 | while let Some(s) = sibling { |
| 185 | if is_comment(s.kind()) { |
| 186 | comments.push(s); |
| 187 | sibling = s.prev_named_sibling(); |
| 188 | } else if step_over.contains(&s.kind()) { |
| 189 | sibling = s.prev_named_sibling(); |
| 190 | } else { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | comments.reverse(); // collected nearest-first; TS unshifts to keep source order |
| 195 | |
| 196 | let mut first = 0; |
| 197 | if skip_trailing { |
| 198 | let mut end_row = 0; |
| 199 | for c in &comments { |
| 200 | let trails = if first == 0 { |
| 201 | follows_code_on_its_line(*c, src) |
| 202 | } else { |
| 203 | c.start_position().row == end_row |
| 204 | }; |
| 205 | if !trails { |
| 206 | break; |
| 207 | } |
| 208 | end_row = c.end_position().row; |
| 209 | first += 1; |
| 210 | } |
| 211 | } |
| 212 | if skip_detached { |
| 213 | // Walk back from the declaration: the last comment must end on the |
| 214 | // line above it, and each one before joins while it ends on the line |
| 215 | // the next one starts, or the line before that. |
| 216 | let mut start = comments.len(); |
| 217 | if start > first && comments[start - 1].end_position().row + 1 == anchor.start_position().row { |
| 218 | start -= 1; |
| 219 | while start > first |
| 220 | && comments[start - 1].end_position().row + 1 >= comments[start].start_position().row |
| 221 | { |
| 222 | start -= 1; |