mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-23 20:04:21 +00:00
Better strip turbofishes
This commit is contained in:
parent
5168ab16e1
commit
778deb38fe
5 changed files with 52 additions and 8 deletions
|
@ -91,6 +91,10 @@ pub fn path_from_segments(
|
|||
})
|
||||
}
|
||||
|
||||
pub fn path_from_text(text: &str) -> ast::Path {
|
||||
ast_from_text(&format!("fn main() {{ let test = {}; }}", text))
|
||||
}
|
||||
|
||||
pub fn glob_use_tree() -> ast::UseTree {
|
||||
ast_from_text("use *;")
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ pub mod algo;
|
|||
pub mod ast;
|
||||
#[doc(hidden)]
|
||||
pub mod fuzz;
|
||||
pub mod utils;
|
||||
|
||||
use std::{marker::PhantomData, sync::Arc};
|
||||
|
||||
|
|
43
crates/syntax/src/utils.rs
Normal file
43
crates/syntax/src/utils.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
//! A set of utils methods to reuse on other abstraction levels
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{ast, match_ast, AstNode};
|
||||
|
||||
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
|
||||
path.syntax()
|
||||
.children()
|
||||
.filter_map(|node| {
|
||||
match_ast! {
|
||||
match node {
|
||||
ast::PathSegment(it) => {
|
||||
Some(it.name_ref()?.to_string())
|
||||
},
|
||||
ast::Path(it) => {
|
||||
Some(path_to_string_stripping_turbo_fish(&it))
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
})
|
||||
.join("::")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::path_to_string_stripping_turbo_fish;
|
||||
use crate::ast::make;
|
||||
|
||||
#[test]
|
||||
fn turbofishes_are_stripped() {
|
||||
assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
|
||||
assert_eq!(
|
||||
"Vec::new",
|
||||
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
|
||||
);
|
||||
assert_eq!(
|
||||
"Vec::new",
|
||||
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue