diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 44 | ||||
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 5 |
2 files changed, 48 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 82cfa67a9..7dd1939b9 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`. | 1 | //! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`. |
2 | mod lower; | 2 | mod lower; |
3 | 3 | ||
4 | use std::{iter, sync::Arc}; | 4 | use std::{fmt::Display, iter, sync::Arc}; |
5 | 5 | ||
6 | use hir_expand::{ | 6 | use hir_expand::{ |
7 | hygiene::Hygiene, | 7 | hygiene::Hygiene, |
@@ -78,6 +78,12 @@ impl ModPath { | |||
78 | } | 78 | } |
79 | self.segments.first() | 79 | self.segments.first() |
80 | } | 80 | } |
81 | |||
82 | pub fn to_ast(&self) -> ast::Path { | ||
83 | use ast::AstNode; | ||
84 | let parse = ast::SourceFile::parse(&self.to_string()); | ||
85 | parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap() | ||
86 | } | ||
81 | } | 87 | } |
82 | 88 | ||
83 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 89 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -248,6 +254,42 @@ impl From<Name> for ModPath { | |||
248 | } | 254 | } |
249 | } | 255 | } |
250 | 256 | ||
257 | impl Display for ModPath { | ||
258 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
259 | let mut first_segment = true; | ||
260 | let mut add_segment = |s| { | ||
261 | if !first_segment { | ||
262 | f.write_str("::")?; | ||
263 | } | ||
264 | first_segment = false; | ||
265 | f.write_str(s)?; | ||
266 | Ok(()) | ||
267 | }; | ||
268 | match self.kind { | ||
269 | PathKind::Plain => {} | ||
270 | PathKind::Super(n) => { | ||
271 | if n == 0 { | ||
272 | add_segment("self")?; | ||
273 | } | ||
274 | for _ in 0..n { | ||
275 | add_segment("super")?; | ||
276 | } | ||
277 | } | ||
278 | PathKind::Crate => add_segment("crate")?, | ||
279 | PathKind::Abs => add_segment("")?, | ||
280 | PathKind::DollarCrate(_) => add_segment("$crate")?, | ||
281 | } | ||
282 | for segment in &self.segments { | ||
283 | if !first_segment { | ||
284 | f.write_str("::")?; | ||
285 | } | ||
286 | first_segment = false; | ||
287 | write!(f, "{}", segment)?; | ||
288 | } | ||
289 | Ok(()) | ||
290 | } | ||
291 | } | ||
292 | |||
251 | pub use hir_expand::name as __name; | 293 | pub use hir_expand::name as __name; |
252 | 294 | ||
253 | #[macro_export] | 295 | #[macro_export] |
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 5d16dd087..40d0cb588 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -411,6 +411,11 @@ impl Resolver { | |||
411 | }) | 411 | }) |
412 | } | 412 | } |
413 | 413 | ||
414 | pub fn module_id(&self) -> Option<ModuleId> { | ||
415 | let (def_map, local_id) = self.module()?; | ||
416 | Some(ModuleId { krate: def_map.krate, local_id }) | ||
417 | } | ||
418 | |||
414 | pub fn krate(&self) -> Option<CrateId> { | 419 | pub fn krate(&self) -> Option<CrateId> { |
415 | self.module().map(|t| t.0.krate) | 420 | self.module().map(|t| t.0.krate) |
416 | } | 421 | } |