diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index f7e396de2d..328dea87ac 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -62,7 +62,8 @@ pub struct HirFormatter<'a> { fmt: &'a mut dyn HirWrite, buf: String, curr_size: usize, - pub max_size: Option, + pub(crate) max_size: Option, + pub limited_size: Option, omit_verbose_types: bool, closure_style: ClosureStyle, display_target: DisplayTarget, @@ -86,6 +87,7 @@ pub trait HirDisplay { &'a self, db: &'a dyn HirDatabase, max_size: Option, + limited_size: Option, omit_verbose_types: bool, display_target: DisplayTarget, closure_style: ClosureStyle, @@ -101,6 +103,7 @@ pub trait HirDisplay { db, t: self, max_size, + limited_size, omit_verbose_types, display_target, closure_style, @@ -117,6 +120,7 @@ pub trait HirDisplay { db, t: self, max_size: None, + limited_size: None, omit_verbose_types: false, closure_style: ClosureStyle::ImplFn, display_target: DisplayTarget::Diagnostics, @@ -137,6 +141,28 @@ pub trait HirDisplay { db, t: self, max_size, + limited_size: None, + omit_verbose_types: true, + closure_style: ClosureStyle::ImplFn, + display_target: DisplayTarget::Diagnostics, + } + } + + /// 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` + fn display_limited<'a>( + &'a self, + db: &'a dyn HirDatabase, + limited_size: Option, + ) -> HirDisplayWrapper<'a, Self> + where + Self: Sized, + { + HirDisplayWrapper { + db, + t: self, + max_size: None, + limited_size, omit_verbose_types: true, closure_style: ClosureStyle::ImplFn, display_target: DisplayTarget::Diagnostics, @@ -158,6 +184,7 @@ pub trait HirDisplay { buf: String::with_capacity(20), curr_size: 0, max_size: None, + limited_size: None, omit_verbose_types: false, closure_style: ClosureStyle::ImplFn, display_target: DisplayTarget::SourceCode { module_id, allow_opaque }, @@ -178,6 +205,7 @@ pub trait HirDisplay { db, t: self, max_size: None, + limited_size: None, omit_verbose_types: false, closure_style: ClosureStyle::ImplFn, display_target: DisplayTarget::Test, @@ -295,6 +323,7 @@ pub struct HirDisplayWrapper<'a, T> { db: &'a dyn HirDatabase, t: &'a T, max_size: Option, + limited_size: Option, omit_verbose_types: bool, closure_style: ClosureStyle, display_target: DisplayTarget, @@ -323,6 +352,7 @@ impl HirDisplayWrapper<'_, T> { buf: String::with_capacity(20), curr_size: 0, max_size: self.max_size, + limited_size: self.limited_size, omit_verbose_types: self.omit_verbose_types, display_target: self.display_target, closure_style: self.closure_style, diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 08e0c6e752..20ba72d998 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -598,7 +598,7 @@ impl HirDisplay for Trait { let assoc_items = self.items(f.db); let assoc_items_size = assoc_items.len(); - let max_display_size = f.max_size.unwrap_or(assoc_items_size); + let limited_size = f.limited_size.unwrap_or(assoc_items_size); if assoc_items.is_empty() { f.write_str(" {}")?; } else { @@ -617,7 +617,7 @@ impl HirDisplay for Trait { } }; f.write_str(",\n")?; - if index + 1 == max_display_size && index + 1 != assoc_items_size { + if index + 1 == limited_size && index + 1 != assoc_items_size { f.write_str(" ...\n")?; break; } diff --git a/crates/ide-db/src/defs.rs b/crates/ide-db/src/defs.rs index 35aaf86469..1b6ff8bad5 100644 --- a/crates/ide-db/src/defs.rs +++ b/crates/ide-db/src/defs.rs @@ -24,12 +24,6 @@ use crate::documentation::{Documentation, HasDocs}; use crate::famous_defs::FamousDefs; use crate::RootDatabase; -#[derive(Default)] -pub struct HoverDisplayConfig { - pub trait_item_display_num: Option, - // todo: add config for struct & enum -} - // FIXME: a more precise name would probably be `Symbol`? #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)] pub enum Definition { @@ -219,7 +213,7 @@ impl Definition { }) } - pub fn label(&self, db: &RootDatabase, hover_display_config: HoverDisplayConfig) -> String { + pub fn label(&self, db: &RootDatabase) -> String { match *self { Definition::Macro(it) => it.display(db).to_string(), Definition::Field(it) => it.display(db).to_string(), @@ -230,9 +224,7 @@ impl Definition { Definition::Variant(it) => it.display(db).to_string(), Definition::Const(it) => it.display(db).to_string(), Definition::Static(it) => it.display(db).to_string(), - Definition::Trait(it) => { - it.display_truncated(db, hover_display_config.trait_item_display_num).to_string() - } + Definition::Trait(it) => it.display(db).to_string(), Definition::TraitAlias(it) => it.display(db).to_string(), Definition::TypeAlias(it) => it.display(db).to_string(), Definition::BuiltinType(it) => it.name().display(db).to_string(), diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 64346a59b7..b6877c5e8c 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -32,7 +32,7 @@ pub struct HoverConfig { pub documentation: bool, pub keywords: bool, pub format: HoverDocFormat, - pub trait_item_display_num: Option, + pub trait_assoc_items_size: Option, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 6d7886feec..f528bd512c 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -8,7 +8,7 @@ use hir::{ }; use ide_db::{ base_db::SourceDatabase, - defs::{Definition, HoverDisplayConfig}, + defs::Definition, documentation::HasDocs, famous_defs::FamousDefs, generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES}, @@ -406,8 +406,12 @@ pub(super) fn definition( config: &HoverConfig, ) -> Markup { let mod_path = definition_mod_path(db, &def); - let hover_config = HoverDisplayConfig { trait_item_display_num: config.trait_item_display_num }; - let label = def.label(db, hover_config); + let label = match def { + Definition::Trait(trait_) => { + trait_.display_limited(db, config.trait_assoc_items_size).to_string() + } + _ => def.label(db), + }; let docs = def.docs(db, famous_defs); let value = (|| match def { Definition::Variant(it) => { diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index e2278fe4b3..70f4068478 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -17,7 +17,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig { documentation: true, format: HoverDocFormat::Markdown, keywords: true, - trait_item_display_num: Some(7), + trait_assoc_items_size: None, }; fn check_hover_no_result(ra_fixture: &str) { diff --git a/crates/ide/src/static_index.rs b/crates/ide/src/static_index.rs index 8d6e161c1b..0f19da0ac5 100644 --- a/crates/ide/src/static_index.rs +++ b/crates/ide/src/static_index.rs @@ -4,7 +4,7 @@ use hir::{db::HirDatabase, Crate, HirFileIdExt, Module, Semantics}; use ide_db::{ base_db::{FileId, FileRange, SourceDatabaseExt}, - defs::{Definition, HoverDisplayConfig}, + defs::Definition, documentation::Documentation, famous_defs::FamousDefs, helpers::get_definition, @@ -166,7 +166,7 @@ impl StaticIndex<'_> { documentation: true, keywords: true, format: crate::HoverDocFormat::Markdown, - trait_item_display_num: None, + trait_assoc_items_size: None, }; let tokens = tokens.filter(|token| { matches!( @@ -197,7 +197,7 @@ impl StaticIndex<'_> { enclosing_moniker: current_crate .zip(def.enclosing_definition(self.db)) .and_then(|(cc, enclosing_def)| def_to_moniker(self.db, enclosing_def, cc)), - signature: Some(def.label(self.db, HoverDisplayConfig::default())), + signature: Some(def.label(self.db)), kind: def_to_kind(self.db, def), }); self.def_map.insert(def, it); diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 40a2077cb1..259ee3ea8c 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -368,6 +368,9 @@ config_data! { /// How to render the size information in a memory layout hover. hover_memoryLayout_size: Option = "\"both\"", + /// How many associated items of a trait to display when hovering a trait. + hover_show_traitAssocItems: Option = "null", + /// 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. imports_granularity_enforce: bool = "false", /// How imports should be grouped into use statements. @@ -590,9 +593,6 @@ config_data! { /// Show documentation. signatureInfo_documentation_enable: bool = "true", - /// How many trait item display on hover. - traitItemDisplayNum: Option = "7", - /// Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list. typing_autoClosingAngleBrackets_enable: bool = "false", @@ -1685,7 +1685,7 @@ impl Config { } }, keywords: self.data.hover_documentation_keywords_enable, - trait_item_display_num: self.data.traitItemDisplayNum, + trait_assoc_items_size: self.data.hover_show_traitAssocItems, } } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 7d005828b8..7b292fc78f 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -505,6 +505,11 @@ How to render the offset information in a memory layout hover. -- How to render the size information in a memory layout hover. -- +[[rust-analyzer.hover.show.traitAssocItems]]rust-analyzer.hover.show.traitAssocItems (default: `null`):: ++ +-- +How many associated items of a trait to display when hovering a trait. +-- [[rust-analyzer.imports.granularity.enforce]]rust-analyzer.imports.granularity.enforce (default: `false`):: + -- @@ -927,11 +932,6 @@ Show full signature of the callable. Only shows parameters if disabled. -- Show documentation. -- -[[rust-analyzer.traitItemDisplayNum]]rust-analyzer.traitItemDisplayNum (default: `7`):: -+ --- -How many trait item display on hover. --- [[rust-analyzer.typing.autoClosingAngleBrackets.enable]]rust-analyzer.typing.autoClosingAngleBrackets.enable (default: `false`):: + -- diff --git a/editors/code/package.json b/editors/code/package.json index b68ab66cda..719521a224 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -1119,6 +1119,15 @@ } ] }, + "rust-analyzer.hover.show.traitAssocItems": { + "markdownDescription": "How many associated items of a trait to display when hovering a trait.", + "default": null, + "type": [ + "null", + "integer" + ], + "minimum": 0 + }, "rust-analyzer.imports.granularity.enforce": { "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.", "default": false, @@ -1648,15 +1657,6 @@ "default": true, "type": "boolean" }, - "rust-analyzer.traitItemDisplayNum": { - "markdownDescription": "How many trait item display on hover.", - "default": 7, - "type": [ - "null", - "integer" - ], - "minimum": 0 - }, "rust-analyzer.typing.autoClosingAngleBrackets.enable": { "markdownDescription": "Whether to insert closing angle brackets when typing an opening angle bracket of a generic argument list.", "default": false,