From cb3767f28a2f9e443b816b17d5d07b6a1cff90ab Mon Sep 17 00:00:00 2001 From: Martin Asquino Date: Mon, 4 Nov 2019 10:33:10 -0300 Subject: HirDatabase stored attributes --- crates/ra_ide_api/src/completion/presentation.rs | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index cb55d1875..d861303b7 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -1,8 +1,8 @@ //! This modules takes care of rendering various definitions as completion items. -use hir::{db::HirDatabase, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; +use hir::{db::HirDatabase, Attrs, Docs, HasSource, HirDisplay, ScopeDef, Ty, TypeWalk}; use join_to_string::join; -use ra_syntax::ast::{AttrsOwner, NameOwner}; +use ra_syntax::ast::NameOwner; use test_utils::tested_by; use crate::completion::{ @@ -18,11 +18,7 @@ impl Completions { field: hir::StructField, substs: &hir::Substs, ) { - let ast_node = field.source(ctx.db).ast; - let is_deprecated = match ast_node { - hir::FieldSource::Named(m) => is_deprecated(m), - hir::FieldSource::Pos(m) => is_deprecated(m), - }; + let is_deprecated = is_deprecated(field, ctx.db); CompletionItem::new( CompletionKind::Reference, ctx.source_range(), @@ -185,7 +181,7 @@ impl Completions { CompletionItem::new(CompletionKind::Reference, ctx.source_range(), ¯o_declaration) .kind(CompletionItemKind::Macro) .set_documentation(docs.clone()) - .set_deprecated(is_deprecated(ast_node)) + .set_deprecated(is_deprecated(macro_, ctx.db)) .detail(detail); builder = if ctx.use_item_syntax.is_some() { @@ -218,7 +214,7 @@ impl Completions { CompletionItemKind::Function }) .set_documentation(func.docs(ctx.db)) - .set_deprecated(is_deprecated(ast_node)) + .set_deprecated(is_deprecated(func, ctx.db)) .detail(detail); // Add `<>` for generic types @@ -250,7 +246,7 @@ impl Completions { CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) .kind(CompletionItemKind::Const) .set_documentation(constant.docs(ctx.db)) - .set_deprecated(is_deprecated(ast_node)) + .set_deprecated(is_deprecated(constant, ctx.db)) .detail(detail) .add_to(self); } @@ -266,13 +262,13 @@ impl Completions { CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) .kind(CompletionItemKind::TypeAlias) .set_documentation(type_alias.docs(ctx.db)) - .set_deprecated(is_deprecated(type_def)) + .set_deprecated(is_deprecated(type_alias, ctx.db)) .detail(detail) .add_to(self); } pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) { - let is_deprecated = is_deprecated(variant.source(ctx.db).ast); + let is_deprecated = is_deprecated(variant, ctx.db); let name = match variant.name(ctx.db) { Some(it) => it, None => return, @@ -291,8 +287,11 @@ impl Completions { } } -fn is_deprecated(node: impl AttrsOwner) -> bool { - node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated") +fn is_deprecated(node: impl Attrs, db: &impl HirDatabase) -> bool { + match node.attrs(db) { + None => false, + Some(attrs) => attrs.iter().any(|x| x.is_simple_atom("deprecated")), + } } fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { -- cgit v1.2.3 From b5349af05fca31af14f8997ed3f18027b1953517 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 Nov 2019 14:06:44 +0300 Subject: Cleanup complete_postfix --- .../ra_ide_api/src/completion/complete_postfix.rs | 109 +++++++++++---------- 1 file changed, 57 insertions(+), 52 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index 60ed3518b..4f9565441 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -1,5 +1,9 @@ //! FIXME: write short doc here +use hir::{Ty, TypeCtor}; +use ra_syntax::{ast::AstNode, TextRange, TextUnit}; +use ra_text_edit::TextEdit; + use crate::{ completion::{ completion_context::CompletionContext, @@ -7,9 +11,53 @@ use crate::{ }, CompletionItem, }; -use hir::{Ty, TypeCtor}; -use ra_syntax::{ast::AstNode, TextRange, TextUnit}; -use ra_text_edit::TextEdit; + +pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { + let dot_receiver = match &ctx.dot_receiver { + Some(it) => it, + None => return, + }; + + let receiver_text = if ctx.dot_receiver_is_ambiguous_float_literal { + let text = dot_receiver.syntax().text(); + let without_dot = ..text.len() - TextUnit::of_char('.'); + text.slice(without_dot).to_string() + } else { + dot_receiver.syntax().text().to_string() + }; + + let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver); + + if is_bool_or_unknown(receiver_ty) { + postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) + .add_to(acc); + postfix_snippet( + ctx, + "while", + "while expr {}", + &format!("while {} {{\n$0\n}}", receiver_text), + ) + .add_to(acc); + } + + postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc); + + postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc); + postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc); + + postfix_snippet( + ctx, + "match", + "match expr {}", + &format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text), + ) + .add_to(acc); + + postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc); + + postfix_snippet(ctx, "box", "Box::new(expr)", &format!("Box::new({})", receiver_text)) + .add_to(acc); +} fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { let edit = { @@ -24,62 +72,19 @@ fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: } fn is_bool_or_unknown(ty: Option) -> bool { - if let Some(ty) = ty { - match ty { - Ty::Apply(at) => match at.ctor { - TypeCtor::Bool => true, - _ => false, - }, - Ty::Unknown => true, - _ => false, - } - } else { - true - } -} - -pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { - if let Some(dot_receiver) = &ctx.dot_receiver { - let receiver_text = if ctx.dot_receiver_is_ambiguous_float_literal { - let text = dot_receiver.syntax().text(); - let without_dot = ..text.len() - TextUnit::of_char('.'); - text.slice(without_dot).to_string() - } else { - dot_receiver.syntax().text().to_string() - }; - let receiver_ty = ctx.analyzer.type_of(ctx.db, &dot_receiver); - if is_bool_or_unknown(receiver_ty) { - postfix_snippet(ctx, "if", "if expr {}", &format!("if {} {{$0}}", receiver_text)) - .add_to(acc); - postfix_snippet( - ctx, - "while", - "while expr {}", - &format!("while {} {{\n$0\n}}", receiver_text), - ) - .add_to(acc); - } - postfix_snippet(ctx, "not", "!expr", &format!("!{}", receiver_text)).add_to(acc); - postfix_snippet(ctx, "ref", "&expr", &format!("&{}", receiver_text)).add_to(acc); - postfix_snippet(ctx, "refm", "&mut expr", &format!("&mut {}", receiver_text)).add_to(acc); - postfix_snippet( - ctx, - "match", - "match expr {}", - &format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text), - ) - .add_to(acc); - postfix_snippet(ctx, "dbg", "dbg!(expr)", &format!("dbg!({})", receiver_text)).add_to(acc); - postfix_snippet(ctx, "box", "Box::new(expr)", &format!("Box::new({})", receiver_text)) - .add_to(acc); + match &ty { + Some(Ty::Apply(app)) if app.ctor == TypeCtor::Bool => true, + Some(Ty::Unknown) | None => true, + Some(_) => false, } } #[cfg(test)] mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; use insta::assert_debug_snapshot; + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + fn do_postfix_completion(code: &str) -> Vec { do_completion(code, CompletionKind::Postfix) } -- cgit v1.2.3