From 82173c8de4b1283b6b54bd0def25b9c432614841 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 21 Feb 2019 18:49:03 +0200 Subject: Move `non_trivia_sibling` to `ra_syntax::algo` --- crates/ra_assists/src/flip_comma.rs | 3 ++- crates/ra_assists/src/lib.rs | 6 +----- crates/ra_ide_api_light/src/join_lines.rs | 12 ++++-------- crates/ra_syntax/src/algo.rs | 7 ++++++- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/crates/ra_assists/src/flip_comma.rs b/crates/ra_assists/src/flip_comma.rs index 08644d720..0d4a789fc 100644 --- a/crates/ra_assists/src/flip_comma.rs +++ b/crates/ra_assists/src/flip_comma.rs @@ -2,9 +2,10 @@ use hir::db::HirDatabase; use ra_syntax::{ Direction, SyntaxKind::COMMA, + algo::non_trivia_sibling, }; -use crate::{AssistCtx, Assist, non_trivia_sibling}; +use crate::{AssistCtx, Assist}; pub(crate) fn flip_comma(mut ctx: AssistCtx) -> Option { let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?; diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 7bd9b5ae6..e1e899edc 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -10,7 +10,7 @@ mod assist_ctx; use itertools::Itertools; use ra_text_edit::TextEdit; -use ra_syntax::{TextRange, TextUnit, SyntaxNode, Direction}; +use ra_syntax::{TextRange, TextUnit}; use ra_db::FileRange; use hir::db::HirDatabase; @@ -104,10 +104,6 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option Option<&SyntaxNode> { - node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia()) -} - #[cfg(test)] mod helpers { use hir::mock::MockDatabase; diff --git a/crates/ra_ide_api_light/src/join_lines.rs b/crates/ra_ide_api_light/src/join_lines.rs index 9a400199f..b5bcd62fb 100644 --- a/crates/ra_ide_api_light/src/join_lines.rs +++ b/crates/ra_ide_api_light/src/join_lines.rs @@ -2,8 +2,9 @@ use itertools::Itertools; use ra_syntax::{ SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, - algo::find_covering_node, + algo::{find_covering_node, non_trivia_sibling}, ast, + Direction, }; use ra_fmt::{ compute_ws, extract_trivial_expression @@ -121,13 +122,8 @@ fn remove_newline( } fn has_comma_after(node: &SyntaxNode) -> bool { - let next = node.next_sibling(); - let nnext = node.next_sibling().and_then(|n| n.next_sibling()); - - match (next, nnext) { - // Whitespace followed by a comma is fine - (Some(ws), Some(comma)) if ws.kind() == WHITESPACE && comma.kind() == COMMA => true, - (Some(n), _) => n.kind() == COMMA, + match non_trivia_sibling(node, Direction::Next) { + Some(n) => n.kind() == COMMA, _ => false, } } diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index e8cf0d4b5..e2b4f0388 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -2,7 +2,7 @@ pub mod visit; use rowan::TransparentNewType; -use crate::{SyntaxNode, TextRange, TextUnit, AstNode}; +use crate::{SyntaxNode, TextRange, TextUnit, AstNode, Direction}; pub use rowan::LeafAtOffset; @@ -29,6 +29,11 @@ pub fn find_node_at_offset(syntax: &SyntaxNode, offset: TextUnit) -> find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) } +/// Finds the first sibling in the given direction which is not `trivia` +pub fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> { + node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia()) +} + pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { SyntaxNode::from_repr(root.0.covering_node(range)) } -- cgit v1.2.3