@@ -14,7 +14,7 @@ pub struct SyntaxMapping {
14
14
15
15
// mappings -> parents
16
16
entry_parents : Vec < SyntaxNode > ,
17
- node_mappings : FxHashMap < SyntaxNode , ( u32 , u32 ) > ,
17
+ node_mappings : FxHashMap < SyntaxNode , MappingEntry > ,
18
18
}
19
19
20
20
impl SyntaxMapping {
@@ -80,11 +80,10 @@ impl SyntaxMapping {
80
80
return None ;
81
81
}
82
82
83
- if let Some ( next) = self . upmap_node ( & parent) {
84
- Some ( ( parent. index ( ) , next) )
85
- } else {
86
- Some ( ( parent. index ( ) , parent) )
87
- }
83
+ Some ( ( parent. index ( ) , match self . upmap_node ( & parent) {
84
+ Some ( next) => next,
85
+ None => parent
86
+ } ) )
88
87
} ) . map ( |( i, _) | i) . collect :: < Vec < _ > > ( )
89
88
} else {
90
89
vec ! [ ]
@@ -100,7 +99,7 @@ impl SyntaxMapping {
100
99
. children_with_tokens ( )
101
100
. nth ( index)
102
101
. and_then ( |it| it. into_node ( ) )
103
- . expect ( "yep " ) ;
102
+ . expect ( "equivalent ancestor node should be present in target tree " ) ;
104
103
}
105
104
106
105
debug_assert_eq ! ( child. kind( ) , target. kind( ) ) ;
@@ -109,7 +108,7 @@ impl SyntaxMapping {
109
108
}
110
109
111
110
pub fn upmap_node ( & self , input : & SyntaxNode ) -> Option < SyntaxNode > {
112
- let ( parent, child_slot) = self . node_mappings . get ( input) ?;
111
+ let MappingEntry { parent, child_slot } = self . node_mappings . get ( input) ?;
113
112
114
113
let output = self . entry_parents [ * parent as usize ]
115
114
. children_with_tokens ( )
@@ -121,14 +120,25 @@ impl SyntaxMapping {
121
120
Some ( output)
122
121
}
123
122
123
+ pub fn merge ( & mut self , mut other : SyntaxMapping ) {
124
+ // Remap other's entry parents to be after the current list of entry parents
125
+ let remap_base: u32 = self . entry_parents . len ( ) . try_into ( ) . unwrap ( ) ;
126
+
127
+ self . entry_parents . append ( & mut other. entry_parents ) ;
128
+ self . node_mappings . extend ( other. node_mappings . into_iter ( ) . map ( |( node, entry) | {
129
+ ( node, MappingEntry { parent : entry. parent + remap_base, ..entry } )
130
+ } ) ) ;
131
+ }
132
+
124
133
fn add_mapping ( & mut self , syntax_mapping : SyntaxMappingBuilder ) {
125
134
let SyntaxMappingBuilder { parent_node, node_mappings } = syntax_mapping;
126
135
127
- let parent_entry: u32 = self . entry_parents . len ( ) as u32 ;
136
+ let parent_entry: u32 = self . entry_parents . len ( ) . try_into ( ) . unwrap ( ) ;
128
137
self . entry_parents . push ( parent_node) ;
129
138
130
- let node_entries =
131
- node_mappings. into_iter ( ) . map ( |( node, slot) | ( node, ( parent_entry, slot) ) ) ;
139
+ let node_entries = node_mappings
140
+ . into_iter ( )
141
+ . map ( |( node, slot) | ( node, MappingEntry { parent : parent_entry, child_slot : slot } ) ) ;
132
142
133
143
self . node_mappings . extend ( node_entries) ;
134
144
}
@@ -172,3 +182,9 @@ impl SyntaxMappingBuilder {
172
182
editor. mappings . add_mapping ( self ) ;
173
183
}
174
184
}
185
+
186
+ #[ derive( Debug , Clone , Copy ) ]
187
+ struct MappingEntry {
188
+ parent : u32 ,
189
+ child_slot : u32 ,
190
+ }
0 commit comments