aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/Cargo.toml2
-rw-r--r--crates/ra_syntax/src/algo.rs21
2 files changed, 20 insertions, 3 deletions
diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml
index 7891628dc..8efc6b368 100644
--- a/crates/ra_syntax/Cargo.toml
+++ b/crates/ra_syntax/Cargo.toml
@@ -12,7 +12,7 @@ doctest = false
12 12
13[dependencies] 13[dependencies]
14itertools = "0.8.2" 14itertools = "0.8.2"
15rowan = "0.9.0" 15rowan = "0.9.1"
16rustc_lexer = "0.1.0" 16rustc_lexer = "0.1.0"
17rustc-hash = "1.1.0" 17rustc-hash = "1.1.0"
18arrayvec = "0.5.1" 18arrayvec = "0.5.1"
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index 21fca99a6..ebf59288a 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -4,10 +4,11 @@ use std::ops::RangeInclusive;
4 4
5use itertools::Itertools; 5use itertools::Itertools;
6use ra_text_edit::TextEditBuilder; 6use ra_text_edit::TextEditBuilder;
7use rustc_hash::FxHashMap; 7use rustc_hash::{FxHashMap, FxHashSet};
8 8
9use crate::{ 9use crate::{
10 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit, 10 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken,
11 TextRange, TextUnit,
11}; 12};
12 13
13/// Returns ancestors of the node at the offset, sorted by length. This should 14/// Returns ancestors of the node at the offset, sorted by length. This should
@@ -37,6 +38,17 @@ pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) ->
37 ancestors_at_offset(syntax, offset).find_map(N::cast) 38 ancestors_at_offset(syntax, offset).find_map(N::cast)
38} 39}
39 40
41/// Skip to next non `trivia` token
42pub fn skip_trivia_token(mut token: SyntaxToken, direction: Direction) -> Option<SyntaxToken> {
43 while token.kind().is_trivia() {
44 token = match direction {
45 Direction::Next => token.next_token()?,
46 Direction::Prev => token.prev_token()?,
47 }
48 }
49 Some(token)
50}
51
40/// Finds the first sibling in the given direction which is not `trivia` 52/// Finds the first sibling in the given direction which is not `trivia`
41pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> { 53pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> {
42 return match element { 54 return match element {
@@ -56,6 +68,11 @@ pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxEleme
56 root.covering_element(range) 68 root.covering_element(range)
57} 69}
58 70
71pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> {
72 let u_ancestors = u.ancestors().collect::<FxHashSet<SyntaxNode>>();
73 v.ancestors().find(|it| u_ancestors.contains(it))
74}
75
59#[derive(Debug, PartialEq, Eq, Clone, Copy)] 76#[derive(Debug, PartialEq, Eq, Clone, Copy)]
60pub enum InsertPosition<T> { 77pub enum InsertPosition<T> {
61 First, 78 First,