aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-02-21 16:49:03 +0000
committerVille Penttinen <[email protected]>2019-02-21 16:49:03 +0000
commit82173c8de4b1283b6b54bd0def25b9c432614841 (patch)
treefdf55d583a4bac9a8e66fbbe48aeed1096f55f1e
parent3c22c64725a4a2ea13a30422a4f822fb1259e7b5 (diff)
Move `non_trivia_sibling` to `ra_syntax::algo`
-rw-r--r--crates/ra_assists/src/flip_comma.rs3
-rw-r--r--crates/ra_assists/src/lib.rs6
-rw-r--r--crates/ra_ide_api_light/src/join_lines.rs12
-rw-r--r--crates/ra_syntax/src/algo.rs7
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;
2use ra_syntax::{ 2use ra_syntax::{
3 Direction, 3 Direction,
4 SyntaxKind::COMMA, 4 SyntaxKind::COMMA,
5 algo::non_trivia_sibling,
5}; 6};
6 7
7use crate::{AssistCtx, Assist, non_trivia_sibling}; 8use crate::{AssistCtx, Assist};
8 9
9pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 10pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
10 let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?; 11 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;
10use itertools::Itertools; 10use itertools::Itertools;
11 11
12use ra_text_edit::TextEdit; 12use ra_text_edit::TextEdit;
13use ra_syntax::{TextRange, TextUnit, SyntaxNode, Direction}; 13use ra_syntax::{TextRange, TextUnit};
14use ra_db::FileRange; 14use ra_db::FileRange;
15use hir::db::HirDatabase; 15use hir::db::HirDatabase;
16 16
@@ -104,10 +104,6 @@ fn all_assists<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assis
104 ] 104 ]
105} 105}
106 106
107fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
108 node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia())
109}
110
111#[cfg(test)] 107#[cfg(test)]
112mod helpers { 108mod helpers {
113 use hir::mock::MockDatabase; 109 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;
2use ra_syntax::{ 2use ra_syntax::{
3 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, 3 SourceFile, TextRange, TextUnit, AstNode, SyntaxNode,
4 SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, 4 SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK},
5 algo::find_covering_node, 5 algo::{find_covering_node, non_trivia_sibling},
6 ast, 6 ast,
7 Direction,
7}; 8};
8use ra_fmt::{ 9use ra_fmt::{
9 compute_ws, extract_trivial_expression 10 compute_ws, extract_trivial_expression
@@ -121,13 +122,8 @@ fn remove_newline(
121} 122}
122 123
123fn has_comma_after(node: &SyntaxNode) -> bool { 124fn has_comma_after(node: &SyntaxNode) -> bool {
124 let next = node.next_sibling(); 125 match non_trivia_sibling(node, Direction::Next) {
125 let nnext = node.next_sibling().and_then(|n| n.next_sibling()); 126 Some(n) => n.kind() == COMMA,
126
127 match (next, nnext) {
128 // Whitespace followed by a comma is fine
129 (Some(ws), Some(comma)) if ws.kind() == WHITESPACE && comma.kind() == COMMA => true,
130 (Some(n), _) => n.kind() == COMMA,
131 _ => false, 127 _ => false,
132 } 128 }
133} 129}
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;
2 2
3use rowan::TransparentNewType; 3use rowan::TransparentNewType;
4 4
5use crate::{SyntaxNode, TextRange, TextUnit, AstNode}; 5use crate::{SyntaxNode, TextRange, TextUnit, AstNode, Direction};
6 6
7pub use rowan::LeafAtOffset; 7pub use rowan::LeafAtOffset;
8 8
@@ -29,6 +29,11 @@ pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) ->
29 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast)) 29 find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
30} 30}
31 31
32/// Finds the first sibling in the given direction which is not `trivia`
33pub fn non_trivia_sibling(node: &SyntaxNode, direction: Direction) -> Option<&SyntaxNode> {
34 node.siblings(direction).skip(1).find(|node| !node.kind().is_trivia())
35}
36
32pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { 37pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
33 SyntaxNode::from_repr(root.0.covering_node(range)) 38 SyntaxNode::from_repr(root.0.covering_node(range))
34} 39}