makepad_live_compiler/
live_node_vec.rs

1#![allow(clippy::collapsible_if)]
2use {
3    std::{
4        sync::Arc,
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        println!("{}", 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, "{}{} <Int64> {}", node.id, pt, v).unwrap();
675                }
676                LiveValue::Uint64(v) => {
677                    writeln!(f, "{}{} <Uint64> {}", node.id, pt, v).unwrap();
678                }
679                LiveValue::Float64(v) => {
680                    writeln!(f, "{}{} <Float> {}", node.id, pt, v).unwrap();
681                },
682                LiveValue::Float32(v) => {
683                    writeln!(f, "{}{} <Float32> {}", node.id, pt, v).unwrap();
684                },
685                LiveValue::Color(v) => {
686                    writeln!(f, "{}{} <Color>{:08x}", node.id, pt, v).unwrap();
687                },
688                LiveValue::Vec2(v) => {
689                    writeln!(f, "{}{} <Vec2> {:?}", node.id, pt, v).unwrap();
690                },
691                LiveValue::Vec3(v) => {
692                    writeln!(f, "{}{} <Vec3> {:?}", node.id, pt, v).unwrap();
693                },
694                LiveValue::Vec4(v) => {
695                    writeln!(f, "{}{} <Vec4> {:?}", node.id, pt, v).unwrap();
696                },
697                LiveValue::Id(id) => {
698                    writeln!(f, "{}{} <Id> {}", node.id, pt, id).unwrap();
699                },
700                LiveValue::IdPath(p) => {
701                    writeln!(f, "<IdPath> {:?}", p).unwrap();
702                }
703                LiveValue::ExprBinOp(id) => {
704                    writeln!(f, "{}{} <ExprBinOp> {:?}", node.id, pt, id).unwrap();
705                },
706                LiveValue::ExprUnOp(id) => {
707                    writeln!(f, "{}{} <ExprUnOp> {:?}", node.id, pt, id).unwrap();
708                },
709                LiveValue::ExprMember(id) => {
710                    writeln!(f, "{}{} <ExprMember> {:?}", node.id, pt, id).unwrap();
711                },
712                LiveValue::BareEnum(variant) => {
713                    writeln!(f, "{}{} <BareEnum> {}", node.id, pt, variant).unwrap();
714                },
715                LiveValue::Expr => {
716                    writeln!(f, "{} <Expr> {:?}", node.id, pt).unwrap();
717                    stack_depth += 1;
718                },
719                LiveValue::ExprCall {ident, args} => {
720                    writeln!(f, "{}{} <ExprCall> {}({})", node.id, pt, ident, args).unwrap();
721                },
722                LiveValue::Array => {
723                    writeln!(f, "{}{} <Array>", node.id, pt).unwrap();
724                    stack_depth += 1;
725                },
726                LiveValue::TupleEnum (variant) => {
727                    writeln!(f, "{}{} <TupleEnum> {}", node.id, pt, variant).unwrap();
728                    stack_depth += 1;
729                },
730                LiveValue::NamedEnum (variant)=> {
731                    writeln!(f, "{}{} <NamedEnum> {}", node.id, pt, variant).unwrap();
732                    stack_depth += 1;
733                },
734                LiveValue::Object => {
735                    writeln!(f, "{}{} <Object>", node.id, pt).unwrap();
736                    stack_depth += 1;
737                }, // subnodes including this one
738                LiveValue::Clone{clone,..}=> {
739                    writeln!(f, "{}{} <Clone> {}", node.id, pt, clone).unwrap();
740                    stack_depth += 1;
741                }, // subnodes including this one
742                LiveValue::Class {live_type, ..} => {
743                    writeln!(f, "{}{} <Class> {:?}", node.id, pt, live_type).unwrap();
744                    stack_depth += 1;
745                }, // subnodes including this one
746                LiveValue::Deref {live_type, clone,..} => {
747                    writeln!(f, "{}{} <Deref> {:?} {}", node.id, pt, live_type, clone).unwrap();
748                    stack_depth += 1;
749                }, // subnodes including this one
750                LiveValue::Root {..} => {
751                    writeln!(f, "{}{} <Root>", node.id, pt).unwrap();
752                    stack_depth += 1;
753                }, // subnodes including this one                
754                LiveValue::Close => {
755                    if stack_depth == 0 {
756                        writeln!(f, "<CloseMisaligned> {}", node.id).unwrap();
757                        break;
758                    }
759                    writeln!(f, "<Close> {}", node.id).unwrap();
760                    stack_depth -= 1;
761                    if stack_depth == 0 {
762                        break;
763                    }
764                },
765                // the shader code types
766                LiveValue::DSL {
767                    token_start,
768                    token_count,
769                    expand_index
770                } => {
771                    writeln!(f, "<DSL> {} :token_start:{}, token_count:{} expand_index:{:?}", node.id, token_start, token_count, expand_index).unwrap();
772                },
773                LiveValue::Import(live_import) => {
774                    writeln!(f, "<Import> {}::{} as {}", live_import.module_id, live_import.import_id, node.id).unwrap();
775                }
776                LiveValue::Font (s) => {
777                    writeln!(f, "{}{} <Font> {:?}", node.id, pt, s).unwrap();
778                },
779                /*LiveValue::Registry(component_id) => {
780                    writeln!(f, "<Registry> {}::{}", component_id, node.id).unwrap();
781                }*/
782            }
783            index += 1;
784        }
785        if stack_depth != 0 {
786            writeln!(f, "[[ERROR Stackdepth not 0 at end {}]]", stack_depth).unwrap()
787        }
788        f
789    } 
790}
791
792
793impl LiveNodeVecApi for LiveNodeVec {
794    fn insert_children_from_other(&mut self, source_index: usize, insert_point: usize, other: &[LiveNode]) {
795        if !other[source_index].is_open() {
796            panic!("Failed to insert non-open LiveNode: {:?}, \
797                \n\t --> Double-check your usage of {:?}!",
798                &other[source_index],
799                &other[source_index].id,
800            );
801        }
802        let next_source = other.skip_node(source_index);
803        let num_children = (next_source - source_index) - 2;
804        self.splice(insert_point..insert_point, other[source_index + 1..(source_index + 1 + num_children)].iter().cloned());
805    }
806    
807    
808    fn insert_children_from_self(&mut self, source_index: usize, insert_point: usize) {
809        // get the # of children here
810        if !self[source_index].is_open() {
811            panic!();
812        }
813        let next_source = self.skip_node(source_index);
814        let num_children = (next_source - source_index) - 2;
815        
816        self.splice(insert_point..insert_point, iter::repeat(LiveNode::empty()).take(num_children));
817        
818        let source_index = if insert_point < source_index {source_index + num_children}else {source_index};
819        
820        for i in 0..num_children {
821            self[insert_point + i] = self[source_index + 1 + i].clone();
822        }
823    }
824    
825    
826    fn insert_node_from_self(&mut self, source_index: usize, insert_point: usize) -> usize {
827        let next_source = self.skip_node(source_index);
828        let num_nodes = next_source - source_index;
829        // make space
830        self.splice(insert_point..insert_point, iter::repeat(LiveNode::empty()).take(num_nodes));
831        
832        let source_index = if insert_point < source_index {source_index + num_nodes}else {source_index};
833        
834        for i in 0..num_nodes {
835            self[insert_point + i] = self[source_index + i].clone();
836        }
837        
838        insert_point + num_nodes
839    }
840    
841    fn insert_node_from_other(&mut self, source_index: usize, insert_point: usize, other: &[LiveNode]) -> usize {
842        let next_source = other.skip_node(source_index);
843        let num_nodes = next_source - source_index;
844        // make space
845        self.splice(insert_point..insert_point, other[source_index..(source_index + num_nodes)].iter().cloned());
846        
847        insert_point + num_nodes
848    }
849    
850    fn write_field_value(&mut self, path: &[LiveId], value: LiveValue){
851        self.write_field_nodes(path, &[LiveNode::from_value(value)])
852    }
853
854    fn write_field_nodes(&mut self, path: &[LiveId], nodes: &[LiveNode]) {
855        let mut ids = [LiveProp(LiveId(0), LivePropType::Field); 8];
856        if path.len() > ids.len(){
857            eprintln!("write_by_field_path too many path segs");
858            return
859        }
860        for (index, step) in path.iter().enumerate(){
861            if index >= ids.len() {
862                eprintln!("write_value_by_path too many path segs");
863                return
864            }
865            ids[index] = LiveProp(*step, LivePropType::Field);
866        }
867        let was_empty = self.is_empty();
868        if was_empty {
869            self.open_object(LiveId(0));
870        }
871        self.replace_or_insert_last_node_by_path(
872            0,
873            &ids[0..path.len()],
874            nodes
875        );
876        if was_empty {
877            self.close();
878        }
879    }
880    
881    fn replace_or_insert_last_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]) {
882        let mut index = start_index;
883        let mut depth = 0;
884        while depth < path.len() {
885            match self.child_or_append_index_by_name(index, path[depth]) {
886                Ok(found_index) => {
887                    index = found_index;
888                    if depth == path.len() - 1 { // last
889                        let next_index = self.skip_node(found_index);
890                        self.splice(found_index..next_index, other.iter().cloned());
891                        // overwrite id
892                        self[found_index].origin.set_prop_type(path[depth].1);
893                        self[found_index].id = path[depth].0;
894                        return
895                    }
896                }
897                Err(append_index) => {
898                    index = append_index;
899                    if depth == path.len() - 1 { // last
900                        self.splice(append_index..append_index, other.iter().cloned());
901                        // lets overwrite the id
902                        self[append_index].origin.set_prop_type(path[depth].1);
903                        self[append_index].id = path[depth].0;
904                        return
905                    }
906                    else { // insert an empty object
907                        self.splice(append_index..append_index, live_object!{
908                            [path[depth].0]: {}
909                        }.iter().cloned());
910                        self[append_index].origin.set_prop_type(path[depth].1);
911                    }
912                }
913            }
914            depth += 1;
915        }
916    }
917    
918    fn replace_or_insert_first_node_by_path(&mut self, start_index: usize, path: &[LiveProp], other: &[LiveNode]) {
919        let mut index = start_index;
920        let mut depth = 0;
921        while depth < path.len() {
922            match self.child_by_name(index, path[depth]) {
923                Some(found_index) => {
924                    index = found_index;
925                    if depth == path.len() - 1 { // last
926                        let next_index = self.skip_node(found_index);
927                        self.splice(found_index..next_index, other.iter().cloned());
928                        // overwrite id
929                        self[found_index].id = path[depth].0;
930                        return
931                    }
932                }
933                None => {
934                    index += 1;
935                    if depth == path.len() - 1 { // last
936                        self.splice(index..index, other.iter().cloned());
937                        // lets overwrite the id
938                        self[index].id = path[depth].0;
939                        self[index].origin = LiveNodeOrigin::empty().with_prop_type(path[depth].1);
940                        return
941                    }
942                    else { // insert an empty object
943                        self.splice(index..index, live_object!{
944                            [path[depth].0]: {}
945                        }.iter().cloned());
946                        self[index].origin = LiveNodeOrigin::empty().with_prop_type(path[depth].1);
947                    }
948                }
949            }
950            depth += 1;
951        }
952    }
953    
954    
955    fn push_live(&mut self, v: &[LiveNode]) {self.extend_from_slice(v)}
956    
957    fn push_str(&mut self, id: LiveId, v: &'static str) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Str(v)})}
958    fn push_string(&mut self, id: LiveId, v: &str) {
959        //let bytes = v.as_bytes();
960        if let Some(inline_str) = InlineString::from_str(v) {
961            self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::InlineString(inline_str)});
962        }
963        else {
964            self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::String(Arc::new(v.to_string()))});
965        }
966    }
967    
968    fn push_bool(&mut self, id: LiveId, v: bool) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Bool(v)})}
969    fn push_int64(&mut self, id: LiveId, v: i64) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Int64(v)})}
970    fn push_float64(&mut self, id: LiveId, v: f64) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Float64(v)})}
971    fn push_color(&mut self, id: LiveId, v: u32) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Color(v)})}
972    fn push_vec2(&mut self, id: LiveId, v: Vec2) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec2(v)})}
973    fn push_vec3(&mut self, id: LiveId, v: Vec3) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec3(v)})}
974    fn push_vec4(&mut self, id: LiveId, v: Vec4) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Vec4(v)})}
975    fn push_id(&mut self, id: LiveId, v: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Id(v)})}
976    
977    fn push_bare_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::BareEnum(variant)})}
978    fn open_tuple_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::TupleEnum(variant)})}
979    fn open_named_enum(&mut self, id: LiveId, variant: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::NamedEnum(variant)})}
980    fn open_object(&mut self, id: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Object})}
981    fn open_clone(&mut self, id: LiveId, clone: LiveId) {
982        self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Clone{clone}})}
983    fn open_array(&mut self, id: LiveId) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id, value: LiveValue::Array})}
984    fn close(&mut self) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id: LiveId(0), value: LiveValue::Close})}
985    fn root2(&mut self) {self.push(LiveNode {origin: LiveNodeOrigin::empty(), id: LiveId(0), value: LiveValue::Root(Default::default())})}
986}