add HoverDisplayConfig

This commit is contained in:
Young-Flash 2024-02-22 17:37:40 +08:00
parent 35ec5955eb
commit 2e87f31fe9
9 changed files with 29 additions and 18 deletions

View File

@ -17,7 +17,10 @@ use hir_ty::{
};
use crate::{
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl, Field, Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module, SelfParam, Static, Struct, Trait, TraitAlias, TupleField, TyBuilder, Type, TypeAlias, TypeOrConstParam, TypeParam, Union, Variant
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Enum, ExternCrateDecl,
Field, Function, GenericParam, HasCrate, HasVisibility, LifetimeParam, Macro, Module,
SelfParam, Static, Struct, Trait, TraitAlias, TupleField, TyBuilder, Type, TypeAlias,
TypeOrConstParam, TypeParam, Union, Variant,
};
impl HirDisplay for Function {
@ -593,9 +596,9 @@ impl HirDisplay for Trait {
write_generic_params(def_id, f)?;
write_where_clause(def_id, f)?;
let mut display_size = 0;
let max_display_size = f.max_size.unwrap_or(7);
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);
if assoc_items.is_empty() {
f.write_str(" {}")?;
} else {
@ -614,8 +617,7 @@ impl HirDisplay for Trait {
}
};
f.write_str(",\n")?;
display_size += 1;
if display_size == max_display_size && index != assoc_items.len() - 1{
if index + 1 == max_display_size && index + 1 != assoc_items_size {
f.write_str(" ...\n")?;
break;
}

View File

@ -24,6 +24,12 @@ 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 {
@ -213,7 +219,7 @@ impl Definition {
})
}
pub fn label(&self, db: &RootDatabase, max_size: Option<usize>) -> String {
pub fn label(&self, db: &RootDatabase, hover_display_config: HoverDisplayConfig) -> String {
match *self {
Definition::Macro(it) => it.display(db).to_string(),
Definition::Field(it) => it.display(db).to_string(),
@ -224,7 +230,9 @@ 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, max_size).to_string(),
Definition::Trait(it) => {
it.display_truncated(db, hover_display_config.trait_item_display_num).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_on_hover: Option<usize>,
pub trait_item_display_num: Option<usize>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

View File

@ -8,7 +8,7 @@ use hir::{
};
use ide_db::{
base_db::SourceDatabase,
defs::Definition,
defs::{Definition, HoverDisplayConfig},
documentation::HasDocs,
famous_defs::FamousDefs,
generated::lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
@ -406,7 +406,8 @@ pub(super) fn definition(
config: &HoverConfig,
) -> Markup {
let mod_path = definition_mod_path(db, &def);
let label = def.label(db, config.trait_item_display_on_hover);
let hover_config = HoverDisplayConfig { trait_item_display_num: config.trait_item_display_num };
let label = def.label(db, hover_config);
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_on_hover: Some(7),
trait_item_display_num: Some(7),
};
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,
defs::{Definition, HoverDisplayConfig},
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_on_hover: Some(7)
trait_item_display_num: 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, hover_config.trait_item_display_on_hover)),
signature: Some(def.label(self.db, HoverDisplayConfig::default())),
kind: def_to_kind(self.db, def),
});
self.def_map.insert(def, it);

View File

@ -591,7 +591,7 @@ config_data! {
signatureInfo_documentation_enable: bool = "true",
/// How many trait item display on hover.
trait_item_display_on_hover: Option<usize> = "7",
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_on_hover: self.data.trait_item_display_on_hover,
trait_item_display_num: self.data.traitItemDisplayNum,
}
}

View File

@ -927,7 +927,7 @@ Show full signature of the callable. Only shows parameters if disabled.
--
Show documentation.
--
[[rust-analyzer.trait.item.display.on.hover]]rust-analyzer.trait.item.display.on.hover (default: `7`)::
[[rust-analyzer.traitItemDisplayNum]]rust-analyzer.traitItemDisplayNum (default: `7`)::
+
--
How many trait item display on hover.

View File

@ -1648,7 +1648,7 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.trait.item.display.on.hover": {
"rust-analyzer.traitItemDisplayNum": {
"markdownDescription": "How many trait item display on hover.",
"default": 7,
"type": [