1 //! Collection of assorted algorithms for syntax trees.
3 use std::hash::BuildHasherDefault;
5 use indexmap::IndexMap;
6 use itertools::Itertools;
7 use rustc_hash::FxHashMap;
8 use text_edit::TextEditBuilder;
11 AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
15 /// Returns ancestors of the node at the offset, sorted by length. This should
16 /// do the right thing at an edge, e.g. when searching for expressions at `{
17 /// $0foo }` we will get the name reference instead of the whole block, which
18 /// we would get if we just did `find_token_at_offset(...).flat_map(|t|
19 /// t.parent().ancestors())`.
20 pub fn ancestors_at_offset(
23 ) -> impl Iterator<Item = SyntaxNode> {
24 node.token_at_offset(offset)
25 .map(|token| token.ancestors())
26 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
29 /// Finds a node of specific Ast type at offset. Note that this is slightly
30 /// imprecise: if the cursor is strictly between two nodes of the desired type,
34 /// struct Foo {}|struct Bar;
37 /// then the shorter node will be silently preferred.
38 pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextSize) -> Option<N> {
39 ancestors_at_offset(syntax, offset).find_map(N::cast)
42 pub fn find_node_at_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
43 syntax.covering_element(range).ancestors().find_map(N::cast)
46 /// Skip to next non `trivia` token
47 pub fn skip_trivia_token(mut token: SyntaxToken, direction: Direction) -> Option<SyntaxToken> {
48 while token.kind().is_trivia() {
49 token = match direction {
50 Direction::Next => token.next_token()?,
51 Direction::Prev => token.prev_token()?,
57 /// Finds the first sibling in the given direction which is not `trivia`
58 pub fn non_trivia_sibling(element: SyntaxElement, direction: Direction) -> Option<SyntaxElement> {
59 return match element {
60 NodeOrToken::Node(node) => node.siblings_with_tokens(direction).skip(1).find(not_trivia),
61 NodeOrToken::Token(token) => token.siblings_with_tokens(direction).skip(1).find(not_trivia),
64 fn not_trivia(element: &SyntaxElement) -> bool {
66 NodeOrToken::Node(_) => true,
67 NodeOrToken::Token(token) => !token.kind().is_trivia(),
72 pub fn least_common_ancestor(u: &SyntaxNode, v: &SyntaxNode) -> Option<SyntaxNode> {
74 return Some(u.clone());
77 let u_depth = u.ancestors().count();
78 let v_depth = v.ancestors().count();
79 let keep = u_depth.min(v_depth);
81 let u_candidates = u.ancestors().skip(u_depth - keep);
82 let v_candidates = v.ancestors().skip(v_depth - keep);
83 let (res, _) = u_candidates.zip(v_candidates).find(|(x, y)| x == y)?;
87 pub fn neighbor<T: AstNode>(me: &T, direction: Direction) -> Option<T> {
88 me.syntax().siblings(direction).skip(1).find_map(T::cast)
91 pub fn has_errors(node: &SyntaxNode) -> bool {
92 node.children().any(|it| it.kind() == SyntaxKind::ERROR)
95 type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<rustc_hash::FxHasher>>;
97 #[derive(Debug, Hash, PartialEq, Eq)]
98 enum TreeDiffInsertPos {
100 AsFirstChild(SyntaxElement),
104 pub struct TreeDiff {
105 replacements: FxHashMap<SyntaxElement, SyntaxElement>,
106 deletions: Vec<SyntaxElement>,
107 // the vec as well as the indexmap are both here to preserve order
108 insertions: FxIndexMap<TreeDiffInsertPos, Vec<SyntaxElement>>,
112 pub fn into_text_edit(&self, builder: &mut TextEditBuilder) {
113 let _p = profile::span("into_text_edit");
115 for (anchor, to) in self.insertions.iter() {
116 let offset = match anchor {
117 TreeDiffInsertPos::After(it) => it.text_range().end(),
118 TreeDiffInsertPos::AsFirstChild(it) => it.text_range().start(),
120 to.iter().for_each(|to| builder.insert(offset, to.to_string()));
122 for (from, to) in self.replacements.iter() {
123 builder.replace(from.text_range(), to.to_string())
125 for text_range in self.deletions.iter().map(SyntaxElement::text_range) {
126 builder.delete(text_range);
130 pub fn is_empty(&self) -> bool {
131 self.replacements.is_empty() && self.deletions.is_empty() && self.insertions.is_empty()
135 /// Finds a (potentially minimal) diff, which, applied to `from`, will result in `to`.
137 /// Specifically, returns a structure that consists of a replacements, insertions and deletions
138 /// such that applying this map on `from` will result in `to`.
140 /// This function tries to find a fine-grained diff.
141 pub fn diff(from: &SyntaxNode, to: &SyntaxNode) -> TreeDiff {
142 let _p = profile::span("diff");
144 let mut diff = TreeDiff {
145 replacements: FxHashMap::default(),
146 insertions: FxIndexMap::default(),
147 deletions: Vec::new(),
149 let (from, to) = (from.clone().into(), to.clone().into());
151 if !syntax_element_eq(&from, &to) {
152 go(&mut diff, from, to);
156 fn syntax_element_eq(lhs: &SyntaxElement, rhs: &SyntaxElement) -> bool {
157 lhs.kind() == rhs.kind()
158 && lhs.text_range().len() == rhs.text_range().len()
159 && match (&lhs, &rhs) {
160 (NodeOrToken::Node(lhs), NodeOrToken::Node(rhs)) => {
161 lhs == rhs || lhs.text() == rhs.text()
163 (NodeOrToken::Token(lhs), NodeOrToken::Token(rhs)) => lhs.text() == rhs.text(),
168 // FIXME: this is horribly inefficient. I bet there's a cool algorithm to diff trees properly.
169 fn go(diff: &mut TreeDiff, lhs: SyntaxElement, rhs: SyntaxElement) {
170 let (lhs, rhs) = match lhs.as_node().zip(rhs.as_node()) {
171 Some((lhs, rhs)) => (lhs, rhs),
173 cov_mark::hit!(diff_node_token_replace);
174 diff.replacements.insert(lhs, rhs);
179 let mut look_ahead_scratch = Vec::default();
181 let mut rhs_children = rhs.children_with_tokens();
182 let mut lhs_children = lhs.children_with_tokens();
183 let mut last_lhs = None;
185 let lhs_child = lhs_children.next();
186 match (lhs_child.clone(), rhs_children.next()) {
187 (None, None) => break,
188 (None, Some(element)) => {
189 let insert_pos = match last_lhs.clone() {
191 cov_mark::hit!(diff_insert);
192 TreeDiffInsertPos::After(prev)
194 // first iteration, insert into out parent as the first child
196 cov_mark::hit!(diff_insert_as_first_child);
197 TreeDiffInsertPos::AsFirstChild(lhs.clone().into())
200 diff.insertions.entry(insert_pos).or_insert_with(Vec::new).push(element);
202 (Some(element), None) => {
203 cov_mark::hit!(diff_delete);
204 diff.deletions.push(element);
206 (Some(ref lhs_ele), Some(ref rhs_ele)) if syntax_element_eq(lhs_ele, rhs_ele) => {}
207 (Some(lhs_ele), Some(rhs_ele)) => {
208 // nodes differ, look for lhs_ele in rhs, if its found we can mark everything up
209 // until that element as insertions. This is important to keep the diff minimal
210 // in regards to insertions that have been actually done, this is important for
211 // use insertions as we do not want to replace the entire module node.
212 look_ahead_scratch.push(rhs_ele.clone());
213 let mut rhs_children_clone = rhs_children.clone();
214 let mut insert = false;
215 while let Some(rhs_child) = rhs_children_clone.next() {
216 if syntax_element_eq(&lhs_ele, &rhs_child) {
217 cov_mark::hit!(diff_insertions);
221 look_ahead_scratch.push(rhs_child);
224 let drain = look_ahead_scratch.drain(..);
226 let insert_pos = if let Some(prev) = last_lhs.clone().filter(|_| insert) {
227 TreeDiffInsertPos::After(prev)
229 cov_mark::hit!(insert_first_child);
230 TreeDiffInsertPos::AsFirstChild(lhs.clone().into())
233 diff.insertions.entry(insert_pos).or_insert_with(Vec::new).extend(drain);
234 rhs_children = rhs_children_clone;
236 go(diff, lhs_ele, rhs_ele)
240 last_lhs = lhs_child.or(last_lhs);
247 use expect_test::{expect, Expect};
248 use itertools::Itertools;
249 use parser::SyntaxKind;
250 use text_edit::TextEdit;
252 use crate::{AstNode, SyntaxElement};
255 fn replace_node_token() {
256 cov_mark::check!(diff_node_token_replace);
267 Line 0: Token(USE_KW@0..3 "use") -> ident
279 fn replace_parent() {
280 cov_mark::check!(diff_insert_as_first_child);
287 Line 0: AsFirstChild(Node(SOURCE_FILE@0..0))
303 cov_mark::check!(diff_insert);
315 Line 2: After(Node(USE@10..18))
343 Line 2: After(Token(WHITESPACE@9..10 "\n"))
371 Line 0: After(Token(WHITESPACE@0..1 "\n"))
387 fn first_child_insertion() {
388 cov_mark::check!(insert_first_child);
401 Line 0: AsFirstChild(Node(SOURCE_FILE@0..30))
418 cov_mark::check!(diff_delete);
442 cov_mark::check!(diff_insertions);
445 use expect_test::{expect, Expect};
446 use text_edit::TextEdit;
451 use expect_test::{expect, Expect};
458 Line 1: After(Node(USE@1..35))
460 -> use crate::AstNode;
468 Line 2: use text_edit::TextEdit;
470 Line 4: use crate::AstNode;
480 use text_edit::TextEdit;
494 Line 2: Token(IDENT@5..14 "text_edit") -> crate
495 Line 2: Token(IDENT@16..24 "TextEdit") -> AstNode
496 Line 2: Token(WHITESPACE@25..27 "\n\n") -> "\n"
500 Line 3: use crate::AstNode;
512 hash::BuildHasherDefault,
513 ops::{self, RangeInclusive},
518 use std::hash::BuildHasherDefault;
519 use std::ops::{self, RangeInclusive};
524 Line 2: After(Node(PATH_SEGMENT@5..8))
527 Line 6: After(Token(WHITESPACE@86..87 "\n"))
528 -> use std::hash::BuildHasherDefault;
530 -> use std::ops::{self, RangeInclusive};
535 Line 2: Token(IDENT@5..8 "std") -> std
542 hash::BuildHasherDefault,
543 ops::{self, RangeInclusive},
550 fn early_return_assist() {
554 if let Ok(x) = Err(92) {
561 let x = match Err(92) {
571 Line 3: After(Node(BLOCK_EXPR@40..63))
578 Line 3: After(Node(IF_EXPR@17..63))
584 Line 3: Token(IF_KW@17..19 "if") -> let
585 Line 3: Token(LET_KW@20..23 "let") -> x
586 Line 3: Node(BLOCK_EXPR@40..63) -> =
600 fn check_diff(from: &str, to: &str, expected_diff: Expect) {
601 let from_node = crate::SourceFile::parse(from).tree().syntax().clone();
602 let to_node = crate::SourceFile::parse(to).tree().syntax().clone();
603 let diff = super::diff(&from_node, &to_node);
606 |syn: &SyntaxElement| from[..syn.text_range().start().into()].lines().count();
608 let fmt_syntax = |syn: &SyntaxElement| match syn.kind() {
609 SyntaxKind::WHITESPACE => format!("{:?}", syn.to_string()),
610 _ => format!("{}", syn),
614 diff.insertions.iter().format_with("\n", |(k, v), f| -> Result<(), std::fmt::Error> {
616 "Line {}: {:?}\n-> {}",
617 line_number(match k {
618 super::TreeDiffInsertPos::After(syn) => syn,
619 super::TreeDiffInsertPos::AsFirstChild(syn) => syn,
622 v.iter().format_with("\n-> ", |v, f| f(&fmt_syntax(v)))
626 let replacements = diff
629 .sorted_by_key(|(syntax, _)| syntax.text_range().start())
630 .format_with("\n", |(k, v), f| {
631 f(&format!("Line {}: {:?} -> {}", line_number(k), k, fmt_syntax(v)))
637 .format_with("\n", |v, f| f(&format!("Line {}: {}", line_number(v), &fmt_syntax(v))));
639 let actual = format!(
640 "insertions:\n\n{}\n\nreplacements:\n\n{}\n\ndeletions:\n\n{}\n",
641 insertions, replacements, deletions
643 expected_diff.assert_eq(&actual);
645 let mut from = from.to_owned();
646 let mut text_edit = TextEdit::builder();
647 diff.into_text_edit(&mut text_edit);
648 text_edit.finish().apply(&mut from);
649 assert_eq!(&*from, to, "diff did not turn `from` to `to`");