aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r--crates/ra_analysis/src/completion.rs2
-rw-r--r--crates/ra_analysis/src/completion/complete_fn_param.rs3
-rw-r--r--crates/ra_analysis/src/completion/complete_keyword.rs16
-rw-r--r--crates/ra_analysis/src/completion/complete_path.rs8
-rw-r--r--crates/ra_analysis/src/completion/complete_scope.rs14
-rw-r--r--crates/ra_analysis/src/completion/complete_snippet.rs37
-rw-r--r--crates/ra_analysis/src/completion/completion_item.rs54
-rw-r--r--crates/ra_analysis/src/lib.rs2
8 files changed, 84 insertions, 52 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs
index 2d61a3aef..d742d6295 100644
--- a/crates/ra_analysis/src/completion.rs
+++ b/crates/ra_analysis/src/completion.rs
@@ -18,7 +18,7 @@ use crate::{
18 }, 18 },
19}; 19};
20 20
21pub use crate::completion::completion_item::{CompletionItem, InsertText}; 21pub use crate::completion::completion_item::{CompletionItem, InsertText, CompletionItemKind};
22 22
23/// Main entry point for copmletion. We run comletion as a two-phase process. 23/// Main entry point for copmletion. We run comletion as a two-phase process.
24/// 24///
diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs
index 6a6213e67..bb5fdfda0 100644
--- a/crates/ra_analysis/src/completion/complete_fn_param.rs
+++ b/crates/ra_analysis/src/completion/complete_fn_param.rs
@@ -34,9 +34,8 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
34 } 34 }
35 }) 35 })
36 .for_each(|(label, lookup)| { 36 .for_each(|(label, lookup)| {
37 CompletionItem::new(label) 37 CompletionItem::new(CompletionKind::Magic, label)
38 .lookup_by(lookup) 38 .lookup_by(lookup)
39 .kind(CompletionKind::Magic)
40 .add_to(acc) 39 .add_to(acc)
41 }); 40 });
42 41
diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs
index dead15bb6..5427fcb11 100644
--- a/crates/ra_analysis/src/completion/complete_keyword.rs
+++ b/crates/ra_analysis/src/completion/complete_keyword.rs
@@ -5,7 +5,14 @@ use ra_syntax::{
5 SyntaxKind::*, SyntaxNodeRef, 5 SyntaxKind::*, SyntaxNodeRef,
6}; 6};
7 7
8use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind::*}; 8use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind};
9
10fn keyword(kw: &str, snippet: &str) -> CompletionItem {
11 CompletionItem::new(CompletionKind::Keyword, kw)
12 .kind(CompletionItemKind::Keyword)
13 .snippet(snippet)
14 .build()
15}
9 16
10pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { 17pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
11 if !ctx.is_trivial_path { 18 if !ctx.is_trivial_path {
@@ -60,13 +67,6 @@ fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option<CompletionItem>
60 Some(keyword("return", snip)) 67 Some(keyword("return", snip))
61} 68}
62 69
63fn keyword(kw: &str, snippet: &str) -> CompletionItem {
64 CompletionItem::new(kw)
65 .kind(Keyword)
66 .snippet(snippet)
67 .build()
68}
69
70#[cfg(test)] 70#[cfg(test)]
71mod tests { 71mod tests {
72 use crate::completion::{CompletionKind, check_completion}; 72 use crate::completion::{CompletionKind, check_completion};
diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs
index 5fc24af72..ad4d68a33 100644
--- a/crates/ra_analysis/src/completion/complete_path.rs
+++ b/crates/ra_analysis/src/completion/complete_path.rs
@@ -1,6 +1,6 @@
1use crate::{ 1use crate::{
2 Cancelable, 2 Cancelable,
3 completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, 3 completion::{CompletionItem, Completions, CompletionKind, CompletionContext},
4}; 4};
5 5
6pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 6pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
@@ -17,9 +17,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
17 _ => return Ok(()), 17 _ => return Ok(()),
18 }; 18 };
19 let module_scope = target_module.scope(ctx.db)?; 19 let module_scope = target_module.scope(ctx.db)?;
20 module_scope.entries().for_each(|(name, _res)| { 20 module_scope.entries().for_each(|(name, res)| {
21 CompletionItem::new(name.to_string()) 21 CompletionItem::new(CompletionKind::Reference, name.to_string())
22 .kind(Reference) 22 .from_resolution(ctx.db, res)
23 .add_to(acc) 23 .add_to(acc)
24 }); 24 });
25 Ok(()) 25 Ok(())
diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs
index d07c0e46d..82610d63f 100644
--- a/crates/ra_analysis/src/completion/complete_scope.rs
+++ b/crates/ra_analysis/src/completion/complete_scope.rs
@@ -3,7 +3,7 @@ use ra_syntax::TextUnit;
3 3
4use crate::{ 4use crate::{
5 Cancelable, 5 Cancelable,
6 completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, 6 completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
7}; 7};
8 8
9pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 9pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> {
@@ -29,9 +29,9 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
29 } 29 }
30 } 30 }
31 }) 31 })
32 .for_each(|(name, _res)| { 32 .for_each(|(name, res)| {
33 CompletionItem::new(name.to_string()) 33 CompletionItem::new(CompletionKind::Reference, name.to_string())
34 .kind(Reference) 34 .from_resolution(ctx.db, res)
35 .add_to(acc) 35 .add_to(acc)
36 }); 36 });
37 } 37 }
@@ -46,12 +46,12 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit)
46 .flat_map(|scope| scopes.entries(scope).iter()) 46 .flat_map(|scope| scopes.entries(scope).iter())
47 .filter(|entry| shadowed.insert(entry.name())) 47 .filter(|entry| shadowed.insert(entry.name()))
48 .for_each(|entry| { 48 .for_each(|entry| {
49 CompletionItem::new(entry.name().to_string()) 49 CompletionItem::new(CompletionKind::Reference, entry.name().to_string())
50 .kind(Reference) 50 .kind(CompletionItemKind::Binding)
51 .add_to(acc) 51 .add_to(acc)
52 }); 52 });
53 if scopes.self_param.is_some() { 53 if scopes.self_param.is_some() {
54 CompletionItem::new("self").kind(Reference).add_to(acc); 54 CompletionItem::new(CompletionKind::Reference, "self").add_to(acc);
55 } 55 }
56} 56}
57 57
diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs
index ccd68832b..fb9da0a4f 100644
--- a/crates/ra_analysis/src/completion/complete_snippet.rs
+++ b/crates/ra_analysis/src/completion/complete_snippet.rs
@@ -1,38 +1,35 @@
1use crate::completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}; 1use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionItemKind, CompletionContext, completion_item::Builder};
2
3fn snippet(label: &str, snippet: &str) -> Builder {
4 CompletionItem::new(CompletionKind::Snippet, label)
5 .snippet(snippet)
6 .kind(CompletionItemKind::Snippet)
7}
2 8
3pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { 9pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
4 if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { 10 if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
5 return; 11 return;
6 } 12 }
7 CompletionItem::new("pd") 13 snippet("pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
8 .snippet("eprintln!(\"$0 = {:?}\", $0);") 14 snippet("ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
9 .kind(Snippet)
10 .add_to(acc);
11 CompletionItem::new("ppd")
12 .snippet("eprintln!(\"$0 = {:#?}\", $0);")
13 .kind(Snippet)
14 .add_to(acc);
15} 15}
16 16
17pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { 17pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
18 if !ctx.is_new_item { 18 if !ctx.is_new_item {
19 return; 19 return;
20 } 20 }
21 CompletionItem::new("Test function") 21 snippet(
22 .lookup_by("tfn") 22 "Test function",
23 .snippet( 23 "\
24 "\
25#[test] 24#[test]
26fn ${1:feature}() { 25fn ${1:feature}() {
27 $0 26 $0
28}", 27}",
29 ) 28 )
30 .kind(Snippet) 29 .lookup_by("tfn")
31 .add_to(acc); 30 .add_to(acc);
32 CompletionItem::new("pub(crate)") 31
33 .snippet("pub(crate) $0") 32 snippet("pub(crate)", "pub(crate) $0").add_to(acc);
34 .kind(Snippet)
35 .add_to(acc);
36} 33}
37 34
38#[cfg(test)] 35#[cfg(test)]
diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs
index d5d751759..911f08468 100644
--- a/crates/ra_analysis/src/completion/completion_item.rs
+++ b/crates/ra_analysis/src/completion/completion_item.rs
@@ -1,13 +1,17 @@
1use crate::db;
2
1/// `CompletionItem` describes a single completion variant in the editor pop-up. 3/// `CompletionItem` describes a single completion variant in the editor pop-up.
2/// It is basically a POD with various properties. To construct a 4/// It is basically a POD with various properties. To construct a
3/// `CompletionItem`, use `new` method and the `Builder` struct. 5/// `CompletionItem`, use `new` method and the `Builder` struct.
4#[derive(Debug)] 6#[derive(Debug)]
5pub struct CompletionItem { 7pub struct CompletionItem {
8 /// Used only internally in tests, to check only specific kind of
9 /// completion.
10 completion_kind: CompletionKind,
6 label: String, 11 label: String,
7 lookup: Option<String>, 12 lookup: Option<String>,
8 snippet: Option<String>, 13 snippet: Option<String>,
9 /// Used only internally in test, to check only specific kind of completion. 14 kind: Option<CompletionItemKind>,
10 kind: CompletionKind,
11} 15}
12 16
13pub enum InsertText { 17pub enum InsertText {
@@ -15,6 +19,15 @@ pub enum InsertText {
15 Snippet { text: String }, 19 Snippet { text: String },
16} 20}
17 21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub enum CompletionItemKind {
24 Snippet,
25 Keyword,
26 Module,
27 Function,
28 Binding,
29}
30
18#[derive(Debug, PartialEq, Eq)] 31#[derive(Debug, PartialEq, Eq)]
19pub(crate) enum CompletionKind { 32pub(crate) enum CompletionKind {
20 /// Parser-based keyword completion. 33 /// Parser-based keyword completion.
@@ -24,17 +37,17 @@ pub(crate) enum CompletionKind {
24 /// "Secret sauce" completions. 37 /// "Secret sauce" completions.
25 Magic, 38 Magic,
26 Snippet, 39 Snippet,
27 Unspecified,
28} 40}
29 41
30impl CompletionItem { 42impl CompletionItem {
31 pub(crate) fn new(label: impl Into<String>) -> Builder { 43 pub(crate) fn new(completion_kind: CompletionKind, label: impl Into<String>) -> Builder {
32 let label = label.into(); 44 let label = label.into();
33 Builder { 45 Builder {
46 completion_kind,
34 label, 47 label,
35 lookup: None, 48 lookup: None,
36 snippet: None, 49 snippet: None,
37 kind: CompletionKind::Unspecified, 50 kind: None,
38 } 51 }
39 } 52 }
40 /// What user sees in pop-up in the UI. 53 /// What user sees in pop-up in the UI.
@@ -57,15 +70,20 @@ impl CompletionItem {
57 Some(it) => InsertText::Snippet { text: it.clone() }, 70 Some(it) => InsertText::Snippet { text: it.clone() },
58 } 71 }
59 } 72 }
73
74 pub fn kind(&self) -> Option<CompletionItemKind> {
75 self.kind
76 }
60} 77}
61 78
62/// A helper to make `CompletionItem`s. 79/// A helper to make `CompletionItem`s.
63#[must_use] 80#[must_use]
64pub(crate) struct Builder { 81pub(crate) struct Builder {
82 completion_kind: CompletionKind,
65 label: String, 83 label: String,
66 lookup: Option<String>, 84 lookup: Option<String>,
67 snippet: Option<String>, 85 snippet: Option<String>,
68 kind: CompletionKind, 86 kind: Option<CompletionItemKind>,
69} 87}
70 88
71impl Builder { 89impl Builder {
@@ -79,6 +97,7 @@ impl Builder {
79 lookup: self.lookup, 97 lookup: self.lookup,
80 snippet: self.snippet, 98 snippet: self.snippet,
81 kind: self.kind, 99 kind: self.kind,
100 completion_kind: self.completion_kind,
82 } 101 }
83 } 102 }
84 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { 103 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
@@ -89,8 +108,25 @@ impl Builder {
89 self.snippet = Some(snippet.into()); 108 self.snippet = Some(snippet.into());
90 self 109 self
91 } 110 }
92 pub(crate) fn kind(mut self, kind: CompletionKind) -> Builder { 111 pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
93 self.kind = kind; 112 self.kind = Some(kind);
113 self
114 }
115 pub(crate) fn from_resolution(
116 mut self,
117 db: &db::RootDatabase,
118 resolution: &hir::Resolution,
119 ) -> Builder {
120 if let Some(def_id) = resolution.def_id {
121 if let Ok(def) = def_id.resolve(db) {
122 let kind = match def {
123 hir::Def::Module(..) => CompletionItemKind::Module,
124 hir::Def::Function(..) => CompletionItemKind::Function,
125 _ => return self,
126 };
127 self.kind = Some(kind);
128 }
129 }
94 self 130 self
95 } 131 }
96} 132}
@@ -154,7 +190,7 @@ impl Completions {
154 fn debug_render(&self, kind: CompletionKind) -> String { 190 fn debug_render(&self, kind: CompletionKind) -> String {
155 let mut res = String::new(); 191 let mut res = String::new();
156 for c in self.buf.iter() { 192 for c in self.buf.iter() {
157 if c.kind == kind { 193 if c.completion_kind == kind {
158 if let Some(lookup) = &c.lookup { 194 if let Some(lookup) = &c.lookup {
159 res.push_str(lookup); 195 res.push_str(lookup);
160 res.push_str(&format!(" {:?}", c.label)); 196 res.push_str(&format!(" {:?}", c.label));
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index b2f4cd228..6fd157880 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -30,7 +30,7 @@ use crate::{
30}; 30};
31 31
32pub use crate::{ 32pub use crate::{
33 completion::{CompletionItem, InsertText}, 33 completion::{CompletionItem, CompletionItemKind, InsertText},
34}; 34};
35pub use ra_editor::{ 35pub use ra_editor::{
36 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, 36 FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode,