aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_dot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_dot.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs45
1 files changed, 27 insertions, 18 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs
index 76c2f8173..32fd497be 100644
--- a/crates/ra_ide_api/src/completion/complete_dot.rs
+++ b/crates/ra_ide_api/src/completion/complete_dot.rs
@@ -1,6 +1,7 @@
1use hir::{Ty, Def}; 1use hir::{Ty, Def};
2 2
3use crate::completion::{CompletionContext, Completions, CompletionKind, CompletionItem, CompletionItemKind}; 3use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
4use crate::completion::completion_item::CompletionKind;
4 5
5/// Complete dot accesses, i.e. fields or methods (currently only fields). 6/// Complete dot accesses, i.e. fields or methods (currently only fields).
6pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { 7pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
@@ -32,6 +33,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
32 for field in s.fields(ctx.db) { 33 for field in s.fields(ctx.db) {
33 CompletionItem::new( 34 CompletionItem::new(
34 CompletionKind::Reference, 35 CompletionKind::Reference,
36 ctx.source_range(),
35 field.name().to_string(), 37 field.name().to_string(),
36 ) 38 )
37 .kind(CompletionItemKind::Field) 39 .kind(CompletionItemKind::Field)
@@ -45,9 +47,13 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
45 } 47 }
46 Ty::Tuple(fields) => { 48 Ty::Tuple(fields) => {
47 for (i, _ty) in fields.iter().enumerate() { 49 for (i, _ty) in fields.iter().enumerate() {
48 CompletionItem::new(CompletionKind::Reference, i.to_string()) 50 CompletionItem::new(
49 .kind(CompletionItemKind::Field) 51 CompletionKind::Reference,
50 .add_to(acc); 52 ctx.source_range(),
53 i.to_string(),
54 )
55 .kind(CompletionItemKind::Field)
56 .add_to(acc);
51 } 57 }
52 } 58 }
53 _ => {} 59 _ => {}
@@ -59,10 +65,14 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty
59 receiver.iterate_methods(ctx.db, |func| { 65 receiver.iterate_methods(ctx.db, |func| {
60 let sig = func.signature(ctx.db); 66 let sig = func.signature(ctx.db);
61 if sig.has_self_param() { 67 if sig.has_self_param() {
62 CompletionItem::new(CompletionKind::Reference, sig.name().to_string()) 68 CompletionItem::new(
63 .from_function(ctx, func) 69 CompletionKind::Reference,
64 .kind(CompletionItemKind::Method) 70 ctx.source_range(),
65 .add_to(acc); 71 sig.name().to_string(),
72 )
73 .from_function(ctx, func)
74 .kind(CompletionItemKind::Method)
75 .add_to(acc);
66 } 76 }
67 None::<()> 77 None::<()>
68 }); 78 });
@@ -71,27 +81,29 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty
71#[cfg(test)] 81#[cfg(test)]
72mod tests { 82mod tests {
73 use crate::completion::*; 83 use crate::completion::*;
84 use crate::completion::completion_item::check_completion;
74 85
75 fn check_ref_completion(code: &str, expected_completions: &str) { 86 fn check_ref_completion(name: &str, code: &str) {
76 check_completion(code, expected_completions, CompletionKind::Reference); 87 check_completion(name, code, CompletionKind::Reference);
77 } 88 }
78 89
79 #[test] 90 #[test]
80 fn test_struct_field_completion() { 91 fn test_struct_field_completion() {
81 check_ref_completion( 92 check_ref_completion(
93 "struct_field_completion",
82 r" 94 r"
83 struct A { the_field: u32 } 95 struct A { the_field: u32 }
84 fn foo(a: A) { 96 fn foo(a: A) {
85 a.<|> 97 a.<|>
86 } 98 }
87 ", 99 ",
88 r#"the_field "u32""#,
89 ); 100 );
90 } 101 }
91 102
92 #[test] 103 #[test]
93 fn test_struct_field_completion_self() { 104 fn test_struct_field_completion_self() {
94 check_ref_completion( 105 check_ref_completion(
106 "struct_field_completion_self",
95 r" 107 r"
96 struct A { the_field: (u32,) } 108 struct A { the_field: (u32,) }
97 impl A { 109 impl A {
@@ -100,14 +112,13 @@ mod tests {
100 } 112 }
101 } 113 }
102 ", 114 ",
103 r#"the_field "(u32,)"
104 foo "foo($0)""#,
105 ); 115 );
106 } 116 }
107 117
108 #[test] 118 #[test]
109 fn test_struct_field_completion_autoderef() { 119 fn test_struct_field_completion_autoderef() {
110 check_ref_completion( 120 check_ref_completion(
121 "struct_field_completion_autoderef",
111 r" 122 r"
112 struct A { the_field: (u32, i32) } 123 struct A { the_field: (u32, i32) }
113 impl A { 124 impl A {
@@ -116,27 +127,26 @@ mod tests {
116 } 127 }
117 } 128 }
118 ", 129 ",
119 r#"the_field "(u32, i32)"
120 foo "foo($0)""#,
121 ); 130 );
122 } 131 }
123 132
124 #[test] 133 #[test]
125 fn test_no_struct_field_completion_for_method_call() { 134 fn test_no_struct_field_completion_for_method_call() {
126 check_ref_completion( 135 check_ref_completion(
136 "no_struct_field_completion_for_method_call",
127 r" 137 r"
128 struct A { the_field: u32 } 138 struct A { the_field: u32 }
129 fn foo(a: A) { 139 fn foo(a: A) {
130 a.<|>() 140 a.<|>()
131 } 141 }
132 ", 142 ",
133 r#""#,
134 ); 143 );
135 } 144 }
136 145
137 #[test] 146 #[test]
138 fn test_method_completion() { 147 fn test_method_completion() {
139 check_ref_completion( 148 check_ref_completion(
149 "method_completion",
140 r" 150 r"
141 struct A {} 151 struct A {}
142 impl A { 152 impl A {
@@ -146,13 +156,13 @@ mod tests {
146 a.<|> 156 a.<|>
147 } 157 }
148 ", 158 ",
149 r#"the_method "the_method($0)""#,
150 ); 159 );
151 } 160 }
152 161
153 #[test] 162 #[test]
154 fn test_no_non_self_method() { 163 fn test_no_non_self_method() {
155 check_ref_completion( 164 check_ref_completion(
165 "no_non_self_method",
156 r" 166 r"
157 struct A {} 167 struct A {}
158 impl A { 168 impl A {
@@ -162,7 +172,6 @@ mod tests {
162 a.<|> 172 a.<|>
163 } 173 }
164 ", 174 ",
165 r#""#,
166 ); 175 );
167 } 176 }
168} 177}