internal: Adjust a few things for trait assoc item hovers

This commit is contained in:
Lukas Wirth 2024-03-05 11:06:36 +01:00
parent ce3216e0ae
commit b20e467373
7 changed files with 397 additions and 324 deletions

View File

@ -63,7 +63,7 @@ pub struct HirFormatter<'a> {
buf: String,
curr_size: usize,
pub(crate) max_size: Option<usize>,
pub limited_size: Option<usize>,
pub entity_limit: Option<usize>,
omit_verbose_types: bool,
closure_style: ClosureStyle,
display_target: DisplayTarget,
@ -148,8 +148,8 @@ fn display_truncated<'a>(
}
}
/// Returns a `Display`able type that is human-readable and tries to limit the item inside this type.
/// Use this for showing types which may contain two many item when user hover on, like `trait`, `struct`, `enum`
/// Returns a `Display`able type that is human-readable and tries to limit the number of items inside.
/// Use this for showing definitions which may contain too many items, like `trait`, `struct`, `enum`
fn display_limited<'a>(
&'a self,
db: &'a dyn HirDatabase,
@ -184,7 +184,7 @@ fn display_source_code<'a>(
buf: String::with_capacity(20),
curr_size: 0,
max_size: None,
limited_size: None,
entity_limit: None,
omit_verbose_types: false,
closure_style: ClosureStyle::ImplFn,
display_target: DisplayTarget::SourceCode { module_id, allow_opaque },
@ -352,7 +352,7 @@ pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
buf: String::with_capacity(20),
curr_size: 0,
max_size: self.max_size,
limited_size: self.limited_size,
entity_limit: self.limited_size,
omit_verbose_types: self.omit_verbose_types,
display_target: self.display_target,
closure_style: self.closure_style,

View File

@ -596,34 +596,33 @@ fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
write_generic_params(def_id, f)?;
write_where_clause(def_id, f)?;
if let Some(limit) = f.entity_limit {
let assoc_items = self.items(f.db);
let assoc_items_size = assoc_items.len();
let limited_size = f.limited_size.unwrap_or(assoc_items_size);
let count = assoc_items.len().min(limit);
if count == 0 {
if assoc_items.is_empty() {
f.write_str(" {}")?;
} else {
f.write_str(" { /* … */ }")?;
}
} else {
f.write_str(" {\n")?;
for (index, item) in assoc_items.iter().enumerate() {
for item in &assoc_items[..count] {
f.write_str(" ")?;
match item {
AssocItem::Function(func) => {
func.hir_fmt(f)?;
}
AssocItem::Const(cst) => {
cst.hir_fmt(f)?;
}
AssocItem::TypeAlias(type_alias) => {
type_alias.hir_fmt(f)?;
}
};
f.write_str(",\n")?;
if index + 1 == limited_size && index + 1 != assoc_items_size {
f.write_str(" ...\n")?;
break;
AssocItem::Function(func) => func.hir_fmt(f),
AssocItem::Const(cst) => cst.hir_fmt(f),
AssocItem::TypeAlias(type_alias) => type_alias.hir_fmt(f),
}?;
f.write_str(";\n")?;
}
if assoc_items.len() > count {
f.write_str(" /* … */\n")?;
}
f.write_str("}")?;
}
}
Ok(())
}

View File

