Skip to content

Commit ae8ae65

Browse files
committed
Fixed bug that allowed for completion in a nested method.
1 parent fc13b7f commit ae8ae65

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

crates/ra_ide/src/completion/complete_trait_impl.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use crate::{
1717

1818
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
1919
let trigger = ctx.token.ancestors().find(|p| match p.kind() {
20-
SyntaxKind::FN_DEF | SyntaxKind::TYPE_ALIAS_DEF | SyntaxKind::CONST_DEF => true,
20+
SyntaxKind::FN_DEF
21+
| SyntaxKind::TYPE_ALIAS_DEF
22+
| SyntaxKind::CONST_DEF
23+
| SyntaxKind::BLOCK_EXPR => true,
2124
_ => false,
2225
});
2326

@@ -98,10 +101,9 @@ fn add_function_impl(
98101

99102
let snippet = format!("{} {{}}", display);
100103

101-
builder
102-
.text_edit(TextEdit::replace(fn_def_node.text_range(), snippet))
103-
.kind(completion_kind)
104-
.add_to(acc);
104+
let range = TextRange::from_to(fn_def_node.text_range().start(), ctx.source_range().end());
105+
106+
builder.text_edit(TextEdit::replace(range, snippet)).kind(completion_kind).add_to(acc);
105107
}
106108

107109
fn add_type_alias_impl(
@@ -114,8 +116,10 @@ fn add_type_alias_impl(
114116

115117
let snippet = format!("type {} = ", alias_name);
116118

119+
let range = TextRange::from_to(type_def_node.text_range().start(), ctx.source_range().end());
120+
117121
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
118-
.text_edit(TextEdit::replace(type_def_node.text_range(), snippet))
122+
.text_edit(TextEdit::replace(range, snippet))
119123
.lookup_by(alias_name)
120124
.kind(CompletionItemKind::TypeAlias)
121125
.set_documentation(type_alias.docs(ctx.db))
@@ -133,8 +137,11 @@ fn add_const_impl(
133137
if let Some(const_name) = const_name {
134138
let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
135139

140+
let range =
141+
TextRange::from_to(const_def_node.text_range().start(), ctx.source_range().end());
142+
136143
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
137-
.text_edit(TextEdit::replace(const_def_node.text_range(), snippet))
144+
.text_edit(TextEdit::replace(range, snippet))
138145
.lookup_by(const_name)
139146
.kind(CompletionItemKind::Const)
140147
.set_documentation(const_.docs(ctx.db))
@@ -235,6 +242,28 @@ mod tests {
235242
"###);
236243
}
237244

245+
#[test]
246+
fn completes_only_on_top_level() {
247+
let completions = complete(
248+
r"
249+
trait Test {
250+
fn foo();
251+
252+
fn foo_bar();
253+
}
254+
255+
struct T1;
256+
257+
impl Test for T1 {
258+
fn foo() {
259+
<|>
260+
}
261+
}
262+
",
263+
);
264+
assert_debug_snapshot!(completions, @r###"[]"###);
265+
}
266+
238267
#[test]
239268
fn generic_fn() {
240269
let completions = complete(

0 commit comments

Comments
 (0)