Skip to content

Commit 3b820bc

Browse files
bors[bot]matklad
andcommitted
Merge #346
346: add function to completion ctx r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 7a1ed64 + e4de2c8 commit 3b820bc

File tree

6 files changed

+19
-21
lines changed

6 files changed

+19
-21
lines changed

crates/ra_analysis/src/completion/complete_dot.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,9 @@ use crate::completion::{CompletionContext, Completions, CompletionKind, Completi
66

77
/// Complete dot accesses, i.e. fields or methods (currently only fields).
88
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
9-
let module = if let Some(module) = &ctx.module {
10-
module
11-
} else {
12-
return Ok(());
13-
};
14-
let function = if let Some(fn_def) = ctx.enclosing_fn {
15-
hir::source_binder::function_from_module(ctx.db, module, fn_def)
16-
} else {
17-
return Ok(());
18-
};
19-
let receiver = if let Some(receiver) = ctx.dot_receiver {
20-
receiver
21-
} else {
22-
return Ok(());
9+
let (function, receiver) = match (&ctx.function, ctx.dot_receiver) {
10+
(Some(function), Some(receiver)) => (function, receiver),
11+
_ => return Ok(()),
2312
};
2413
let infer_result = function.infer(ctx.db)?;
2514
let receiver_ty = if let Some(ty) = infer_result.type_of_node(receiver.syntax()) {

crates/ra_analysis/src/completion/complete_keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
1818
if !ctx.is_trivial_path {
1919
return;
2020
}
21-
let fn_def = match ctx.enclosing_fn {
21+
let fn_def = match ctx.function_syntax {
2222
Some(it) => it,
2323
None => return,
2424
};

crates/ra_analysis/src/completion/complete_scope.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
1414
Some(it) => it,
1515
None => return Ok(()),
1616
};
17-
if let Some(fn_def) = ctx.enclosing_fn {
18-
let function = hir::source_binder::function_from_module(ctx.db, module, fn_def);
17+
if let Some(function) = &ctx.function {
1918
let scopes = function.scopes(ctx.db);
2019
complete_fn(acc, &scopes, ctx.offset);
2120
}

crates/ra_analysis/src/completion/complete_snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn snippet(label: &str, snippet: &str) -> Builder {
77
}
88

99
pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
10-
if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
10+
if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) {
1111
return;
1212
}
1313
snippet("pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);

crates/ra_analysis/src/completion/completion_context.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub(super) struct CompletionContext<'a> {
2222
pub(super) offset: TextUnit,
2323
pub(super) leaf: SyntaxNodeRef<'a>,
2424
pub(super) module: Option<hir::Module>,
25-
pub(super) enclosing_fn: Option<ast::FnDef<'a>>,
25+
pub(super) function: Option<hir::Function>,
26+
pub(super) function_syntax: Option<ast::FnDef<'a>>,
2627
pub(super) is_param: bool,
2728
/// A single-indent path, like `foo`.
2829
pub(super) is_trivial_path: bool,
@@ -52,7 +53,8 @@ impl<'a> CompletionContext<'a> {
5253
leaf,
5354
offset: position.offset,
5455
module,
55-
enclosing_fn: None,
56+
function: None,
57+
function_syntax: None,
5658
is_param: false,
5759
is_trivial_path: false,
5860
path_prefix: None,
@@ -112,11 +114,18 @@ impl<'a> CompletionContext<'a> {
112114
_ => (),
113115
}
114116

115-
self.enclosing_fn = self
117+
self.function_syntax = self
116118
.leaf
117119
.ancestors()
118120
.take_while(|it| it.kind() != SOURCE_FILE && it.kind() != MODULE)
119121
.find_map(ast::FnDef::cast);
122+
match (&self.module, self.function_syntax) {
123+
(Some(module), Some(fn_def)) => {
124+
let function = source_binder::function_from_module(self.db, module, fn_def);
125+
self.function = Some(function);
126+
}
127+
_ => (),
128+
}
120129

121130
let parent = match name_ref.syntax().parent() {
122131
Some(it) => it,

crates/ra_hir/src/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub use self::scope::FnScopes;
1818
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1919
pub struct FnId(pub(crate) DefId);
2020

21+
#[derive(Debug)]
2122
pub struct Function {
2223
pub(crate) fn_id: FnId,
2324
}

0 commit comments

Comments
 (0)