Move non_trivia_sibling to ra_syntax::algo

This commit is contained in:
Ville Penttinen 2019-02-21 18:49:03 +02:00
parent 3c22c64725
commit 82173c8de4
4 changed files with 13 additions and 15 deletions

View File

@ -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<impl HirDatabase>) -> Option<Assist> {
let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;

View File

@ -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<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
]
}
fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia())
}
#[cfg(test)]
mod helpers {
use hir::mock::MockDatabase;

View File

@ -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,
}
}

View File

@ -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<N: AstNode>(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))
}