aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r--crates/ra_syntax/src/algo.rs17
-rw-r--r--crates/ra_syntax/src/lib.rs6
-rw-r--r--crates/ra_syntax/src/syntax_node.rs1
3 files changed, 7 insertions, 17 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index ecd42c133..45f624810 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -5,16 +5,9 @@ use std::ops::RangeInclusive;
5use itertools::Itertools; 5use itertools::Itertools;
6 6
7use crate::{ 7use crate::{
8 AstNode, Direction, NodeOrToken, SourceFile, SyntaxElement, SyntaxNode, SyntaxNodePtr, 8 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, TextRange, TextUnit,
9 SyntaxToken, TextRange, TextUnit,
10}; 9};
11 10
12pub use rowan::TokenAtOffset;
13
14pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> {
15 node.token_at_offset(offset)
16}
17
18/// Returns ancestors of the node at the offset, sorted by length. This should 11/// Returns ancestors of the node at the offset, sorted by length. This should
19/// do the right thing at an edge, e.g. when searching for expressions at `{ 12/// do the right thing at an edge, e.g. when searching for expressions at `{
20/// <|>foo }` we will get the name reference instead of the whole block, which 13/// <|>foo }` we will get the name reference instead of the whole block, which
@@ -24,7 +17,7 @@ pub fn ancestors_at_offset(
24 node: &SyntaxNode, 17 node: &SyntaxNode,
25 offset: TextUnit, 18 offset: TextUnit,
26) -> impl Iterator<Item = SyntaxNode> { 19) -> impl Iterator<Item = SyntaxNode> {
27 find_token_at_offset(node, offset) 20 node.token_at_offset(offset)
28 .map(|token| token.parent().ancestors()) 21 .map(|token| token.parent().ancestors())
29 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) 22 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
30} 23}
@@ -137,14 +130,14 @@ fn with_children(
137 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>(); 130 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>();
138 let new_node = 131 let new_node =
139 rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children); 132 rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children);
140 let new_file_node = parent.replace_with(new_node); 133 let new_root_node = parent.replace_with(new_node);
141 let file = SourceFile::new(new_file_node); 134 let new_root_node = SyntaxNode::new_root(new_root_node);
142 135
143 // FIXME: use a more elegant way to re-fetch the node (#1185), make 136 // FIXME: use a more elegant way to re-fetch the node (#1185), make
144 // `range` private afterwards 137 // `range` private afterwards
145 let mut ptr = SyntaxNodePtr::new(parent); 138 let mut ptr = SyntaxNodePtr::new(parent);
146 ptr.range = TextRange::offset_len(ptr.range().start(), len); 139 ptr.range = TextRange::offset_len(ptr.range().start(), len);
147 ptr.to_node(file.syntax()).to_owned() 140 ptr.to_node(&new_root_node)
148} 141}
149 142
150fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize { 143fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize {
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index 7b778f38c..d02078256 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -44,12 +44,10 @@ pub use crate::{
44 syntax_error::{Location, SyntaxError, SyntaxErrorKind}, 44 syntax_error::{Location, SyntaxError, SyntaxErrorKind},
45 syntax_node::{ 45 syntax_node::{
46 Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder, 46 Direction, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken, SyntaxTreeBuilder,
47 WalkEvent,
48 }, 47 },
49}; 48};
50pub use ra_parser::SyntaxKind; 49pub use ra_parser::{SyntaxKind, T};
51pub use ra_parser::T; 50pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit, TokenAtOffset, WalkEvent};
52pub use rowan::{SmolStr, SyntaxText, TextRange, TextUnit};
53 51
54/// `Parse` is the result of the parsing: a syntax tree and a collection of 52/// `Parse` is the result of the parsing: a syntax tree and a collection of
55/// errors. 53/// errors.
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs
index 95795a27a..b2f5b8c64 100644
--- a/crates/ra_syntax/src/syntax_node.rs
+++ b/crates/ra_syntax/src/syntax_node.rs
@@ -14,7 +14,6 @@ use crate::{
14 Parse, SmolStr, SyntaxKind, TextUnit, 14 Parse, SmolStr, SyntaxKind, TextUnit,
15}; 15};
16 16
17pub use rowan::WalkEvent;
18pub(crate) use rowan::{GreenNode, GreenToken}; 17pub(crate) use rowan::{GreenNode, GreenToken};
19 18
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]