aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/algo.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-07-20 18:04:34 +0100
committerAleksey Kladov <[email protected]>2019-07-20 18:12:06 +0100
commitc9cfd57eeaa53657c0af7b9c4ba74d6b7b9889ed (patch)
tree422c1d8fb7f8af663df3c6c6183783253692acad /crates/ra_syntax/src/algo.rs
parent7bde8012cb28c44de7ffc779003781d385323808 (diff)
switch to upstream rowan's API
Diffstat (limited to 'crates/ra_syntax/src/algo.rs')
-rw-r--r--crates/ra_syntax/src/algo.rs117
1 files changed, 104 insertions, 13 deletions
diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs
index f47e11e66..6bb46b021 100644
--- a/crates/ra_syntax/src/algo.rs
+++ b/crates/ra_syntax/src/algo.rs
@@ -1,17 +1,18 @@
1pub mod visit; 1pub mod visit;
2 2
3use std::ops::RangeInclusive;
4
3use itertools::Itertools; 5use itertools::Itertools;
4 6
5use crate::{AstNode, Direction, SyntaxElement, SyntaxNode, SyntaxToken, TextRange, TextUnit}; 7use crate::{
8 AstNode, Direction, InsertPosition, NodeOrToken, SourceFile, SyntaxElement, SyntaxNode,
9 SyntaxNodePtr, SyntaxToken, TextRange, TextUnit,
10};
6 11
7pub use rowan::TokenAtOffset; 12pub use rowan::TokenAtOffset;
8 13
9pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> { 14pub fn find_token_at_offset(node: &SyntaxNode, offset: TextUnit) -> TokenAtOffset<SyntaxToken> {
10 match node.0.token_at_offset(offset) { 15 node.token_at_offset(offset)
11 TokenAtOffset::None => TokenAtOffset::None,
12 TokenAtOffset::Single(n) => TokenAtOffset::Single(SyntaxToken(n)),
13 TokenAtOffset::Between(l, r) => TokenAtOffset::Between(SyntaxToken(l), SyntaxToken(r)),
14 }
15} 16}
16 17
17/// Returns ancestors of the node at the offset, sorted by length. This should 18/// Returns ancestors of the node at the offset, sorted by length. This should
@@ -44,20 +45,110 @@ pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) ->
44/// Finds the first sibling in the given direction which is not `trivia` 45/// Finds the first sibling in the given direction which is not `trivia`
45pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> { 46pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> {
46 return match element { 47 return match element {
47 SyntaxElement::Node(node) => node.siblings_with_tokens(direction).skip(1).find(not_trivia), 48 NodeOrToken::Node(node) => node.siblings_with_tokens(direction).skip(1).find(not_trivia),
48 SyntaxElement::Token(token) => { 49 NodeOrToken::Token(token) => token.siblings_with_tokens(direction).skip(1).find(not_trivia),
49 token.siblings_with_tokens(direction).skip(1).find(not_trivia)
50 }
51 }; 50 };
52 51
53 fn not_trivia(element: &SyntaxElement) -> bool { 52 fn not_trivia(element: &SyntaxElement) -> bool {
54 match element { 53 match element {
55 SyntaxElement::Node(_) => true, 54 NodeOrToken::Node(_) => true,
56 SyntaxElement::Token(token) => !token.kind().is_trivia(), 55 NodeOrToken::Token(token) => !token.kind().is_trivia(),
57 } 56 }
58 } 57 }
59} 58}
60 59
61pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement { 60pub fn find_covering_element(root: &SyntaxNode, range: TextRange) -> SyntaxElement {
62 SyntaxElement::new(root.0.covering_node(range)) 61 root.covering_element(range)
62}
63
64/// Adds specified children (tokens or nodes) to the current node at the
65/// specific position.
66///
67/// This is a type-unsafe low-level editing API, if you need to use it,
68/// prefer to create a type-safe abstraction on top of it instead.
69pub fn insert_children(
70 parent: &SyntaxNode,
71 position: InsertPosition<SyntaxElement>,
72 to_insert: impl Iterator<Item = SyntaxElement>,
73) -> SyntaxNode {
74 let mut delta = TextUnit::default();
75 let to_insert = to_insert.map(|element| {
76 delta += element.text_range().len();
77 to_green_element(element)
78 });
79
80 let old_children = parent.green().children();
81
82 let new_children = match &position {
83 InsertPosition::First => {
84 to_insert.chain(old_children.iter().cloned()).collect::<Box<[_]>>()
85 }
86 InsertPosition::Last => old_children.iter().cloned().chain(to_insert).collect::<Box<[_]>>(),
87 InsertPosition::Before(anchor) | InsertPosition::After(anchor) => {
88 let take_anchor = if let InsertPosition::After(_) = position { 1 } else { 0 };
89 let split_at = position_of_child(parent, anchor.clone()) + take_anchor;
90 let (before, after) = old_children.split_at(split_at);
91 before
92 .iter()
93 .cloned()
94 .chain(to_insert)
95 .chain(after.iter().cloned())
96 .collect::<Box<[_]>>()
97 }
98 };
99
100 with_children(parent, new_children)
101}
102
103/// Replaces all nodes in `to_delete` with nodes from `to_insert`
104///
105/// This is a type-unsafe low-level editing API, if you need to use it,
106/// prefer to create a type-safe abstraction on top of it instead.
107pub fn replace_children(
108 parent: &SyntaxNode,
109 to_delete: RangeInclusive<SyntaxElement>,
110 to_insert: impl Iterator<Item = SyntaxElement>,
111) -> SyntaxNode {
112 let start = position_of_child(parent, to_delete.start().clone());
113 let end = position_of_child(parent, to_delete.end().clone());
114 let old_children = parent.green().children();
115
116 let new_children = old_children[..start]
117 .iter()
118 .cloned()
119 .chain(to_insert.map(to_green_element))
120 .chain(old_children[end + 1..].iter().cloned())
121 .collect::<Box<[_]>>();
122 with_children(parent, new_children)
123}
124
125fn with_children(
126 parent: &SyntaxNode,
127 new_children: Box<[NodeOrToken<rowan::GreenNode, rowan::GreenToken>]>,
128) -> SyntaxNode {
129 let len = new_children.iter().map(|it| it.text_len()).sum::<TextUnit>();
130 let new_node =
131 rowan::GreenNode::new(rowan::cursor::SyntaxKind(parent.kind() as u16), new_children);
132 let new_file_node = parent.replace_with(new_node);
133 let file = SourceFile::new(new_file_node);
134
135 // FIXME: use a more elegant way to re-fetch the node (#1185), make
136 // `range` private afterwards
137 let mut ptr = SyntaxNodePtr::new(parent);
138 ptr.range = TextRange::offset_len(ptr.range().start(), len);
139 ptr.to_node(file.syntax()).to_owned()
140}
141
142fn position_of_child(parent: &SyntaxNode, child: SyntaxElement) -> usize {
143 parent
144 .children_with_tokens()
145 .position(|it| it == child)
146 .expect("element is not a child of current element")
147}
148
149fn to_green_element(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
150 match element {
151 NodeOrToken::Node(it) => it.green().clone().into(),
152 NodeOrToken::Token(it) => it.green().clone().into(),
153 }
63} 154}