From 82ae228d9864cfb47e349a702053ba189d776966 Mon Sep 17 00:00:00 2001 From: Dezhi Wu Date: Tue, 7 Sep 2021 14:44:30 +0800 Subject: [PATCH 1/4] fix: path display error when start with `crate` --- crates/hir_ty/src/display.rs | 2 +- crates/ide/src/hover.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index bce24b825c..6281e3c2a5 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1154,7 +1154,7 @@ impl HirDisplay for Path { } for (seg_idx, segment) in self.segments().iter().enumerate() { - if seg_idx != 0 { + if seg_idx != 0 || matches!(self.kind(), PathKind::Crate) { write!(f, "::")?; } write!(f, "{}", segment.name)?; diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 2a505c621f..3fb1956d80 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -962,6 +962,25 @@ fn main() { let foo_test = fo$0o(); } ``` "#]], ); + + // use literal `crate` in path + check(r#" +pub struct X; + +fn foo() -> crate::X { X } + +fn main() { f$0oo(); } + "#, expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + fn foo() -> crate::X + ``` + "#]]); } #[test] From 6d2154e40916c7a20b8601048f7e4da947f81ef6 Mon Sep 17 00:00:00 2001 From: Dezhi Wu Date: Tue, 7 Sep 2021 14:50:33 +0800 Subject: [PATCH 2/4] cargo fmt --- crates/ide/src/hover.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 3fb1956d80..f9000ca000 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -964,13 +964,15 @@ fn main() { let foo_test = fo$0o(); } ); // use literal `crate` in path - check(r#" + check( + r#" pub struct X; fn foo() -> crate::X { X } fn main() { f$0oo(); } - "#, expect![[r#" + "#, + expect![[r#" *foo* ```rust @@ -980,7 +982,8 @@ fn main() { f$0oo(); } ```rust fn foo() -> crate::X ``` - "#]]); + "#]], + ); } #[test] From 880af425d40764a743d06b3bbd48661ace78b6c8 Mon Sep 17 00:00:00 2001 From: Dezhi Wu Date: Tue, 7 Sep 2021 16:54:02 +0800 Subject: [PATCH 3/4] fix path wrong display --- crates/hir_ty/src/display.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 6281e3c2a5..e394b72d5d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1141,7 +1141,7 @@ impl HirDisplay for Path { write!(f, ">")?; } (_, PathKind::Plain) => {} - (_, PathKind::Abs) => write!(f, "::")?, + (_, PathKind::Abs) => write!(f, "")?, (_, PathKind::Crate) => write!(f, "crate")?, (_, PathKind::Super(0)) => write!(f, "self")?, (_, PathKind::Super(n)) => { @@ -1154,7 +1154,7 @@ impl HirDisplay for Path { } for (seg_idx, segment) in self.segments().iter().enumerate() { - if seg_idx != 0 || matches!(self.kind(), PathKind::Crate) { + if !matches!(self.kind(), PathKind::Plain) || seg_idx > 0 { write!(f, "::")?; } write!(f, "{}", segment.name)?; From 87436a08fa63b7e5d0d6010d8c76379aa5ee16fd Mon Sep 17 00:00:00 2001 From: Dezhi Wu Date: Tue, 7 Sep 2021 17:49:46 +0800 Subject: [PATCH 4/4] fix `super` path wrong display --- crates/hir_ty/src/display.rs | 10 ++++++---- crates/ide/src/hover.rs | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index e394b72d5d..1ae718a36b 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1141,13 +1141,15 @@ impl HirDisplay for Path { write!(f, ">")?; } (_, PathKind::Plain) => {} - (_, PathKind::Abs) => write!(f, "")?, + (_, PathKind::Abs) => {} (_, PathKind::Crate) => write!(f, "crate")?, (_, PathKind::Super(0)) => write!(f, "self")?, (_, PathKind::Super(n)) => { - write!(f, "super")?; - for _ in 0..*n { - write!(f, "::super")?; + for i in 0..*n { + if i > 0 { + write!(f, "::")?; + } + write!(f, "super")?; } } (_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?, diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index f9000ca000..62a322f976 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -963,7 +963,7 @@ fn main() { let foo_test = fo$0o(); } "#]], ); - // use literal `crate` in path + // Use literal `crate` in path check( r#" pub struct X; @@ -984,6 +984,28 @@ fn main() { f$0oo(); } ``` "#]], ); + + // Check `super` in path + check( + r#" +pub struct X; + +mod m { pub fn foo() -> super::X { super::X } } + +fn main() { m::f$0oo(); } + "#, + expect![[r#" + *foo* + + ```rust + test::m + ``` + + ```rust + pub fn foo() -> super::X + ``` + "#]], + ); } #[test]