]> git.lizzy.rs Git - rust.git/commitdiff
wip
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 21 Dec 2018 16:10:07 +0000 (19:10 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 21 Dec 2018 16:10:07 +0000 (19:10 +0300)
crates/ra_analysis/src/completion.rs

index a11e98ac0be8ebdb07c42b91ebabce54ca2f8e1f..ae12802564efe65470e33ba4700018ad59a158d2 100644 (file)
@@ -8,6 +8,8 @@
     ast,
     AstNode,
     SyntaxNodeRef,
+    SourceFileNode,
+    TextUnit,
 };
 use ra_db::SyntaxDatabase;
 use rustc_hash::{FxHashMap};
@@ -27,11 +29,6 @@ pub(crate) fn completions(
 ) -> Cancelable<Option<Completions>> {
     let original_file = db.source_file(position.file_id);
     // Insert a fake ident to get a valid parse tree
-    let file = {
-        let edit = AtomTextEdit::insert(position.offset, "intellijRulezz".to_string());
-        original_file.reparse(&edit)
-    };
-
     let module = ctry!(source_binder::module_from_position(db, position)?);
 
     let mut acc = Completions::default();
@@ -59,6 +56,32 @@ pub(crate) fn completions(
     Ok(Some(acc))
 }
 
+/// `SyntaxContext` is created early during completion to figure out, where
+/// exactly is the cursor, syntax-wise.
+#[derive(Debug)]
+pub(super) enum SyntaxContext<'a> {
+    ParameterName(SyntaxNodeRef<'a>),
+    Other,
+}
+
+impl SyntaxContext {
+    pub(super) fn new(original_file: &SourceFileNode, offset: TextUnit) -> SyntaxContext {
+        let file = {
+            let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string());
+            original_file.reparse(&edit)
+        };
+        if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) {
+            if is_node::<ast::Param>(name.syntax()) {
+                if let Some(node) = find_leaf_at_offset(original_file, offset).left_biased() {
+                    return SyntaxContext::ParameterName(node);
+                }
+            }
+        }
+
+        SyntaxContext::Other
+    }
+}
+
 /// Complete repeated parametes, both name and type. For example, if all
 /// functions in a file have a `spam: &mut Spam` parameter, a completion with
 /// `spam: &mut Spam` insert text/label and `spam` lookup string will be