44using Rewrite . RewriteCSharp ;
55using Rewrite . RewriteCSharp . Tree ;
66using Rewrite . RewriteJava . Tree ;
7+ using FileAttributes = Rewrite . Core . FileAttributes ;
78
89namespace Rewrite . Rpc . Serialization ;
910
1011public class CSharpDeltaDeserializer : CSharpVisitor < DeserializationContext >
1112{
12- public override J ? PreVisit ( Tree ? tree , DeserializationContext p , [ CallerMemberName ] string callingMethodName = "" , [ CallerArgumentExpression ( nameof ( tree ) ) ] string callingArgumentExpression = "" )
13+ // public override J? PreVisit(Tree? node, DeserializationContext p,
14+ // [CallerMemberName] string callingMethodName = "",
15+ // [CallerArgumentExpression(nameof(node))] string callingArgumentExpression = "")
16+ // {
17+ // // This handles the common properties for all Tree nodes
18+ // var ctx = p.As((J?)node);
19+ //
20+ // var id = ctx.DeserializeProperty(x => x?.Id) ?? (node?.Id ?? Tree.RandomId());
21+ // var prefix = ctx.DeserializeProperty(x => x?.Prefix, Visit) ?? Space.EMPTY;
22+ // var markers = ctx.DeserializeProperty(x => x?.Markers, (markers, context) => Visit(markers, context)) ?? Markers.EMPTY;
23+ //
24+ // // For ADD operations, we need to create a new instance
25+ // // The specific tree type will be handled by the appropriate Visit method
26+ // return (J?)node;
27+ // }
28+
29+ public override J ? VisitCompilationUnit ( Cs . CompilationUnit ? node , DeserializationContext p )
30+ {
31+ var ctx = p . As ( node ) ;
32+ var ( id , prefix , markers ) = DeserializeJHeader ( node , p ) ;
33+ var externs = ctx . DeserializeList ( x => x . Padding . Externs , ( x , context ) => Visit ( x , context ) ) ;
34+ var usings = ctx . DeserializeList ( x => x . Padding . Usings , x => x . Element . Id , ( x , context ) => Visit ( x , context ) ) ;
35+ var attributeLists = ctx . DeserializeList ( x => x . AttributeLists , x => x . Id , ( x , context ) => ( Cs . AttributeList ? ) Visit ( x , context ) ) ;
36+ var members = ctx . DeserializeList ( x => x . Padding . Members , ( x , context ) => Visit ( x , context ) ) ?? new List < JRightPadded < Statement > > ( ) ;
37+ var eof = ctx . DeserializeProperty ( x => x ? . Eof , ( x , context ) => Visit ( x , context ) ) ?? Space . EMPTY ;
38+
39+ return new Cs . CompilationUnit (
40+ id ,
41+ prefix ,
42+ markers ,
43+ null ! , // sourcePath - not shown in serializer
44+ new FileAttributes ( ) , // fileAttributes - not shown in serializer
45+ null , // charsetName - not shown in serializer
46+ false , // charsetBomMarked - not shown in serializer
47+ null , // checksum - not shown in serializer
48+ new List < JRightPadded < Cs . ExternAlias > > ( ) ,
49+ new List < JRightPadded < Cs . UsingDirective > > ( ) ,
50+ new List < Cs . AttributeList > ( ) ,
51+ members ,
52+ eof
53+ ) ;
54+ }
55+
56+ public override J ? VisitClassDeclaration ( Cs . ClassDeclaration ? node , DeserializationContext p )
1357 {
14- var node = ( J ) tree ! ;
1558 var ctx = p . As ( node ) ;
1659
17- // ctx.TraceContext.Push(callingArgumentExpression);
18- ctx . DeserializeProperty ( x => x . Id ) ;
19- ctx . SerializeProperty ( x => x . Prefix , Visit ) ;
20- ctx . SerializeProperty ( x => x . Markers , ( markers , context ) => Visit ( markers , context ) ) ;
21- // ctx.TraceContext.Pop();
22- return ( J ? ) node ;
60+ var ( id , prefix , markers ) = DeserializeJHeader ( node , p ) ;
61+
62+ // var attributeLists = ctx.DeserializeList(x => x.AttributeList, x => x.Id, (after, context) => (Cs.AttributeList)Visit(after, context)!);
63+ var modifiers = ctx . DeserializeList ( x => x . Modifiers , x => x . Id , ( after , context ) => VisitModifier ( after , context ) ) ;
64+ var kind = ctx . DeserializeProperty ( x => x . Kind , ( after , context ) => VisitClassDeclarationKind ( after , context ) ) ! ;
65+ var name = ctx . DeserializeProperty ( x => x . Name , ( after , context ) => VisitIdentifier ( after , context ) ) ! ;
66+
67+ // Note: The serializer only shows a partial implementation.
68+ // A complete implementation would handle all properties of ClassDeclaration
69+ return new Cs . ClassDeclaration (
70+ id ,
71+ prefix ,
72+ markers ,
73+ new List < Cs . AttributeList > ( ) ,
74+ modifiers ,
75+ kind ,
76+ name ,
77+ null ,
78+ default ,
79+ default ,
80+ default , default ,
81+ default ,
82+ default ) ;
2383 }
2484
25- public override J ? VisitCompilationUnit ( Cs . CompilationUnit node , DeserializationContext p )
85+ public override J ? VisitClassDeclarationKind ( J . ClassDeclaration . Kind ? node , DeserializationContext p )
2686 {
2787 var ctx = p . As ( node ) ;
28- ctx . SerializeList ( x => x . Padding . Externs , ( x , context ) => Visit ( x , context ) ) ;
29- ctx . SerializeList ( x => x . Padding . Usings , x => x . Element . Id , ( x , context ) => Visit ( x , context ) ) ;
30- ctx . SerializeList ( x => x . AttributeLists , ( x , context ) => Visit ( x , context ) ) ;
31- ctx . SerializeList ( x => x . Padding . Members , ( x , context ) => Visit ( x , context ) ) ;
32- ctx . SerializeProperty ( x => x . Eof , ( x , context ) => Visit ( x , context ) ) ;
33- return node ;
88+ var ( id , prefix , markers ) = DeserializeJHeader ( node , p ) ;
89+ var type = ctx . DeserializeProperty ( x => x . KindType ) ;
90+ return new J . ClassDeclaration . Kind ( id , prefix , markers , new List < J . Annotation > ( ) , type ) ;
3491 }
3592
36- public override J ? VisitClassDeclaration ( Cs . ClassDeclaration node , DeserializationContext p )
93+ private ( Guid , Space , Markers ) DeserializeJHeader ( J ? node , DeserializationContext p )
3794 {
3895 var ctx = p . As ( node ) ;
39- // ctx.SerializeProperty(x => x.Prefix, (after, context) => Visit(after, context));
40- ctx . SerializeList ( x => x . Modifiers , ( after , context ) => Visit ( after , context ) ) ;
41- ctx . SerializeProperty ( x => x . Name , ( after , context ) => Visit ( after , context ) ) ;
42- return node ;
43-
96+ var id = ctx . DeserializeProperty ( x => x . Id ) ;
97+ var prefix = ctx . DeserializeProperty ( x => x . Prefix , Visit ) ?? Space . EMPTY ;
98+ var markers = ctx . DeserializeProperty ( x => x . Markers , ( markers , context ) => Visit ( markers , context ) ) ?? Markers . EMPTY ;
99+ return ( id , prefix , markers ) ;
44100 }
45-
46- public override J ? VisitModifier ( J . Modifier node , DeserializationContext p )
101+
102+ public override J ? VisitModifier ( J . Modifier ? node , DeserializationContext p )
47103 {
48104 var ctx = p . As ( node ) ;
49- ctx . SerializeProperty ( x => x . ModifierType ) ;
50- ctx . SerializeProperty ( x => x . Keyword ) ;
51- return node ;
105+
106+ var ( id , prefix , markers ) = DeserializeJHeader ( node , p ) ;
107+
108+ var modifierType = ctx . DeserializeProperty ( x => x ? . ModifierType ) ?? throw new InvalidOperationException ( "Modifier must have a type" ) ;
109+ var keyword = ctx . DeserializeProperty ( x => x ? . Keyword ) ;
110+
111+ return new J . Modifier (
112+ id ,
113+ prefix ,
114+ markers ,
115+ keyword ,
116+ modifierType ,
117+ new List < J . Annotation > ( ) // annotations - not shown in serializer
118+ ) ;
52119 }
53-
54- public override J ? VisitIdentifier ( J . Identifier node , DeserializationContext p )
120+
121+ public override J ? VisitIdentifier ( J . Identifier ? node , DeserializationContext p )
55122 {
56123 var ctx = p . As ( node ) ;
57- ctx . SerializeProperty ( x => x . SimpleName ) ;
58- return node ;
124+
125+ var ( id , prefix , markers ) = DeserializeJHeader ( node , p ) ;
126+
127+ var simpleName = ctx . DeserializeProperty ( x => x . SimpleName ) ! ;
128+
129+ return new J . Identifier (
130+ id ,
131+ prefix ,
132+ markers ,
133+ new List < J . Annotation > ( ) , // annotations - not shown in serializer
134+ simpleName ,
135+ null , // type - not shown in serializer
136+ null // fieldType - not shown in serializer
137+ ) ;
59138 }
60-
61- private void Visit ( Space space , DeserializationContext context )
139+
140+ // Helper visit methods that match the serializer
141+ private Space ? Visit ( Space ? space , DeserializationContext context )
62142 {
143+ // if (space == null) return null;
144+
63145 var ctx = context . As ( space ) ;
64- ctx . SerializeProperty ( x => x . Whitespace ) ;
65- ctx . SerializeList ( x => x . Comments ) ;
66- VisitSpace ( space , Space . Location . ANY , context ) ;
146+ var whitespace = ctx . DeserializeProperty ( x => x . Whitespace ) ;
147+ var comments = ctx . DeserializeList ( x => x . Comments ) ;
148+
149+ return new Space ( comments , whitespace ) ;
67150 }
68-
69- public void Visit ( Markers node , DeserializationContext context ) => VisitMarkers ( node , context ) ;
70- public void Visit ( Marker node , DeserializationContext context ) => VisitMarker ( node , context ) ;
71- public void Visit < T > ( JRightPadded < T > node , DeserializationContext context )
151+
152+ public Markers ? Visit ( Markers ? node , DeserializationContext context )
153+ {
154+ var ctx = context . As ( node ) ;
155+ var id = ctx . DeserializeProperty ( x => x . Id ) ;
156+ var markerList = ctx . DeserializeList ( x => x . MarkerList ) ;
157+
158+ return new Markers ( id , markerList ) ;
159+ }
160+
161+ public Marker ? Visit ( Marker ? node , DeserializationContext context )
162+ {
163+ // The serializer doesn't show Marker implementation,
164+ // so this is a placeholder that would need to be expanded
165+ return node ;
166+ }
167+
168+ public JRightPadded < T > ? Visit < T > ( JRightPadded < T > ? node , DeserializationContext context ) where T : Tree
72169 {
73170 var ctx = context . As ( node ) ;
74- ctx . SerializeProperty ( x => x . Element , ( x , diffContext ) => Visit ( ( Tree ) x ! , diffContext ) , callingArgumentExpression : $ "RP[{ node . Element ! . GetType ( ) . Name } ]") ;
75- ctx . SerializeProperty ( x => x . After , ( x , diffContext ) => Visit ( x , diffContext ) ) ;
76- ctx . SerializeProperty ( x => x . Markers , ( x , diffContext ) => Visit ( x , diffContext ) ) ;
171+ var element = ctx . DeserializeProperty ( x => x . Element , ( x , diffContext ) => Visit ( x , diffContext ) ! ) ;
172+ var after = ctx . DeserializeProperty ( x => x . After , ( x , diffContext ) => Visit ( x , diffContext ) ) ?? Space . EMPTY ;
173+ var markers = ctx . DeserializeProperty ( x => x ? . Markers , ( x , diffContext ) => Visit ( x , diffContext ) ) ?? Markers . EMPTY ;
174+
175+ return new JRightPadded < T > ( ( T ) element ! , after , markers ) ;
176+ }
177+
178+ public override J ? Visit ( Tree ? tree , DeserializationContext p , string callingMethodName = "" , string callingArgumentExpression = "" )
179+ {
180+ if ( tree is null )
181+ {
182+ var type = Type . GetType ( p . Current . ValueType ! ) ;
183+
184+ if ( type == typeof ( Cs . ClassDeclaration ) )
185+ {
186+ return VisitClassDeclaration ( null , p ) ;
187+ }
188+ else if ( type == typeof ( Cs . Modifier ) )
189+ {
190+ return VisitModifier ( null , p ) ;
191+ }
192+ }
193+ return base . Visit ( tree , p , callingMethodName , callingArgumentExpression ) ;
77194 }
78195
79196 public override Markers VisitMarkers ( Markers ? node , DeserializationContext p )
80197 {
81- var ctx = p . As ( node ) ;
82- ctx . SerializeProperty ( x => x . Id ) ;
83- ctx . SerializeList ( x => x . MarkerList ) ;
84- return node ! ;
198+ return Visit ( node , p ) ?? Markers . EMPTY ;
85199 }
86200}
0 commit comments