Skip to content

Commit 1d8e6fd

Browse files
committed
Reduce the use of string builder
1 parent 7ffa820 commit 1d8e6fd

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,8 @@ public partial class MyReactiveControl : UserControl
567567

568568
this will generate the following code to enable you register the marked Views as `IViewFor<ViewModel>` with Splat:
569569
```csharp
570+
using ReactiveUI.SourceGenerators;
571+
570572
Splat.Locator.CurrentMutable.RegisterViewsForViewModelsSourceGenerated();
571573
```
572574

src/ReactiveUI.SourceGenerators.Execute/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// The ReactiveUI and contributors licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6-
using ReactiveUI;
6+
using ReactiveUI.SourceGenerators;
77
using Splat;
88

99
namespace SGReactiveUI.SourceGenerators.Test;

src/ReactiveUI.SourceGenerators.Roslyn/IViewFor/IViewForGenerator.Execute.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -323,27 +323,7 @@ private static string GenerateRegistrationExtensions(in ImmutableArray<IViewForI
323323
.ToImmutableArray();
324324

325325
var sb = new StringBuilder();
326-
sb
327-
.AppendLine("// <auto-generated/>")
328-
.AppendLine("#pragma warning disable")
329-
.AppendLine("#nullable enable")
330-
.AppendLine()
331-
.AppendLine("using global::ReactiveUI;")
332-
.AppendLine("using global::Splat;")
333-
.AppendLine()
334-
.AppendLine("namespace ReactiveUI")
335-
.AppendLine("{")
336-
.AppendLine(" /// <summary>")
337-
.AppendLine(" /// Source-generated registration extensions for ReactiveUI views.")
338-
.AppendLine(" /// </summary>")
339-
.AppendLine(" internal static class ReactiveUISourceGeneratorsExtensions")
340-
.AppendLine(" {")
341-
.AppendLine($" [{AttributeDefinitions.GeneratedCode}(\"{GeneratorName}\", \"{GeneratorVersion}\")] ")
342-
.AppendLine(" public static void RegisterViewsForViewModelsSourceGenerated(this global::Splat.IMutableDependencyResolver resolver)")
343-
.AppendLine(" {")
344-
.AppendLine(" if (resolver is null) throw new global::System.ArgumentNullException(nameof(resolver));")
345-
.AppendLine();
346-
326+
sb.AppendLine("if (resolver is null) throw new global::System.ArgumentNullException(nameof(resolver));");
347327
foreach (var item in registrations)
348328
{
349329
var vmType = item.ViewModelTypeName;
@@ -370,13 +350,32 @@ private static string GenerateRegistrationExtensions(in ImmutableArray<IViewForI
370350
}
371351
}
372352

373-
sb
374-
.AppendLine(" }")
375-
.AppendLine(" }")
376-
.AppendLine("}")
377-
.AppendLine("#nullable restore")
378-
.AppendLine("#pragma warning restore");
353+
var registrationsBody = sb.ToString().TrimEnd();
354+
return
355+
$$"""
356+
// <auto-generated/>
357+
#pragma warning disable
358+
#nullable enable
359+
360+
using global::ReactiveUI;
361+
using global::Splat;
379362
380-
return sb.ToString();
363+
namespace ReactiveUI.SourceGenerators
364+
{
365+
/// <summary>
366+
/// Source-generated registration extensions for ReactiveUI views.
367+
/// </summary>
368+
internal static class ReactiveUISourceGeneratorsExtensions
369+
{
370+
[global::System.CodeDom.Compiler.GeneratedCode("{{GeneratorName}}", "{{GeneratorVersion}}")]
371+
public static void RegisterViewsForViewModelsSourceGenerated(this global::Splat.IMutableDependencyResolver resolver)
372+
{
373+
{{registrationsBody}}
374+
}
375+
}
376+
}
377+
#nullable restore
378+
#pragma warning restore
379+
""";
381380
}
382381
}

0 commit comments

Comments
 (0)