From a0e690a7e999ac1a3260bc4cf41b8e3219b74e4f Mon Sep 17 00:00:00 2001 From: dfireBird Date: Tue, 10 Oct 2023 08:30:49 +0530 Subject: [PATCH 1/9] add different completion for fn fields --- crates/ide-completion/src/completions/dot.rs | 34 ++++++++++++++++++++ crates/ide-completion/src/render.rs | 19 ++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 5bcc867fe181..12591449cdbc 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -28,6 +28,13 @@ pub(crate) fn complete_dot( if let DotAccessKind::Method { .. } = dot_access.kind { cov_mark::hit!(test_no_struct_field_completion_for_method_call); + complete_fn_fields( + acc, + ctx, + receiver_ty, + |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty), + |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty), + ); } else { complete_fields( acc, @@ -144,6 +151,33 @@ fn complete_methods( ); } +fn complete_fn_fields( + acc: &mut Completions, + ctx: &CompletionContext<'_>, + receiver: &hir::Type, + mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type), + mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type), +) { + let mut seen_names = FxHashSet::default(); + for receiver in receiver.autoderef(ctx.db) { + for (field, ty) in receiver.fields(ctx.db) { + if seen_names.insert(field.name(ctx.db)) && (ty.is_fn() || ty.is_closure()) { + named_field(acc, field, ty); + } + } + for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { + // Tuples are always the last type in a deref chain, so just check if the name is + // already seen without inserting into the hashset. + if !seen_names.contains(&hir::Name::new_tuple_field(i)) + && (ty.is_fn() || ty.is_closure()) + { + // Tuple fields are always public (tuple struct fields are handled above). + tuple_index(acc, i, ty); + } + } + } +} + #[cfg(test)] mod tests { use expect_test::{expect, Expect}; diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 00a9081985b8..f05cc78ac6e0 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -18,6 +18,7 @@ use ide_db::{ RootDatabase, SnippetCap, SymbolKind, }; use syntax::{AstNode, SmolStr, SyntaxKind, TextRange}; +use text_edit::TextEdit; use crate::{ context::{DotAccess, PathCompletionCtx, PathKind, PatternContext}, @@ -147,7 +148,23 @@ pub(crate) fn render_field( .set_documentation(field.docs(db)) .set_deprecated(is_deprecated) .lookup_by(name); - item.insert_text(field_with_receiver(db, receiver.as_ref(), &escaped_name)); + if ty.is_fn() || ty.is_closure() { + let mut builder = TextEdit::builder(); + // Use TextEdit to insert / replace the ranges: + // 1. Insert one character ('(') before start of struct name + // 2. Insert one character (')') before call parens + // 3. Variable character of the actual field name + // 4. Optionally, two character ('()') for fn call + // + // TODO: Find a way to get the above ranges, especially the first two + builder.replace( + ctx.source_range(), + field_with_receiver(db, receiver.as_ref(), &escaped_name).into(), + ); + item.text_edit(builder.finish()); + } else { + item.insert_text(field_with_receiver(db, receiver.as_ref(), &escaped_name)); + } if let Some(receiver) = &dot_access.receiver { if let Some(original) = ctx.completion.sema.original_ast_node(receiver.clone()) { if let Some(ref_match) = compute_ref_match(ctx.completion, ty) { From 7cf3ab4bd2927eda9799be5df747ac810cf2b0bb Mon Sep 17 00:00:00 2001 From: dfireBird Date: Sun, 12 Nov 2023 14:49:31 +0530 Subject: [PATCH 2/9] implement completion render for callable fields --- crates/ide-completion/src/render.rs | 33 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index f05cc78ac6e0..a59b95e1e6e1 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -21,7 +21,7 @@ use syntax::{AstNode, SmolStr, SyntaxKind, TextRange}; use text_edit::TextEdit; use crate::{ - context::{DotAccess, PathCompletionCtx, PathKind, PatternContext}, + context::{DotAccess, DotAccessKind, PathCompletionCtx, PathKind, PatternContext}, item::{Builder, CompletionRelevanceTypeMatch}, render::{ function::render_fn, @@ -150,17 +150,34 @@ pub(crate) fn render_field( .lookup_by(name); if ty.is_fn() || ty.is_closure() { let mut builder = TextEdit::builder(); - // Use TextEdit to insert / replace the ranges: - // 1. Insert one character ('(') before start of struct name - // 2. Insert one character (')') before call parens - // 3. Variable character of the actual field name - // 4. Optionally, two character ('()') for fn call - // - // TODO: Find a way to get the above ranges, especially the first two + // Using TextEdit, insert '(' before the struct name and ')' before the + // dot access, then comes the field name and optionally insert function + // call parens. + + if let Some(receiver) = &dot_access.receiver { + let range = receiver.syntax().text_range(); + builder.insert(range.start(), "(".to_string()); + builder.insert(range.end(), ")".to_string()); + } builder.replace( ctx.source_range(), field_with_receiver(db, receiver.as_ref(), &escaped_name).into(), ); + + let is_fn_expected = + ctx.completion.expected_type.as_ref().map_or(false, |ty| ty.is_fn() || ty.is_closure()); + + // This could be refactored as method of DotAccessKind + let is_parens_needed = if let DotAccessKind::Method { has_parens } = dot_access.kind { + !has_parens + } else { + true + }; + + if !is_fn_expected && is_parens_needed { + builder.insert(ctx.source_range().end(), "()".to_string()); + } + item.text_edit(builder.finish()); } else { item.insert_text(field_with_receiver(db, receiver.as_ref(), &escaped_name)); From 5bcafd7dc06e2cede234f0df5a7138d63ac92f6a Mon Sep 17 00:00:00 2001 From: dfireBird Date: Sun, 12 Nov 2023 16:44:57 +0530 Subject: [PATCH 3/9] add tests for the completion of the callable field --- crates/ide-completion/src/completions/dot.rs | 13 +++++++ crates/ide-completion/src/render.rs | 38 ++++++++++++++++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 12591449cdbc..430dea222572 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -1206,4 +1206,17 @@ impl> Foo { "#]], ); } + + #[test] + fn test_struct_function_field_completion() { + check( + r#" +struct S { field: fn() } +fn foo() { S { field: || {} }.fi$0() } +"#, + expect![[r#" + fd field fn() + "#]], + ); + } } diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index a59b95e1e6e1..fa6854b0888f 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -1634,7 +1634,7 @@ fn main() { fn struct_field_method_ref() { check_kinds( r#" -struct Foo { bar: u32 } +struct Foo { bar: u32, qux: fn() } impl Foo { fn baz(&self) -> u32 { 0 } } fn foo(f: Foo) { let _: &u32 = f.b$0 } @@ -1644,24 +1644,48 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 } [ CompletionItem { label: "baz()", - source_range: 98..99, - delete: 98..99, + source_range: 109..110, + delete: 109..110, insert: "baz()$0", kind: Method, lookup: "baz", detail: "fn(&self) -> u32", - ref_match: "&@96", + ref_match: "&@107", }, CompletionItem { label: "bar", - source_range: 98..99, - delete: 98..99, + source_range: 109..110, + delete: 109..110, insert: "bar", kind: SymbolKind( Field, ), detail: "u32", - ref_match: "&@96", + ref_match: "&@107", + }, + CompletionItem { + label: "qux", + source_range: 109..110, + text_edit: TextEdit { + indels: [ + Indel { + insert: "(", + delete: 107..107, + }, + Indel { + insert: ")", + delete: 108..108, + }, + Indel { + insert: "qux()", + delete: 109..110, + }, + ], + }, + kind: SymbolKind( + Field, + ), + detail: "fn()", }, ] "#]], From eedeb58a4eb04e15e6eb82f9f2485ebf38ecd30c Mon Sep 17 00:00:00 2001 From: dfireBird Date: Wed, 15 Nov 2023 18:26:29 +0530 Subject: [PATCH 4/9] refactor obtaining receivers into idiomatic way variable name change for clearer usage indication --- crates/ide-completion/src/render.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index fa6854b0888f..26488f49bea2 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -155,26 +155,24 @@ pub(crate) fn render_field( // call parens. if let Some(receiver) = &dot_access.receiver { - let range = receiver.syntax().text_range(); - builder.insert(range.start(), "(".to_string()); - builder.insert(range.end(), ")".to_string()); + if let Some(receiver) = ctx.completion.sema.original_ast_node(receiver.clone()) { + let range = receiver.syntax().text_range(); + builder.insert(range.start(), "(".to_string()); + builder.insert(range.end(), ")".to_string()); + } } builder.replace( ctx.source_range(), field_with_receiver(db, receiver.as_ref(), &escaped_name).into(), ); - let is_fn_expected = - ctx.completion.expected_type.as_ref().map_or(false, |ty| ty.is_fn() || ty.is_closure()); + let expected_fn_type = + ctx.completion.expected_type.as_ref().is_some_and(|ty| ty.is_fn() || ty.is_closure()); - // This could be refactored as method of DotAccessKind - let is_parens_needed = if let DotAccessKind::Method { has_parens } = dot_access.kind { - !has_parens - } else { - true - }; + let is_parens_needed = + !matches!(dot_access.kind, DotAccessKind::Method { has_parens: true }); - if !is_fn_expected && is_parens_needed { + if !expected_fn_type && is_parens_needed { builder.insert(ctx.source_range().end(), "()".to_string()); } From aa1cf8d357f75c7376f933f7274b77bf754e836e Mon Sep 17 00:00:00 2001 From: dfireBird Date: Wed, 15 Nov 2023 21:46:30 +0530 Subject: [PATCH 5/9] add tests for tuple fields and expect fn type cases --- crates/ide-completion/src/completions/dot.rs | 16 ++++++ crates/ide-completion/src/render.rs | 56 ++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 430dea222572..2f0b6988f217 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -1219,4 +1219,20 @@ fn foo() { S { field: || {} }.fi$0() } "#]], ); } + + #[test] + fn test_tuple_function_field_completion() { + check( + r#" +struct B(u32, fn()) +fn foo() { + let b = B(0, || {}); + b.$0() +} +"#, + expect![[r#" + fd 1 fn() + "#]], + ); + } } diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 26488f49bea2..28f7166a22a4 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -1690,6 +1690,62 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 } ); } + #[test] + fn expected_fn_type_ref() { + check_kinds( + r#" +struct S { field: fn() } + +fn foo() { + let foo: fn() = S { fields: || {}}.fi$0; +} +"#, + &[CompletionItemKind::SymbolKind(SymbolKind::Field)], + expect![[r#" + [ + CompletionItem { + label: "field", + source_range: 76..78, + text_edit: TextEdit { + indels: [ + Indel { + insert: "(", + delete: 57..57, + }, + Indel { + insert: ")", + delete: 75..75, + }, + Indel { + insert: "field", + delete: 76..78, + }, + ], + }, + kind: SymbolKind( + Field, + ), + detail: "fn()", + relevance: CompletionRelevance { + exact_name_match: false, + type_match: Some( + Exact, + ), + is_local: false, + is_item_from_trait: false, + is_name_already_imported: false, + requires_import: false, + is_op_method: false, + is_private_editable: false, + postfix_match: None, + is_definite: false, + }, + }, + ] + "#]], + ) + } + #[test] fn qualified_path_ref() { check_kinds( From 8296b16f38eea267d9eb3e6f1300f0534bc87961 Mon Sep 17 00:00:00 2001 From: dfireBird Date: Sat, 18 Nov 2023 08:40:57 +0530 Subject: [PATCH 6/9] fix the insertion of the surronding parens Before it was inserting whenever function field is found but it should happend only in the case of function call. --- crates/ide-completion/src/render.rs | 43 +++++++++++------------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 28f7166a22a4..f733f36f5d6f 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -154,13 +154,6 @@ pub(crate) fn render_field( // dot access, then comes the field name and optionally insert function // call parens. - if let Some(receiver) = &dot_access.receiver { - if let Some(receiver) = ctx.completion.sema.original_ast_node(receiver.clone()) { - let range = receiver.syntax().text_range(); - builder.insert(range.start(), "(".to_string()); - builder.insert(range.end(), ")".to_string()); - } - } builder.replace( ctx.source_range(), field_with_receiver(db, receiver.as_ref(), &escaped_name).into(), @@ -169,11 +162,21 @@ pub(crate) fn render_field( let expected_fn_type = ctx.completion.expected_type.as_ref().is_some_and(|ty| ty.is_fn() || ty.is_closure()); - let is_parens_needed = - !matches!(dot_access.kind, DotAccessKind::Method { has_parens: true }); + if !expected_fn_type { + if let Some(receiver) = &dot_access.receiver { + if let Some(receiver) = ctx.completion.sema.original_ast_node(receiver.clone()) { + let range = receiver.syntax().text_range(); + builder.insert(range.start(), "(".to_string()); + builder.insert(range.end(), ")".to_string()); + } + } + + let is_parens_needed = + !matches!(dot_access.kind, DotAccessKind::Method { has_parens: true }); - if !expected_fn_type && is_parens_needed { - builder.insert(ctx.source_range().end(), "()".to_string()); + if is_parens_needed { + builder.insert(ctx.source_range().end(), "()".to_string()); + } } item.text_edit(builder.finish()); @@ -1706,22 +1709,8 @@ fn foo() { CompletionItem { label: "field", source_range: 76..78, - text_edit: TextEdit { - indels: [ - Indel { - insert: "(", - delete: 57..57, - }, - Indel { - insert: ")", - delete: 75..75, - }, - Indel { - insert: "field", - delete: 76..78, - }, - ], - }, + delete: 76..78, + insert: "field", kind: SymbolKind( Field, ), From 5c0c8ceaf7eba67355564bd54c7f4e215e485854 Mon Sep 17 00:00:00 2001 From: dfireBird Date: Wed, 22 Nov 2023 18:33:38 +0530 Subject: [PATCH 7/9] refactor `complete_fn_fields` function and correct branch checks --- crates/ide-completion/src/completions/dot.rs | 63 ++++++-------------- crates/ide-completion/src/render.rs | 4 +- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 2f0b6988f217..e427be381bbb 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -26,23 +26,19 @@ pub(crate) fn complete_dot( item.add_to(acc, ctx.db); } + let is_field_access = matches!(dot_access.kind, DotAccessKind::Field { .. }); + + complete_fields( + acc, + ctx, + receiver_ty, + |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty), + |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty), + is_field_access, + ); + if let DotAccessKind::Method { .. } = dot_access.kind { cov_mark::hit!(test_no_struct_field_completion_for_method_call); - complete_fn_fields( - acc, - ctx, - receiver_ty, - |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty), - |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty), - ); - } else { - complete_fields( - acc, - ctx, - receiver_ty, - |acc, field, ty| acc.add_field(ctx, dot_access, None, field, &ty), - |acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty), - ); } complete_methods(ctx, receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None)); } @@ -89,6 +85,7 @@ pub(crate) fn complete_undotted_self( ) }, |acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty), + true, ); complete_methods(ctx, &ty, |func| { acc.add_method( @@ -111,18 +108,23 @@ fn complete_fields( receiver: &hir::Type, mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type), mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type), + is_field_access: bool, ) { let mut seen_names = FxHashSet::default(); for receiver in receiver.autoderef(ctx.db) { for (field, ty) in receiver.fields(ctx.db) { - if seen_names.insert(field.name(ctx.db)) { + if seen_names.insert(field.name(ctx.db)) + && (is_field_access || ty.is_fn() || ty.is_closure()) + { named_field(acc, field, ty); } } for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { // Tuples are always the last type in a deref chain, so just check if the name is // already seen without inserting into the hashset. - if !seen_names.contains(&hir::Name::new_tuple_field(i)) { + if !seen_names.contains(&hir::Name::new_tuple_field(i)) + && (is_field_access || ty.is_fn() || ty.is_closure()) + { // Tuple fields are always public (tuple struct fields are handled above). tuple_index(acc, i, ty); } @@ -151,33 +153,6 @@ fn complete_methods( ); } -fn complete_fn_fields( - acc: &mut Completions, - ctx: &CompletionContext<'_>, - receiver: &hir::Type, - mut named_field: impl FnMut(&mut Completions, hir::Field, hir::Type), - mut tuple_index: impl FnMut(&mut Completions, usize, hir::Type), -) { - let mut seen_names = FxHashSet::default(); - for receiver in receiver.autoderef(ctx.db) { - for (field, ty) in receiver.fields(ctx.db) { - if seen_names.insert(field.name(ctx.db)) && (ty.is_fn() || ty.is_closure()) { - named_field(acc, field, ty); - } - } - for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { - // Tuples are always the last type in a deref chain, so just check if the name is - // already seen without inserting into the hashset. - if !seen_names.contains(&hir::Name::new_tuple_field(i)) - && (ty.is_fn() || ty.is_closure()) - { - // Tuple fields are always public (tuple struct fields are handled above). - tuple_index(acc, i, ty); - } - } - } -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect}; diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index f733f36f5d6f..453ff061bce4 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -148,7 +148,9 @@ pub(crate) fn render_field( .set_documentation(field.docs(db)) .set_deprecated(is_deprecated) .lookup_by(name); - if ty.is_fn() || ty.is_closure() { + + let is_field_access = matches!(dot_access.kind, DotAccessKind::Field { .. }); + if !is_field_access || ty.is_fn() || ty.is_closure() { let mut builder = TextEdit::builder(); // Using TextEdit, insert '(' before the struct name and ')' before the // dot access, then comes the field name and optionally insert function From 21c09eb5449ef972cd15bca4ca10e937e1c92f5a Mon Sep 17 00:00:00 2001 From: dfireBird Date: Wed, 22 Nov 2023 23:59:45 +0530 Subject: [PATCH 8/9] update dot tests function with check_edit --- crates/ide-completion/src/completions/dot.rs | 36 ++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index e427be381bbb..81acfc8ecffc 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -1186,13 +1186,25 @@ impl> Foo { fn test_struct_function_field_completion() { check( r#" -struct S { field: fn() } -fn foo() { S { field: || {} }.fi$0() } +struct S { va_field: u32, fn_field: fn() } +fn foo() { S { va_field: 0, fn_field: || {} }.fi$0() } "#, expect![[r#" - fd field fn() + fd fn_field fn() "#]], ); + + check_edit( + "fn_field", + r#" +struct S { va_field: u32, fn_field: fn() } +fn foo() { S { va_field: 0, fn_field: || {} }.fi$0() } +"#, + r#" +struct S { va_field: u32, fn_field: fn() } +fn foo() { (S { va_field: 0, fn_field: || {} }).fn_field() } +"#, + ); } #[test] @@ -1209,5 +1221,23 @@ fn foo() { fd 1 fn() "#]], ); + + check_edit( + "1", + r#" +struct B(u32, fn()) +fn foo() { + let b = B(0, || {}); + b.$0() +} +"#, + r#" +struct B(u32, fn()) +fn foo() { + let b = B(0, || {}); + (b).1() +} +"#, + ) } } From b7effe54eef2f72408356d0d80c5158eea0c57aa Mon Sep 17 00:00:00 2001 From: dfireBird Date: Fri, 1 Dec 2023 18:54:29 +0530 Subject: [PATCH 9/9] fix close parens position to move after field access --- crates/ide-completion/src/completions/dot.rs | 8 ++------ crates/ide-completion/src/render.rs | 11 +++-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/crates/ide-completion/src/completions/dot.rs b/crates/ide-completion/src/completions/dot.rs index 81acfc8ecffc..57e06461099e 100644 --- a/crates/ide-completion/src/completions/dot.rs +++ b/crates/ide-completion/src/completions/dot.rs @@ -37,9 +37,6 @@ pub(crate) fn complete_dot( is_field_access, ); - if let DotAccessKind::Method { .. } = dot_access.kind { - cov_mark::hit!(test_no_struct_field_completion_for_method_call); - } complete_methods(ctx, receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None)); } @@ -259,7 +256,6 @@ impl A { #[test] fn test_no_struct_field_completion_for_method_call() { - cov_mark::check!(test_no_struct_field_completion_for_method_call); check( r#" struct A { the_field: u32 } @@ -1202,7 +1198,7 @@ fn foo() { S { va_field: 0, fn_field: || {} }.fi$0() } "#, r#" struct S { va_field: u32, fn_field: fn() } -fn foo() { (S { va_field: 0, fn_field: || {} }).fn_field() } +fn foo() { (S { va_field: 0, fn_field: || {} }.fn_field)() } "#, ); } @@ -1235,7 +1231,7 @@ fn foo() { struct B(u32, fn()) fn foo() { let b = B(0, || {}); - (b).1() + (b.1)() } "#, ) diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 453ff061bce4..048730c078d7 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -167,9 +167,8 @@ pub(crate) fn render_field( if !expected_fn_type { if let Some(receiver) = &dot_access.receiver { if let Some(receiver) = ctx.completion.sema.original_ast_node(receiver.clone()) { - let range = receiver.syntax().text_range(); - builder.insert(range.start(), "(".to_string()); - builder.insert(range.end(), ")".to_string()); + builder.insert(receiver.syntax().text_range().start(), "(".to_string()); + builder.insert(ctx.source_range().end(), ")".to_string()); } } @@ -1676,11 +1675,7 @@ fn foo(f: Foo) { let _: &u32 = f.b$0 } delete: 107..107, }, Indel { - insert: ")", - delete: 108..108, - }, - Indel { - insert: "qux()", + insert: "qux)()", delete: 109..110, }, ],