Skip to content

Commit dba67b4

Browse files
committed
update for review
1 parent 2e87f31 commit dba67b4

File tree

10 files changed

+65
-39
lines changed

10 files changed

+65
-39
lines changed

crates/hir-ty/src/display.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ pub struct HirFormatter<'a> {
6262
fmt: &'a mut dyn HirWrite,
6363
buf: String,
6464
curr_size: usize,
65-
pub max_size: Option<usize>,
65+
pub(crate) max_size: Option<usize>,
66+
pub limited_size: Option<usize>,
6667
omit_verbose_types: bool,
6768
closure_style: ClosureStyle,
6869
display_target: DisplayTarget,
@@ -86,6 +87,7 @@ pub trait HirDisplay {
8687
&'a self,
8788
db: &'a dyn HirDatabase,
8889
max_size: Option<usize>,
90+
limited_size: Option<usize>,
8991
omit_verbose_types: bool,
9092
display_target: DisplayTarget,
9193
closure_style: ClosureStyle,
@@ -101,6 +103,7 @@ pub trait HirDisplay {
101103
db,
102104
t: self,
103105
max_size,
106+
limited_size,
104107
omit_verbose_types,
105108
display_target,
106109
closure_style,
@@ -117,6 +120,7 @@ pub trait HirDisplay {
117120
db,
118121
t: self,
119122
max_size: None,
123+
limited_size: None,
120124
omit_verbose_types: false,
121125
closure_style: ClosureStyle::ImplFn,
122126
display_target: DisplayTarget::Diagnostics,
@@ -137,6 +141,28 @@ pub trait HirDisplay {
137141
db,
138142
t: self,
139143
max_size,
144+
limited_size: None,
145+
omit_verbose_types: true,
146+
closure_style: ClosureStyle::ImplFn,
147+
display_target: DisplayTarget::Diagnostics,
148+
}
149+
}
150+
151+
/// Returns a `Display`able type that is human-readable and tries to limit the item inside this type.
152+
/// Use this for showing types which may contain two many item when user hover on, like `trait`, `struct`, `enum`
153+
fn display_limited<'a>(
154+
&'a self,
155+
db: &'a dyn HirDatabase,
156+
limited_size: Option<usize>,
157+
) -> HirDisplayWrapper<'a, Self>
158+
where
159+
Self: Sized,
160+
{
161+
HirDisplayWrapper {
162+
db,
163+
t: self,
164+
max_size: None,
165+
limited_size,
140166
omit_verbose_types: true,
141167
closure_style: ClosureStyle::ImplFn,
142168
display_target: DisplayTarget::Diagnostics,
@@ -158,6 +184,7 @@ pub trait HirDisplay {
158184
buf: String::with_capacity(20),
159185
curr_size: 0,
160186
max_size: None,
187+
limited_size: None,
161188
omit_verbose_types: false,
162189
closure_style: ClosureStyle::ImplFn,
163190
display_target: DisplayTarget::SourceCode { module_id, allow_opaque },
@@ -178,6 +205,7 @@ pub trait HirDisplay {
178205
db,
179206
t: self,
180207
max_size: None,
208+
limited_size: None,
181209
omit_verbose_types: false,
182210
closure_style: ClosureStyle::ImplFn,
183211
display_target: DisplayTarget::Test,
@@ -295,6 +323,7 @@ pub struct HirDisplayWrapper<'a, T> {
295323
db: &'a dyn HirDatabase,
296324
t: &'a T,
297325
max_size: Option<usize>,
326+
limited_size: Option<usize>,
298327
omit_verbose_types: bool,
299328
closure_style: ClosureStyle,
300329
display_target: DisplayTarget,
@@ -323,6 +352,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
323352
buf: String::with_capacity(20),
324353
curr_size: 0,
325354
max_size: self.max_size,
355+
limited_size: self.limited_size,
326356
omit_verbose_types: self.omit_verbose_types,
327357
display_target: self.display_target,
328358
closure_style: self.closure_style,

crates/hir/src/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl HirDisplay for Trait {
598598

599599
let assoc_items = self.items(f.db);
600600
let assoc_items_size = assoc_items.len();
601-
let max_display_size = f.max_size.unwrap_or(assoc_items_size);
601+
let limited_size = f.limited_size.unwrap_or(assoc_items_size);
602602
if assoc_items.is_empty() {
603603
f.write_str(" {}")?;
604604
} else {
@@ -617,7 +617,7 @@ impl HirDisplay for Trait {
617617
}
618618
};
619619
f.write_str(",\n")?;
620-
if index + 1 == max_display_size && index + 1 != assoc_items_size {
620+
if index + 1 == limited_size && index + 1 != assoc_items_size {
621621
f.write_str(" ...\n")?;
622622
break;
623623
}

crates/ide-db/src/defs.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ use crate::documentation::{Documentation, HasDocs};
2424
use crate::famous_defs::FamousDefs;
2525
use crate::RootDatabase;
2626

27-
#[derive(Default)]
28-
pub struct HoverDisplayConfig {
29-
pub trait_item_display_num: Option<usize>,
30-
// todo: add config for struct & enum
31-
}
32-
3327
// FIXME: a more precise name would probably be `Symbol`?
3428
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
3529
pub enum Definition {
@@ -219,7 +213,7 @@ impl Definition {
219213
})
220214
}
221215

222-
pub fn label(&self, db: &RootDatabase, hover_display_config: HoverDisplayConfig) -> String {
216+
pub fn label(&self, db: &RootDatabase) -> String {
223217
match *self {
224218
Definition::Macro(it) => it.display(db).to_string(),
225219
Definition::Field(it) => it.display(db).to_string(),
@@ -230,9 +224,7 @@ impl Definition {
230224
Definition::Variant(it) => it.display(db).to_string(),
231225
Definition::Const(it) => it.display(db).to_string(),
232226
Definition::Static(it) => it.display(db).to_string(),
233-
Definition::Trait(it) => {
234-
it.display_truncated(db, hover_display_config.trait_item_display_num).to_string()
235-
}
227+
Definition::Trait(it) => it.display(db).to_string(),
236228
Definition::TraitAlias(it) => it.display(db).to_string(),
237229
Definition::TypeAlias(it) => it.display(db).to_string(),
238230
Definition::BuiltinType(it) => it.name().display(db).to_string(),

crates/ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct HoverConfig {
3232
pub documentation: bool,
3333
pub keywords: bool,
3434
pub format: HoverDocFormat,
35-
pub trait_item_display_num: Option<usize>,
35+
pub trait_assoc_items_size: Option<usize>,
3636
}
3737

3838
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

crates/ide/src/hover/render.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir::{
88
};
99
use ide_db::{
1010
base_db::SourceDatabase,
11-
defs::{Definition, HoverDisplayConfig},
11+
defs::Definition,
1212
documentation::HasDocs,
1313
famous_defs::FamousDefs,
1414
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
@@ -406,8 +406,12 @@ pub(super) fn definition(
406406
config: &HoverConfig,
407407
) -> Markup {
408408
let mod_path = definition_mod_path(db, &def);
409-
let hover_config = HoverDisplayConfig { trait_item_display_num: config.trait_item_display_num };
410-
let label = def.label(db, hover_config);
409+
let label = match def {
410+
Definition::Trait(trait_) => {
411+
trait_.display_limited(db, config.trait_assoc_items_size).to_string()
412+
}
413+
_ => def.label(db),
414+
};
411415
let docs = def.docs(db, famous_defs);
412416
let value = (|| match def {
413417
Definition::Variant(it) => {

crates/ide/src/hover/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
1717
documentation: true,
1818
format: HoverDocFormat::Markdown,
1919
keywords: true,
20-
trait_item_display_num: Some(7),
20+
trait_assoc_items_size: None,
2121
};
2222

2323
fn check_hover_no_result(ra_fixture: &str) {

crates/ide/src/static_index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module, Semantics};
55
use ide_db::{
66
base_db::{FileId, FileRange, SourceDatabaseExt},
7-
defs::{Definition, HoverDisplayConfig},
7+
defs::Definition,
88
documentation::Documentation,
99
famous_defs::FamousDefs,
1010
helpers::get_definition,
@@ -166,7 +166,7 @@ impl StaticIndex<'_> {
166166
documentation: true,
167167
keywords: true,
168168
format: crate::HoverDocFormat::Markdown,
169-
trait_item_display_num: None,
169+
trait_assoc_items_size: None,
170170
};
171171
let tokens = tokens.filter(|token| {
172172
matches!(
@@ -197,7 +197,7 @@ impl StaticIndex<'_> {
197197
enclosing_moniker: current_crate
198198
.zip(def.enclosing_definition(self.db))
199199
.and_then(|(cc, enclosing_def)| def_to_moniker(self.db, enclosing_def, cc)),
200-
signature: Some(def.label(self.db, HoverDisplayConfig::default())),
200+
signature: Some(def.label(self.db)),
201201
kind: def_to_kind(self.db, def),
202202
});
203203
self.def_map.insert(def, it);

crates/rust-analyzer/src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ config_data! {
368368
/// How to render the size information in a memory layout hover.
369369
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",
370370

371+
/// How many associated items of a trait to display when hovering a trait.
372+
hover_show_traitAssocItems: Option<usize> = "null",
373+
371374
/// Whether to enforce the import granularity setting for all files. If set to false rust-analyzer will try to keep import styles consistent per file.
372375
imports_granularity_enforce: bool = "false",
373376
/// How imports should be grouped into use statements.
@@ -590,9 +593,6 @@ config_data! {
590593
/// Show documentation.
591594
signatureInfo_documentation_enable: bool = "true",
592595

593-
/// How many trait item display on hover.
594-
traitItemDisplayNum: Option<usize> = "7",
595-
596596
/// Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.
597597
typing_autoClosingAngleBrackets_enable: bool = "false",
598598

@@ -1685,7 +1685,7 @@ impl Config {
16851685
}
16861686
},
16871687
keywords: self.data.hover_documentation_keywords_enable,
1688-
trait_item_display_num: self.data.traitItemDisplayNum,
1688+
trait_assoc_items_size: self.data.hover_show_traitAssocItems,
16891689
}
16901690
}
16911691

docs/user/generated_config.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ How to render the offset information in a memory layout hover.
505505
--
506506
How to render the size information in a memory layout hover.
507507
--
508+
[[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`)::
509+
+
510+
--
511+
How many associated items of a trait to display when hovering a trait.
512+
--
508513
[[rust-analyzer.imports.granularity.enforce]]rust-analyzer.imports.granularity.enforce (default: `false`)::
509514
+
510515
--
@@ -927,11 +932,6 @@ Show full signature of the callable. Only shows parameters if disabled.
927932
--
928933
Show documentation.
929934
--
930-
[[rust-analyzer.traitItemDisplayNum]]rust-analyzer.traitItemDisplayNum (default: `7`)::
931-
+
932-
--
933-
How many trait item display on hover.
934-
--
935935
[[rust-analyzer.typing.autoClosingAngleBrackets.enable]]rust-analyzer.typing.autoClosingAngleBrackets.enable (default: `false`)::
936936
+
937937
--

editors/code/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,15 @@
11191119
}
11201120
]
11211121
},
1122+
"rust-analyzer.hover.show.traitAssocItems": {
1123+
"markdownDescription": "How many associated items of a trait to display when hovering a trait.",
1124+
"default": null,
1125+
"type": [
1126+
"null",
1127+
"integer"
1128+
],
1129+
"minimum": 0
1130+
},
11221131
"rust-analyzer.imports.granularity.enforce": {
11231132
"markdownDescription": "Whether to enforce the import granularity setting for all files. If set to false rust-analyzer will try to keep import styles consistent per file.",
11241133
"default": false,
@@ -1648,15 +1657,6 @@
16481657
"default": true,
16491658
"type": "boolean"
16501659
},
1651-
"rust-analyzer.traitItemDisplayNum": {
1652-
"markdownDescription": "How many trait item display on hover.",
1653-
"default": 7,
1654-
"type": [
1655-
"null",
1656-
"integer"
1657-
],
1658-
"minimum": 0
1659-
},
16601660
"rust-analyzer.typing.autoClosingAngleBrackets.enable": {
16611661
"markdownDescription": "Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.",
16621662
"default": false,

0 commit comments

Comments
 (0)