From 69e8393963daa3d222fc7f8c65e9802670906e9e Mon Sep 17 00:00:00 2001 From: DropDemBits Date: Mon, 2 Sep 2024 22:53:54 -0400 Subject: [PATCH] misc fixes --- crates/syntax/src/syntax_editor.rs | 24 +++++--------------- crates/syntax/src/syntax_editor/edit_algo.rs | 14 +++++++----- crates/syntax/src/syntax_editor/mapping.rs | 8 +++++-- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/crates/syntax/src/syntax_editor.rs b/crates/syntax/src/syntax_editor.rs index 3a05cc480b..139c6518bf 100644 --- a/crates/syntax/src/syntax_editor.rs +++ b/crates/syntax/src/syntax_editor.rs @@ -150,6 +150,12 @@ impl SyntaxAnnotation { } } +impl Default for SyntaxAnnotation { + fn default() -> Self { + Self::new() + } +} + /// Position describing where to insert elements #[derive(Debug)] pub struct Position { @@ -445,24 +451,6 @@ mod tests { .all(|element| element.ancestors().any(|it| &it == edit.root()))) } - #[test] - #[should_panic = "some replace change ranges intersect: [Replace(Node(TUPLE_EXPR@5..7), Some(Node(NAME_REF@0..8))), Replace(Node(TUPLE_EXPR@5..7), Some(Node(NAME_REF@0..8)))]"] - fn fail_on_non_disjoint_single_replace() { - let root = make::match_arm([make::wildcard_pat().into()], None, make::expr_tuple([])); - - let to_wrap = root.syntax().descendants().find_map(ast::TupleExpr::cast).unwrap(); - - let mut editor = SyntaxEditor::new(root.syntax().clone()); - - let name_ref = make::name_ref("var_name").clone_for_update(); - - // should die, ranges are not disjoint - editor.replace(to_wrap.syntax(), name_ref.syntax()); - editor.replace(to_wrap.syntax(), name_ref.syntax()); - - let _ = editor.finish(); - } - #[test] fn test_insert_independent() { let root = make::block_expr( diff --git a/crates/syntax/src/syntax_editor/edit_algo.rs b/crates/syntax/src/syntax_editor/edit_algo.rs index 3b92ac1cbd..55e8867a46 100644 --- a/crates/syntax/src/syntax_editor/edit_algo.rs +++ b/crates/syntax/src/syntax_editor/edit_algo.rs @@ -1,3 +1,5 @@ +//! Implementation of applying changes to a syntax tree. + use std::{cmp::Ordering, collections::VecDeque, ops::RangeInclusive}; use rowan::TextRange; @@ -209,7 +211,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit { } }, Change::Replace(target, _) | Change::ReplaceWithMany(target, _) => { - *target = upmap_target(&target); + *target = upmap_target(target); } Change::ReplaceAll(range, _) => { *range = upmap_target(range.start())..=upmap_target(range.end()); @@ -233,7 +235,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit { Change::Replace(target, None) => { target.detach(); } - Change::Replace(SyntaxElement::Node(target), Some(new_target)) if &target == &root => { + Change::Replace(SyntaxElement::Node(target), Some(new_target)) if target == root => { root = new_target.into_node().expect("root node replacement should be a node"); } Change::Replace(target, Some(new_target)) => { @@ -288,7 +290,7 @@ struct ChangedAncestor { enum ChangedAncestorKind { Single { node: SyntaxNode }, - Range { _changed_elements: RangeInclusive, in_parent: SyntaxNode }, + Range { _changed_elements: RangeInclusive, _in_parent: SyntaxNode }, } impl ChangedAncestor { @@ -307,7 +309,7 @@ impl ChangedAncestor { Self { kind: ChangedAncestorKind::Range { _changed_elements: range.clone(), - in_parent: range.start().parent().unwrap(), + _in_parent: range.start().parent().unwrap(), }, change_index, } @@ -316,7 +318,7 @@ impl ChangedAncestor { fn affected_range(&self) -> TextRange { match &self.kind { ChangedAncestorKind::Single { node } => node.text_range(), - ChangedAncestorKind::Range { _changed_elements: changed_nodes, in_parent: _ } => { + ChangedAncestorKind::Range { _changed_elements: changed_nodes, _in_parent: _ } => { TextRange::new( changed_nodes.start().text_range().start(), changed_nodes.end().text_range().end(), @@ -339,7 +341,7 @@ impl TreeMutator { fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement { match element { - SyntaxElement::Node(node) => SyntaxElement::Node(self.make_syntax_mut(&node)), + SyntaxElement::Node(node) => SyntaxElement::Node(self.make_syntax_mut(node)), SyntaxElement::Token(token) => { let parent = self.make_syntax_mut(&token.parent().unwrap()); parent.children_with_tokens().nth(token.index()).unwrap() diff --git a/crates/syntax/src/syntax_editor/mapping.rs b/crates/syntax/src/syntax_editor/mapping.rs index b2c677c869..9bb5e6d933 100644 --- a/crates/syntax/src/syntax_editor/mapping.rs +++ b/crates/syntax/src/syntax_editor/mapping.rs @@ -1,3 +1,7 @@ +//! Maps syntax elements through disjoint syntax nodes. +//! +//! [`SyntaxMappingBuilder`] should be used to create mappings to add to a [`SyntaxEditor`] + use itertools::Itertools; use rustc_hash::FxHashMap; @@ -168,11 +172,11 @@ impl SyntaxMapping { match (input_mapping, input_ancestor) { (Some(input_mapping), _) => { // A mapping exists at the input, follow along the tree - Some(self.upmap_child(&input_mapping, &input_mapping, &output_root)) + Some(self.upmap_child(&input_mapping, &input_mapping, output_root)) } (None, Some(input_ancestor)) => { // A mapping exists at an ancestor, follow along the tree - Some(self.upmap_child(input, &input_ancestor, &output_root)) + Some(self.upmap_child(input, &input_ancestor, output_root)) } (None, None) => { // No mapping exists at all, is the same position in the final tree