Skip to content

Commit 30948e1

Browse files
committed
simplify
1 parent 6ec4ea8 commit 30948e1

File tree

7 files changed

+37
-36
lines changed

7 files changed

+37
-36
lines changed

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
110110
if !ctx.config.enable_imports_on_the_fly {
111111
return None;
112112
}
113-
if ctx.use_item_syntax.is_some()
114-
|| ctx.attribute_under_caret.is_some()
115-
|| ctx.mod_declaration_under_caret.is_some()
116-
|| ctx.record_lit_syntax.is_some()
117-
|| ctx.has_impl_or_trait_parent()
118-
{
113+
if ctx.use_item_syntax.is_some() || ctx.is_path_disallowed() {
119114
return None;
120115
}
121116
let potential_import_name = {

crates/ide_completion/src/completions/lifetime.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext)
88
if !ctx.lifetime_allowed {
99
return;
1010
}
11+
let lp_string;
1112
let param_lifetime = match (
1213
&ctx.lifetime_syntax,
1314
ctx.lifetime_param_syntax.as_ref().and_then(|lp| lp.lifetime()),
1415
) {
1516
(Some(lt), Some(lp)) if lp == lt.clone() => return,
16-
(Some(_), Some(lp)) => Some(lp.to_string()),
17+
(Some(_), Some(lp)) => {
18+
lp_string = lp.to_string();
19+
Some(&lp_string)
20+
}
1721
_ => None,
1822
};
1923

2024
ctx.scope.process_all_names(&mut |name, res| {
2125
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
22-
if param_lifetime != Some(name.to_string()) {
23-
acc.add_resolution(ctx, name.to_string(), &res);
26+
let name = name.to_string();
27+
if param_lifetime != Some(&name) {
28+
acc.add_resolution(ctx, name, &res);
2429
}
2530
}
2631
});

crates/ide_completion/src/completions/macro_in_item_position.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use crate::{CompletionContext, Completions};
44

55
pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
66
// Show only macros in top level.
7-
if ctx.is_new_item {
8-
ctx.scope.process_all_names(&mut |name, res| {
9-
if let hir::ScopeDef::MacroDef(mac) = res {
10-
acc.add_macro(ctx, Some(name.to_string()), mac);
11-
}
12-
})
7+
if !ctx.is_new_item {
8+
return;
139
}
10+
11+
ctx.scope.process_all_names(&mut |name, res| {
12+
if let hir::ScopeDef::MacroDef(mac) = res {
13+
acc.add_macro(ctx, Some(name.to_string()), mac);
14+
}
15+
})
1416
}
1517

1618
#[cfg(test)]

crates/ide_completion/src/completions/qualified_path.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ use syntax::AstNode;
77
use crate::{CompletionContext, Completions};
88

99
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
10+
if ctx.is_path_disallowed() {
11+
return;
12+
}
1013
let path = match &ctx.path_qual {
1114
Some(path) => path.clone(),
1215
None => return,
1316
};
1417

15-
if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() {
16-
return;
17-
}
18-
19-
let context_module = ctx.scope.module();
20-
2118
let resolution = match ctx.sema.resolve_path(&path) {
2219
Some(res) => res,
2320
None => return,
2421
};
22+
let context_module = ctx.scope.module();
2523

2624
// Add associated types on type parameters and `Self`.
2725
resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {

crates/ide_completion/src/completions/record.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
1313
let ty = ctx.sema.type_of_expr(&Expr::RecordExpr(record_lit.clone()));
1414
let default_trait = FamousDefs(&ctx.sema, ctx.krate).core_default_Default();
1515
let impl_default_trait = default_trait
16-
.and_then(|default_trait| ty.map(|ty| ty.impls_trait(ctx.db, default_trait, &[])))
17-
.unwrap_or(false);
16+
.zip(ty)
17+
.map_or(false, |(default_trait, ty)| ty.impls_trait(ctx.db, default_trait, &[]));
1818

1919
let missing_fields = ctx.sema.record_literal_missing_fields(record_lit);
2020
if impl_default_trait && !missing_fields.is_empty() {
2121
let completion_text = "..Default::default()";
22-
let completion_text = completion_text
23-
.strip_prefix(ctx.token.to_string().as_str())
24-
.unwrap_or(completion_text);
2522
let mut item = CompletionItem::new(
2623
CompletionKind::Snippet,
2724
ctx.source_range(),
28-
"..Default::default()",
25+
completion_text,
2926
);
27+
let completion_text =
28+
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
3029
item.insert_text(completion_text).kind(SymbolKind::Field);
3130
item.add_to(acc);
3231
}

crates/ide_completion/src/completions/unqualified_path.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
99
if !ctx.is_trivial_path {
1010
return;
1111
}
12-
if ctx.record_lit_syntax.is_some()
13-
|| ctx.record_pat_syntax.is_some()
14-
|| ctx.attribute_under_caret.is_some()
15-
|| ctx.mod_declaration_under_caret.is_some()
16-
|| ctx.has_impl_or_trait_parent()
17-
{
12+
if ctx.is_path_disallowed() {
1813
return;
1914
}
2015

crates/ide_completion/src/context.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,13 @@ pub(crate) struct CompletionContext<'a> {
115115
pub(super) is_path_type: bool,
116116
pub(super) has_type_args: bool,
117117
pub(super) attribute_under_caret: Option<ast::Attr>,
118-
pub(super) locals: Vec<(String, Local)>,
119-
120118
pub(super) mod_declaration_under_caret: Option<ast::Module>,
119+
pub(super) locals: Vec<(String, Local)>,
121120

122121
// keyword patterns
123122
pub(super) previous_token: Option<SyntaxToken>,
124-
pub(super) in_loop_body: bool,
125123
pub(super) prev_sibling: Option<PrevSibling>,
124+
pub(super) in_loop_body: bool,
126125
pub(super) is_match_arm: bool,
127126
pub(super) incomplete_let: bool,
128127

@@ -316,6 +315,14 @@ impl<'a> CompletionContext<'a> {
316315
self.prev_sibling.is_some()
317316
}
318317

318+
pub(crate) fn is_path_disallowed(&self) -> bool {
319+
self.record_lit_syntax.is_some()
320+
|| self.record_pat_syntax.is_some()
321+
|| self.attribute_under_caret.is_some()
322+
|| self.mod_declaration_under_caret.is_some()
323+
|| self.has_impl_or_trait_parent()
324+
}
325+
319326
fn fill_keyword_patterns(&mut self, file_with_fake_ident: &SyntaxNode, offset: TextSize) {
320327
let fake_ident_token = file_with_fake_ident.token_at_offset(offset).right_biased().unwrap();
321328
let syntax_element = NodeOrToken::Token(fake_ident_token);

0 commit comments

Comments
 (0)