aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/handlers/add_missing_impl_members.rs4
-rw-r--r--crates/ra_assists/src/utils.rs10
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs38
3 files changed, 23 insertions, 29 deletions
diff --git a/crates/ra_assists/src/handlers/add_missing_impl_members.rs b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
index 4c937b154..ab21388c8 100644
--- a/crates/ra_assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ra_assists/src/handlers/add_missing_impl_members.rs
@@ -6,8 +6,8 @@ use ra_syntax::{
6 6
7use crate::{ 7use crate::{
8 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams}, 8 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
9 Assist, AssistCtx, AssistId,
10 utils::{get_missing_impl_items, resolve_target_trait}, 9 utils::{get_missing_impl_items, resolve_target_trait},
10 Assist, AssistCtx, AssistId,
11}; 11};
12 12
13#[derive(PartialEq)] 13#[derive(PartialEq)]
@@ -129,7 +129,7 @@ fn add_missing_impl_members_inner(
129 ast::ImplItem::FnDef(def) => match mode { 129 ast::ImplItem::FnDef(def) => match mode {
130 AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(), 130 AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
131 AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(), 131 AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
132 } 132 },
133 _ => mode == AddMissingImplMembersMode::NoDefaultMethods, 133 _ => mode == AddMissingImplMembersMode::NoDefaultMethods,
134 }) 134 })
135 .collect::<Vec<_>>(); 135 .collect::<Vec<_>>();
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs
index 7628933fb..461f01536 100644
--- a/crates/ra_assists/src/utils.rs
+++ b/crates/ra_assists/src/utils.rs
@@ -9,14 +9,13 @@ use hir::db::HirDatabase;
9 9
10use rustc_hash::FxHashSet; 10use rustc_hash::FxHashSet;
11 11
12/// Generate a collection of associated items that are missing from a 12/// Generate a collection of associated items that are missing from a
13/// `impl Trait for` block. 13/// `impl Trait for` block.
14pub fn get_missing_impl_items( 14pub fn get_missing_impl_items(
15 db: &impl HirDatabase, 15 db: &impl HirDatabase,
16 analyzer: &hir::SourceAnalyzer, 16 analyzer: &hir::SourceAnalyzer,
17 impl_block: &ast::ImplBlock, 17 impl_block: &ast::ImplBlock,
18) -> Vec<hir::AssocItem> { 18) -> Vec<hir::AssocItem> {
19
20 // Names must be unique between constants and functions. However, type aliases 19 // Names must be unique between constants and functions. However, type aliases
21 // may share the same name as a function or constant. 20 // may share the same name as a function or constant.
22 let mut impl_fns_consts = FxHashSet::default(); 21 let mut impl_fns_consts = FxHashSet::default();
@@ -53,9 +52,10 @@ pub fn get_missing_impl_items(
53 .filter(|i| match i { 52 .filter(|i| match i {
54 hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()), 53 hir::AssocItem::Function(f) => !impl_fns_consts.contains(&f.name(db).to_string()),
55 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()), 54 hir::AssocItem::TypeAlias(t) => !impl_type.contains(&t.name(db).to_string()),
56 hir::AssocItem::Const(c) => { 55 hir::AssocItem::Const(c) => c
57 c.name(db).map(|n| !impl_fns_consts.contains(&n.to_string())).unwrap_or_default() 56 .name(db)
58 } 57 .map(|n| !impl_fns_consts.contains(&n.to_string()))
58 .unwrap_or_default(),
59 }) 59 })
60 .map(|i| i.clone()) 60 .map(|i| i.clone())
61 .collect() 61 .collect()
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs
index f6f4a99c5..cd3f016bf 100644
--- a/crates/ra_ide/src/completion/complete_trait_impl.rs
+++ b/crates/ra_ide/src/completion/complete_trait_impl.rs
@@ -2,53 +2,55 @@ use crate::completion::{
2 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, 2 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
3}; 3};
4 4
5use ra_syntax::{ast::{self, edit}, AstNode, SyntaxKind, TextRange};
6use hir::{self, Docs, HasSource}; 5use hir::{self, Docs, HasSource};
6use ra_syntax::{
7 ast::{self, edit},
8 AstNode, SyntaxKind, TextRange,
9};
7 10
8use ra_assists::utils::get_missing_impl_items; 11use ra_assists::utils::get_missing_impl_items;
9 12
10/// Analyzes the specified `CompletionContext` and provides magic completions 13/// Analyzes the specified `CompletionContext` and provides magic completions
11/// if the context falls within a `impl Trait for` block. 14/// if the context falls within a `impl Trait for` block.
12/// 15///
13/// # Completion Activation 16/// # Completion Activation
14/// The completion will activate when a user begins to type a function 17/// The completion will activate when a user begins to type a function
15/// definition, an associated type, or an associated constant. 18/// definition, an associated type, or an associated constant.
16/// 19///
17/// ### Functions 20/// ### Functions
18/// ```ignore 21/// ```ignore
19/// trait SomeTrait { 22/// trait SomeTrait {
20/// fn foo(&self); 23/// fn foo(&self);
21/// } 24/// }
22/// 25///
23/// impl SomeTrait for () { 26/// impl SomeTrait for () {
24/// fn <|> 27/// fn <|>
25/// } 28/// }
26/// ``` 29/// ```
27/// 30///
28/// ### Associated Types 31/// ### Associated Types
29/// ```ignore 32/// ```ignore
30/// trait SomeTrait { 33/// trait SomeTrait {
31/// type SomeType; 34/// type SomeType;
32/// } 35/// }
33/// 36///
34/// impl SomeTrait for () { 37/// impl SomeTrait for () {
35/// type <|> 38/// type <|>
36/// } 39/// }
37/// ``` 40/// ```
38/// 41///
39/// ### Associated Constants 42/// ### Associated Constants
40/// ```ignore 43/// ```ignore
41/// trait SomeTrait { 44/// trait SomeTrait {
42/// const SOME_CONST: u16; 45/// const SOME_CONST: u16;
43/// } 46/// }
44/// 47///
45/// impl SomeTrait for () { 48/// impl SomeTrait for () {
46/// const <|> 49/// const <|>
47/// } 50/// }
48/// ``` 51/// ```
49pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 52pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
50 53 // it is possible to have a parent `fn` and `impl` block. Ignore completion
51 // it is possible to have a parent `fn` and `impl` block. Ignore completion
52 // attempts from within a `fn` block. 54 // attempts from within a `fn` block.
53 if ctx.function_syntax.is_some() { 55 if ctx.function_syntax.is_some() {
54 return; 56 return;
@@ -111,11 +113,7 @@ fn add_type_alias_impl(
111 .add_to(acc); 113 .add_to(acc);
112} 114}
113 115
114fn add_const_impl( 116fn add_const_impl(acc: &mut Completions, ctx: &CompletionContext, const_: &hir::Const) {
115 acc: &mut Completions,
116 ctx: &CompletionContext,
117 const_: &hir::Const,
118) {
119 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value); 117 let snippet = make_const_compl_syntax(&const_.source(ctx.db).value);
120 118
121 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()) 119 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
@@ -131,12 +129,8 @@ fn make_const_compl_syntax(const_: &ast::ConstDef) -> String {
131 let const_start = const_.syntax().text_range().start(); 129 let const_start = const_.syntax().text_range().start();
132 let const_end = const_.syntax().text_range().end(); 130 let const_end = const_.syntax().text_range().end();
133 131
134 let start = const_ 132 let start =
135 .syntax() 133 const_.syntax().first_child_or_token().map_or(const_start, |f| f.text_range().start());
136 .first_child_or_token()
137 .map_or(
138 const_start,
139 |f| f.text_range().start());
140 134
141 let end = const_ 135 let end = const_
142 .syntax() 136 .syntax()