Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 83e8f3a

Browse files
committed
Move CompletionContext::incomplete_let into PathKind::Expr
1 parent 7369e51 commit 83e8f3a

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,18 @@ impl Completions {
110110
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
111111
}
112112

113-
pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
113+
pub(crate) fn add_keyword_snippet_expr(
114+
&mut self,
115+
ctx: &CompletionContext,
116+
kw: &str,
117+
snippet: &str,
118+
incomplete_let: bool,
119+
) {
114120
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
115121

116122
match ctx.config.snippet_cap {
117123
Some(cap) => {
118-
if snippet.ends_with('}') && ctx.incomplete_let {
124+
if snippet.ends_with('}') && incomplete_let {
119125
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
120126
cov_mark::hit!(let_semi);
121127
item.insert_snippet(cap, format!("{};", snippet));
@@ -130,6 +136,16 @@ impl Completions {
130136
item.add_to(self);
131137
}
132138

139+
pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
140+
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
141+
142+
match ctx.config.snippet_cap {
143+
Some(cap) => item.insert_snippet(cap, snippet),
144+
None => item.insert_text(if snippet.contains('$') { kw } else { snippet }),
145+
};
146+
item.add_to(self);
147+
}
148+
133149
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
134150
ctx.process_all_names(&mut |name, res| match res {
135151
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {

crates/ide-completion/src/completions/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(crate) fn complete_expr_path(
2626
wants_mut_token,
2727
in_condition,
2828
ty,
29+
incomplete_let,
2930
) = match path_ctx {
3031
&PathCompletionCtx {
3132
kind:
@@ -34,6 +35,7 @@ pub(crate) fn complete_expr_path(
3435
in_loop_body,
3536
after_if_expr,
3637
in_condition,
38+
incomplete_let,
3739
ref ref_expr_parent,
3840
ref is_func_update,
3941
ref innermost_ret_ty,
@@ -50,6 +52,7 @@ pub(crate) fn complete_expr_path(
5052
ref_expr_parent.as_ref().map(|it| it.mut_token().is_none()).unwrap_or(false),
5153
in_condition,
5254
innermost_ret_ty,
55+
incomplete_let,
5356
),
5457
_ => return,
5558
};
@@ -220,7 +223,8 @@ pub(crate) fn complete_expr_path(
220223
});
221224

222225
if !is_func_update {
223-
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
226+
let mut add_keyword =
227+
|kw, snippet| acc.add_keyword_snippet_expr(ctx, kw, snippet, incomplete_let);
224228

225229
if !in_block_expr {
226230
add_keyword("unsafe", "unsafe {\n $0\n}");

crates/ide-completion/src/context.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub(super) enum PathKind {
9393
after_if_expr: bool,
9494
/// Whether this expression is the direct condition of an if or while expression
9595
in_condition: bool,
96+
incomplete_let: bool,
9697
ref_expr_parent: Option<ast::RefExpr>,
9798
is_func_update: Option<ast::RecordExpr>,
9899
self_param: Option<hir::SelfParam>,
@@ -322,9 +323,6 @@ pub(crate) struct CompletionContext<'a> {
322323
/// The parent impl of the cursor position if it exists.
323324
// FIXME: This probably doesn't belong here
324325
pub(super) impl_def: Option<ast::Impl>,
325-
/// Are we completing inside a let statement with a missing semicolon?
326-
// FIXME: This should be part of PathKind::Expr
327-
pub(super) incomplete_let: bool,
328326

329327
// FIXME: This shouldn't exist
330328
pub(super) previous_token: Option<SyntaxToken>,
@@ -500,7 +498,6 @@ impl<'a> CompletionContext<'a> {
500498
expected_name: None,
501499
expected_type: None,
502500
impl_def: None,
503-
incomplete_let: false,
504501
previous_token: None,
505502
// dummy value, will be overwritten
506503
ident_ctx: IdentContext::UnexpandedAttrTT { fake_attribute_under_caret: None },

crates/ide-completion/src/context/analysis.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,6 @@ impl<'a> CompletionContext<'a> {
330330
self.previous_token =
331331
syntax_element.clone().into_token().and_then(previous_non_trivia_token);
332332

333-
self.incomplete_let =
334-
syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
335-
it.syntax().text_range().end() == syntax_element.text_range().end()
336-
});
337-
338333
(self.expected_type, self.expected_name) = self.expected_type_and_name();
339334

340335
// Overwrite the path kind for derives
@@ -767,6 +762,10 @@ impl<'a> CompletionContext<'a> {
767762
};
768763
let is_func_update = func_update_record(it);
769764
let in_condition = is_in_condition(&expr);
765+
let incomplete_let = it
766+
.parent()
767+
.and_then(ast::LetStmt::cast)
768+
.map_or(false, |it| it.semicolon_token().is_none());
770769

771770
PathKind::Expr {
772771
in_block_expr,
@@ -777,6 +776,7 @@ impl<'a> CompletionContext<'a> {
777776
is_func_update,
778777
innermost_ret_ty,
779778
self_param,
779+
incomplete_let,
780780
}
781781
};
782782
let make_path_kind_type = |ty: ast::Type| {

0 commit comments

Comments
 (0)