makepad_live_compiler/
live_node_vec.rs

1#![allow(clippy::collapsible_if)]
2use {
3    std::{
4        rc::Rc,
5        fmt::Write,
6        iter
7    },
8    crate::{
9        makepad_error_log::*,
10        makepad_derive_live::{
11            live_object
12        },
13        makepad_math::{
14            Vec2,
15            Vec3,
16            Vec4
17        },
18        makepad_live_tokenizer::LiveId,
19        live_token::LiveTokenId,
20        live_node::{LivePropType, LiveNode, LiveValue, LiveNodeOrigin, InlineString, LiveProp},
21    }
22};
23
24pub trait LiveNodeSliceApi {
25    fn parent(&self, child_index: usize) -> Option<usize>;
26    fn append_child_index(&self, parent_index: usize) -> usize;
27    fn first_child(&self, parent_index: usize) -> Option<usize>;
28    fn last_child(&self, parent_index: usize) -> Option<usize>;
29    fn next_child(&self, child_index: usize) -> Option<usize>;
30    fn node_slice(&self, parent_index: usize) -> &[LiveNode];
31    fn children_slice(&self, parent_index: usize) -> &[LiveNode];
32    
33    fn child_by_number(&self, parent_index: usize, child_number: usize) -> Option<usize>;
34    fn child_or_append_index_by_name(&self, parent_index: usize, name: LiveProp) -> Result<usize, usize>;
35    //fn next_child_by_name(&self, child_index: usize, name: LiveId) -> Option<usize>;
36    fn child_by_name(&self, parent_index: usize, name: LiveProp) -> Option<usize>;
37    
38    fn sibling_by_name(&self, start_index: usize, name: LiveProp) -> Option<usize>;
39
40    fn child_by_path(&self, parent_index: usize, path: &[LiveProp]) -> Option<usize>;
41    
42    fn child_by_field_path(&self, parent_index: usize, path:&[LiveId]) -> Option<usize>;
43
44    fn child_value_by_path(&self, parent_index: usize, path: &[LiveProp]) -> Option<&LiveValue>;
45
46    fn read_field_value(&self, path: &[LiveId]) -> Option<&LiveValue>;
47    
48    fn first_node_with_token_id(&self, match_token_id: LiveTokenId, also_in_dsl: bool) -> Option<usize>;
49    
50    fn get_num_sibling_nodes(&self, child_index: usize) -> usize;
51    
52    fn scope_up_by_name(&self, parent_index: usize, name: LiveProp) -> Option<usize>;
53    fn scope_up_down_by_name(&self, parent_index: usize, name: LiveProp, levels_up:usize) -> Option<usize>;
54    
55    fn count_children(&self, parent_index: usize) -> usize;
56    fn skip_node(&self, node_index: usize) -> usize;
57    fn clone_child(&self, parent_index: usize, out_vec: &mut Vec<LiveNode>);
58    fn to_string(&self, parent_index: usize, max_depth: usize) -> String;
59    fn debug_print(&self, parent_index: usize, max_depth: usize);
60}
61
62pub type LiveNodeSlice<'a> = &'a[LiveNode];
63
64pub trait LiveNodeVecApi {
65    fn insert_node_from_other(&mut self, from_index: usize, insert_start: usize, other: &[LiveNode]) -> usize;
66    fn insert_node_from_self(&mut self, from_index: usize, insert_start: usize) -> usize;
67    
68    fn insert_children_from_other(&mut self, from_index: usize, insert_start: usize, other: &[LiveNode]);
69    fn insert_children_from_self(&mut self, from_index: usize, insert_start: usize);
70    
71    fn write_field_nodes(&mut self, path: &[LiveId], nodes: &[LiveNode]);
72    fn write_field_value(&mut self, path: &[LiveId], values: LiveValue);
73    fn replace_or_insert_last_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]);
74    fn replace_or_insert_first_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]);
75    
76    fn push_live(&mut self, v: &[LiveNode]);
77    fn push_str(&mut self, id: LiveId, v: &'static str);
78    fn push_string(&mut self, id: LiveId, v: &str);
79    fn push_bool(&mut self, id: LiveId, v: bool);
80    fn push_int64(&mut self, id: LiveId, v: i64);
81    fn push_float64(&mut self, id: LiveId, v: f64);
82    fn push_color(&mut self, id: LiveId, v: u32);
83    fn push_vec2(&mut self, id: LiveId, v: Vec2);
84    fn push_vec3(&mut self, id: LiveId, v: Vec3);
85    fn push_vec4(&mut self, id: LiveId, v: Vec4);
86    fn push_id(&mut self, id: LiveId, v: LiveId);
87    fn push_bare_enum(&mut self, id: LiveId, variant: LiveId);
88    
89    fn open_tuple_enum(&mut self, id: LiveId, variant: LiveId);
90    fn open_named_enum(&mut self, id: LiveId, variant: LiveId);
91    fn open_object(&mut self, id: LiveId);
92    fn open_clone(&mut self, id: LiveId, clone: LiveId);
93    fn open_array(&mut self, id: LiveId);
94    
95    fn root2(&mut self);
96    fn close(&mut self);
97}
98
99pub type LiveNodeVec = Vec<LiveNode>;
100
101// accessing the Gen structure like a tree
102impl<T> LiveNodeSliceApi for T where T: AsRef<[LiveNode]> {
103    
104    fn first_node_with_token_id(&self, match_token_id: LiveTokenId, also_in_dsl: bool) -> Option<usize> {
105        for (node_index, node) in self.as_ref().iter().enumerate() {
106            if let Some(token_id) = node.origin.token_id() {
107                if token_id == match_token_id {
108                    return Some(node_index)
109                }
110                // lets see if we are a DSL node then match the token range
111                if also_in_dsl && token_id.file_id() == match_token_id.file_id() {
112                    if let LiveValue::DSL { token_start, token_count, .. } = node.value {
113                        let index = match_token_id.token_index() as u32;
114                        if index >= token_start && index <= token_start + token_count {
115                            return Some(node_index);
116                        }
117                    }
118                }
119            }
120            // we might have to do a range scan
121            
122        }
123        None
124    }
125    
126    fn parent(&self, child_index: usize) -> Option<usize> {
127        let self_ref = self.as_ref();
128        if self_ref.is_empty() {
129            return None
130        }
131        let mut stack_depth = 0;
132        let mut index = child_index - 1;
133        // we are going to scan backwards
134        loop {
135            if self_ref[index].is_open() {
136                if stack_depth == 0 {
137                    return Some(index)
138                }
139                stack_depth -= 1;
140            }
141            else if self_ref[index].is_close() {
142                stack_depth += 1;
143            }
144            if index == 0 {
145                break
146            }
147            index -= 1;
148        }
149        Some(0)
150    }
151    
152    fn get_num_sibling_nodes(&self, start_index: usize) -> usize {
153        let self_ref = self.as_ref();
154        if self_ref.is_empty() {
155            return 0
156        }
157        let mut stack_depth: isize = 0;
158        let mut index = start_index;
159        // scan backwards to find a node with this name
160        loop {
161            if self_ref[index].is_open() {
162                if stack_depth>0 {
163                    stack_depth -= 1;
164                }
165                else {
166                    return start_index - index;
167                }
168            }
169            else if self_ref[index].is_close() {
170                stack_depth += 1;
171            }
172            if index == 0 {
173                break
174            }
175            index -= 1;
176        }
177        0
178    }
179    
180    fn scope_up_by_name(&self, index: usize, name: LiveProp) -> Option<usize> {
181        let self_ref = self.as_ref();
182        if self_ref.is_empty() {
183            return None
184        }
185        let mut stack_depth: isize = 0;
186        let mut index = index;
187        // scan backwards to find a node with this name
188        loop {
189            if self_ref[index].is_open() {
190                if stack_depth>0 {
191                    stack_depth -= 1;
192                }
193            }
194            else if self_ref[index].is_close() {
195                stack_depth += 1;
196            }
197            if stack_depth == 0 && self_ref[index].id == name.0 && self_ref[index].origin.has_prop_type(name.1) && !self_ref[index].is_close() { // valuenode
198                return Some(index)
199            }
200            
201            if index == 0 {
202                break
203            }
204            index -= 1;
205        }
206        None
207    }
208    
209    fn scope_up_down_by_name(&self, start_index: usize,  name: LiveProp, mut levels_up:usize) -> Option<usize> {
210        let self_ref = self.as_ref();
211        if self_ref.is_empty() {
212            return None
213        }
214        let mut stack_depth: isize = 0;
215        let mut index = start_index; 
216        // scan backwards to find a node with this name
217        loop {
218            if self_ref[index].is_open() {
219                if stack_depth == 0 {
220                    if let Some(child_index) = self.child_by_name(index, name) {
221                        if child_index != start_index {
222                            return Some(child_index)
223                        }
224                    }
225                    if levels_up == 1{
226                        return None
227                    }
228                    if levels_up > 1{
229                        levels_up -= 1;
230                    }
231                }
232                if stack_depth>0 {
233                    stack_depth -= 1;
234                }
235            }
236            else if self_ref[index].is_close() { 
237                stack_depth += 1;
238            }
239            if index == 0 {
240                break
241            }
242            index -= 1;
243        }
244        None
245    }
246    
247    
248    fn child_by_number(&self, parent_index: usize, child_number: usize) -> Option<usize> {
249        let self_ref = self.as_ref();
250        let mut stack_depth = 0;
251        let mut index = parent_index;
252        let mut child_count = 0;
253        if !self_ref[index].is_open() {
254            panic!()
255        }
256        while index < self_ref.len() {
257            if self_ref[index].is_open() {
258                if stack_depth == 1 {
259                    if child_number == child_count {
260                        return Some(index);
261                    }
262                    child_count += 1;
263                }
264                stack_depth += 1;
265            }
266            else if self_ref[index].is_close() {
267                stack_depth -= 1;
268                if stack_depth == 0 {
269                    return None
270                }
271            } else if stack_depth == 1 {
272                if child_number == child_count {
273                    return Some(index);
274                }
275                child_count += 1;
276            } else if stack_depth == 0 {
277                panic!()
278            }
279            index += 1;
280        }
281        panic!()
282    }
283    
284    fn first_child(&self, parent_index: usize) -> Option<usize> {
285        let self_ref = self.as_ref();
286        if self_ref[parent_index].is_open() {
287            if self_ref[parent_index + 1].is_close() {
288                return None
289            }
290            return Some(parent_index + 1) // our first child
291        }
292        panic!("first_child called on non tree node")
293    }
294    
295    fn next_child(&self, child_index: usize) -> Option<usize> {
296        let self_ref = self.as_ref();
297        let mut index = child_index;
298        let mut stack_depth = 0;
299        while index < self_ref.len() {
300            if self_ref[index].is_open() {
301                if stack_depth == 0 && index != child_index {
302                    return Some(index)
303                }
304                stack_depth += 1;
305            }
306            else if self_ref[index].is_close() {
307                if stack_depth == 0 { // double close
308                    return None;
309                }
310                stack_depth -= 1;
311            } else if stack_depth == 0 && index != child_index {
312                return Some(index);
313            }
314            index += 1;
315        }
316        None
317    }
318    
319    fn last_child(&self, parent_index: usize) -> Option<usize> {
320        let self_ref = self.as_ref();
321        let mut stack_depth = 0;
322        let mut index = parent_index;
323        //let mut child_count = 0;
324        let mut found_child = None;
325        if !self_ref[index].is_open() {
326            panic!()
327        }
328        while index < self_ref.len() {
329            if self_ref[index].is_open() {
330                if stack_depth == 1 {
331                    found_child = Some(index);
332                    //child_count += 1;
333                }
334                stack_depth += 1;
335            }
336            else if self_ref[index].is_close() {
337                stack_depth -= 1;
338                if stack_depth == 0 {
339                    return found_child
340                }
341            } else if stack_depth == 1 {
342                found_child = Some(index);
343                //child_count += 1;
344            } else if stack_depth == 0 {
345                panic!()
346            }
347            index += 1;
348        }
349        None
350    }
351    
352    fn node_slice(&self, start_index: usize) -> &[LiveNode] {
353        let next_index = self.skip_node(start_index);
354        &self.as_ref()[start_index..next_index]
355    }
356    
357    fn children_slice(&self, start_index: usize) -> &[LiveNode] {
358        if !self.as_ref()[start_index].is_open() {
359            &self.as_ref()[start_index..start_index]
360        }
361        else {
362            let next_index = self.as_ref().skip_node(start_index);
363            &self.as_ref()[start_index + 1..next_index - 1]
364        }
365    }
366    
367    
368    fn append_child_index(&self, parent_index: usize) -> usize {
369        let self_ref = self.as_ref();
370        let mut stack_depth = 0;
371        let mut index = parent_index;
372        if !self_ref[index].is_open() {
373            panic!()
374        }
375        while index < self_ref.len() {
376            if self_ref[index].is_open() {
377                stack_depth += 1;
378            }
379            else if self_ref[index].is_close() {
380                stack_depth -= 1;
381                if stack_depth == 0 {
382                    return index
383                }
384            }
385            index += 1;
386        }
387        index
388    }
389    
390    fn child_or_append_index_by_name(&self, parent_index: usize, child_name: LiveProp) -> Result<usize, usize> {
391        let self_ref = self.as_ref();
392        let mut stack_depth = 0;
393        let mut index = parent_index;
394        if !self_ref[index].is_open() {
395            return Err(index)
396            //panic!()
397        }
398        while index < self_ref.len() {
399            if self_ref[index].is_open() {
400                if stack_depth == 1 {
401                    if self_ref[index].origin.has_prop_type(child_name.1) && self_ref[index].id == child_name.0 {
402                        return Ok(index);
403                    }
404                }
405                stack_depth += 1;
406            }
407            else if self_ref[index].is_close() {
408                stack_depth -= 1;
409                if stack_depth == 0 {
410                    return Err(index)
411                }
412            }
413            else {
414                if stack_depth == 1 {
415                    if self_ref[index].origin.has_prop_type(child_name.1) && self_ref[index].id == child_name.0 {
416                        return Ok(index);
417                    }
418                }
419                if stack_depth == 0 {
420                    panic!()
421                }
422            }
423            index += 1;
424        }
425        Err(index)
426    }
427    
428    fn child_by_name(&self, parent_index: usize, name: LiveProp) -> Option<usize> {
429        if let Ok(value) = self.child_or_append_index_by_name(parent_index, name) {
430            Some(value)
431        }
432        else {
433            None
434        }
435    }
436    
437    /*
438    fn sibling_by_name(&self, start_index: usize, name: LiveId) -> Option<usize>{
439        let self_ref = self.as_ref();
440        let mut stack_depth = 0;
441        let mut index = start_index;
442        while index < self_ref.len() {
443            if self_ref[index].is_open() {
444                if stack_depth == 0 {
445                    if !self_ref[index].origin.id_non_unique() && self_ref[index].id == name {
446                        return Some(index);
447                    }
448                }
449                stack_depth += 1;
450            }
451            else if self_ref[index].is_close() {
452                if stack_depth == 0 {
453                    return None
454                }
455                stack_depth -= 1;
456            }
457            else {
458                if stack_depth == 0 {
459                    if !self_ref[index].origin.id_non_unique() && self_ref[index].id == name {
460                        return Some(index);
461                    }
462                }
463            }
464            index += 1;
465        }
466        None
467    }*/
468    
469    
470    fn sibling_by_name(&self, child_index: usize, child_name: LiveProp) -> Option<usize> {
471        let self_ref = self.as_ref();
472        let mut stack_depth = 1;
473        let mut index = child_index;
474        while index < self_ref.len() {
475            if self_ref[index].is_open() {
476                if stack_depth == 1 {
477                    if self_ref[index].id == child_name.0 {
478                        return Some(index);
479                    }
480                }
481                stack_depth += 1;
482            }
483            else if self_ref[index].is_close() {
484                stack_depth -= 1;
485                if stack_depth == 0 {
486                    return None
487                }
488            }
489            else {
490                if stack_depth == 1 {
491                    if !self_ref[index].origin.has_prop_type(child_name.1) && self_ref[index].id == child_name.0 {
492                        return Some(index);
493                    }
494                }
495                if stack_depth == 0 {
496                    panic!()
497                }
498            }
499            index += 1;
500        }
501        None
502    }
503    
504    fn child_by_field_path(&self, parent_index: usize, path: &[LiveId]) -> Option<usize> {
505        let mut index = parent_index;
506        for level in path {
507            if let Some(child) = self.child_by_name(index, LiveProp(*level, LivePropType::Field)) {
508                index = child
509            }
510            else {
511                return None
512            }
513        }
514        Some(index)
515    }
516    
517    fn child_by_path(&self, parent_index: usize, path: &[LiveProp]) -> Option<usize> {
518        let mut index = parent_index;
519        for level in path {
520            if let Some(child) = self.child_by_name(index, *level) {
521                index = child
522            }
523            else {
524                return None
525            }
526        }
527        Some(index)
528    }
529    
530    fn child_value_by_path(&self, parent_index: usize, path: &[LiveProp]) -> Option<&LiveValue> {
531        if let Some(index) = self.child_by_path(parent_index, path) {
532            Some(&self.as_ref()[index].value)
533        }
534        else {
535            None
536        }
537    }
538    
539    fn read_field_value(&self, path: &[LiveId]) -> Option<&LiveValue> {
540        if let Some(index) = self.child_by_field_path(0, path) {
541            Some(&self.as_ref()[index].value)
542        }
543        else {
544            None
545        }
546    }
547    
548    fn count_children(&self, parent_index: usize) -> usize {
549        let self_ref = self.as_ref();
550        let mut stack_depth = 0;
551        let mut index = parent_index;
552        let mut count = 0;
553        if !self_ref[index].is_open() {
554            panic!()
555        }
556        while index < self_ref.len() {
557            if self_ref[index].is_open() {
558                if stack_depth == 1 {
559                    count += 1;
560                }
561                stack_depth += 1;
562            }
563            else if self_ref[index].is_close() {
564                stack_depth -= 1;
565                if stack_depth == 0 {
566                    return count
567                }
568            } else if stack_depth == 1 {
569                count += 1;
570            } else if stack_depth == 0 {
571                panic!()
572            }
573            index += 1;
574        }
575        panic!()
576    }
577    
578    fn skip_node(&self, node_index: usize) -> usize {
579        let self_ref = self.as_ref();
580        let mut index = node_index;
581        let mut stack_depth = 0;
582        while index < self_ref.len() {
583            if self_ref[index].is_open() {
584                stack_depth += 1;
585            } else if self_ref[index].is_close() {
586                if stack_depth == 0 {
587                    panic!()
588                }
589                stack_depth -= 1;
590                if stack_depth == 0 {
591                    index += 1;
592                    return index
593                }
594            } else if stack_depth == 0 {
595                index += 1;
596                return index
597            }
598            index += 1;
599        }
600        index
601    }
602    
603    fn clone_child(&self, parent_index: usize, out: &mut Vec<LiveNode>) {
604        let self_ref = self.as_ref();
605        let mut index = parent_index;
606        let mut stack_depth = 0;
607        while index < self_ref.len() {
608            out.push(self_ref[index].clone());
609            if self_ref[index].is_open() {
610                stack_depth += 1;
611            }
612            else if self_ref[index].is_close() {
613                stack_depth -= 1;
614                if stack_depth == 0 {
615                    return
616                }
617            } else if stack_depth == 0 {
618                return
619            }
620            index += 1;
621        }
622    }
623    
624    fn debug_print(&self, parent_index: usize, max_depth: usize) {
625        log!("{}", self.to_string(parent_index, max_depth));
626    }
627    
628    fn to_string(&self, parent_index: usize, max_depth: usize) -> String {
629        let self_ref = self.as_ref();
630        let mut stack_depth = 0;
631        let mut f = String::new();
632        let mut index = parent_index;
633        while index < self_ref.len() {
634            let node = &self_ref[index];
635            if stack_depth > max_depth {
636                if node.is_open() {
637                    stack_depth += 1;
638                }
639                else if node.is_close() {
640                    stack_depth -= 1;
641                }
642                index += 1;
643                continue
644            }
645            for _ in 0..stack_depth {
646                write!(f, "|   ").unwrap();
647            }
648            let pt = match node.origin.prop_type() {
649                LivePropType::Field => ":",
650                LivePropType::Instance => "=",
651                //LivePropType::Template => "=?",
652                LivePropType::Nameless => "??"
653            };
654            match &node.value {
655                LiveValue::None => {
656                    writeln!(f, "{}{} <None>", node.id, pt).unwrap();
657                },
658                LiveValue::Str(s) => {
659                    writeln!(f, "{}{} <Str> {}", node.id, pt, s).unwrap();
660                },
661                LiveValue::InlineString(s) => {
662                    writeln!(f, "{}{} <InlineString> {}", node.id, pt, s.as_str()).unwrap();
663                },
664                LiveValue::String(s) => {
665                    writeln!(f, "{}{} <String> {}", node.id, pt, s.as_str()).unwrap();
666                },
667                LiveValue::Dependency (s) => {
668                    writeln!(f, "{}{} <Dependency> {}", node.id, pt, s).unwrap();
669                },
670                LiveValue::Bool(v) => {
671                    writeln!(f, "{}{} <Bool> {}", node.id, pt, v).unwrap();
672                }
673                LiveValue::Int64(v) => {
674                    writeln!(f, "{}{} <Int> {}", node.id, pt, v).unwrap();
675                }
676                LiveValue::Float64(v) => {
677                    writeln!(f, "{}{} <Float> {}", node.id, pt, v).unwrap();
678                },
679                LiveValue::Float32(v) => {
680                    writeln!(f, "{}{} <Float32> {}", node.id, pt, v).unwrap();
681                },
682                LiveValue::Color(v) => {
683                    writeln!(f, "{}{} <Color>{:08x}", node.id, pt, v).unwrap();
684                },
685                LiveValue::Vec2(v) => {
686                    writeln!(f, "{}{} <Vec2> {:?}", node.id, pt, v).unwrap();
687                },
688                LiveValue::Vec3(v) => {
689                    writeln!(f, "{}{} <Vec3> {:?}", node.id, pt, v).unwrap();
690                },
691                LiveValue::Vec4(v) => {
692                    writeln!(f, "{}{} <Vec4> {:?}", node.id, pt, v).unwrap();
693                },
694                LiveValue::Id(id) => {
695                    writeln!(f, "{}{} <Id> {}", node.id, pt, id).unwrap();
696                },
697                LiveValue::IdPath(p) => {
698                    writeln!(f, "<IdPath> {:?}", p).unwrap();
699                }
700                LiveValue::ExprBinOp(id) => {
701                    writeln!(f, "{}{} <ExprBinOp> {:?}", node.id, pt, id).unwrap();
702                },
703                LiveValue::ExprUnOp(id) => {
704                    writeln!(f, "{}{} <ExprUnOp> {:?}", node.id, pt, id).unwrap();
705                },
706                LiveValue::ExprMember(id) => {
707                    writeln!(f, "{}{} <ExprMember> {:?}", node.id, pt, id).unwrap();
708                },
709                LiveValue::BareEnum(variant) => {
710                    writeln!(f, "{}{} <BareEnum> {}", node.id, pt, variant).unwrap();
711                },
712                LiveValue::Expr {expand_index} => {
713                    writeln!(f, "{}{} <Expr> {:?}", node.id, pt, expand_index).unwrap();
714                    stack_depth += 1;
715                },
716                LiveValue::ExprCall {ident, args} => {
717                    writeln!(f, "{}{} <ExprCall> {}({})", node.id, pt, ident, args).unwrap();
718                },
719                LiveValue::Array => {
720                    writeln!(f, "{}{} <Array>", node.id, pt).unwrap();
721                    stack_depth += 1;
722                },
723                LiveValue::TupleEnum (variant) => {
724                    writeln!(f, "{}{} <TupleEnum> {}", node.id, pt, variant).unwrap();
725                    stack_depth += 1;
726                },
727                LiveValue::NamedEnum (variant)=> {
728                    writeln!(f, "{}{} <NamedEnum> {}", node.id, pt, variant).unwrap();
729                    stack_depth += 1;
730                },
731                LiveValue::Object => {
732                    writeln!(f, "{}{} <Object>", node.id, pt).unwrap();
733                    stack_depth += 1;
734                }, // subnodes including this one
735                LiveValue::Clone(clone) => {
736                    writeln!(f, "{}{} <Clone> {}", node.id, pt, clone).unwrap();
737                    stack_depth += 1;
738                }, // subnodes including this one
739                LiveValue::Class {live_type, ..} => {
740                    writeln!(f, "{}{} <Class> {:?}", node.id, pt, live_type).unwrap();
741                    stack_depth += 1;
742                }, // subnodes including this one
743                LiveValue::Root {..} => {
744                    writeln!(f, "{}{} <Root>", node.id, pt).unwrap();
745                    stack_depth += 1;
746                }, // subnodes including this one                
747                LiveValue::Close => {
748                    if stack_depth == 0 {
749                        writeln!(f, "<CloseMisaligned> {}", node.id).unwrap();
750                        break;
751                    }
752                    writeln!(f, "<Close> {}", node.id).unwrap();
753                    stack_depth -= 1;
754                    if stack_depth == 0 {
755                        break;
756                    }
757                },
758                // the shader code types
759                LiveValue::DSL {
760                    token_start,
761                    token_count,
762                    expand_index
763                } => {
764                    writeln!(f, "<DSL> {} :token_start:{}, token_count:{} expand_index:{:?}", node.id, token_start, token_count, expand_index).unwrap();
765                },
766                LiveValue::Import(live_import) => {
767                    writeln!(f, "<Import> {}::{} as {}", live_import.module_id, live_import.import_id, node.id).unwrap();
768                }
769                /*LiveValue::Registry(component_id) => {
770                    writeln!(f, "<Registry> {}::{}", component_id, node.id).unwrap();
771                }*/
772            }
773            index += 1;
774        }
775        if stack_depth != 0 {
776            writeln!(f, "[[ERROR Stackdepth not 0 at end {}]]", stack_depth).unwrap()
777        }
778        f
779    } 
780}
781
782
783impl LiveNodeVecApi for LiveNodeVec {
784    fn insert_children_from_other(&mut self, source_index: usize, insert_point: usize, other: &[LiveNode]) {
785        
786        if !other[source_index].is_open() {
787            panic!();
788        }
789        let next_source = other.skip_node(source_index);
790        let num_children = (next_source - source_index) - 2;
791        self.splice(insert_point..insert_point, other[source_index + 1..(source_index + 1 + num_children)].iter().cloned());
792    }
793    
794    
795    fn insert_children_from_self(&mut self, source_index: usize, insert_point: usize) {
796        // get the # of children here
797        if !self[source_index].is_open() {
798            panic!();
799        }
800        let next_source = self.skip_node(source_index);
801        let num_children = (next_source - source_index) - 2;
802        
803        self.splice(insert_point..insert_point, iter::repeat(LiveNode::empty()).take(num_children));
804        
805        let source_index = if insert_point < source_index {source_index + num_children}else {source_index};
806        
807        for i in 0..num_children {
808            self[insert_point + i] = self[source_index + 1 + i].clone();
809        }
810    }
811    
812    
813    fn insert_node_from_self(&mut self, source_index: usize, insert_point: usize) -> usize {
814        let next_source = self.skip_node(source_index);
815        let num_nodes = next_source - source_index;
816        // make space
817        self.splice(insert_point..insert_point, iter::repeat(LiveNode::empty()).take(num_nodes));
818        
819        let source_index = if insert_point < source_index {source_index + num_nodes}else {source_index};
820        
821        for i in 0..num_nodes {
822            self[insert_point + i] = self[source_index + i].clone();
823        }
824        
825        insert_point + num_nodes
826    }
827    
828    fn insert_node_from_other(&mut self, source_index: usize, insert_point: usize, other: &[LiveNode]) -> usize {
829        let next_source = other.skip_node(source_index);
830        let num_nodes = next_source - source_index;
831        // make space
832        self.splice(insert_point..insert_point, other[source_index..(source_index + num_nodes)].iter().cloned());
833        
834        insert_point + num_nodes
835    }
836    
837    fn write_field_value(&mut self, path: &[LiveId], value: LiveValue){
838        self.write_field_nodes(path, &[LiveNode::from_value(value)])
839    }
840
841    fn write_field_nodes(&mut self, path: &[LiveId], nodes: &[LiveNode]) {
842        let mut ids = [LiveProp(LiveId(0), LivePropType::Field); 8];
843        if path.len() > ids.len(){
844            eprintln!("write_by_field_path too many path segs");
845            return
846        }
847        for (index, step) in path.iter().enumerate(){
848            if index >= ids.len() {
849                eprintln!("write_value_by_path too many path segs");
850                return
851            }
852            ids[index] = LiveProp(*step, LivePropType::Field);
853        }
854        let was_empty = self.is_empty();
855        if was_empty {
856            self.open_object(LiveId(0));
857        }
858        self.replace_or_insert_last_node_by_path(
859            0,
860            &ids[0..path.len()],
861            nodes
862        );
863        if was_empty {
864            self.close();
865        }
866    }
867    
868    fn replace_or_insert_last_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]) {
869        let mut index = start_index;
870        let mut depth = 0;
871        while depth < path.len() {
872            match self.child_or_append_index_by_name(index, path[depth]) {
873                Ok(found_index) => {
874                    index = found_index;
875                    if depth == path.len() - 1 { // last
876                        let next_index = self.skip_node(found_index);
877                        self.splice(found_index..next_index, other.iter().cloned());
878                        // overwrite id
879                        self[found_index].origin.set_prop_type(path[depth].1);
880                        self[found_index].id = path[depth].0;
881                        return
882                    }
883                }
884                Err(append_index) => {
885                    index = append_index;
886                    if depth == path.len() - 1 { // last
887                        self.splice(append_index..append_index, other.iter().cloned());
888                        // lets overwrite the id
889                        self[append_index].origin.set_prop_type(path[depth].1);
890                        self[append_index].id = path[depth].0;
891                        return
892                    }
893                    else { // insert an empty object
894                        self.splice(append_index..append_index, live_object!{
895                            [path[depth].0]: {}
896                        }.iter().cloned());
897                        self[append_index].origin.set_prop_type(path[depth].1);
898                    }
899                }
900            }
901            depth += 1;
902        }
903    }
904    
905    fn replace_or_insert_first_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]) {
906        let mut index = start_index;
907        let mut depth = 0;
908        while depth < path.len() {
909            match self.child_by_name(index, path[depth]) {
910                Some(found_index) => {
911                    index = found_index;
912                    if depth == path.len() - 1 { // last
913                        let next_index = self.skip_node(found_index);
914                        self.splice(found_index..next_index, other.iter().cloned());
915                        // overwrite id
916                        self[found_index].id = path[depth].0;
917                        return
918                    }
919                }
920                None => {
921                    index += 1;
922                    if depth == path.len() - 1 { // last
923                        self.splice(index..index, other.iter().cloned());
924                        // lets overwrite the id
925                        self[index].id = path[depth].0;
926                        self[index].origin = LiveNodeOrigin::empty().with_prop_type(path[depth].1);
927                        return
928                    }
929                    else { // insert an empty object
930                        self.splice(index..index, live_object!{
931                            [path[depth].0]: {}
932                        }.iter().cloned());
933                        self[index].origin = LiveNodeOrigin::empty().with_prop_type(path[depth].1);
934                    }
935                }
936            }
937            depth += 1;
938        }
939    }
940    
941    
942    fn push_live(&mut self, v: &[LiveNode]) {self.extend_from_slice(v)}
943    
944    fn push_str(&mut self, id: LiveId, v: &'static str) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Str(v)})}
945    fn push_string(&mut self, id: LiveId, v: &str) {
946        //let bytes = v.as_bytes();
947        if let Some(inline_str) = InlineString::from_str(v) {
948            self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::InlineString(inline_str)});
949        }
950        else {
951            self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::String(Rc::new(v.to_string()))});
952        }
953    }
954    
955    fn push_bool(&mut self, id: LiveId, v: bool) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Bool(v)})}
956    fn push_int64(&mut self, id: LiveId, v: i64) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Int64(v)})}
957    fn push_float64(&mut self, id: LiveId, v: f64) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Float64(v)})}
958    fn push_color(&mut self, id: LiveId, v: u32) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Color(v)})}
959    fn push_vec2(&mut self, id: LiveId, v: Vec2) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec2(v)})}
960    fn push_vec3(&mut self, id: LiveId, v: Vec3) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec3(v)})}
961    fn push_vec4(&mut self, id: LiveId, v: Vec4) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec4(v)})}
962    fn push_id(&mut self, id: LiveId, v: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Id(v)})}
963    
964    fn push_bare_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::BareEnum(variant)})}
965    fn open_tuple_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::TupleEnum(variant)})}
966    fn open_named_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::NamedEnum(variant)})}
967    fn open_object(&mut self, id: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Object})}
968    fn open_clone(&mut self, id: LiveId, clone: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Clone(clone)})}
969    fn open_array(&mut self, id: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Array})}
970    fn close(&mut self) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id: LiveId(0), value: LiveValue::Close})}
971    fn root2(&mut self) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id: LiveId(0), value: LiveValue::Root{id_resolve:Box::default()}})}
972}