aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion/completion_context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion/completion_context.rs')
-rw-r--r--crates/ra_analysis/src/completion/completion_context.rs29
1 files changed, 11 insertions, 18 deletions
diff --git a/crates/ra_analysis/src/completion/completion_context.rs b/crates/ra_analysis/src/completion/completion_context.rs
index 4584f355d..988c21c58 100644
--- a/crates/ra_analysis/src/completion/completion_context.rs
+++ b/crates/ra_analysis/src/completion/completion_context.rs
@@ -1,13 +1,9 @@
1use ra_editor::find_node_at_offset; 1use ra_editor::find_node_at_offset;
2use ra_text_edit::AtomTextEdit; 2use ra_text_edit::AtomTextEdit;
3use ra_syntax::{ 3use ra_syntax::{
4 algo::{find_leaf_at_offset, find_covering_node}, 4 AstNode, SyntaxNode, SourceFile, TextUnit, TextRange,
5 ast, 5 ast,
6 AstNode, 6 algo::{find_leaf_at_offset, find_covering_node},
7 SyntaxNodeRef,
8 SourceFileNode,
9 TextUnit,
10 TextRange,
11 SyntaxKind::*, 7 SyntaxKind::*,
12}; 8};
13use hir::source_binder; 9use hir::source_binder;
@@ -20,11 +16,11 @@ use crate::{db, FilePosition, Cancelable};
20pub(super) struct CompletionContext<'a> { 16pub(super) struct CompletionContext<'a> {
21 pub(super) db: &'a db::RootDatabase, 17 pub(super) db: &'a db::RootDatabase,
22 pub(super) offset: TextUnit, 18 pub(super) offset: TextUnit,
23 pub(super) leaf: SyntaxNodeRef<'a>, 19 pub(super) leaf: &'a SyntaxNode,
24 pub(super) module: Option<hir::Module>, 20 pub(super) module: Option<hir::Module>,
25 pub(super) function: Option<hir::Function>, 21 pub(super) function: Option<hir::Function>,
26 pub(super) function_syntax: Option<ast::FnDef<'a>>, 22 pub(super) function_syntax: Option<&'a ast::FnDef>,
27 pub(super) use_item_syntax: Option<ast::UseItem<'a>>, 23 pub(super) use_item_syntax: Option<&'a ast::UseItem>,
28 pub(super) is_param: bool, 24 pub(super) is_param: bool,
29 /// A single-indent path, like `foo`. 25 /// A single-indent path, like `foo`.
30 pub(super) is_trivial_path: bool, 26 pub(super) is_trivial_path: bool,
@@ -36,7 +32,7 @@ pub(super) struct CompletionContext<'a> {
36 /// Something is typed at the "top" level, in module or impl/trait. 32 /// Something is typed at the "top" level, in module or impl/trait.
37 pub(super) is_new_item: bool, 33 pub(super) is_new_item: bool,
38 /// The receiver if this is a field or method access, i.e. writing something.<|> 34 /// The receiver if this is a field or method access, i.e. writing something.<|>
39 pub(super) dot_receiver: Option<ast::Expr<'a>>, 35 pub(super) dot_receiver: Option<&'a ast::Expr>,
40 /// If this is a method call in particular, i.e. the () are already there. 36 /// If this is a method call in particular, i.e. the () are already there.
41 pub(super) is_method_call: bool, 37 pub(super) is_method_call: bool,
42} 38}
@@ -44,7 +40,7 @@ pub(super) struct CompletionContext<'a> {
44impl<'a> CompletionContext<'a> { 40impl<'a> CompletionContext<'a> {
45 pub(super) fn new( 41 pub(super) fn new(
46 db: &'a db::RootDatabase, 42 db: &'a db::RootDatabase,
47 original_file: &'a SourceFileNode, 43 original_file: &'a SourceFile,
48 position: FilePosition, 44 position: FilePosition,
49 ) -> Cancelable<Option<CompletionContext<'a>>> { 45 ) -> Cancelable<Option<CompletionContext<'a>>> {
50 let module = source_binder::module_from_position(db, position)?; 46 let module = source_binder::module_from_position(db, position)?;
@@ -71,7 +67,7 @@ impl<'a> CompletionContext<'a> {
71 Ok(Some(ctx)) 67 Ok(Some(ctx))
72 } 68 }
73 69
74 fn fill(&mut self, original_file: &'a SourceFileNode, offset: TextUnit) { 70 fn fill(&mut self, original_file: &'a SourceFile, offset: TextUnit) {
75 // Insert a fake ident to get a valid parse tree. We will use this file 71 // Insert a fake ident to get a valid parse tree. We will use this file
76 // to determine context, though the original_file will be used for 72 // to determine context, though the original_file will be used for
77 // actual completion. 73 // actual completion.
@@ -100,7 +96,7 @@ impl<'a> CompletionContext<'a> {
100 } 96 }
101 } 97 }
102 } 98 }
103 fn classify_name_ref(&mut self, original_file: &'a SourceFileNode, name_ref: ast::NameRef) { 99 fn classify_name_ref(&mut self, original_file: &'a SourceFile, name_ref: &ast::NameRef) {
104 let name_range = name_ref.syntax().range(); 100 let name_range = name_ref.syntax().range();
105 let top_node = name_ref 101 let top_node = name_ref
106 .syntax() 102 .syntax()
@@ -197,15 +193,12 @@ impl<'a> CompletionContext<'a> {
197 } 193 }
198} 194}
199 195
200fn find_node_with_range<'a, N: AstNode<'a>>( 196fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<&N> {
201 syntax: SyntaxNodeRef<'a>,
202 range: TextRange,
203) -> Option<N> {
204 let node = find_covering_node(syntax, range); 197 let node = find_covering_node(syntax, range);
205 node.ancestors().find_map(N::cast) 198 node.ancestors().find_map(N::cast)
206} 199}
207 200
208fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool { 201fn is_node<N: AstNode>(node: &SyntaxNode) -> bool {
209 match node.ancestors().filter_map(N::cast).next() { 202 match node.ancestors().filter_map(N::cast).next() {
210 None => false, 203 None => false,
211 Some(n) => n.syntax().range() == node.range(), 204 Some(n) => n.syntax().range() == node.range(),