@ -32,7 +32,7 @@ pub struct HoverConfig {
pub documentation: bool,
pub keywords: bool,
pub format: HoverDocFormat,
pub trait_assoc_items_size: Option<usize>,
pub max_trait_assoc_items_count: Option<usize>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

View File

@ -408,7 +408,7 @@ pub(super) fn definition(
let mod_path = definition_mod_path(db, &def);
let label = match def {
Definition::Trait(trait_) => {
trait_.display_limited(db, config.trait_assoc_items_size).to_string()
trait_.display_limited(db, config.max_trait_assoc_items_count).to_string()
}
_ => def.label(db),
};

View File

@ -17,7 +17,7 @@
documentation: true,
format: HoverDocFormat::Markdown,
keywords: true,
trait_assoc_items_size: None,
max_trait_assoc_items_count: None,
};
fn check_hover_no_result(ra_fixture: &str) {
@ -49,6 +49,28 @@ fn check(ra_fixture: &str, expect: Expect) {
expect.assert_eq(&actual)
}
#[track_caller]
fn check_assoc_count(count: usize, ra_fixture: &str, expect: Expect) {
let (analysis, position) = fixture::position(ra_fixture);
let hover = analysis
.hover(
&HoverConfig {
links_in_hover: true,
max_trait_assoc_items_count: Some(count),
..HOVER_BASE_CONFIG
},
FileRange { file_id: position.file_id, range: TextRange::empty(position.offset) },
)
.unwrap()
.unwrap();
let content = analysis.db.file_text(position.file_id);
let hovered_element = &content[hover.range];
let actual = format!("*{hovered_element}*\n{}\n", hover.info.markup);
expect.assert_eq(&actual)
}
fn check_hover_no_links(ra_fixture: &str, expect: Expect) {
let (analysis, position) = fixture::position(ra_fixture);
let hover = analysis
@ -412,7 +434,7 @@ fn main() {
name: "FnOnce",
kind: Trait,
container_name: "function",
description: "pub trait FnOnce<Args>\nwhere\n Args: Tuple, {\n pub type Output,\n pub extern \"rust-call\" fn call_once(self, args: Args) -> Self::Output,\n}",
description: "pub trait FnOnce<Args>\nwhere\n Args: Tuple,",
},
},
],
@ -2686,7 +2708,7 @@ fn foo() -> impl Foo {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -2720,7 +2742,7 @@ fn foo() -> impl Foo<S> {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T> {}",
description: "trait Foo<T>",
},
},
HoverGotoTypeData {
@ -2767,7 +2789,7 @@ fn foo() -> impl Foo + Bar {}
focus_range: 19..22,
name: "Bar",
kind: Trait,
description: "trait Bar {}",
description: "trait Bar",
},
},
HoverGotoTypeData {
@ -2780,7 +2802,7 @@ fn foo() -> impl Foo + Bar {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -2817,7 +2839,7 @@ fn foo() -> impl Foo<S1> + Bar<S2> {}
focus_range: 22..25,
name: "Bar",
kind: Trait,
description: "trait Bar<T> {}",
description: "trait Bar<T>",
},
},
HoverGotoTypeData {
@ -2830,7 +2852,7 @@ fn foo() -> impl Foo<S1> + Bar<S2> {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T> {}",
description: "trait Foo<T>",
},
},
HoverGotoTypeData {
@ -2887,7 +2909,7 @@ fn foo(ar$0g: &impl Foo) {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -2921,7 +2943,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
focus_range: 19..22,
name: "Bar",
kind: Trait,
description: "trait Bar<T> {}",
description: "trait Bar<T>",
},
},
HoverGotoTypeData {
@ -2934,7 +2956,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
HoverGotoTypeData {
@ -2989,7 +3011,7 @@ pub trait Future {}
name: "Future",
kind: Trait,
container_name: "future",
description: "pub trait Future {}",
description: "pub trait Future",
},
},
HoverGotoTypeData {
@ -3034,7 +3056,7 @@ fn foo(ar$0g: &impl Foo<S>) {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T> {}",
description: "trait Foo<T>",
},
},
HoverGotoTypeData {
@ -3097,7 +3119,7 @@ fn foo() -> B<dyn Foo> {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -3128,7 +3150,7 @@ fn foo(ar$0g: &dyn Foo) {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -3160,7 +3182,7 @@ fn foo(ar$0g: &dyn Foo<S>) {}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo<T> {}",
description: "trait Foo<T>",
},
},
HoverGotoTypeData {
@ -3221,7 +3243,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
focus_range: 28..36,
name: "DynTrait",
kind: Trait,
description: "trait DynTrait<T> {}",
description: "trait DynTrait<T>",
},
},
HoverGotoTypeData {
@ -3234,7 +3256,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
focus_range: 6..15,
name: "ImplTrait",
kind: Trait,
description: "trait ImplTrait<T> {}",
description: "trait ImplTrait<T>",
},
},
HoverGotoTypeData {
@ -3289,7 +3311,7 @@ fn test() -> impl Foo { S {} }
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {\n type Item,\n fn get(self) -> Self::Item,\n}",
description: "trait Foo",
},
},
],
@ -3354,7 +3376,7 @@ fn foo<T: Foo>(t: T$0){}
focus_range: 6..9,
name: "Foo",
kind: Trait,
description: "trait Foo {}",
description: "trait Foo",
},
},
],
@ -6330,7 +6352,8 @@ fn func()
#[test]
fn hover_trait_show_assoc_items() {
check(
check_assoc_count(
0,
r#"
trait T {}
impl T$0 for () {}
@ -6348,11 +6371,10 @@ trait T {}
"#]],
);
check(
check_assoc_count(
1,
r#"
trait T {
fn func() {}
}
trait T {}
impl T$0 for () {}
"#,
expect![[r#"
@ -6363,18 +6385,18 @@ impl T$0 for () {}
```
```rust
trait T {
fn func(),
}
trait T {}
```
"#]],
);
check(
check_assoc_count(
0,
r#"
trait T {
fn func() {}
const FLAG: i32 = 34;
type Bar;
}
impl T$0 for () {}
"#,
@ -6386,15 +6408,13 @@ impl T$0 for () {}
```
```rust
trait T {
fn func(),
const FLAG: i32,
}
trait T { /**/ }
```
"#]],
);
check(
check_assoc_count(
2,
r#"
trait T {
fn func() {}
@ -6412,9 +6432,63 @@ impl T$0 for () {}
```rust
trait T {
fn func(),
const FLAG: i32,
type Bar,
fn func();
const FLAG: i32;
/**/
}
```
"#]],
);
check_assoc_count(
3,
r#"
trait T {
fn func() {}
const FLAG: i32 = 34;
type Bar;
}
impl T$0 for () {}
"#,
expect![[r#"
*T*
```rust
test
```
```rust
trait T {
fn func();
const FLAG: i32;
type Bar;
}
```
"#]],
);
check_assoc_count(
4,
r#"
trait T {
fn func() {}
const FLAG: i32 = 34;
type Bar;
}
impl T$0 for () {}
"#,
expect![[r#"
*T*
```rust
test
```
```rust
trait T {
fn func();
const FLAG: i32;
type Bar;
}
```
"#]],
@ -7532,7 +7606,7 @@ impl Iterator for S {
name: "Future",
kind: Trait,
container_name: "future",
description: "pub trait Future {\n pub type Output,\n pub fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>,\n}",
description: "pub trait Future",
},
},
HoverGotoTypeData {
@ -7546,7 +7620,7 @@ impl Iterator for S {
name: "Iterator",
kind: Trait,
container_name: "iterator",
description: "pub trait Iterator {\n pub type Item,\n pub fn next(&mut self) -> Option<Self::Item>,\n pub fn nth(&mut self, n: usize) -> Option<Self::Item>,\n pub fn by_ref(&mut self) -> &mut Self\nwhere\n Self: Sized,,\n}",
description: "pub trait Iterator",
},
},
HoverGotoTypeData {
@ -7559,7 +7633,7 @@ impl Iterator for S {
focus_range: 49..56,
name: "Notable",
kind: Trait,
description: "trait Notable {}",
description: "trait Notable",
},
},
HoverGotoTypeData {

View File

@ -166,7 +166,7 @@ fn add_file(&mut self, file_id: FileId) {
documentation: true,
keywords: true,
format: crate::HoverDocFormat::Markdown,
trait_assoc_items_size: None,
max_trait_assoc_items_count: None,
};
let tokens = tokens.filter(|token| {
matches!(

View File

@ -1682,7 +1682,7 @@ pub fn hover(&self) -> HoverConfig {
}
},
keywords: self.data.hover_documentation_keywords_enable,
trait_assoc_items_size: self.data.hover_show_traitAssocItems,
max_trait_assoc_items_count: self.data.hover_show_traitAssocItems,
}
}