diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_def/src/path.rs | 4 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/import_assets.rs | 8 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 4 | ||||
-rw-r--r-- | crates/syntax/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/syntax/src/utils.rs | 43 |
5 files changed, 52 insertions, 8 deletions
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 1dc085199..0e60dc2b6 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -44,10 +44,6 @@ pub enum ImportAlias { | |||
44 | } | 44 | } |
45 | 45 | ||
46 | impl ModPath { | 46 | impl ModPath { |
47 | pub fn from_src_unhygienic(path: ast::Path) -> Option<ModPath> { | ||
48 | lower::lower_path(path, &Hygiene::new_unhygienic()).map(|it| it.mod_path) | ||
49 | } | ||
50 | |||
51 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { | 47 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { |
52 | lower::lower_path(path, hygiene).map(|it| it.mod_path) | 48 | lower::lower_path(path, hygiene).map(|it| it.mod_path) |
53 | } | 49 | } |
diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 3d9df463d..e03ccd351 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs | |||
@@ -5,7 +5,7 @@ use hir::{ | |||
5 | }; | 5 | }; |
6 | use itertools::Itertools; | 6 | use itertools::Itertools; |
7 | use rustc_hash::FxHashSet; | 7 | use rustc_hash::FxHashSet; |
8 | use syntax::{ast, AstNode, SyntaxNode}; | 8 | use syntax::{ast, utils::path_to_string_stripping_turbo_fish, AstNode, SyntaxNode}; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT}, | 11 | items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT}, |
@@ -57,7 +57,7 @@ pub struct PathImportCandidate { | |||
57 | #[derive(Debug)] | 57 | #[derive(Debug)] |
58 | pub struct FirstSegmentUnresolved { | 58 | pub struct FirstSegmentUnresolved { |
59 | fist_segment: ast::NameRef, | 59 | fist_segment: ast::NameRef, |
60 | full_qualifier: ModPath, | 60 | full_qualifier: ast::Path, |
61 | } | 61 | } |
62 | 62 | ||
63 | /// A name that will be used during item lookups. | 63 | /// A name that will be used during item lookups. |
@@ -310,7 +310,7 @@ fn path_applicable_imports( | |||
310 | } | 310 | } |
311 | Some(first_segment_unresolved) => ( | 311 | Some(first_segment_unresolved) => ( |
312 | first_segment_unresolved.fist_segment.to_string(), | 312 | first_segment_unresolved.fist_segment.to_string(), |
313 | first_segment_unresolved.full_qualifier.to_string(), | 313 | path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier), |
314 | ), | 314 | ), |
315 | }; | 315 | }; |
316 | 316 | ||
@@ -583,7 +583,7 @@ fn path_import_candidate( | |||
583 | ImportCandidate::Path(PathImportCandidate { | 583 | ImportCandidate::Path(PathImportCandidate { |
584 | qualifier: Some(FirstSegmentUnresolved { | 584 | qualifier: Some(FirstSegmentUnresolved { |
585 | fist_segment: qualifier_start, | 585 | fist_segment: qualifier_start, |
586 | full_qualifier: ModPath::from_src_unhygienic(qualifier)?, | 586 | full_qualifier: qualifier, |
587 | }), | 587 | }), |
588 | name, | 588 | name, |
589 | }) | 589 | }) |
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( | |||
91 | }) | 91 | }) |
92 | } | 92 | } |
93 | 93 | ||
94 | pub fn path_from_text(text: &str) -> ast::Path { | ||
95 | ast_from_text(&format!("fn main() {{ let test = {}; }}", text)) | ||
96 | } | ||
97 | |||
94 | pub fn glob_use_tree() -> ast::UseTree { | 98 | pub fn glob_use_tree() -> ast::UseTree { |
95 | ast_from_text("use *;") | 99 | ast_from_text("use *;") |
96 | } | 100 | } |
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; | |||
37 | pub mod ast; | 37 | pub mod ast; |
38 | #[doc(hidden)] | 38 | #[doc(hidden)] |
39 | pub mod fuzz; | 39 | pub mod fuzz; |
40 | pub mod utils; | ||
40 | 41 | ||
41 | use std::{marker::PhantomData, sync::Arc}; | 42 | use std::{marker::PhantomData, sync::Arc}; |
42 | 43 | ||
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 @@ | |||
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 | } | ||