]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/syntax/src/utils.rs
Rollup merge of #101389 - lukaslueg:rcgetmutdocs, r=m-ou-se
[rust.git] / src / tools / rust-analyzer / crates / syntax / src / utils.rs
1 //! A set of utils methods to reuse on other abstraction levels
2
3 use itertools::Itertools;
4
5 use crate::{ast, match_ast, AstNode};
6
7 pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
8     path.syntax()
9         .children()
10         .filter_map(|node| {
11             match_ast! {
12                 match node {
13                     ast::PathSegment(it) => {
14                         Some(it.name_ref()?.to_string())
15                     },
16                     ast::Path(it) => {
17                         Some(path_to_string_stripping_turbo_fish(&it))
18                     },
19                     _ => None,
20                 }
21             }
22         })
23         .join("::")
24 }
25
26 #[cfg(test)]
27 mod tests {
28     use super::path_to_string_stripping_turbo_fish;
29     use crate::ast::make;
30
31     #[test]
32     fn turbofishes_are_stripped() {
33         assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
34         assert_eq!(
35             "Vec::new",
36             path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
37         );
38         assert_eq!(
39             "Vec::new",
40             path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
41         );
42     }
43 }