From a450142aca947b9364e498897f522f854f19781d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Aug 2018 09:12:18 +0300 Subject: fix stray curly --- crates/libeditor/src/code_actions.rs | 18 ++++-------------- crates/libeditor/src/completion.rs | 31 +++++++++++++++++++++++++++++++ crates/libeditor/src/lib.rs | 21 ++++++++++++++++++--- 3 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 crates/libeditor/src/completion.rs (limited to 'crates/libeditor/src') diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index b3305be2a..f53a8f9c6 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs @@ -3,7 +3,7 @@ use std::{ }; use libsyntax2::{ - File, + File, TextUnit, ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, SyntaxKind::COMMA, SyntaxNodeRef, @@ -13,7 +13,7 @@ use libsyntax2::{ }, }; -use {TextUnit, EditBuilder, Edit}; +use {EditBuilder, Edit, find_node_at_offset}; #[derive(Debug)] pub struct ActionResult { @@ -39,7 +39,7 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option(file: &'a File, offset: TextUnit) -> Option ActionResult + 'a> { - let nominal = find_node::(file.syntax(), offset)?; + let nominal = find_node_at_offset::(file.syntax(), offset)?; Some(move || { let derive_attr = nominal .attrs() @@ -66,7 +66,7 @@ pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option(file: &'a File, offset: TextUnit) -> Option ActionResult + 'a> { - let nominal = find_node::(file.syntax(), offset)?; + let nominal = find_node_at_offset::(file.syntax(), offset)?; let name = nominal.name()?; Some(move || { @@ -105,16 +105,6 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option { - let leaves = find_leaf_at_offset(syntax, offset); - let leaf = leaves.clone() - .find(|leaf| !leaf.kind().is_trivia()) - .or_else(|| leaves.right_biased())?; - ancestors(leaf) - .filter_map(N::cast) - .next() -} - fn comma_list(buf: &mut String, bra: &str, ket: &str, items: impl Iterator) { buf.push_str(bra); let mut first = true; diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs new file mode 100644 index 000000000..cf61ec784 --- /dev/null +++ b/crates/libeditor/src/completion.rs @@ -0,0 +1,31 @@ +use libsyntax2::{ + File, TextUnit, + ast, + algo::find_leaf_at_offset, +}; + +use { + AtomEdit, find_node_at_offset, +}; + +#[derive(Debug)] +pub struct CompletionItem { + name: String, +} + +pub fn scope_completion(file: &File, offset: TextUnit) -> Option> { + // Insert a fake ident to get a valid parse tree + let file = { + let edit = AtomEdit::insert(offset, "intellijRulezz".to_string()); + // Don't bother with completion if incremental reparse fails + file.incremental_reparse(&edit)? + }; + let name_ref = find_node_at_offset::(file.syntax(), offset)?; + Some(complete(name_ref)) +} + +fn complete(name_ref: ast::NameRef) -> Vec { + vec![CompletionItem { + name: "foo".to_string() + }] +} diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 55302265f..60489f7e3 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs @@ -8,11 +8,12 @@ mod line_index; mod edit; mod code_actions; mod typing; +mod completion; use libsyntax2::{ - File, TextUnit, TextRange, + File, TextUnit, TextRange, SyntaxNodeRef, ast::{AstNode, NameOwner}, - algo::{walk, find_leaf_at_offset}, + algo::{walk, find_leaf_at_offset, ancestors}, SyntaxKind::{self, *}, }; pub use libsyntax2::AtomEdit; @@ -22,10 +23,11 @@ pub use self::{ symbols::{StructureNode, file_structure, FileSymbol, file_symbols}, edit::{EditBuilder, Edit}, code_actions::{ - ActionResult, find_node, + ActionResult, flip_comma, add_derive, add_impl, }, typing::join_lines, + completion::scope_completion, }; #[derive(Debug)] @@ -138,3 +140,16 @@ pub fn runnables(file: &File) -> Vec { }) .collect() } + +pub fn find_node_at_offset<'a, N: AstNode<'a>>( + syntax: SyntaxNodeRef<'a>, + offset: TextUnit, +) -> Option { + let leaves = find_leaf_at_offset(syntax, offset); + let leaf = leaves.clone() + .find(|leaf| !leaf.kind().is_trivia()) + .or_else(|| leaves.right_biased())?; + ancestors(leaf) + .filter_map(N::cast) + .next() +} -- cgit v1.2.3