aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-21 11:02:14 +0000
committerAleksey Kladov <[email protected]>2018-12-21 11:02:14 +0000
commit74406ca8ea45df8b44cb38ecba4a5b561038c4a0 (patch)
treefcff4bfc73f566fc958862e5491d434d2fff0176 /crates
parent463e5af3f2ff54b74e4aeb73e75047c00b6339be (diff)
introduce completion_item module
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_analysis/src/completion.rs23
-rw-r--r--crates/ra_analysis/src/completion/completion_item.rs44
-rw-r--r--crates/ra_analysis/src/completion/reference_completion.rs8
3 files changed, 54 insertions, 21 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs
index f480af611..ed1b6dd0c 100644
--- a/crates/ra_analysis/src/completion.rs
+++ b/crates/ra_analysis/src/completion.rs
@@ -1,3 +1,4 @@
1mod completion_item;
1mod reference_completion; 2mod reference_completion;
2 3
3use ra_editor::find_node_at_offset; 4use ra_editor::find_node_at_offset;
@@ -17,15 +18,7 @@ use crate::{
17 Cancelable, FilePosition 18 Cancelable, FilePosition
18}; 19};
19 20
20#[derive(Debug)] 21pub use crate::completion::completion_item::CompletionItem;
21pub struct CompletionItem {
22 /// What user sees in pop-up
23 pub label: String,
24 /// What string is used for filtering, defaults to label
25 pub lookup: Option<String>,
26 /// What is inserted, defaults to label
27 pub snippet: Option<String>,
28}
29 22
30pub(crate) fn completions( 23pub(crate) fn completions(
31 db: &db::RootDatabase, 24 db: &db::RootDatabase,
@@ -63,6 +56,10 @@ pub(crate) fn completions(
63 Ok(res) 56 Ok(res)
64} 57}
65 58
59/// Complete repeated parametes, both name and type. For example, if all
60/// functions in a file have a `spam: &mut Spam` parameter, a completion with
61/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
62/// suggested.
66fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) { 63fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
67 let mut params = FxHashMap::default(); 64 let mut params = FxHashMap::default();
68 for node in ctx.ancestors() { 65 for node in ctx.ancestors() {
@@ -81,13 +78,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec<CompletionItem>) {
81 Some((label, lookup)) 78 Some((label, lookup))
82 } 79 }
83 }) 80 })
84 .for_each(|(label, lookup)| { 81 .for_each(|(label, lookup)| CompletionItem::new(label).lookup_by(lookup).add_to(acc));
85 acc.push(CompletionItem {
86 label,
87 lookup: Some(lookup),
88 snippet: None,
89 })
90 });
91 82
92 fn process<'a, N: ast::FnDefOwner<'a>>( 83 fn process<'a, N: ast::FnDefOwner<'a>>(
93 node: N, 84 node: N,
diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs
new file mode 100644
index 000000000..309b0108d
--- /dev/null
+++ b/crates/ra_analysis/src/completion/completion_item.rs
@@ -0,0 +1,44 @@
1#[derive(Debug)]
2pub struct CompletionItem {
3 /// What user sees in pop-up in the UI.
4 pub label: String,
5 /// What string is used for filtering, defaults to label.
6 pub lookup: Option<String>,
7 /// What is inserted, defaults to label.
8 pub snippet: Option<String>,
9}
10
11impl CompletionItem {
12 pub(crate) fn new(label: impl Into<String>) -> Builder {
13 let label = label.into();
14 Builder {
15 label,
16 lookup: None,
17 snippet: None,
18 }
19 }
20}
21
22pub(crate) struct Builder {
23 label: String,
24 lookup: Option<String>,
25 snippet: Option<String>,
26}
27
28impl Builder {
29 pub fn add_to(self, acc: &mut Vec<CompletionItem>) {
30 acc.push(self.build())
31 }
32
33 pub fn build(self) -> CompletionItem {
34 CompletionItem {
35 label: self.label,
36 lookup: self.lookup,
37 snippet: self.snippet,
38 }
39 }
40 pub fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
41 self.lookup = Some(lookup.into());
42 self
43 }
44}
diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs
index f483ed045..457ca13cc 100644
--- a/crates/ra_analysis/src/completion/reference_completion.rs
+++ b/crates/ra_analysis/src/completion/reference_completion.rs
@@ -6,11 +6,9 @@ use ra_syntax::{
6 ast::{self, LoopBodyOwner}, 6 ast::{self, LoopBodyOwner},
7 SyntaxKind::*, 7 SyntaxKind::*,
8}; 8};
9use hir::{ 9use hir::{
10 self, 10 self,
11 FnScopes, 11 FnScopes, Def, Path
12 Def,
13 Path,
14}; 12};
15 13
16use crate::{ 14use crate::{