From d323c81d5cc6a198239285abcede2166181d8f39 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 2 Oct 2018 18:02:57 +0300 Subject: make ancestors and descendants inherent --- crates/ra_editor/src/code_actions.rs | 5 ++--- crates/ra_editor/src/completion.rs | 13 ++++++------- crates/ra_editor/src/extend_selection.rs | 4 ++-- crates/ra_editor/src/folding_ranges.rs | 8 +++----- crates/ra_editor/src/lib.rs | 10 +++++----- crates/ra_editor/src/scope/fn_scope.rs | 6 +++--- crates/ra_editor/src/symbols.rs | 4 ++-- crates/ra_editor/src/typing.rs | 3 +-- 8 files changed, 24 insertions(+), 29 deletions(-) (limited to 'crates/ra_editor') diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs index 83f7956d2..b0f0d8f1d 100644 --- a/crates/ra_editor/src/code_actions.rs +++ b/crates/ra_editor/src/code_actions.rs @@ -9,7 +9,6 @@ use ra_syntax::{ Direction, siblings, find_leaf_at_offset, find_covering_node, - ancestors, }, }; @@ -101,8 +100,8 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option LocalEdit + 'a> { let node = find_covering_node(file.syntax(), range); - let expr = ancestors(node).filter_map(ast::Expr::cast).next()?; - let anchor_stmt = ancestors(expr.syntax()).filter_map(ast::Stmt::cast).next()?; + let expr = node.ancestors().filter_map(ast::Expr::cast).next()?; + let anchor_stmt = expr.syntax().ancestors().filter_map(ast::Stmt::cast).next()?; let indent = anchor_stmt.syntax().prev_sibling()?; if indent.kind() != WHITESPACE { return None; diff --git a/crates/ra_editor/src/completion.rs b/crates/ra_editor/src/completion.rs index ff9de20d3..62a63fb04 100644 --- a/crates/ra_editor/src/completion.rs +++ b/crates/ra_editor/src/completion.rs @@ -4,7 +4,6 @@ use ra_syntax::{ File, TextUnit, AstNode, SyntaxNodeRef, SyntaxKind::*, ast::{self, LoopBodyOwner, ModuleItemOwner}, algo::{ - ancestors, visit::{visitor, Visitor, visitor_ctx, VisitorCtx}, }, text_utils::is_subrange, @@ -59,7 +58,7 @@ fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec(|it| Some(it.items())) .visit::(|it| Some(it.item_list()?.items())) @@ -92,7 +91,7 @@ fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec) { let mut params = HashMap::new(); - for node in ancestors(ctx) { + for node in ctx.ancestors() { let _ = visitor_ctx(&mut params) .visit::(process) .visit::(process) @@ -123,7 +122,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec) { } fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool { - match ancestors(node).filter_map(N::cast).next() { + match node.ancestors().filter_map(N::cast).next() { None => false, Some(n) => n.syntax().range() == node.range(), } @@ -152,7 +151,7 @@ fn complete_expr_keywords(file: &File, fn_def: ast::FnDef, name_ref: ast::NameRe } fn is_in_loop_body(name_ref: ast::NameRef) -> bool { - for node in ancestors(name_ref.syntax()) { + for node in name_ref.syntax().ancestors() { if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR { break; } @@ -171,7 +170,7 @@ fn is_in_loop_body(name_ref: ast::NameRef) -> bool { } fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option { - // let is_last_in_block = ancestors(name_ref.syntax()).filter_map(ast::Expr::cast) + // let is_last_in_block = name_ref.syntax().ancestors().filter_map(ast::Expr::cast) // .next() // .and_then(|it| it.syntax().parent()) // .and_then(ast::Block::cast) @@ -181,7 +180,7 @@ fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option false, Some(expr_stmt) => expr_stmt.syntax().range() == name_ref.syntax().range() }; diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index dc914a26a..2f8a04b2b 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs @@ -1,7 +1,7 @@ use ra_syntax::{ File, TextRange, SyntaxNodeRef, TextUnit, SyntaxKind::*, - algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node, ancestors, Direction, siblings}, + algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node, Direction, siblings}, }; pub fn extend_selection(file: &File, range: TextRange) -> Option { @@ -30,7 +30,7 @@ pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option } } - match ancestors(node).skip_while(|n| n.range() == range).next() { + match node.ancestors().skip_while(|n| n.range() == range).next() { None => None, Some(parent) => Some(parent.range()), } diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 817da28d1..892aaf97b 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -3,7 +3,7 @@ use std::collections::HashSet; use ra_syntax::{ File, TextRange, SyntaxNodeRef, SyntaxKind, - algo::{walk, Direction, siblings}, + algo::{Direction, siblings}, }; #[derive(Debug, PartialEq, Eq)] @@ -19,12 +19,10 @@ pub struct Fold { } pub fn folding_ranges(file: &File) -> Vec { - let syntax = file.syntax(); - let mut res = vec![]; let mut visited = HashSet::new(); - for node in walk::preorder(syntax) { + for node in file.syntax().descendants() { if visited.contains(&node) { continue; } @@ -139,4 +137,4 @@ fn main() { } -} \ No newline at end of file +} diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index de929d73a..a93924e00 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -21,7 +21,7 @@ mod test_utils; use ra_syntax::{ File, TextUnit, TextRange, SyntaxNodeRef, ast::{self, AstNode, NameOwner}, - algo::{walk, find_leaf_at_offset, ancestors}, + algo::find_leaf_at_offset, SyntaxKind::{self, *}, }; pub use ra_syntax::AtomEdit; @@ -86,7 +86,7 @@ pub fn matching_brace(file: &File, offset: TextUnit) -> Option { pub fn highlight(file: &File) -> Vec { let mut res = Vec::new(); - for node in walk::preorder(file.syntax()) { + for node in file.syntax().descendants() { let tag = match node.kind() { ERROR => "error", COMMENT | DOC_COMMENT => "comment", @@ -110,7 +110,7 @@ pub fn highlight(file: &File) -> Vec { pub fn diagnostics(file: &File) -> Vec { let mut res = Vec::new(); - for node in walk::preorder(file.syntax()) { + for node in file.syntax().descendants() { if node.kind() == ERROR { res.push(Diagnostic { range: node.range(), @@ -130,7 +130,7 @@ pub fn syntax_tree(file: &File) -> String { } pub fn runnables(file: &File) -> Vec { - walk::preorder(file.syntax()) + file.syntax().descendants() .filter_map(ast::FnDef::cast) .filter_map(|f| { let name = f.name()?.text(); @@ -159,7 +159,7 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( let leaf = leaves.clone() .find(|leaf| !leaf.kind().is_trivia()) .or_else(|| leaves.right_biased())?; - ancestors(leaf) + leaf.ancestors() .filter_map(N::cast) .next() } diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 3ae5276a2..eddd87495 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs @@ -6,7 +6,7 @@ use std::{ use ra_syntax::{ SyntaxNodeRef, SyntaxNode, SmolStr, AstNode, ast::{self, NameOwner, LoopBodyOwner, ArgListOwner}, - algo::{ancestors, generate, walk::preorder} + algo::{generate} }; type ScopeId = usize; @@ -51,7 +51,7 @@ impl FnScopes { res } fn add_bindings(&mut self, scope: ScopeId, pat: ast::Pat) { - let entries = preorder(pat.syntax()) + let entries = pat.syntax().descendants() .filter_map(ast::BindPat::cast) .filter_map(ScopeEntry::new); self.scopes[scope].entries.extend(entries); @@ -66,7 +66,7 @@ impl FnScopes { self.scope_for.insert(node.owned(), scope); } fn scope_for(&self, node: SyntaxNodeRef) -> Option { - ancestors(node) + node.ancestors() .filter_map(|it| self.scope_for.get(&it.owned()).map(|&scope| scope)) .next() } diff --git a/crates/ra_editor/src/symbols.rs b/crates/ra_editor/src/symbols.rs index 917984177..e5cc5ca28 100644 --- a/crates/ra_editor/src/symbols.rs +++ b/crates/ra_editor/src/symbols.rs @@ -3,7 +3,7 @@ use ra_syntax::{ ast::{self, NameOwner}, algo::{ visit::{visitor, Visitor}, - walk::{walk, WalkEvent, preorder}, + walk::{walk, WalkEvent}, }, }; use TextRange; @@ -25,7 +25,7 @@ pub struct FileSymbol { } pub fn file_symbols(file: &File) -> Vec { - preorder(file.syntax()) + file.syntax().descendants() .filter_map(to_symbol) .collect() } diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index 0f4e7e0d0..512076941 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -4,7 +4,6 @@ use ra_syntax::{ TextUnit, TextRange, SyntaxNodeRef, File, AstNode, SyntaxKind, ast, algo::{ - walk::preorder, find_covering_node, }, text_utils::{intersect, contains_offset_nonstrict}, @@ -33,7 +32,7 @@ pub fn join_lines(file: &File, range: TextRange) -> LocalEdit { }; let node = find_covering_node(file.syntax(), range); let mut edit = EditBuilder::new(); - for node in preorder(node) { + for node in node.descendants() { let text = match node.leaf_text() { Some(text) => text, None => continue, -- cgit v1.2.3