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
#[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(

View file

@ -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<SyntaxElement>, in_parent: SyntaxNode },
Range { _changed_elements: RangeInclusive<SyntaxElement>, _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()

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 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