From 4d75430e912491c19fb1a7b1a95ee812f6a8a124 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 31 Dec 2019 16:17:08 +0100 Subject: Qualify some paths in 'add missing impl members' --- crates/ra_hir_def/src/path.rs | 44 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/path.rs') 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 @@ //! A desugared representation of paths like `crate::foo` or `::bar`. mod lower; -use std::{iter, sync::Arc}; +use std::{fmt::Display, iter, sync::Arc}; use hir_expand::{ hygiene::Hygiene, @@ -78,6 +78,12 @@ impl ModPath { } self.segments.first() } + + pub fn to_ast(&self) -> ast::Path { + use ast::AstNode; + let parse = ast::SourceFile::parse(&self.to_string()); + parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap() + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -248,6 +254,42 @@ impl From for ModPath { } } +impl Display for ModPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut first_segment = true; + let mut add_segment = |s| { + if !first_segment { + f.write_str("::")?; + } + first_segment = false; + f.write_str(s)?; + Ok(()) + }; + match self.kind { + PathKind::Plain => {} + PathKind::Super(n) => { + if n == 0 { + add_segment("self")?; + } + for _ in 0..n { + add_segment("super")?; + } + } + PathKind::Crate => add_segment("crate")?, + PathKind::Abs => add_segment("")?, + PathKind::DollarCrate(_) => add_segment("$crate")?, + } + for segment in &self.segments { + if !first_segment { + f.write_str("::")?; + } + first_segment = false; + write!(f, "{}", segment)?; + } + Ok(()) + } +} + pub use hir_expand::name as __name; #[macro_export] -- cgit v1.2.3 From 4496e2a06a91e5825f355ce730af802643e8018a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 10 Jan 2020 18:40:45 +0100 Subject: Apply review suggestions --- crates/ra_hir_def/src/path.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def/src/path.rs') diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 7dd1939b9..9f93a5424 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -1,7 +1,11 @@ //! A desugared representation of paths like `crate::foo` or `::bar`. mod lower; -use std::{fmt::Display, iter, sync::Arc}; +use std::{ + fmt::{self, Display}, + iter, + sync::Arc, +}; use hir_expand::{ hygiene::Hygiene, @@ -78,12 +82,6 @@ impl ModPath { } self.segments.first() } - - pub fn to_ast(&self) -> ast::Path { - use ast::AstNode; - let parse = ast::SourceFile::parse(&self.to_string()); - parse.tree().syntax().descendants().find_map(ast::Path::cast).unwrap() - } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -255,7 +253,7 @@ impl From for ModPath { } impl Display for ModPath { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut first_segment = true; let mut add_segment = |s| { if !first_segment { -- cgit v1.2.3