misc fixes

This commit is contained in:
DropDemBits 2024-09-02 22:53:54 -04:00
parent d929121f7b
commit 69e8393963
No known key found for this signature in database
GPG key ID: 7FE02A6C1EDFA075
3 changed files with 20 additions and 26 deletions

View file

@ -150,6 +150,12 @@ impl SyntaxAnnotation {
} }
} }
impl Default for SyntaxAnnotation {
fn default() -> Self {
Self::new()
}
}
/// Position describing where to insert elements /// Position describing where to insert elements
#[derive(Debug)] #[derive(Debug)]
pub struct Position { pub struct Position {
@ -445,24 +451,6 @@ mod tests {
.all(|element| element.ancestors().any(|it| &it == edit.root()))) .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] #[test]
fn test_insert_independent() { fn test_insert_independent() {
let root = make::block_expr( let root = make::block_expr(

View file

@ -1,3 +1,5 @@
//! Implementation of applying changes to a syntax tree.
use std::{cmp::Ordering, collections::VecDeque, ops::RangeInclusive}; use std::{cmp::Ordering, collections::VecDeque, ops::RangeInclusive};
use rowan::TextRange; use rowan::TextRange;
@ -209,7 +211,7 @@ pub(super) fn apply_edits(editor: SyntaxEditor) -> SyntaxEdit {
} }
}, },
Change::Replace(target, _) | Change::ReplaceWithMany(target, _) => { Change::Replace(target, _) | Change::ReplaceWithMany(target, _) => {
*target = upmap_target(&target); *target = upmap_target(target);
} }
Change::ReplaceAll(range, _) => { Change::ReplaceAll(range, _) => {
*range = upmap_target(range.start())..=upmap_target(range.end()); *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) => { Change::Replace(target, None) => {
target.detach(); 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"); root = new_target.into_node().expect("root node replacement should be a node");
} }
Change::Replace(target, Some(new_target)) => { Change::Replace(target, Some(new_target)) => {
@ -288,7 +290,7 @@ struct ChangedAncestor {
enum ChangedAncestorKind { enum ChangedAncestorKind {
Single { node: SyntaxNode }, Single { node: SyntaxNode },
Range { _changed_elements: RangeInclusive<SyntaxElement>, in_parent: SyntaxNode }, Range { _changed_elements: RangeInclusive<SyntaxElement>, _in_parent: SyntaxNode },
} }
impl ChangedAncestor { impl ChangedAncestor {
@ -307,7 +309,7 @@ impl ChangedAncestor {
Self { Self {
kind: ChangedAncestorKind::Range { kind: ChangedAncestorKind::Range {
_changed_elements: range.clone(), _changed_elements: range.clone(),
in_parent: range.start().parent().unwrap(), _in_parent: range.start().parent().unwrap(),
}, },
change_index, change_index,
} }
@ -316,7 +318,7 @@ impl ChangedAncestor {
fn affected_range(&self) -> TextRange { fn affected_range(&self) -> TextRange {
match &self.kind { match &self.kind {
ChangedAncestorKind::Single { node } => node.text_range(), ChangedAncestorKind::Single { node } => node.text_range(),
ChangedAncestorKind::Range { _changed_elements: changed_nodes, in_parent: _ } => { ChangedAncestorKind::Range { _changed_elements: changed_nodes, _in_parent: _ } => {
TextRange::new( TextRange::new(
changed_nodes.start().text_range().start(), changed_nodes.start().text_range().start(),
changed_nodes.end().text_range().end(), changed_nodes.end().text_range().end(),
@ -339,7 +341,7 @@ impl TreeMutator {
fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement { fn make_element_mut(&self, element: &SyntaxElement) -> SyntaxElement {
match element { 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) => { SyntaxElement::Token(token) => {
let parent = self.make_syntax_mut(&token.parent().unwrap()); let parent = self.make_syntax_mut(&token.parent().unwrap());
parent.children_with_tokens().nth(token.index()).unwrap() parent.children_with_tokens().nth(token.index()).unwrap()

View file

@ -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 itertools::Itertools;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
@ -168,11 +172,11 @@ impl SyntaxMapping {
match (input_mapping, input_ancestor) { match (input_mapping, input_ancestor) {
(Some(input_mapping), _) => { (Some(input_mapping), _) => {
// A mapping exists at the input, follow along the tree // 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)) => { (None, Some(input_ancestor)) => {
// A mapping exists at an ancestor, follow along the tree // 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) => { (None, None) => {
// No mapping exists at all, is the same position in the final tree // No mapping exists at all, is the same position in the final tree