Skip to content

Commit

Permalink
add config item for hover display
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Feb 18, 2024
1 parent 69eadf9 commit 675bd36
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/hir-ty/src/display.rs
Expand Up @@ -62,7 +62,7 @@ pub struct HirFormatter<'a> {
fmt: &'a mut dyn HirWrite,
buf: String,
curr_size: usize,
pub(crate) max_size: Option<usize>,
pub max_size: Option<usize>,
omit_verbose_types: bool,
closure_style: ClosureStyle,
display_target: DisplayTarget,
Expand Down
9 changes: 8 additions & 1 deletion crates/hir/src/display.rs
Expand Up @@ -593,12 +593,14 @@ 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);
if assoc_items.is_empty() {
f.write_str(" {}")?;
} else {
f.write_str(" {\n")?;
for item in assoc_items {
for (index, item) in assoc_items.iter().enumerate() {
f.write_str(" ")?;
match item {
AssocItem::Function(func) => {
Expand All @@ -612,6 +614,11 @@ impl HirDisplay for Trait {
}
};

Check warning on line 615 in crates/hir/src/display.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

Diff in /home/runner/work/rust-analyzer/rust-analyzer/crates/hir/src/display.rs
f.write_str(",\n")?;
display_size += 1;
if display_size == max_display_size && index != assoc_items.len() - 1{
f.write_str(" ...\n")?;
break;
}
}
f.write_str("}")?;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/ide-db/src/defs.rs
Expand Up @@ -213,7 +213,7 @@ impl Definition {
})
}

pub fn label(&self, db: &RootDatabase) -> Option<String> {
pub fn label(&self, db: &RootDatabase, max_size: Option<usize>) -> Option<String> {
let label = match *self {
Definition::Macro(it) => it.display(db).to_string(),
Definition::Field(it) => it.display(db).to_string(),
Expand All @@ -224,7 +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(db).to_string(),
Definition::Trait(it) => it.display_truncated(db, max_size).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(),
Expand All @@ -241,7 +241,7 @@ impl Definition {
}
}
Definition::SelfType(impl_def) => {
impl_def.self_ty(db).as_adt().and_then(|adt| Definition::Adt(adt).label(db))?
impl_def.self_ty(db).as_adt().and_then(|adt| Definition::Adt(adt).label(db, max_size))?
}
Definition::GenericParam(it) => it.display(db).to_string(),
Definition::Label(it) => it.name(db).display(db).to_string(),
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/hover.rs
Expand Up @@ -32,6 +32,7 @@ pub struct HoverConfig {
pub documentation: bool,
pub keywords: bool,
pub format: HoverDocFormat,
pub trait_item_display_on_hover: Option<usize>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/hover/render.rs
Expand Up @@ -398,7 +398,7 @@ pub(super) fn definition(
config: &HoverConfig,
) -> Option<Markup> {
let mod_path = definition_mod_path(db, &def);
let label = def.label(db)?;
let label = def.label(db, config.trait_item_display_on_hover)?;
let docs = def.docs(db, famous_defs);
let value = match def {
Definition::Variant(it) => {
Expand Down
1 change: 1 addition & 0 deletions crates/ide/src/hover/tests.rs
Expand Up @@ -17,6 +17,7 @@ const HOVER_BASE_CONFIG: HoverConfig = HoverConfig {
documentation: true,
format: HoverDocFormat::Markdown,
keywords: true,
trait_item_display_on_hover: Some(7),
};

fn check_hover_no_result(ra_fixture: &str) {
Expand Down
3 changes: 2 additions & 1 deletion crates/ide/src/static_index.rs
Expand Up @@ -166,6 +166,7 @@ impl StaticIndex<'_> {
documentation: true,

Check warning on line 166 in crates/ide/src/static_index.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

Diff in /home/runner/work/rust-analyzer/rust-analyzer/crates/ide/src/static_index.rs
keywords: true,
format: crate::HoverDocFormat::Markdown,
trait_item_display_on_hover: Some(7)
};
let tokens = tokens.filter(|token| {
matches!(
Expand Down Expand Up @@ -196,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: def.label(self.db),
signature: def.label(self.db, hover_config.trait_item_display_on_hover),
kind: def_to_kind(self.db, def),
});
self.def_map.insert(def, it);
Expand Down
5 changes: 4 additions & 1 deletion crates/rust-analyzer/src/config.rs
Expand Up @@ -511,7 +511,6 @@ config_data! {
/// Exclude tests from find-all-references.
references_excludeTests: bool = "false",


/// Command to be executed instead of 'cargo' for runnables.
runnables_command: Option<String> = "null",
/// Additional arguments to be passed to cargo for runnables such as
Expand Down Expand Up @@ -591,6 +590,9 @@ config_data! {
/// Show documentation.
signatureInfo_documentation_enable: bool = "true",

/// How many trait item display on hover.
trait_item_display_on_hover: 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",

Expand Down Expand Up @@ -1683,6 +1685,7 @@ impl Config {
}
},
keywords: self.data.hover_documentation_keywords_enable,
trait_item_display_on_hover: self.data.trait_item_display_on_hover,
}
}

Expand Down
5 changes: 5 additions & 0 deletions docs/user/generated_config.adoc
Expand Up @@ -927,6 +927,11 @@ 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`)::
+
--
How many trait item display on hover.
--
[[rust-analyzer.typing.autoClosingAngleBrackets.enable]]rust-analyzer.typing.autoClosingAngleBrackets.enable (default: `false`)::
+
--
Expand Down
9 changes: 9 additions & 0 deletions editors/code/package.json
Expand Up @@ -1648,6 +1648,15 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.trait.item.display.on.hover": {
"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,
Expand Down

0 comments on commit 675bd36

Please sign in to comment.