update for review

This commit is contained in:
Young-Flash 2024-03-02 10:01:04 +08:00
parent 2e87f31fe9
commit dba67b46a1
10 changed files with 65 additions and 39 deletions

View file

@ -62,7 +62,8 @@ pub struct HirFormatter<'a> {
fmt: &'a mut dyn HirWrite,
buf: String,
curr_size: usize,
pub max_size: Option<usize>,
pub(crate) max_size: Option<usize>,
pub limited_size: Option<usize>,
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<usize>,
limited_size: Option<usize>,
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<usize>,
) -> 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<usize>,
limited_size: Option<usize>,
omit_verbose_types: bool,
closure_style: ClosureStyle,
display_target: DisplayTarget,
@ -323,6 +352,7 @@ impl<T: HirDisplay> 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,

View file

@ -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;
}

View file

@ -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<usize>,
// 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(),

View file

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

View file

@ -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) => {

View file

@ -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) {

View file

@ -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);

View file

@ -368,6 +368,9 @@ config_data! {
/// How to render the size information in a memory layout hover.
hover_memoryLayout_size: Option<MemoryLayoutHoverRenderKindDef> = "\"both\"",
/// How many associated items of a trait to display when hovering a trait.
hover_show_traitAssocItems: Option<usize> = "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<usize> = "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,
}
}

View file

@ -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`)::
+
--

View file

@ -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,