add 'for' postfix completion

This commit is contained in:
mahdi-frms 2021-07-11 16:28:02 +04:30
parent 60e304c7b6
commit 7bae9c9187
2 changed files with 20 additions and 1 deletions

View File

@ -2,7 +2,10 @@
mod format_like;
use ide_db::{helpers::SnippetCap, ty_filter::TryEnum};
use ide_db::{
helpers::{FamousDefs, SnippetCap},
ty_filter::TryEnum,
};
use syntax::{
ast::{self, AstNode, AstToken},
SyntaxKind::{BLOCK_EXPR, EXPR_STMT},
@ -110,6 +113,18 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
.add_to(acc);
postfix_snippet(ctx, cap, dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
.add_to(acc);
} else if let Some(trait_) = FamousDefs(&ctx.sema, ctx.krate).core_iter_IntoIterator() {
if receiver_ty.impls_trait(ctx.db, trait_, &[]) {
postfix_snippet(
ctx,
cap,
dot_receiver,
"for",
"for ele in expr {}",
&format!("for ele in {} {{\n $0\n}}", receiver_text),
)
.add_to(acc);
}
}
postfix_snippet(ctx, cap, dot_receiver, "ref", "&expr", &format!("&{}", receiver_text))

View File

@ -134,6 +134,10 @@ impl FamousDefs<'_, '_> {
self.find_trait("core:iter:traits:iterator:Iterator")
}
pub fn core_iter_IntoIterator(&self) -> Option<Trait> {
self.find_trait("core:iter:traits:collect:IntoIterator")
}
pub fn core_iter(&self) -> Option<Module> {
self.find_module("core:iter")
}