Skip to content

Commit 5904726

Browse files
bors[bot]Veykril
andauthored
Merge #9171
9171: internal: simplify r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 7f16b42 + 2987e05 commit 5904726

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

crates/ide_completion/src/completions/keyword.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::iter;
55
use syntax::{SyntaxKind, T};
66

77
use crate::{
8-
patterns::ImmediateLocation, CompletionContext, CompletionItem, CompletionItemKind,
9-
CompletionKind, Completions,
8+
context::PathCompletionContext, patterns::ImmediateLocation, CompletionContext, CompletionItem,
9+
CompletionItemKind, CompletionKind, Completions,
1010
};
1111

1212
pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
@@ -128,8 +128,15 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
128128
add_keyword("mut", "mut ");
129129
}
130130

131-
if ctx.in_loop_body {
132-
if ctx.can_be_stmt() {
131+
let (can_be_stmt, in_loop_body) = match ctx.path_context {
132+
Some(PathCompletionContext {
133+
is_trivial_path: true, can_be_stmt, in_loop_body, ..
134+
}) => (can_be_stmt, in_loop_body),
135+
_ => return,
136+
};
137+
138+
if in_loop_body {
139+
if can_be_stmt {
133140
add_keyword("continue", "continue;");
134141
add_keyword("break", "break;");
135142
} else {
@@ -138,17 +145,14 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
138145
}
139146
}
140147

141-
if !ctx.is_trivial_path() {
142-
return;
143-
}
144148
let fn_def = match &ctx.function_def {
145149
Some(it) => it,
146150
None => return,
147151
};
148152

149153
add_keyword(
150154
"return",
151-
match (ctx.can_be_stmt(), fn_def.ret_type().is_some()) {
155+
match (can_be_stmt, fn_def.ret_type().is_some()) {
152156
(true, true) => "return $0;",
153157
(true, false) => "return;",
154158
(false, true) => "return $0",

crates/ide_completion/src/completions/snippet.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use ide_db::helpers::SnippetCap;
44

55
use crate::{
6-
item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
7-
Completions,
6+
context::PathCompletionContext, item::Builder, CompletionContext, CompletionItem,
7+
CompletionItemKind, CompletionKind, Completions,
88
};
99

1010
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
@@ -14,15 +14,21 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
1414
}
1515

1616
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
17-
if !(ctx.is_trivial_path() && ctx.function_def.is_some()) {
17+
if ctx.function_def.is_none() {
1818
return;
1919
}
20+
21+
let can_be_stmt = match ctx.path_context {
22+
Some(PathCompletionContext { is_trivial_path: true, can_be_stmt, .. }) => can_be_stmt,
23+
_ => return,
24+
};
25+
2026
let cap = match ctx.config.snippet_cap {
2127
Some(it) => it,
2228
None => return,
2329
};
2430

25-
if ctx.can_be_stmt() {
31+
if can_be_stmt {
2632
snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
2733
snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
2834
}

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,13 @@
3434
use hir::{self, HasAttrs, HasSource};
3535
use ide_db::{traits::get_missing_assoc_items, SymbolKind};
3636
use syntax::{
37-
ast::{self, edit, Impl},
37+
ast::{self, edit},
3838
display::function_declaration,
39-
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, T,
39+
AstNode, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, T,
4040
};
4141
use text_edit::TextEdit;
4242

43-
use crate::{
44-
CompletionContext,
45-
CompletionItem,
46-
CompletionItemKind,
47-
CompletionKind,
48-
Completions,
49-
// display::function_declaration,
50-
};
43+
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
5144

5245
#[derive(Debug, PartialEq, Eq)]
5346
enum ImplCompletionKind {
@@ -58,7 +51,7 @@ enum ImplCompletionKind {
5851
}
5952

6053
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
61-
if let Some((kind, trigger, impl_def)) = completion_match(ctx) {
54+
if let Some((kind, trigger, impl_def)) = completion_match(ctx.token.clone()) {
6255
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| match item {
6356
hir::AssocItem::Function(fn_item)
6457
if kind == ImplCompletionKind::All || kind == ImplCompletionKind::Fn =>
@@ -80,8 +73,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
8073
}
8174
}
8275

83-
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, SyntaxNode, Impl)> {
84-
let mut token = ctx.token.clone();
76+
fn completion_match(mut token: SyntaxToken) -> Option<(ImplCompletionKind, SyntaxNode, ast::Impl)> {
8577
// For keyword without name like `impl .. { fn $0 }`, the current position is inside
8678
// the whitespace token, which is outside `FN` syntax node.
8779
// We need to follow the previous token in this case.

crates/ide_completion/src/context.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub(crate) struct PathCompletionContext {
4343
pub(super) can_be_stmt: bool,
4444
/// `true` if we expect an expression at the cursor position.
4545
pub(super) is_expr: bool,
46+
pub(super) in_loop_body: bool,
4647
}
4748

4849
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -94,7 +95,6 @@ pub(crate) struct CompletionContext<'a> {
9495
pub(super) active_parameter: Option<ActiveParameter>,
9596
pub(super) locals: Vec<(String, Local)>,
9697

97-
pub(super) in_loop_body: bool,
9898
pub(super) incomplete_let: bool,
9999

100100
no_completion_required: bool,
@@ -160,7 +160,6 @@ impl<'a> CompletionContext<'a> {
160160
path_context: None,
161161
active_parameter: ActiveParameter::at(db, position),
162162
locals,
163-
in_loop_body: false,
164163
incomplete_let: false,
165164
no_completion_required: false,
166165
};
@@ -324,10 +323,6 @@ impl<'a> CompletionContext<'a> {
324323
self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
325324
}
326325

327-
pub(crate) fn can_be_stmt(&self) -> bool {
328-
self.path_context.as_ref().map_or(false, |it| it.can_be_stmt)
329-
}
330-
331326
fn fill_impl_def(&mut self) {
332327
self.impl_def = self
333328
.sema
@@ -453,7 +448,6 @@ impl<'a> CompletionContext<'a> {
453448
let for_is_prev2 = for_is_prev2(syntax_element.clone());
454449
(fn_is_prev && !inside_impl_trait_block) || for_is_prev2
455450
};
456-
self.in_loop_body = is_in_loop_body(syntax_element.clone());
457451

458452
self.incomplete_let =
459453
syntax_element.ancestors().take(6).find_map(ast::LetStmt::cast).map_or(false, |it| {
@@ -584,7 +578,9 @@ impl<'a> CompletionContext<'a> {
584578
is_path_type: false,
585579
can_be_stmt: false,
586580
is_expr: false,
581+
in_loop_body: false,
587582
});
583+
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
588584
let path = segment.parent_path();
589585

590586
if let Some(p) = path.syntax().parent() {

crates/ide_completion/src/patterns.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,8 @@ fn test_for_is_prev2() {
272272
check_pattern_is_applicable(r"for i i$0", for_is_prev2);
273273
}
274274

275-
pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
276-
element
277-
.ancestors()
275+
pub(crate) fn is_in_loop_body(node: &SyntaxNode) -> bool {
276+
node.ancestors()
278277
.take_while(|it| it.kind() != FN && it.kind() != CLOSURE_EXPR)
279278
.find_map(|it| {
280279
let loop_body = match_ast! {
@@ -285,7 +284,7 @@ pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
285284
_ => None,
286285
}
287286
};
288-
loop_body.filter(|it| it.syntax().text_range().contains_range(element.text_range()))
287+
loop_body.filter(|it| it.syntax().text_range().contains_range(node.text_range()))
289288
})
290289
.is_some()
291290
}

0 commit comments

Comments
 (0)