Unnamed repository; edit this file 'description' to name the repository.
add mod_path_to_ast_with_factory variant
bit-aloo 4 weeks ago
parent ac4e8e3 · commit f444b0a
-rw-r--r--crates/ide-db/src/helpers.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/crates/ide-db/src/helpers.rs b/crates/ide-db/src/helpers.rs
index 08cf1eeed3..838aac2283 100644
--- a/crates/ide-db/src/helpers.rs
+++ b/crates/ide-db/src/helpers.rs
@@ -7,7 +7,7 @@ use hir::{Crate, ItemInNs, ModuleDef, Name, Semantics};
use span::{Edition, FileId};
use syntax::{
AstToken, SyntaxKind, SyntaxToken, ToSmolStr, TokenAtOffset,
- ast::{self, make},
+ ast::{self, make, syntax_factory::SyntaxFactory},
};
use crate::{
@@ -57,6 +57,31 @@ pub fn mod_path_to_ast(path: &hir::ModPath, edition: Edition) -> ast::Path {
make::path_from_segments(segments, is_abs)
}
+pub fn mod_path_to_ast_with_factory(
+ make: &SyntaxFactory,
+ path: &hir::ModPath,
+ edition: Edition,
+) -> ast::Path {
+ let _p = tracing::info_span!("mod_path_to_ast").entered();
+
+ let mut segments = Vec::new();
+ let mut is_abs = false;
+ match path.kind {
+ hir::PathKind::Plain => {}
+ hir::PathKind::SELF => segments.push(make.path_segment_self()),
+ hir::PathKind::Super(n) => segments.extend((0..n).map(|_| make.path_segment_super())),
+ hir::PathKind::DollarCrate(_) | hir::PathKind::Crate => {
+ segments.push(make.path_segment_crate())
+ }
+ hir::PathKind::Abs => is_abs = true,
+ }
+
+ segments.extend(path.segments().iter().map(|segment| {
+ make.path_segment(make.name_ref(&segment.display_no_db(edition).to_smolstr()))
+ }));
+ make.path_from_segments(segments, is_abs)
+}
+
/// Iterates all `ModuleDef`s and `Impl` blocks of the given file.
pub fn visit_file_defs(
sema: &Semantics<'_, RootDatabase>,