239 references to WriteLine
CSharpSyntaxGenerator (239)
AbstractFileWriter.cs (3)
71
WriteLine
("");
121
WriteLine
("{");
128
WriteLine
("}" + extra);
SourceWriter.cs (185)
31
WriteLine
("// <auto-generated />");
33
WriteLine
("#nullable enable");
35
WriteLine
("using System;");
36
WriteLine
("using System.Collections.Generic;");
37
WriteLine
("using System.Diagnostics.CodeAnalysis;");
38
WriteLine
("using Microsoft.CodeAnalysis.Syntax.InternalSyntax;");
39
WriteLine
("using Roslyn.Utilities;");
40
WriteLine
("using CoreSyntax = Microsoft.CodeAnalysis.Syntax.InternalSyntax;");
48
WriteLine
("namespace Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax;");
59
WriteLine
("namespace Microsoft.CodeAnalysis.CSharp.Syntax;");
66
WriteLine
("namespace Microsoft.CodeAnalysis.CSharp;");
67
WriteLine
("using System.Diagnostics.CodeAnalysis;");
68
WriteLine
("using Microsoft.CodeAnalysis.CSharp.Syntax;");
91
WriteLine
($"internal abstract partial class {node.Name} : {node.Base}");
95
WriteLine
($"internal {node.Name}(SyntaxKind kind, DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations)");
96
WriteLine
(" : base(kind, diagnostics, annotations)");
100
WriteLine
("SetFlags(NodeFlags.ContainsDirectives);");
105
WriteLine
($"internal {node.Name}(SyntaxKind kind)");
106
WriteLine
(" : base(kind)");
110
WriteLine
("SetFlags(NodeFlags.ContainsDirectives);");
127
WriteLine
($"public abstract {(IsNew(field) ? "new " : "")}CoreSyntax.{field.Type} {field.Name} {{ get; }}");
131
WriteLine
($"public abstract {(IsNew(field) ? "new " : "")}{(GetFieldType(field, green: true))} {field.Name} {{ get; }}");
141
WriteLine
($"public abstract {(IsNew(field) ? "new " : "")}{field.Type} {field.Name} {{ get; }}");
150
WriteLine
($"internal sealed partial class {node.Name} : {node.Base}");
159
WriteLine
($"internal readonly {type} {CamelCase(field.Name)};");
164
WriteLine
($"internal readonly {field.Type} {CamelCase(field.Name)};");
173
WriteLine
(", DiagnosticInfo[]? diagnostics, SyntaxAnnotation[]? annotations)");
174
WriteLine
(" : base(kind, diagnostics, annotations)");
185
WriteLine
(", SyntaxFactoryContext context)");
186
WriteLine
(" : base(kind)");
188
WriteLine
("this.SetFactoryContext(context);");
198
WriteLine
(")");
199
WriteLine
(" : base(kind)");
212
WriteLine
($"public {OverrideOrNewModifier(field)}{type} {field.Name} => new {type}(this.{CamelCase(field.Name)});");
217
WriteLine
($"public {OverrideOrNewModifier(field)}{type} {field.Name} => new {type}(new CoreSyntax.SyntaxList<CSharpSyntaxNode>(this.{CamelCase(field.Name)}));");
222
WriteLine
($"public {OverrideOrNewModifier(field)}{type} {field.Name} => new {type}(this.{CamelCase(field.Name)});");
226
WriteLine
($"public {OverrideOrNewModifier(field)}{(GetFieldType(field, green: true))} {field.Name} => this.{CamelCase(field.Name)};");
233
WriteLine
($"public {OverrideOrNewModifier(field)}{field.Type} {field.Name} => this.{CamelCase(field.Name)};");
242
WriteLine
(" => null;");
248
WriteLine
($"=> index == 0 ? this.{CamelCase(nodeFields[0].Name)} : null;");
255
WriteLine
("=> index switch");
260
WriteLine
($"{i} => this.{CamelCase(field.Name)},");
262
WriteLine
("_ => null,");
268
WriteLine
($"internal override SyntaxNode CreateRed(SyntaxNode? parent, int position) => new CSharp.Syntax.{node.Name}(this, parent, position);");
296
WriteLine
("SetFlags(NodeFlags.ContainsAttributes);");
300
WriteLine
($"this.SlotCount = {nodeFields.Count};");
306
WriteLine
($"if ({CamelCase(field.Name)} != null)");
308
WriteLine
($"this.AdjustFlagsAndWidth({CamelCase(field.Name)});");
309
WriteLine
($"this.{CamelCase(field.Name)} = {CamelCase(field.Name)};");
314
WriteLine
($"this.AdjustFlagsAndWidth({CamelCase(field.Name)});");
315
WriteLine
($"this.{CamelCase(field.Name)} = {CamelCase(field.Name)};");
321
WriteLine
($"this.{CamelCase(field.Name)} = {CamelCase(field.Name)};");
328
WriteLine
("internal override GreenNode SetAnnotations(SyntaxAnnotation[]? annotations)");
335
WriteLine
(");");
341
WriteLine
("internal override GreenNode SetDiagnostics(DiagnosticInfo[]? diagnostics)");
348
WriteLine
(");");
354
WriteLine
($"public override void Accept(CSharpSyntaxVisitor visitor) => visitor.Visit{StripPost(node.Name, "Syntax")}(this);");
355
WriteLine
($"public override TResult Accept<TResult>(CSharpSyntaxVisitor<TResult> visitor) => visitor.Visit{StripPost(node.Name, "Syntax")}(this);");
369
WriteLine
("internal partial class CSharpSyntaxVisitor" + (withResult ? "<TResult>" : ""));
373
WriteLine
($"public virtual {(withResult ? "TResult" : "void")} Visit{StripPost(node.Name, "Syntax")}({node.Name} node) => this.DefaultVisit(node);");
393
WriteLine
(")");
410
WriteLine
(")");
416
WriteLine
(");");
417
WriteLine
("var diags = GetDiagnostics();");
418
WriteLine
("if (diags?.Length > 0)");
419
WriteLine
(" newNode = newNode.WithDiagnosticsGreen(diags);");
420
WriteLine
("var annotations = GetAnnotations();");
421
WriteLine
("if (annotations?.Length > 0)");
422
WriteLine
(" newNode = newNode.WithAnnotationsGreen(annotations);");
423
WriteLine
("return newNode;");
428
WriteLine
("return this;");
437
WriteLine
("internal partial class CSharpSyntaxRewriter : CSharpSyntaxVisitor<CSharpSyntaxNode>");
447
WriteLine
($"public override CSharpSyntaxNode Visit{StripPost(node.Name, "Syntax")}({node.Name} node)");
452
WriteLine
("=> node;");
466
WriteLine
(");");
479
WriteLine
("internal partial class ContextAwareSyntax");
482
WriteLine
("private SyntaxFactoryContext context;");
485
WriteLine
("public ContextAwareSyntax(SyntaxFactoryContext context)");
486
WriteLine
(" => this.context = context;");
496
WriteLine
("internal static partial class SyntaxFactory");
518
WriteLine
(")");
524
WriteLine
("switch (kind)");
529
WriteLine
($"case SyntaxKind.{kind.Name}:{(kind == kinds.Last() ? " break;" : "")}");
531
WriteLine
("default: throw new ArgumentException(nameof(kind));");
543
WriteLine
($"if ({CamelCase(field.Name)} == null) throw new ArgumentNullException(nameof({CamelCase(field.Name)}));");
549
WriteLine
($"if ({CamelCase(field.Name)} != null)");
555
WriteLine
($"if ({pname}.Kind != SyntaxKind.{field.Kinds[0].Name}) throw new ArgumentException(nameof({pname}));");
559
WriteLine
($"switch ({pname}.Kind)");
570
WriteLine
($"case SyntaxKind.{kind.Name}:{(kind == kinds.Last() ? " break;" : "")}");
573
WriteLine
($"default: throw new ArgumentException(nameof({pname}));");
606
WriteLine
("int hash;");
618
WriteLine
(", out hash);");
620
WriteLine
($"if (cached != null) return ({nd.Name})cached;");
626
WriteLine
(");");
628
WriteLine
("if (hash >= 0)");
632
WriteLine
("SyntaxNodeCache.AddNode(result, hash);");
638
WriteLine
("return result;");
645
WriteLine
(");");
705
WriteLine
($"public abstract partial class {node.Name} : {node.Base}");
707
WriteLine
($"internal {node.Name}(InternalSyntax.CSharpSyntaxNode green, SyntaxNode? parent, int position)");
708
WriteLine
(" : base(green, parent, position)");
723
WriteLine
($"{"public"} abstract {(IsNew(field) ? "new " : "")}{fieldType} {field.Name} {{ get; }}");
724
WriteLine
($"public {node.Name} With{field.Name}({fieldType} {CamelCase(field.Name)}) => With{field.Name}Core({CamelCase(field.Name)});");
725
WriteLine
($"internal abstract {node.Name} With{field.Name}Core({fieldType} {CamelCase(field.Name)});");
731
WriteLine
($"public {node.Name} Add{field.Name}(params {argType}[] items) => Add{field.Name}Core(items);");
732
WriteLine
($"internal abstract {node.Name} Add{field.Name}Core(params {argType}[] items);");
746
WriteLine
($"public {node.Name} Add{StripPost(field.Name, "Opt")}{referencedNodeField.Name}(params {argType}[] items) => Add{StripPost(field.Name, "Opt")}{referencedNodeField.Name}Core(items);");
747
WriteLine
($"internal abstract {node.Name} Add{StripPost(field.Name, "Opt")}{referencedNodeField.Name}Core(params {argType}[] items);");
759
WriteLine
($"{"public"} abstract {(IsNew(field) ? "new " : "")}{field.Type} {field.Name} {{ get; }}");
773
WriteLine
($"public new {node.Name} With{baseField.Name}({GetRedFieldType(baseField)} {CamelCase(baseField.Name)}) => ({node.Name})With{baseField.Name}Core({CamelCase(baseField.Name)});");
782
WriteLine
($"public new {node.Name} Add{baseField.Name}(params {argType}[] items) => ({node.Name})Add{baseField.Name}Core(items);");
797
WriteLine
($"public new {baseType.Name} Add{StripPost(baseField.Name, "Opt")}{referencedNodeField.Name}(params {argType}[] items) => Add{StripPost(baseField.Name, "Opt")}{referencedNodeField.Name}Core(items);");
821
WriteLine
($"public sealed partial class {node.Name} : {node.Base}");
834
WriteLine
($"private SyntaxNode? {CamelCase(field.Name)};");
839
WriteLine
($"private {type} {CamelCase(field.Name)};");
846
WriteLine
($"internal {node.Name}(InternalSyntax.CSharpSyntaxNode green, SyntaxNode? parent, int position)");
847
WriteLine
(" : base(green, parent, position)");
864
WriteLine
("get");
866
WriteLine
($"var slot = ((Syntax.InternalSyntax.{node.Name})this.Green).{CamelCase(field.Name)};");
867
WriteLine
($"return slot != null ? new SyntaxToken(this, slot, {GetChildPosition(i)}, {GetChildIndex(i)}) : default;");
873
WriteLine
($" => new SyntaxToken(this, ((InternalSyntax.{node.Name})this.Green).{CamelCase(field.Name)}, {GetChildPosition(i)}, {GetChildIndex(i)});");
879
WriteLine
($"public {OverrideOrNewModifier(field)}SyntaxTokenList {field.Name}");
881
WriteLine
("get");
883
WriteLine
($"var slot = this.Green.GetSlot({i});");
884
WriteLine
($"return slot != null ? new SyntaxTokenList(this, slot, {GetChildPosition(i)}, {GetChildIndex(i)}) : default;");
895
WriteLine
($" => new {field.Type}(GetRed(ref this.{CamelCase(field.Name)}, {i}));");
901
WriteLine
("get");
904
WriteLine
($"var red = GetRed(ref this.{CamelCase(field.Name)}, {i});");
905
WriteLine
($"return red != null ? new {field.Type}(red, {GetChildIndex(i)}) : default;");
918
WriteLine
($" => GetRedAtZero(ref this.{CamelCase(field.Name)}){suffix};");
922
WriteLine
($" => GetRed(ref this.{CamelCase(field.Name)}, {i}){suffix};");
932
WriteLine
($"{"public"} {OverrideOrNewModifier(field)}{field.Type} {field.Name} => ((InternalSyntax.{node.Name})this.Green).{field.Name};");
944
WriteLine
(" => null;");
954
WriteLine
($" => index == {index} ? {whenTrue}{suffix} : null;");
960
WriteLine
("=> index switch");
967
WriteLine
($"{index} => GetRedAtZero(ref this.{CamelCase(field.Name)}){suffix},");
971
WriteLine
($"{index} => GetRed(ref this.{CamelCase(field.Name)}, {index}){suffix},");
974
WriteLine
("_ => null,");
990
WriteLine
(" => null;");
995
WriteLine
($" => index == {index} ? this.{CamelCase(field.Name)} : null;");
1001
WriteLine
("=> index switch");
1005
WriteLine
($"{index} => this.{CamelCase(field.Name)},");
1007
WriteLine
("_ => null,");
1049
WriteLine
($"public override {(genericResult ? "TResult?" : "void")} Accept{genericArgs}(CSharpSyntaxVisitor{genericArgs} visitor){(genericResult ? " where TResult : default" : "")} => visitor.Visit{StripPost(node.Name, "Syntax")}(this);");
1064
WriteLine
("public partial class CSharpSyntaxVisitor" + genericArgs);
1073
WriteLine
($"public virtual {(genericResult ? "TResult?" : "void")} Visit{StripPost(node.Name, "Syntax")}({node.Name} node) => this.DefaultVisit(node);");
1084
WriteLine
(")");
1101
WriteLine
(")");
1107
WriteLine
(");");
1108
WriteLine
("var annotations = GetAnnotations();");
1109
WriteLine
("return annotations?.Length > 0 ? newNode.WithAnnotations(annotations) : newNode;");
1114
WriteLine
("return this;");
1140
WriteLine
(");");
1153
WriteLine
(");");
1242
WriteLine
($"internal override {baseType.Name} Add{field.Name}Core(params {baseArgType}[] items) => Add{field.Name}(items);");
1247
WriteLine
($"public{(isNew ? " new " : " ")}{node.Name} Add{field.Name}(params {argType}[] items) => With{StripPost(field.Name, "Opt")}(this.{field.Name}.AddRange(items));");
1260
WriteLine
($"internal override {baseType.Name} Add{StripPost(field.Name, "Opt")}{referencedNodeField.Name}Core(params {argType}[] items) => Add{StripPost(field.Name, "Opt")}{referencedNodeField.Name}(items);");
1274
WriteLine
($"var {varName} = this.{field.Name} ?? SyntaxFactory.{factoryName}();");
1275
WriteLine
($"return With{StripPost(field.Name, "Opt")}({varName}.With{StripPost(referencedNodeField.Name, "Opt")}({varName}.{referencedNodeField.Name}.AddRange(items)));");
1280
WriteLine
($" => With{StripPost(field.Name, "Opt")}(this.{field.Name}.With{StripPost(referencedNodeField.Name, "Opt")}(this.{field.Name}.{referencedNodeField.Name}.AddRange(items)));");
1289
WriteLine
("public partial class CSharpSyntaxRewriter : CSharpSyntaxVisitor<SyntaxNode?>");
1298
WriteLine
($"public override SyntaxNode? Visit{StripPost(node.Name, "Syntax")}({node.Name} node)");
1302
WriteLine
(" => node;");
1324
WriteLine
(");");
1334
WriteLine
("public static partial class SyntaxFactory");
1427
WriteLine
(")");
1433
WriteLine
("switch (kind)");
1438
WriteLine
($"case SyntaxKind.{kind.Name}:{(kind == kinds.Last() ? " break;" : "")}");
1440
WriteLine
("default: throw new ArgumentException(nameof(kind));");
1462
WriteLine
($"if ({pname}.Kind() != SyntaxKind.{kinds[0].Name}) throw new ArgumentException(nameof({pname}));");
1466
WriteLine
($"switch ({pname}.Kind())");
1470
WriteLine
($"case SyntaxKind.{kind.Name}:{(kind == kinds.Last() ? " break;" : "")}");
1472
WriteLine
($"default: throw new ArgumentException(nameof({pname}));");
1479
WriteLine
($"if ({CamelCase(field.Name)} == null) throw new ArgumentNullException(nameof({CamelCase(field.Name)}));");
1512
WriteLine
(").CreateRed();");
1568
WriteLine
($"private static SyntaxKind Get{StripPost(nd.Name, "Syntax")}{StripPost(field.Name, "Opt")}Kind(SyntaxKind kind)");
1570
WriteLine
("=> kind switch");
1577
WriteLine
($"SyntaxKind.{nKind.Name} => SyntaxKind.{pKind.Name},");
1580
WriteLine
("_ => throw new ArgumentOutOfRangeException(),");
1614
WriteLine
(")");
1623
WriteLine
(");");
1720
WriteLine
(")");
1749
WriteLine
(");");
1799
WriteLine
($"/// {line.TrimStart()}");
1817
WriteLine
($"{indent}/// {line.TrimStart()}");
TestWriter.cs (51)
26
WriteLine
("// <auto-generated />");
28
WriteLine
("using Microsoft.CodeAnalysis.CSharp.Syntax;");
29
WriteLine
("using Roslyn.Utilities;");
30
WriteLine
("using Xunit;");
31
WriteLine
("using InternalSyntaxFactory = Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory;");
34
WriteLine
("namespace Microsoft.CodeAnalysis.CSharp.UnitTests");
38
WriteLine
("public partial class GreenNodeTests");
41
WriteLine
("#region Green Generators");
43
WriteLine
("#endregion Green Generators");
46
WriteLine
("#region Green Factory and Property Tests");
48
WriteLine
("#endregion Green Factory and Property Tests");
51
WriteLine
("#region Green Rewriters");
53
WriteLine
("#endregion Green Rewriters");
58
WriteLine
("public partial class RedNodeTests");
61
WriteLine
("#region Red Generators");
63
WriteLine
("#endregion Red Generators");
66
WriteLine
("#region Red Factory and Property Tests");
68
WriteLine
("#endregion Red Factory and Property Tests");
71
WriteLine
("#region Red Rewriters");
73
WriteLine
("#endregion Red Rewriters");
106
WriteLine
($"private static {csharpNamespace}{node.Name} Generate{strippedName}()");
209
WriteLine
(");");
234
WriteLine
("[Fact]");
235
WriteLine
($"public void Test{strippedName}FactoryAndProperties()");
238
WriteLine
($"var node = Generate{strippedName}();");
251
WriteLine
($"Assert.Equal(SyntaxKind.None, node.{field.Name}.Kind());");
255
WriteLine
($"Assert.Null(node.{field.Name});");
263
WriteLine
($"Assert.Equal(SyntaxKind.{kind}, node.{field.Name}.Kind());");
267
WriteLine
($"Assert.Equal(SyntaxKind.{kind}, node.{field.Name}.Kind);");
274
WriteLine
($"Assert.NotEqual(default, node.{field.Name});");
281
WriteLine
($"Assert.Equal(default, node.{field.Name});");
285
WriteLine
($"Assert.NotNull(node.{field.Name});");
297
WriteLine
($"Assert.Equal(new {field.Type}(), node.{field.Name});");
306
WriteLine
($"var newNode = node{withStat};");
307
WriteLine
("Assert.Equal(node, newNode);");
314
WriteLine
("AttachAndCheckDiagnostics(node);");
344
WriteLine
("[Fact]");
345
WriteLine
($"public void Test{strippedName}TokenDeleteRewriter()");
348
WriteLine
($"var oldNode = Generate{strippedName}();");
349
WriteLine
("var rewriter = new TokenDeleteRewriter();");
350
WriteLine
("var newNode = rewriter.Visit(oldNode);");
353
WriteLine
("if(!oldNode.IsMissing)");
355
WriteLine
("Assert.NotEqual(oldNode, newNode);");
359
WriteLine
("Assert.NotNull(newNode);");
360
WriteLine
("Assert.True(newNode.IsMissing, \"No tokens => missing\");");
372
WriteLine
("[Fact]");
373
WriteLine
($"public void Test{strippedName}IdentityRewriter()");
376
WriteLine
($"var oldNode = Generate{strippedName}();");
377
WriteLine
("var rewriter = new IdentityRewriter();");
378
WriteLine
("var newNode = rewriter.Visit(oldNode);");
382
WriteLine
("Assert.Same(oldNode, newNode);");