]> git.proxmox.com Git - rustc.git/blame - src/tools/rust-analyzer/crates/syntax/src/utils.rs
New upstream version 1.69.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / syntax / src / utils.rs
CommitLineData
064997fb
FG
1//! A set of utils methods to reuse on other abstraction levels
2
3use itertools::Itertools;
4
9ffffee4 5use crate::{ast, match_ast, AstNode, SyntaxKind};
064997fb
FG
6
7pub 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
9ffffee4
FG
26pub fn is_raw_identifier(name: &str) -> bool {
27 let is_keyword = SyntaxKind::from_keyword(name).is_some();
28 is_keyword && !matches!(name, "self" | "crate" | "super" | "Self")
29}
30
064997fb
FG
31#[cfg(test)]
32mod tests {
33 use super::path_to_string_stripping_turbo_fish;
34 use crate::ast::make;
35
36 #[test]
37 fn turbofishes_are_stripped() {
38 assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
39 assert_eq!(
40 "Vec::new",
41 path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
42 );
43 assert_eq!(
44 "Vec::new",
45 path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
46 );
47 }
48}