From 12810b93c5141b9ae31f4af17dcc61b0166314b0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 19:10:07 +0300 Subject: wip --- crates/ra_analysis/src/completion.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index a11e98ac0..ae1280256 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -8,6 +8,8 @@ use ra_syntax::{ ast, AstNode, SyntaxNodeRef, + SourceFileNode, + TextUnit, }; use ra_db::SyntaxDatabase; use rustc_hash::{FxHashMap}; @@ -27,11 +29,6 @@ pub(crate) fn completions( ) -> Cancelable> { 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::(file.syntax(), offset) { + if is_node::(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 -- cgit v1.2.3