From 778deb38fe7e1bac8833934224d26f44eb80a6cc Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 8 Mar 2021 14:59:54 +0200 Subject: Better strip turbofishes --- crates/syntax/src/ast/make.rs | 4 ++++ crates/syntax/src/lib.rs | 1 + crates/syntax/src/utils.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 crates/syntax/src/utils.rs (limited to 'crates/syntax') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index b6c5de658..70ba8adb4 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -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 *;") } diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs index 11294c5b2..09e212e8c 100644 --- a/crates/syntax/src/lib.rs +++ b/crates/syntax/src/lib.rs @@ -37,6 +37,7 @@ pub mod algo; pub mod ast; #[doc(hidden)] pub mod fuzz; +pub mod utils; use std::{marker::PhantomData, sync::Arc}; diff --git a/crates/syntax/src/utils.rs b/crates/syntax/src/utils.rs new file mode 100644 index 000000000..f4c02518b --- /dev/null +++ b/crates/syntax/src/utils.rs @@ -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::")),); + assert_eq!( + "Vec::new", + path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::::new")), + ); + assert_eq!( + "Vec::new", + path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")), + ); + } +} -- cgit v1.2.3 From 867fdf8f03a25862c122614688c38f5e26e08e1f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 9 Mar 2021 14:54:50 +0300 Subject: Improve compilation speed --- crates/syntax/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/syntax') diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index c39095def..33bde099b 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -25,10 +25,10 @@ serde = { version = "1.0.106", features = ["derive"] } stdx = { path = "../stdx", version = "0.0.0" } text_edit = { path = "../text_edit", version = "0.0.0" } parser = { path = "../parser", version = "0.0.0" } -test_utils = { path = "../test_utils", version = "0.0.0" } profile = { path = "../profile", version = "0.0.0" } [dev-dependencies] +test_utils = { path = "../test_utils" } walkdir = "2.3.1" rayon = "1" expect-test = "1.1" -- cgit v1.2.3