Skip to content

Commit e5e937a

Browse files
committed
Auto merge of #15582 - vxpm:master, r=HKalbasi
add option to show full function signatures in completion docs implements #15538 with `"rust-analyzer.completion.fullFunctionSignatures.enable": false`: ![image](https://github.com/rust-lang/rust-analyzer/assets/59714841/ff739ad1-9975-461f-a62d-22c7823e7b71) with `"rust-analyzer.completion.fullFunctionSignatures.enable": true`: ![image](https://github.com/rust-lang/rust-analyzer/assets/59714841/9bc98300-cef6-44ef-a353-dcf35cd36fce)
2 parents 2b580a1 + 10fae62 commit e5e937a

File tree

8 files changed

+109
-3
lines changed

8 files changed

+109
-3
lines changed

crates/ide-completion/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct CompletionConfig {
1414
pub enable_imports_on_the_fly: bool,
1515
pub enable_self_on_the_fly: bool,
1616
pub enable_private_editable: bool,
17+
pub full_function_signatures: bool,
1718
pub callable: Option<CallableSnippets>,
1819
pub snippet_cap: Option<SnippetCap>,
1920
pub insert_use: InsertUseConfig,

crates/ide-completion/src/render/function.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ fn render(
9898
_ => (),
9999
}
100100

101+
let detail = if ctx.completion.config.full_function_signatures {
102+
detail_full(db, func)
103+
} else {
104+
detail(db, func)
105+
};
101106
item.set_documentation(ctx.docs(func))
102107
.set_deprecated(ctx.is_deprecated(func) || ctx.is_deprecated_assoc_item(func))
103-
.detail(detail(db, func))
108+
.detail(detail)
104109
.lookup_by(name.unescaped().to_smol_str());
105110

106111
match ctx.completion.config.snippet_cap {
@@ -263,6 +268,21 @@ fn detail(db: &dyn HirDatabase, func: hir::Function) -> String {
263268
detail
264269
}
265270

271+
fn detail_full(db: &dyn HirDatabase, func: hir::Function) -> String {
272+
let signature = format!("{}", func.display(db));
273+
let mut detail = String::with_capacity(signature.len());
274+
275+
for segment in signature.split_whitespace() {
276+
if !detail.is_empty() {
277+
detail.push(' ');
278+
}
279+
280+
detail.push_str(segment);
281+
}
282+
283+
detail
284+
}
285+
266286
fn params_display(db: &dyn HirDatabase, func: hir::Function) -> String {
267287
if let Some(self_param) = func.self_param(db) {
268288
let assoc_fn_params = func.assoc_fn_params(db);

crates/ide-completion/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
6464
enable_imports_on_the_fly: true,
6565
enable_self_on_the_fly: true,
6666
enable_private_editable: false,
67+
full_function_signatures: false,
6768
callable: Some(CallableSnippets::FillArguments),
6869
snippet_cap: SnippetCap::new(true),
6970
prefer_no_std: false,

crates/ide-completion/src/tests/special.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
33
use expect_test::{expect, Expect};
44

5-
use crate::tests::{
6-
check_edit, completion_list, completion_list_no_kw, completion_list_with_trigger_character,
5+
use crate::{
6+
tests::{
7+
check_edit, completion_list, completion_list_no_kw, completion_list_with_trigger_character,
8+
},
9+
CompletionItemKind,
710
};
811

12+
use super::{do_completion_with_config, TEST_CONFIG};
13+
914
fn check_no_kw(ra_fixture: &str, expect: Expect) {
1015
let actual = completion_list_no_kw(ra_fixture);
1116
expect.assert_eq(&actual)
@@ -1303,3 +1308,67 @@ struct Foo<T: PartialOrd
13031308
"#,
13041309
);
13051310
}
1311+
1312+
fn check_signatures(src: &str, kind: CompletionItemKind, reduced: Expect, full: Expect) {
1313+
const FULL_SIGNATURES_CONFIG: crate::CompletionConfig = {
1314+
let mut x = TEST_CONFIG;
1315+
x.full_function_signatures = true;
1316+
x
1317+
};
1318+
1319+
// reduced signature
1320+
let completion = do_completion_with_config(TEST_CONFIG, src, kind);
1321+
assert!(completion[0].detail.is_some());
1322+
reduced.assert_eq(completion[0].detail.as_ref().unwrap());
1323+
1324+
// full signature
1325+
let completion = do_completion_with_config(FULL_SIGNATURES_CONFIG, src, kind);
1326+
assert!(completion[0].detail.is_some());
1327+
full.assert_eq(completion[0].detail.as_ref().unwrap());
1328+
}
1329+
1330+
#[test]
1331+
fn respects_full_function_signatures() {
1332+
check_signatures(
1333+
r#"
1334+
pub fn foo<'x, T>(x: &'x mut T) -> u8 where T: Clone, { 0u8 }
1335+
fn main() { fo$0 }
1336+
"#,
1337+
CompletionItemKind::SymbolKind(ide_db::SymbolKind::Function),
1338+
expect!("fn(&mut T) -> u8"),
1339+
expect!("pub fn foo<'x, T>(x: &'x mut T) -> u8 where T: Clone,"),
1340+
);
1341+
1342+
check_signatures(
1343+
r#"
1344+
struct Foo;
1345+
struct Bar;
1346+
impl Bar {
1347+
pub const fn baz(x: Foo) -> ! { loop {} };
1348+
}
1349+
1350+
fn main() { Bar::b$0 }
1351+
"#,
1352+
CompletionItemKind::SymbolKind(ide_db::SymbolKind::Function),
1353+
expect!("const fn(Foo) -> !"),
1354+
expect!("pub const fn baz(x: Foo) -> !"),
1355+
);
1356+
1357+
check_signatures(
1358+
r#"
1359+
struct Foo;
1360+
struct Bar;
1361+
impl Bar {
1362+
pub const fn baz<'foo>(&'foo mut self, x: &'foo Foo) -> ! { loop {} };
1363+
}
1364+
1365+
fn main() {
1366+
let mut bar = Bar;
1367+
bar.b$0
1368+
}
1369+
"#,
1370+
CompletionItemKind::Method,
1371+
expect!("const fn(&'foo mut self, &Foo) -> !"),
1372+
expect!("pub const fn baz<'foo>(&'foo mut self, x: &'foo Foo) -> !"),
1373+
);
1374+
}

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ config_data! {
217217
completion_autoself_enable: bool = "true",
218218
/// Whether to add parenthesis and argument snippets when completing function.
219219
completion_callable_snippets: CallableCompletionDef = "\"fill_arguments\"",
220+
/// Whether to show full function/method signatures in completion docs.
221+
completion_fullFunctionSignatures_enable: bool = "false",
220222
/// Maximum number of completions to return. If `None`, the limit is infinite.
221223
completion_limit: Option<usize> = "null",
222224
/// Whether to show postfix snippets like `dbg`, `if`, `not`, etc.
@@ -1455,6 +1457,7 @@ impl Config {
14551457
&& completion_item_edit_resolve(&self.caps),
14561458
enable_self_on_the_fly: self.data.completion_autoself_enable,
14571459
enable_private_editable: self.data.completion_privateEditable_enable,
1460+
full_function_signatures: self.data.completion_fullFunctionSignatures_enable,
14581461
callable: match self.data.completion_callable_snippets {
14591462
CallableCompletionDef::FillArguments => Some(CallableSnippets::FillArguments),
14601463
CallableCompletionDef::AddParentheses => Some(CallableSnippets::AddParentheses),

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ fn integrated_completion_benchmark() {
134134
enable_imports_on_the_fly: true,
135135
enable_self_on_the_fly: true,
136136
enable_private_editable: true,
137+
full_function_signatures: false,
137138
callable: Some(CallableSnippets::FillArguments),
138139
snippet_cap: SnippetCap::new(true),
139140
insert_use: InsertUseConfig {
@@ -173,6 +174,7 @@ fn integrated_completion_benchmark() {
173174
enable_imports_on_the_fly: true,
174175
enable_self_on_the_fly: true,
175176
enable_private_editable: true,
177+
full_function_signatures: false,
176178
callable: Some(CallableSnippets::FillArguments),
177179
snippet_cap: SnippetCap::new(true),
178180
insert_use: InsertUseConfig {

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ with `self` prefixed to them when inside a method.
252252
--
253253
Whether to add parenthesis and argument snippets when completing function.
254254
--
255+
[[rust-analyzer.completion.fullFunctionSignatures.enable]]rust-analyzer.completion.fullFunctionSignatures.enable (default: `false`)::
256+
+
257+
--
258+
Whether to show full function/method signatures in completion docs.
259+
--
255260
[[rust-analyzer.completion.limit]]rust-analyzer.completion.limit (default: `null`)::
256261
+
257262
--

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,11 @@
799799
"Do no snippet completions for callables."
800800
]
801801
},
802+
"rust-analyzer.completion.fullFunctionSignatures.enable": {
803+
"markdownDescription": "Whether to show full function/method signatures in completion docs.",
804+
"default": false,
805+
"type": "boolean"
806+
},
802807
"rust-analyzer.completion.limit": {
803808
"markdownDescription": "Maximum number of completions to return. If `None`, the limit is infinite.",
804809
"default": null,

0 commit comments

Comments
 (0)